A Robotics Project Page
Google
 
Web    www.PhilBot.com
My other sites
  • OurCoolHouse
  • Phil's Resume
  • Web Portfolio

  • [Home] [Projects] [Hardware] [Software] [Books] [Links] [Downloads]


    BOB: Software: Lesson B - Proximity Detector Test.          [Back to BOB Software]        [Back to BOB index]

    Now that we have the LED's tested (see [Lesson A]) we can more easily test some of the other hardware. 
    First on the list are the three digital Proximity Detectors.  Just like the ones on the USS Enterprise, BOB's Proximity detectors are used to sense when another object is nearby.  BOB's sensors come from the factory calibrated to detect objects that are less than 40cm (16") away.  When they see an object in range, they signal it by sending their digital outputs LOW.  BOB has two detectors on the front, and one on the rear.

    This lesson will show how we can use an LED to indicate when a Proximity detector sees an object.  For the sake of simplicity the program will be written to poll the sensors, but in a real situation I'd probably use a virtual circuit to tie the LED to the sensor.  This would eliminate the need to poll the sensor.  More on Virtual Circuits in a later lesson.

    As always, in this program (PBot_BOB-B_VC6.osc) I start using the I/O definitions described on the main BOB [Software page]. 
    I'm going to need the LED and Prox. Sensor definitions.  These are:

    //-------------------------------------------------------
    // BOB I/O Port definitions
    //-------------------------------------------------------
    Const   IO_PROXIMITY_LEFT       = 8;    // Left Sharp Digital Prox sensor
    Const   IO_PROXIMITY_RIGHT      = 9;    // Right Sharp Digital Prox sensor
    Const   IO_PROXIMITY_REAR       = 10;   // Rear Sharp Digital Prox sensor

    Const   IO_LED1                 = 12;   // General Purpose LED 1
    Const   IO_LED2                 = 13;   // General Purpose LED 2
    Const   IO_LED3                 = 14;   // General Purpose LED 3
    Const   IO_LED4                 = 15;   // General Purpose LED 4
    Const   IO_LED_GROUP            = 1;    // LED I/O Group 1    (Lines 8-15)
    Const   IO_LED_NIBBLE           = 1;    // LED I/O High Nibble (Lines 12-15)

    In this lesson I'm going to use the Individual LED definitions (rather than the group of 4) because each LED is updated independently.  See Lesson A for an example of using the LEDs as a group.

    Here is the code I use to declare the various Objects that I need for this Lesson:

    //-----------------------------------------------------------------
    // User Objects.  Use these to access BOB hardware.
    //-----------------------------------------------------------------
    oCountDownO ActionTimer = New oCountDownO;  // User Timer   

    // LED Object (accessed as individual bits)
    oDIO1       LED1        = New oDIO1;    // Driver for LED1
    oDIO1       LED2        = New oDIO1;    // Driver for LED2
    oDIO1       LED3        = New oDIO1;    // Driver for LED3

    // Digital Proximity sensors
    oDIO1     LeftEye       = New oDIO1;    // Left Prox Sensor
    oDIO1     RightEye      = New oDIO1;    // Right Prox Sensor
    oDIO1     RearEye       = New oDIO1;    // Rear Prox Sensor

    Notice that all of the hardware devices in this lesson are "oDIO1" objects.  This is because they are all just single bit digital inputs or outputs.
    In my programs, I always use the SetupIO( ) function configure the OOPic "Objects" required to interface to the hardware. 
    Here is the object Setup code for this lesson:.

    //-----------------------------------------------------------------
    //  SetupIO()
    //  Configure all the IO Objects here
    //-----------------------------------------------------------------
    Void    SetupIO( Void )
    {
        // --------------------------------
        // setup the ActionTimer Timer for 1/10th Second ticks 
        // Preload counter with 5 second delay
        // --------------------------------
        ActionTimer.PreScale    = CLOCK_PRESCALE;
        ActionTimer.ClockIn.Link(ooPIC.Hz60);
        ActionTimer             = FIVE_SECONDS;
        ActionTimer.Operate     = cvTrue;
        
        // --------------------------------
        // setup the LED Drivers
        // --------------------------------
        LED1.IOLine             = IO_LED1;
        LED1.Direction          = cvOutput ;
        LED2.IOLine             = IO_LED2;
        LED2.Direction          = cvOutput ;
        LED3.IOLine             = IO_LED3;
        LED3.Direction          = cvOutput ;

        // --------------------------------
        // setup the Eye proximity sensors
        // --------------------------------
        LeftEye.IOLine          = IO_PROXIMITY_LEFT;
        LeftEye.Direction       = cvInput;  
        RightEye.IOLine         = IO_PROXIMITY_RIGHT;
        RightEye.Direction      = cvInput;  
        RearEye.IOLine          = IO_PROXIMITY_REAR;
        RearEye.Direction       = cvInput;  
    }

    Notice that the first thing I always do is configure the ActionTimer object.  It's setup to count down to zero in 1/10th of a second.  I use this capability a lot to pace my programs, or just to insert standard delays.

    Then I configure the LED objects.  These are "oDIO1" objects which means a "1 bit Digital Input/Output" object.  I assign an IOLine and direction (cvOutput) for each LED.  Finally I configure the Proximity detectors.  Once again I assign an IOLine and direction (cvInput) for each.

    Now for the actual program.  Main( ) is the name given to the function that is called when the program starts.  Lesson B's main is quite simple: First it calls SetupIO( ), then it waits for the 5 second timer to expire, and finally it enters an infinite loop polling the Proximity sensors.  The loop reads each sensor, inverts its value (since a LOW indicates a detection) and then uses this value to set the appropriate LED object.

    //-----------------------------------------------------------------
    //  BOB Main Program
    //-----------------------------------------------------------------
    Void Main(Void)
    {
        // Always set up the required IO first.
        SetupIO();
        
        // Wait for 5 second "Start Up" to elapse
        While(ActionTimer.NonZero);
            
        // Display Prox state on LEDs
        While(cvTrue)
        {
            LED1.Value  = ~LeftEye.Value;   // Set LED1 based on Left Eye
            LED2.Value  = ~RearEye.Value;   // Set LED2 based on Rear Eye
            LED3.Value  = ~RightEye.Value;  // Set LED3 based on Right Eye
        }
    }

    To test the program, I simply wave my hand in front of each proximity sensor, and verify that the correct LED lights up.

    Don't forget the full source code for this example is available on the [Download] page (PBot_BOB-B_VC6.osc).

    Next, in [Lesson C] I will experiment with the Motor Drives.

     

    Web content is copyright © PhilBot.com 2005, Deep Creek Lake, MD.
    Contact: Phil Malone 301.387.2331, webmaster@PhilBot.com