Build an Onboard Interface

The onboard interface acts as a software driver. It is used to interpret commands sent to a remotely connected robot from an MSRS service. What constitutes the interface depends on the type of robot you are interfacing with. For example, the ARobot uses a BS2 as the coprocessor. The BS2 has only 32 bytes of memory, and only 26 of those are available to a downloaded program. Obviously, neither MSRS nor the .NET Framework is able to execute on this coprocessor. For this reason, we need software that can execute on the robot and pass information back and forth to a remote development machine capable of running MSRS.

The software we need is a BASIC program, which can be written and downloaded to the ARobot using a Basic Stamp Editor, such as the one available as a free download from the Parallax Web site (www.parallax.com/html_pages/downloads/software/software_basic_stamp.asp). The BASIC interface program is responsible for accepting commands from the MSRS services and returning information concerning the whisker sensors. The first thing the interface program does is make the speaker beep twice and turn on the green light-emitting diode (LED) light. This signals that the ARobot is ready to start accepting commands from MSRS. The first portion of this code should look like the following:

Start:
  ' Performs 2 short beeps when the robot starts up
  FREQOUT SPEAKER,150,2000
  PAUSE 10
  FREQOUT SPEAKER,150,2000
  LOW SPEAKER                        'turn off speaker.
  LOW GREENLED          'turn on the Green LED light

The next thing this and any interface program should do is begin a loop. Within the loop should be code that accepts input from the serial port and performs commands on the robot based on the data it receives. The loop should also contain code that returns data from sensors and actuators. For the interface program used by the ARobot, the code looks like the following:

Main:
'-----------------------------------------------------------------
' Main routine - this loop should run continuously
'-----------------------------------------------------------------
DO
  ' Get the next command by using the SERIN command
  ' to receive in the serial data sent in a packet
  ' The packet should contain no more than 4 bytes
  ' after the MSRS header
  SERIN Console, CMD_BAUD,300, NoCmd, [WAIT ("MSRS"), STR inBuffer 4]

  ' Process the incoming command by looking up what subroutine
  ' it is calling
  LOOKDOWN inBuffer(0), = [DRIVE_ROBOT, ON_RED_LED, OFF_RED_LED, ON_GREEN_LED,
OFF_GREEN_LED, SET_SPEAKER], routine
  ON routine GOSUB     DriveRobot, OnRedLED, OffRedLED, OnGreenLED, OffGreenLED, SetSpeaker

  PAUSE 100

 NoCmd: ' We want to always return whisker values

  outBuffer(0) = LEFT_WHISKER       'Left whisker
  outBuffer(1) = RIGHT_WHISKER      'Right Whisker
  outBuffer(2) = 0
  SEROUT Console, CMD_BAUD, ["ROB", STR outBuffer 4]

LOOP

Based on data it receives from MSRS, the interface program executes a subroutine. This subroutine contains specific functions such as driving the robot, setting the speaker, and turning on and off the LED lights. For example, the DriveRobot subroutine accepts parameters representing the distance, direction, and speed from the input buffer. Based on the values it receives, it sends commands to the BS2 coprocessor using SEROUT commands. The code for the DriveRobot subroutine is as follows:

DriveRobot:

  'First, get the input variables
  tDistance = inBuffer(1)
  tDirection = inBuffer(2)
  tSpeed = inBuffer(3)

  SELECT tDirection
    CASE "S"      'Stop the robot
      tmp1 = "0"
      tSteer(0) = "0"
      tSteer(1) = "0"
    CASE "B"      'Go backwards
      tmp1 = "0"
      tSteer(0) = "8"
      tSteer(1) = "0"
    CASE "F"      'Go forwards
      tmp1 = "1"
      tSteer(0) = "8"
      tSteer(1) = "0"
    CASE "R"      'Go right
      tmp1 = "1"
      tSteer(0) = "0"
      tSteer(1) = "1"
    CASE "L"      'Go left
      tmp1 = "1"
      tSteer(0) = "F"
      tSteer(1) = "F"
  ENDSELECT

  'Send commands to the CoProcessor and get a response
  SEROUT COPROC, NET_BAUD, ["!1R1"]       'Send RC command to coprocessor.
  SEROUT COPROC, NET_BAUD, [tSteer(0)]    'Steer command 1
  SEROUT COPROC, NET_BAUD, [tSteer(1)]    'Steer command 2
  SERIN COPROC, NET_BAUD,  [tResponse]    'Get a response
  PAUSE 300                               'Pause 300 ms

  SEROUT COPROC, NET_BAUD, ["!1M1"]       'Starting part of command to co.
  SEROUT COPROC, NET_BAUD, [tmp1]         'Set the direction 1=forward 0=backward
  SEROUT COPROC, NET_BAUD, [HEX1 tSpeed]  'Set the speed
  IF tDistance = 0 AND tDirection = "S" THEN
    SEROUT COPROC, NET_BAUD, ["0001"]     'Turn the motor off
  ELSEIF tDistance = 0 AND tDirection <> "S" THEN
    SEROUT COPROC, NET_BAUD, ["FFFF"]     'Set Distance for forever
  ELSE
    SEROUT COPROC, NET_BAUD, [HEX4 tDistance] 'Set Distance set number of inches
  ENDIF

  SERIN COPROC, NET_BAUD, [tResponse]     'Get a response
  PAUSE 100                               'Pause 100 ms

  RETURN

Note

Note

You may be a bit confused when you notice that the code is sending serial commands to another processor. This is because the ARobot has two controllers. One is the main controller board, and this is where you send the serial commands from the MSRS service. The main controller board is used to control the robot’s speaker and LED lights. Additionally, there is a BS2 controller, which is used to power the RC steering and gear motors.

The remaining subroutines are used to turn on and off the LED lights and set the speaker using duration and frequency parameters passed into the input buffer. The code for these subroutines is as follows:

OnRedLED:

  LOW REDLED            'turn on the Red LED light
  RETURN

OffRedLED:

  HIGH REDLED           'turn off the Red LED light
  RETURN

OnGreenLED:

  LOW GREENLED          'turn on the Green LED light
  RETURN

OffGreenLED:

  HIGH GREENLED         'turn off the Green LED light
  RETURN

SetSpeaker:

  tDuration = inBuffer(1)    'Get time in ms that speaker should sound out
  tFrequency = inBuffer(2)   'get the frequency
  FREQOUT SPEAKER, tDuration * 50, tFrequency * 50   'Set the tone
  LOW SPEAKER           'turn off the speaker

  RETURN

The code for the onboard interface program is available on this book’s companion Web site. The file named ARobotControlForMSRS.bs2 should be loaded into the Basic Stamp Editor and executed by clicking Run from the program menu bar (see Figure 7-3). If the download was successful, you should hear two short beeps, and the green LED light will turn on.

The Basic Stamp Editor is used to download and execute the onboard interface program.

Figure 7-3. The Basic Stamp Editor is used to download and execute the onboard interface program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.110.17