ROBOTC.net forumshttp://www.robotc.net/forums/ RobotC Guard Conditionshttp://www.robotc.net/forums/viewtopic.php?f=52&t=7408 Page 1 of 1

 Author: rjain15 [ Wed Dec 04, 2013 9:20 pm ] Post subject: RobotC Guard Conditions Hi I am new to RobotC / NXT Encoder Programming. I have to program Guard conditions so I don't lock my motor which opening or closing the claw. If the Claw is already open and If I press on the Joystick - Open Button, it locks the motor. Similarly if the Claw is closed, and If I press the Close Button, the motor gets locked. Basically here is my pseudo code based on the code to open the Claw to if (Open Button Pressed) { nMotorEncoder[motorB] = 0; // Reset the Motor Encoder of Motor B. nMotorEncoderTarget[motorB] = 60; // Set the target for Motor Encoder of Motor B to 60. motor[motorB] = 75; // Motor B is run at a power level of 75. while((nMotorRunState[motorB] != runStateIdle) && (nMotorEncoderTarget[motorB] < 60)) // While Motor B is still running: { // Do not continue. } motor[motorB] = 0; // Motor B is stopped at a power level of 0.}if (Close Button Pressed) { nMotorEncoder[motorB] = 0; // Reset the Motor Encoder of Motor B. nMotorEncoderTarget[motorB] = -60; // Set the target for Motor Encoder of Motor B to 60. motor[motorB] = 75; // Motor B is run at a power level of 75. while((nMotorRunState[motorB] != runStateIdle) && (nMotorEncoderTarget[motorB] > -60)) // While Motor B is still running: { // Do not continue. } motor[motorB] = 0; // Motor B is stopped at a power level of 0.}Is this correct or am I missing something basic. This is my first day of Programming RobotC, so please forgive me for not reading the manuals. thanks, Rajesh

Author:  BurningLights [ Thu Dec 05, 2013 1:32 pm ]
Post subject:  Re: RobotC Guard Conditions

Hmm, you're on the right track, but there's a few things you need to know. First of all, putting loops in your driver-control program is typically not a good idea because all of the rest of your code will have to wait for the loop to finish before it runs. For example, you would be unable to drive around your robot while the claw was moving. I'm not 100% sure on this, but I believe you would be able to set the nMotorEncoderTarget when the button is pushed and the motor would automatically stop moving when it reached that target. In other words, you wouldn't need to worry about putting any code in to make it stop.

As far as making sure you don't jam your motor, you could put in a simple Boolean variable. Something like:
 Code:bool bClawOpen = true; //Set to true or false depending on whether your claw starts opened or closed

Then, in your code for opening and closing the claw:
 Code:if (Open Button Pressed && !bClawOpen){  bClawOpen = true;  // Open the claw}else if (Closed Button Pressed && bClawOpen){  bClawOpen = false;  // Close the claw}

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/