our autonomous is having an issue where after it is activated it tries to run multiple lines of code at the same time. What’s weird is that it did not start out that way, it just started happening without any changes to the code.
Just for the sake of coding knowledge, a single code instance would run one line at a time, from top to bottom.
Basically, your code is running one line at a time, but it runs each function so fast where it gives the appearance it is running all of them at the same time.
In order to make them run at noticeably different time periods, you would need to add a wait statement like the following:
motor_group group1(rightwheel,leftwheel);
motor_group group2(lift1,lift2);
motor_group group3(intake1,intake2);
//Run both group 2 for 0.7 seconds and group 3 for 0.8 seconds, reversed
group2.spinFor(reverse, 0.7, seconds);
group3.spinFor(reverse,0.8, seconds);
vex::task::sleep(2000); //Wait 2 seconds (2000 milliseconds)
//Run group 2 for 1 second forward
group2.spinFor(forward,1,seconds);
spinFor with time parameters is a blocking function, they should run sequentially, each motor group should spin for the requested time before the next line of code runs. Probably something else in your program that’s causing it.
spinFor has a parameter called waitForCompletion that will wait until the function is complete before moving onto the next line of code. So you can say
Correct me if I am wrong, but if you don’t say anything for that parameter in general, doesn’t that make the parameter automatically true? For example:
motor.spinFor(5, turns, 100, velocityUnits::pct);
//is the same as
motor.spinFor(5, turns, 100, velcityUnits::pct, true);
Yes, that’s correct, but only when asking a motor or motor group to move some number of turns.
When we ask a motor to move 5 turns, the V5 sends the command to the motor and the motor itself handles moving from the current to the requested new position. We can either have the current task/thread block and wait for that to finish, or move on to the next line of code and leave the motor doing the move while we process more instructions.
but time based moves are different, all we are really doing is
send motor spin
wait some amount of time
send motor stop
If we had the “wait for completion” parameter with the time based spinFor call, the “wait some amount of time” and “motor stop” would need to be skipped, that would add complexity and mean we had to start another thread and stop the motor after the delay. So really a spinFor time call with wait for completion set to false, without starting a thread etc. is just a spin command.
We decided that as moving a motor for a fixed amount of time is a really a simple operation, we would not complicate the code and leave it to the end user to implement this type of functionality if the blocking spinFor call was not appropriate.