Tim Rueger's entry for the TexLUG Austin Lego Sumo Robotics contest, held 6/12/05. See: http://www.brickshelf.com/cgi-bin/gallery.cgi?f=132763 http://www.io.com/~rueger/lego/texlugaustin/meetings/0506.html for contest photos and a summary of the contest. The contest rule set is found here: http://www.io.com/~rueger/lego/texlugaustin/sumo2005/sumobot_ruleset_050517.pdf NQC code is below. ------------------------------------------------------------ // tim's 2-pound sumo bot for 050612 contest // avoid black lines, turn toward a bump // gearshift down when bumped // gearshift up when hitting the border // wiggle out of a jam after a timeout // motors #define Right OUT_A // right motor #define Shifter OUT_B // gear shifter motor #define Left OUT_C // left motor // sensors: piggyback light and touch #define LTouch SENSOR_2 #define LLight SENSOR_2 #define RTouch SENSOR_3 #define RLight SENSOR_3 #define Touching 90 // touch sensor reads 100 when // read as a light sensor #define ShiftTime 200 // how long it takes to shift gears: // 200/100 = 2.0 seconds #define BorderReverseTime 75 // back up for 75/100 = 0.75 second #define BorderTurnTime 150 // turn for 150/100 = 1.5 seconds #define BumpReverseTime 37 // back up for 37/100 = 0.37 second #define BumpTurnTime 37 // turn for 37/100 = 0.37 second #define PanicTimeOut 50 // wiggle around if nothing happens after // 50/10 = 5.0 seconds #define PanicTurnTime 75 // turn for 75/100 = 0.75 second // priority settings #define LWatchLevel 0 #define RWatchLevel 1 #define LBumpLevel 2 #define RBumpLevel 3 #define PanicLevel 4 // power levels #define MaxFwd 7 #define MaxRev 7 #define MaxTurn 7 // light sensor calibration #define LIGHTMARGIN 7 int line=0; int threshold=0; // the state of the transmission - 1=high gear, 0=low gear int gearshift=0; task main() { // initialize sensors SetSensor(LTouch,SENSOR_LIGHT); SetSensor(RTouch,SENSOR_LIGHT); SelectDisplay(DISPLAY_SENSOR_2); // display LLight reading // calibrate left light sensor with right touch Calibrate(); // another right touch says "go!" until(RTouch>Touching); // wait for right touch until(RTouchTouching); // wait for right touch line=LLight; // read left light threshold=line+LIGHTMARGIN; // set light sensor threshold until(RTouchTouching); if (gearshift == 1) { acquire(ACQUIRE_USER_1) { LTurn(); // turn toward the left and downshift } } ClearTimer(0); } } // turn toward the left, then gearshift down if needed void LTurn() { SetPower(Left+Right,MaxRev); // back up OnRev(Left+Right); Wait(BumpReverseTime); Off(Left+Right); SetPower(Left+Right,MaxTurn); // turn left OnFwd(Right); OnRev(Left); Wait(BumpTurnTime); SetPower(Left+Right,MaxFwd); // go forward OnFwd(Left+Right); if (gearshift==1) { gearshift=0; SetPower(Shifter,MaxFwd); OnRev(Shifter); // gearshift down Wait(ShiftTime); Off(Shifter); } } // watch for a touch on the right task RBump() { SetPriority(RBumpLevel); while(true) { // watch for a touch on the right touch sensor until(RTouch>Touching); if (gearshift == 1) { acquire(ACQUIRE_USER_1) { RTurn(); // turn toward the right and downshift } } ClearTimer(0); } } // turn toward the right, then gearshift down if needed void RTurn() { SetPower(Left+Right,MaxRev); // back up OnRev(Left+Right); Wait(BumpReverseTime); Off(Left+Right); SetPower(Left+Right,MaxTurn); // turn right OnFwd(Left); OnRev(Right); Wait(BumpTurnTime); SetPower(Left+Right,MaxFwd); // go forward OnFwd(Left+Right); if (gearshift==1) { gearshift=0; SetPower(Shifter,MaxFwd); OnRev(Shifter); // gearshift down Wait(ShiftTime); Off(Shifter); } } // the panic timer - if nothing has happened, // then try to get out of the situation task CheckPanic() { SetPriority(PanicLevel); while(true) { // if the panic timer goes off until(Timer(0)>=PanicTimeOut); acquire(ACQUIRE_USER_1) { Wriggle(); // try to get out of trouble } } } // try to get out of a hangup position void Wriggle() { Off(Left); SetPower(Right,MaxTurn); // right motor reverse OnRev(Right); Wait(PanicTurnTime); Off(Right); SetPower(Left,MaxTurn); // left motor reverse OnRev(Left); Wait(PanicTurnTime); Off(Left); SetPower(Right,MaxTurn); // right motor reverse OnRev(Right); Wait(PanicTurnTime); Off(Right); SetPower(Left,MaxTurn); // left motor reverse OnRev(Left); Wait(PanicTurnTime); Off(Left); SetPower(Right,MaxTurn); // right motor reverse OnRev(Right); Wait(PanicTurnTime); Off(Right); SetPower(Left,MaxTurn); // left motor reverse OnRev(Left); Wait(PanicTurnTime); SetPower(Left+Right,MaxTurn); // turn left OnFwd(Right); OnRev(Left); Wait(PanicTurnTime); SetPower(Left+Right,MaxFwd); // and go forward OnFwd(Left+Right); ClearTimer(0); }