Drunken tribot v3

From ZENotes
Jump to: navigation, search
/*                       learning NXC with Lola

                   THE MARCH OF THE DRUNKEN TRIBOT v3                    
 
        Simple escape-the-room by bumper programme with Random Moves.
                And Tridiot hates Green Tiles too, makes him puke!                   */ 
 
mutex moveMutex;                      // start of code 

// Case 1: Poking Around 
// Tridiot tries to move forward. That ain't an easy task.
task poke()
{
  while (true)                        // Don't Ask; remove it & it won't compile
  {
    Acquire(moveMutex);               // Grab Control over the engines
    do                                // do the following until... something happens!
    {
      OnFwd(OUT_AC, 50);              // Forward 50%
      Wait(Random(1000)+500);         // For a random amount of time + 500 (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.
    }
    while (SENSOR_1 == 0);            // This one is not supposed to be neccessary.
    Release(moveMutex);               // Give "back" the motors to the other snippets
  }
} 

// Case 2: the Puke
// That's when Tridiot steps over a green tile in my office, the color makes him puke... 
task puke()                           // LightSensor (port 3) activated
{
  while (true)
  {
    Acquire(moveMutex);               // Grab Control over the engines
    if (Sensor(IN_3) > 27)            // YOU PROBABLY WILL HAVE TO EDIT THIS ONE
    {
      OnRev(OUT_C, 45);               // Back up Motor_C
      Wait(400);                      // (briefly)
      until(Sensor(IN_3) <= 30);      // Check sensor: Am I away of this most horrible colour?
      OnFwd(OUT_AC, 35);              // If so, move on! Pfew, I though I was going to be sick!
    }
    Release(moveMutex);               // Give "back" the motors to the other snippets
  }
} 

// Case 3: the Crash 
// Tridiot bumps into something, Aargh, sorry sir, and decides to move away. or so he tries.
task crash()                          // Touch Sensor (port 1) activated
{
  while (true)                        // Don't Ask; remove it & it won't compile
  {
    Acquire(moveMutex);               // Grab Control over the engines
    if (SENSOR_1 == 1);               // When CRAAAASH
    OnRev(OUT_AC, 75);                // Back off, you thingy...
    Wait(750);                        // for the amount of time and speed as defined
    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
    }
    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
    }
    Wait(Random(600)+400);            // Even the duration of the re-direction is random.
    Release(moveMutex);               // Give "back" the motors to the other snippets
  }
} 

//totally superfluous "humming" thread
//should be re-worked in a hiccup noise, that would fit!
#define TIME 200
#define MAXVOL 7
#define MIDVOL 3
#define pause_4th Wait(TIME)
#define pause_8th Wait(TIME/2)
#define note_4th \
  PlayFileEx("! Click.rso",MIDVOL,FALSE); pause_4th
#define note_8th \
  PlayFileEx("! Click.rso",MAXVOL,FALSE); pause_8th

task hum()
{
  while (true)
  {
  Wait(2000);
  note_4th;
  note_8th;
  note_8th;
  note_4th;
  note_4th;
  pause_4th;
  note_4th;
  note_4th;
  Wait(100);
  }
} 

// Actual Starting "mother" thread: lauches the others.
task main()
{
  Precedes(poke, puke, crash, hum);   // These are the threads to launch
  SetSensorTouch(IN_1);               // These are the Sensors we'll use:
  SetSensorLight(IN_3);               // Touch for "Crash" and Light for "Puke"
}

// EoC. Eurgh.

Lola 15:43, 1 July 2008 (UTC)

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox