Has anyone ever had a function that when you tell it to run, every other function on the robot stops until the function you first ran completed? I am consistently having this problem with my code and my team is getting frustrated that I have not fixed it yet. I have been trying different things to fix it, but have gotten nowhere. Can anyone send an example of code where two functions can run simultaneously together? Or maybe (if you can) explain how one could write code like that?
you probably have blocking commands in the function. some commands will wait until completion before moving on, while others continue.
Your code can only do one thing at a time unless you tell it to break off and do two things at once - known as multithreading (or multitasking as that’s also in vexcode). Generally if you want motors to all spin at the same time, you just have commands back to back for each motor. For this, xenon’s response will be your solution.
If you want to run multiple functions at once (ie running int main()
and void doSomething()
then you’ll need threading. if I interpreted your question right, this will be your solution.
To multithread, try adding something like this:
thread(functionName).detach();
For an example just so that everyone knows the context of what kind of problems I am having, take this:
EX: My stacking function will run when I press the B button, but I cannot drive until the function is complete
ah, then you’ll be looking for threading. The idea is to run separately declared functions simultaneously, so make sure this isn’t just a chunk of code you are calling a function
Now I am running into another wall. I told my Deposit();
function when I press the down button in my code:
if (MasterController.ButtonB.pressing()) {
Deposit();
}
So I am unsure where to place thread(functionName).detach();
in my code.
if (MasterController.ButtonB.pressing()) {
thread(Deposit).detach();
}
I love your controller name, I named mine ‘shirt’ so you are pushing on your shirt’s buttons
LOL. Thanks for your help! I am always making simple mistakes in my code, but that one was completely new to me.
No problem! I also forgot to mention that this code means that every time your code sees you are pressing the B button it’ll start a new thread. If you don’t want this I’d suggest adding a statement to in your if statement declaring a variable true and changing the if statement so that it only runs when you are pressing that button and the variable is still set to true.