To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.spyboticsOpen lugnet.robotics.spybotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / Spybotics / 251
250  |  252
Subject: 
Spybot studies: photoelectric eye
Newsgroups: 
lugnet.robotics.spybotics
Date: 
Thu, 6 Nov 2003 16:17:20 GMT
Viewed: 
6417 times
  
I've started a new project to explore the Spybot's capabilities, (now the John
Hansen has given me access to them ;). I plan to undertake s series of "studies"
which each explore a single feature or function. I'll post then as I go.

This first one doesn't use any of the new Spybot API. Nor is this really special,
and I'm sure someone has done it before.

All you need to compile it in BricxCC is set your brick to Spybot.


NQC code for photo_eye3.nqc follows:
/* Photo eye

    Use the Spybot LED and light sensor as a photoelectric eye.

    Physical configuration: fiber-optic cables are affixed to the light sensor
    and LED, and the ends of the cables are pointed directly at each other at
    a distance of one to 14 studs. (I didn't test beyond 14 studs.)
    I've posted an image at
http://www.brickshelf.com/gallery/ALittleSlow/Robotics/SpybotStudies/PhotoEye/photo_eye.jpg

    All relevant files are posted at
http://www.brickshelf.com/cgi-bin/gallery.cgi?f=62324

*/

#define TONE_LOW  220
#define TONE_HIGH 880

int photo_threshold;

task main()
{
      calibrate();

      start detect;

      // wait for bumper to end program
      while (! SENSOR_1) {
           Wait(10);
      }
      SetLight(LIGHT_OFF);
      StopAllTasks();
}

/* calibrate photo-eye
  *
  * This is a very simple calibration algorithm which sets the detection
  * threshold at half of the light which it receives from the LED.
  *
  * With the LED off, take a light reading.
  * Then turn the LED on and take another reading. The difference is the
  * amount of light contributed by the LED. Set the threshold at half that,
  * meaning if we can see less than half of the LED's light, something must
  * be obstructing it.
  */
void calibrate()
{

      SetLight(LIGHT_OFF);
      // default mode is SENSOR_MODE_PERCENT, I think.
      // default (and only possible) type is SENSOR_TYPE_LIGHT
      SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);
      int photo_high = SENSOR_2;
      SetLight(LIGHT_ON);
      Wait(1); // make sure the light is fully bright
      int photo_low = SENSOR_2;
      photo_threshold = (photo_high - photo_low) / 2;


}

/*
  * Task detect
  *
  * Plays a tone when it detects an interruption of the LED beam.
  * To provide some additional feedback,
  * the tone's frequency corresponds to the value of SENSOR_2. In a real
  * application, you might set a global flag instead of playing a tone.
  *
  * To save power, the LED is on only once every 110 ms or so.
  * Adjust the Wait() time to suit your precision or power
  * requirements.
*/
task detect()
{
      int photo_ambient;
      while (true) {
          photo_ambient = SENSOR_2;
          SetLight(LIGHT_ON);
          Wait(1); // wait for the light to get fully bright
          if (photo_ambient - SENSOR_2 < photo_threshold) {
              PlayTone(SENSOR_2, 10);
          }
          SetLight(LIGHT_OFF);
          Wait(10);
      }
}



Message has 1 Reply:
  Re: Spybot studies: photoelectric eye
 
(...) I think this is fantastic, Brian. I look forward to seeing future posts on the results of your studies. I am working with Dave Baum to incorporate the new Spybot API features into the official NQC API. One aspect of that involves documentation (...) (21 years ago, 6-Nov-03, to lugnet.robotics.spybotics)

6 Messages in This Thread:


Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR