Launcher Programming Guide (ROBOTC - TETRIX … · Note: A comment is not part of the code that the...
Transcript of Launcher Programming Guide (ROBOTC - TETRIX … · Note: A comment is not part of the code that the...
235
Introduction:
Inthisguide,theRangerBotwillbeprogrammedtofollowalinewhilecarryingaball.Uponsensingabinwithitsultrasonicsensor,itwilllaunchtheballintothebin.ThisguideisforusewiththeROBOTC®programminglanguage.
Getting Started:
1.Tostarttheprogram,typethe Main Taskfunctionfollowedbyanopening brace.
taskmain()
{
2.Declarethreeinteger variables asseeninthecodebelow.Besideeachvariable,therearetwoslashes(//)followedbywhatthatvariableisrepresenting.Thesemarkacomment.NoticethatcommentsappeargreenwhencodeistypedintoROBOTC.
Note:AcommentisnotpartofthecodethattheNXTBrickwillexecute,butithelpstheprogrammerbyallowingtheadditionofnotestodescribewhathasbeendone.
intnValue; //lightsensorvalue
intmValue; //sonarsensorvalue
intPosition; //servoarmposition
3.TypetheWait functionandsetittowait 50 millisecondstoallowtherobottoinitializemotorsandsensors.
wait1Msec(50);
4.Typethe servo position commandtoinitializethepositionoftheservo.Thismustbesettoavaluebetween0and255,representingapositionbetween0°and180°,theservo’sfullrangeofmotion.
servo[servo1]=128;
5.AddaWhile Looptowaituntilthetouchsensorhasbeentouchedinorderfor motorDand motorE tostartmoving.Ifthetouchsensorhasbeenpressedthenthevalueofthetouchsensor,atthatmoment,wouldbe1;otherwise,thevalueofthetouchsensorwillbezero.Thus,theconditionfortheWhile LoopstatementwouldbetocheckifthetouchsensorvalueisequaltozeroinordertosetbothmotorDandmotorEtoavalueofzero.
while(SensorValue[Touch]==0)
{
motor[motorE]=0;
motor[motorD]=0;
Launcher Programming Guide (ROBOTC®)
TETRIX®GettingStartedGuideExtensions
236
Launcher Programming Guide (ROBOTC®)
6.Setthedefinedvariablestoavalueanddisplaythemtothescreen.
a.SetthenValuevariabletothevalueofthelightsensor,andthemValuevariabletothevalueoftheultrasonicsensor.
b.UsetheNXTwritetoscreencommandtoputthemonthescreen.
Thewritetoscreencommandrequiresthreepiecesofinformation,calledparameters,inordertowritebothtextandavariabletothescreen:
i.Thelinenumber.
ii.Thetext(inquotations)thatincludesadisplayvariablecaller.
Note:Inthiscase,%disthedisplayvariablecaller.Intheplaceof%d,anintegervariableisdisplayedonthe`screen.
iii.Thevariablename.
Thisinformationisinsidethebracketsbesidethecommand,separatedbycommas.
nValue=SensorValue[LightSensor];
nxtDisplayTextLine(0,"LightValue:%d",nValue);
mValue=SensorValue[SonarSensor];
nxtDisplayTextLine(2,"SonarValue:%d",mValue);
7.Belowthetextthatwasjustwrittentothescreen,displayamessagethatsays“START: Press Touch.”Becausenovariablewillbedisplayed,onlytwoparametersneedtobesenttothefunction,thelinenumberandthetext.
nxtDisplayCenteredTextLine(4,"START:PressTouch");
8.Typeaclosing bracetoendtheWhileLoopinstep5.
}
9.Addaninfinite While Looptomakethecontainingcodeexecuteforever.
while(true)
{
10.Initializeboththemotorsthataremovingthetires,whicharemotorAandmotorB,tobeequaltozerousingtheMotorfunction.
motor[motorA]=0;
motor[motorB]=0;
ExtensionsTETRIX®GettingStartedGuide
237
11.Next,setthedefinedvariablestoavalueanddisplaythemtothescreen.SetthenValuevariabletothevalueofthelightsensorandthemValuevariabletothevalueoftheultrasonicsensor.UsetheNXTwritetoscreencommandtoputthemonthescreen.ThisissimilartothecodethatwastypedinStep 6.
nValue=SensorValue[LightSensor];
nxtDisplayTextLine(0,"LightValue:%d",nValue);
mValue=SensorValue[SonarSensor];
nxtDisplayTextLine(2,"SonarValue:%d",mValue);
12.Belowthetextthatwasjustwrittentothescreen,displayamessagethatsays“Searching For Target.”Becausenovariablewillbedisplayed,onlytwoparametersneedtobesenttothefunction,thelinenumberandthetext.
nxtDisplayCenteredTextLine(4,"Searching");
nxtDisplayCenteredTextLine(5,"ForTarget");
Line Follower Code:
13. Addtheline-followingcodethatwascreatedinLineFollowerExtensionPart3.
if(SensorValue[LightSensor]<50)
{
motor[motorD]=30;
motor[motorE]=0;
}
else
{
motor[motorE]=30;
motor[motorD]=0;
}
Launching an Object:
14. Addaconditionalstatementtocheckiftherobotiswithin25cmofthetarget.Iftherobotiswithin25cmofthetarget,display“Found! Activate Launch”ontherobotscreenandstopmotorD andmotorE.AddaWaitfunctiontopauseforonesecondtogivethemotorstimetostopbeforethenextactionisexecuted.
if(SensorValue[SonarSensor]<25)
{
nxtDisplayCenteredTextLine(4,“Found!”);
nxtDisplayCenteredTextLine(5,“ActivateLaunch”);
motor[motorE]=0;
motor[motorD]=0;
wait1Msec(1000);
Launcher Programming Guide (ROBOTC®)
TETRIX®GettingStartedGuideExtensions
238
15.Spinboththetireson motorA andmotorB usingtheMotorfunction,andthenpausetheprogramforthreeseconds.Setthemotorpower,forbothmotors,tobeequalto100.
motor[motorA]=100;
motor[motorB]=100;
wait1Msec(3000);
16.AddaFor Loopthatmakesthearmservomovefromitscurrentpositionat128toalowerpositionof170,waiting10millisecondsbetweeneachmovement.
for(Position=128;Position<170;Position++)
{
servo[servo1]=Position;
wait10Msec(1);
}
wait1Msec(1000);
17.AddaFor Loopthatmakesthearmservomovefromitscurrentpositionat170toahigherpositionof128,waiting10millisecondsbetweeneachmovement.
for(Position=170;Position>128;Position--)
{
servo[servo1]=Position;
wait10Msec(1);
}
18.Next,stopboththemotorsthataremovingthetires,whicharemotorAandmotorB,usingtheMotorfunction.
motor[motorA]=0;
motor[motorB]=0;
19.Next,addaWhile Looptohavetherobotwaituntilthetargethasbeenmoved25cmormorefromtherobot.Thus,theconditionfortheWhileLoopstatementwouldbetocheckifthesonarsensorvalueislessthan25inordertosetbothmotorD and motorEtoavalueof0.
while(SensorValue[SonarSensor]<25)
{
motor[motorE]=0;
motor[motorD]=0;
Launcher Programming Guide (ROBOTC®)
ExtensionsTETRIX®GettingStartedGuide
239
20.Next,displaythevalueofthelightsensorandthesonarsensorontheNXTscreen.Tosavetime,copyandpastethecodefromStep6.
nValue=SensorValue[LightSensor];
nxtDisplayTextLine(0,"LightValue:%d",nValue);
mValue=SensorValue[SonarSensor];
nxtDisplayTextLine(2,"SonarValue:%d",mValue);
21.Belowthetextthatwasjustwrittentothescreen,displayamessagethatsays“To Continue: Move Target.”Becausenovariablewillbedisplayed,onlytwoparametersneedtobesenttothefunction,thelinenumberandthetext.
nxtDisplayCenteredTextLine(4,"ToContinue:");
nxtDisplayCenteredTextLine(5,"MoveTarget");
End the program:
22.Typeaclosing bracetoendtheWhileLoopinStep19.
}
23.Typeaclosing bracetoendtheconditionalstatementinStep14.
}
24.Typeaclosing bracetoendtheinfiniteWhileLoopfromStep9.
}
25.Typeaclosing bracetoendtheprogram.
}
Completed Code:
taskmain()
{
intnValue;//lightsensorvalue
intmValue;//sonarsensorvalue
intPosition;//servoarmposition
wait1Msec(50);//Theprogramwaits50millisecondstoinitializethelightsensor.
servo[servo1]=128;//Initialservoposition
Launcher Programming Guide (ROBOTC®)
TETRIX®GettingStartedGuideExtensions
240
Completed Code (continued):
//// Wait for touch sensor to be pressed to start.
while (SensorValue[Touch] == 0)
{
motor[motorE] = 0; // Motor B is off.
motor[motorD] = 0; // Motor C is off.
nValue = SensorValue[LightSensor];
nxtDisplayTextLine(0, "Light Value: %d", nValue); // update display
mValue = SensorValue[SonarSensor];
nxtDisplayTextLine(2, "Sonar Value: %d", mValue); // update display
nxtDisplayTextLine(4, "START:Press Touch");
}
while(true) // Infinite loop
{
motor[motorA] = 0; // left tire off
motor[motorB] = 0; // right tire off
nValue = SensorValue[LightSensor];
nxtDisplayTextLine(0, "Light Value: %d", nValue);
mValue = SensorValue[SonarSensor];
nxtDisplayTextLine(2, "Sonar Value: %d", mValue);
nxtDisplayCenteredTextLine(4, "Searching");
nxtDisplayCenteredTextLine(5, "For Target");
//// Line following Here;
if(SensorValue[LightSensor] < 50) // If the light sensor reads a value less than 50:
{
motor[motorD] = 30; // Motor D is run at a 30 power level.
motor[motorE] = 0; // Motor E is run at a 0 power level.
}
else // If the light sensor reads a value greater than or equal to 50:
{
motor[motorE] = 30; // Motor E is run at a 30 power level.
motor[motorD] = 0; // Motor D is run at a 0 power level.
}
Launcher Programming Guide (ROBOTC®)
ExtensionsTETRIX® Getting Started Guide
241
Completed Code (continued):
Launcher Programming Guide (ROBOTC®)
//// Sonar ranging here; Stop at target distance, start launcher and lower launch arm
if(SensorValue[SonarSensor] < 25) // Target sensed at 25cm, launch balls.
{
nxtDisplayCenteredTextLine(4, "Found!"); // update display
nxtDisplayCenteredTextLine(5, "Activate Launch"); // update display
motor[motorE] = 0; // Motor E is off
motor[motorD] = 0; // Motor D is off
wait1Msec(1000); // Pause for 1 second
motor[motorA] = 100; // Spin left tire
motor[motorB] = 100; // Spin right tire
wait1Msec(3000); // Pause for 3 seconds
for (Position = 128; Position < 170; Position++)
{
servo[servo1] = Position; // lower dispenser arm
wait10Msec(1);
}
wait1Msec(1000); // Pause for 1 second.
for (Position = 170; Position > 128; Position--)
{
servo[servo1] = Position; // raise dispenser arm
wait10Msec(1);
}
motor[motorA] = 0; // off left tire
motor[motorB] = 0; // off right tire
while(SensorValue[SonarSensor] < 25) // loop here waiting for target to be moved
{
motor[motorE] = 0; // Motor B is off
motor[motorD] = 0; // Motor C is off
nValue = SensorValue[LightSensor];
nxtDisplayTextLine(0, "Light Value: %d", nValue); // update display
mValue = SensorValue[SonarSensor];
nxtDisplayTextLine(2, "Sonar Value: %d", mValue); // update display
nxtDisplayCenteredTextLine(4, "To Continue:"); // update display
nxtDisplayCenteredTextLine(5, "Move Target"); // update display
}
}
}
}
TETRIX® Getting Started GuideExtensions