Page 1 of 1

NXC - command execution times

Posted: 12 Feb 2012, 19:33
by floydbloke
Using NXC, I’m trying to get a better grasp on when commands are executed.

Let’s say I’ve got the following the code:

Code: Select all

RotateMotor(OUT_A, 30, 360);
RotateMotor(OUT_B, 40, 180);
Am I right in thinking that pretty well straight after Motor A starts turning, Motor B will start turning as well, i.e. it won’t wait for Motor A to finish.

If the above is correct, what would happen if:

Code: Select all

RotateMotor(OUT_A, 30, 360);
Off(OUT_A);
Would Motor A not complete its 360 degree turn?

What is the best way to ensure that Motor A finishes its turn? I have tried inserting a Wait command after starting Motor A but results seem rather inconsistent.
Would this work?

Code: Select all

while(MotorRunState(OUT_A)!= OUT_RUNSTATE_IDLE)
  {
  }
To ensure that A finishes prior to starting B

Re: NXC - command execution times

Posted: 12 Feb 2012, 20:37
by afanofosc
The RotateMotor command is an API function that does not return until the motor(s) complete their specified rotation. So if you want both A and B to turn simultaneously you will need to use multiple threads. If you want motor B to start rotating after A stops then simply put the two commands back to back as you show in your first code block.

John Hansen

Re: NXC - command execution times

Posted: 12 Feb 2012, 21:09
by floydbloke
afanofosc wrote:The RotateMotor command is an API function that does not return until the motor(s) complete their specified rotation. So if you want both A and B to turn simultaneously you will need to use multiple threads. If you want motor B to start rotating after A stops then simply put the two commands back to back as you show in your first code block.

John Hansen
Thanks John, that answers the question.
I though I had observed the motors turning simultaneously but am obviously mistaken, or have errors in my code elsewhere.