NXC - command execution times

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
floydbloke
Posts: 35
Joined: 15 Oct 2010, 01:29
Location: Wellington, New Zealand

NXC - command execution times

Post 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
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC - command execution times

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
floydbloke
Posts: 35
Joined: 15 Oct 2010, 01:29
Location: Wellington, New Zealand

Re: NXC - command execution times

Post 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.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests