How to make the autonomous run one line of code at a time

Hello again,

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.

Thanks for the assistance in advance
Cheers:)

motor_group group1(rightwheel,leftwheel);
motor_group group2(lift1,lift2);
motor_group group3(intake1,intake2);

group2.spinFor(reverse, 0.7, seconds);
group3.spinFor(reverse,0.8, seconds);
group2.spinFor(forward,1,seconds);

Maybe try putting a pause in between the lines of 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:

vex::task::sleep(2000); //Wait 2 seconds (2000 milliseconds)

In your case, probably something like this:

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);
3 Likes

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.

3 Likes

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

group2.spinFor(reverse,0.7,seconds,true);
1 Like

Hm, no, spinFor with other units has that parameter, but spinFor with time units does not.

3 Likes

Oh oops I didn’t know that, thanks!

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);
1 Like

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.

3 Likes

Yeah I see that now in the VEXcode API. Thanks.

I would recommend for the sake of accuracy to use group2.spinFor(reverse, 360, degrees); This will make your code run much more accurately.