Drunken tribot v2
From ZENotes
/* learning NXC with Lola THE MARCH OF THE DRUNKEN TRIBOT v2 Simple escape-the-room by bumper programme with Random Moves. */ #define TIME_MOVE 1000 // time used to rethink the situation, backing off... task main() // start of real code { repeat(4) // Say, we bump 4 times & then we stop { SetSensor(IN_1,SENSOR_TOUCH); // We'll need the Touch Sensor in Port 1, OK? do // do the following until... { OnFwd(OUT_AC, 50); // Forward 50% Wait(Random(1000)+500); // For a random amount of time + 1000 (gives a minimal value) OnRev(OUT_A, Random(90)); // turn away by reversing engine A random between 0 & 90 OnRev(OUT_C, Random(90)); // Turn away by reversing motor C at a differennt value Wait(Random(400+200)); // On a random timeframe too. } until (SENSOR_1 == 1); // ...BUMP! CRASH! Off(OUT_AC); // We need to switch off the engines to get a neater result. Wait(100); // Little subtle break, motors stopped; OnRev(OUT_AC, 75); // Back off, you thingy... Wait(TIME_MOVE); // for the amount of time and speed as defined above if (Random() >= 0) // IF statement, what happens if... the value is BELOW Zero: { OnRev(OUT_C, Random(50)+50); // Motor C backs off, at least 50% + random of 50 remaining OnFwd(OUT_A, Random(80)+20); // Motor A backs off, at least 20% + random of 80 remaining } else // ELSE statement: returned value is ABOVE zero. { OnRev(OUT_A, Random(50)+50); // Motor A backs off, at least 50% + random of 50 remaining OnFwd(OUT_C, Random(80)+20); // Motor C backs off, at least 20% + random of 80 remaining } Wait(Random(600)+400); // Even the duration of the re-direction is random. } Off(OUT_AC); // after 4 "rounds", software reach this point: Off A & C } //End of (drunken) v2 code
Lola 15:37, 1 July 2008 (UTC)