At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Robotics » LEGO Mindstorms

Proximity Detection Learning Using NQC

By using Dave Baum's Not Quite C (NQC), it is possible to crudely set up a IR proximity detector. How do we do this? It is surprisingly simple. We need some way to transmit and receive and IR signals. The RCX has an IR port that can both transmit and receive IR messages. We don't have complete control over receiving IR signals, so if we simply pulse IR messages out of the RCX IR port we can then detect them with our light sensor (that happens to be very sensitive to IR!).

The Robot

The robot that I built was basically a Roverbot as described in the Constructopedia with the standard four wheel setup. At the front of robot I set up a light sensor and touch sensor next to each other. What I wanted was for the robot to adapt its proximity threshold to the light settings. The touch sensor is used to help the robot calculate the optimal threshold.

Threshold?

What is the threshold? We will get our IR port to send out a signal roughly every 1/10th second. If we are close enough to something, the IR signal will reflect back and hit our light sensor which will register an abrupt change its reading. The threshold is the minimum change between readings. Let us look at a sample datalog entry:

Briefly looking at the data, you can see there is a rough drop of 200 every time a pulse is sent out. For standard proximity detection, 200 is a good constant to use. I wanted to add a little intelligence to the whole process...

The Program

I thought it'd be cool if the robot tested different proximities to find an optimal proxmity setting. In the end, the method I used was pretty crude, since we start with a proximity threshold of 800, and just halve it every time it collides with something. There are two conditions under which the robot will edit the threshold: if it crashes into a wall before even detecting it, and if it crashes into a wall while floating the motors (I decided not to stop the motors outright - this gives more realistic motion, in my opinion).

Here is the NQC code:

//////////////////////////////////////////////////
//  Program:      Learning Proximity Robot
//  Author:       James Matthews
//  Date:         23rd August, 2000
//  URL:          http://www.generation5.org/
//  
//  This is a simple program using a threshold to 
//  control the proximity. This means that it can
//  used in any lighting and environment, and it will
//  "adapt". The mechanism is incredibly simple, so
//  no guareentees how good it is!! It also plays a
//  little "victory chant" when it doesn't collide
//  upon stopping.

int ls_lastlevel;    // To store the previous light sensor level
int ls_threshold;    // The currently used threshold
int collided;        // Did we collide this session?

task Send_Signal() {
  while(true) 
    {SendMessage(0); Wait(10);}
}

task Check_Signal() {

  while(true) {
    ls_lastlevel = SENSOR_2;
      if (SENSOR_2 > ls_lastlevel + ls_threshold) {
          collided = 0;
          Float(OUT_A+OUT_C);

          start Check_Collision;
          Wait(100);          
          stop  Check_Collision;
          
          if (collided == 0) {
             PlaySound(5);
             StopAllTasks();          
          }
      }
      
      if (SENSOR_1 == 1) {
         ls_threshold = ls_threshold / 2;
         
         OnRev(OUT_A+OUT_C);
         Wait(200);
         OnFwd(OUT_A+OUT_C);
      }
  }
}

task Check_Collision() {
  while (true) {
    if (SENSOR_1 == 1) {
       ls_threshold = ls_threshold / 2;
       collided = 1;
       
       OnRev(OUT_A+OUT_C);
       Wait(200);
       OnFwd(OUT_A+OUT_C);
    }
  }
}

task main() {
  ls_threshold = 800;
  
  // Setup the sensors
  SetSensorType(SENSOR_1, SENSOR_TYPE_TOUCH); 
  SetSensorType(SENSOR_2, SENSOR_TYPE_LIGHT); 
  SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);

  OnFwd(OUT_A+OUT_C);
  
  start Send_Signal;
  start Check_Signal;
}
Pretty simple. I used the excellent RCX Command Center to program, compile, and download the program as well as collect the datalog information. Thanks to Mark Overmars for his excellent program and huge tutorial on NQC programming.

Submitted: 23/08/2000

Article content copyright © James Matthews, 2000.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -