Zip-Bam-Bot Version XK2 - Creating a Program for the Ram-and-Run Approach
(Page 9 of 11 )
Whether you call it ram-and-run or hit-and-run, this approach resembles a sort of guerrilla warfare. Once the touch sensor has registered a hit, the program backs up the sumo-bot, and then turns it around. The effectiveness of this approach (in terms of winning the round) is debatable, but it sure beats getting beat!
To start off, let’s modify our attacking function. Remember that once the touch sensors have registered a hit, XK2 needs to run in the opposite direction. With the addition of a slight amount of waiting time to allow the momentum of XK2 to do its work, the new Win() function looks like this:
// constants #define BACK_TIME 50 #define TURN 40
// this function avoids the other sumo-bot void Win() { Wait(20); Rev(Left+Right); Wait(BACK_TIME); Fwd(Right); // this turns XK2 in the other direction Wait(TURN); OnFwd(Left+Right); ClearTimer(0); }
With this function, XK2 will not continually ram, but instead will ram and run. Now how about that line-avoiding function? Modifying it, too, would not be such a bad idea. Instead of turning right, let’s turn left, just to do something different. With this change, here’s the new line-avoiding function:
// this function avoids the line void Avoid() { OnRev(Left+Right); Wait(LIGHT_BACK); Fwd(Right); Wait(TURN); OnFwd(Left+Right); ClearTimer(0); }
That’s interesting, but for good measure, let’s also modify the maneuvering function. A little change of the motor direction, a little change of the random timing, and we have ourselves a new version of the Change() function:
Now that we have made these changes, the new functions are combined into the program XK2_Access_Control_Two.nqc, which gives us a completed ram-and-run NQC program. Listing 5-2 shows the new program.
Listing 5-2. XK2_Access_Control_Two.nqc
// XK2_Access_Control_Two.nqc // A sumo-bot program for Zip-Bam-Bot Version XK2
// this task watches for the line task LWatch() { // highest priority SetPriority(0);
// infinite loop while(true) { until(See<=threshold); // until we see the line
acquire(ACQUIRE_USER_1) { Avoid(); // avoid the line } } }
// this task watches the touch sensors task Crash() { // second highest priority SetPriority(1);
// infinite loop while(true) { // until left or right touch sensors are pressed until(LBump|RBump==1);
acquire(ACQUIRE_USER_1) { Win(); // take control and crash repeatedly } } }
// this task introduces some random turns task Maneuver() { // third highest priority SetPriority(2);
// infinite loop while(true) { // until timer is >= 2.5 seconds which is determined by LIMIT until(Timer(0)>=LIMIT);
acquire(ACQUIRE_USER_1) { Change(); // execute our new function } } }
// this function avoids the other sumo-bot void Win() { Wait(20); Rev(Left+Right); Wait(BACK_TIME); Fwd(Right); // this turns XK2 in the other direction Wait(TURN); OnFwd(Left+Right); ClearTimer(0); }
// this function avoids the line void Avoid() { OnRev(Left+Right); Wait(LIGHT_BACK); Fwd(Right); Wait(TURN); OnFwd(Left+Right); ClearTimer(0); }
// this function changes the direction of the robot void Change() { Rev(Right); Wait(TURN+Random(30)); Fwd(Right); ClearTimer(0); }
// this is the calibrating function void Calibrate() { until(RBump==1); line=See; threshold=line+AMOUNT; until(RBump==0); PlaySound(SOUND_CLICK); until(RBump==1); until(RBump==0); PlaySound(SOUND_CLICK); Wait(TURN); }
There you have it: the essence of the ram-and-run approach. As you can see, the ram-and-run behavior itself is actually fairly simple. And watching it in action is quite interesting—so why don't we do some testing?
This chapter is from Competitive MINDSTORMS: A Complete Guide to Robotic Sumo Using LEGO MINDSTORMS, by David Perdue (Apress, 2004, ISBN: 1590593758). Check it out at your favorite bookstore today. Buy this book now.
KEITHLEE2/home/servers/www.devhardware.com/www/zdeconfigurator/configs/INFUSIONSOFT_OVERLAY.php/home/servers/www.devhardware.com/www/zdeconfigurator/configs/ OFFLOADING INFUSIONSOFTLOADING INFUSIONSOFT 1debug:overlay status: OFF overlay not displayed overlay cookie defined: TI_CAMPAIGN_1012_D OVERLAY COOKIE set: status off