View unanswered posts | View active topics It is currently Sun Dec 21, 2014 6:57 am






Reply to topic  [ 2 posts ] 
RobotC Guard Conditions 
Author Message
Rookie

Joined: Wed Dec 04, 2013 9:04 pm
Posts: 2
Post 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


Wed Dec 04, 2013 9:20 pm
Profile
Professor
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 214
Post 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
}

_________________
FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
FTC Team 7468 Blue Chariots of Fire - Mentor (2014-2015)
Check out our team website at http://bluechariotsoffire.com/.


Thu Dec 05, 2013 1:32 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.