More Precise BOE Arlo Maneuvers with Encoders
As mentioned earlier, the Arlo has encoders that track distance in 144th increments of a full wheel rotation. These increments are commonly called “counts”, and the Arlo’s encoders send 144 counts per revolution. The Arlo’s encoders are quadrature, meaning that the two encoder sensors are offset by ¼ of a count. By monitoring the pattern of low-high-low transitions for both sensors, the DHB-10 can also determine which direction the wheel is turning. For a given wheel, 144 counts would mean a full forward rotation, and -144 counts would mean a full backward rotation.
The Arlo’s DHB-10 motor controller has a built-in control system that uses the encoder outputs to control motor speed and distance as well as monitor motor position. To take advantage of this more accurate control system, your program will have to send and receive serial messages instead of servo control pulses.
NOTE: To find out more about the DHB-10 commands in this document’s example programs, look them up in the DHB-10 Motor Controller Firmware Guide.
Serial Distance Control
This example program uses distance mode commands TRVL and TURN to send the Arlo forward 2 wheel revolutions, left ¼ turn, right ½ turn, left ¼ turn, and back 2 wheel revolutions. The DHB-10 gets the Arlo’s wheels to the right encoder count; all the program has to do is wait long enough for it complete each command.
- Set power like this: MAIN (on), MOTORS (off), BOE (position-2)
- Run Serial Distance Maneuvers.bs2.
- Don’t worry about the communication error message because Motors power is off.
- Turn all power off, unplug programming cable, and take to your Arlo navigation area.
- Turn MAIN and MOTORS power on.
- Set the BOE power switch to 2.
- Verify that the Arlo goes forward, turns left, then right, then left, then backs up to near where it started.
' -----[ Title ]----------------------------------------------------------- ' Arlo - Serial Distance Maneuvers.bs2 ' Examples that use serial communication to make the arlo travel in certain ' straight-line and turn-in-place distances. ' {$STAMP BS2} ' {$PBASIC 2.5} ' -----[ I/O Definitions ]------------------------------------------------- Tx PIN 13 Rx PIN 13 ' -----[ Variables ]------------------------------------------------------- countsL VAR Word ' L & R distances countsR VAR Word counts VAR countsL ' Distance & top speed topSpeed VAR countsR n VAR Byte ' Stores index of serial exchange ' -----[ Initialization ]------------------------------------------------- FREQOUT 4, 2000, 3000 ' Beep -> program starting DEBUG "Program running...", CR, CR ' Display program running GOSUB Set_Ser_Pace ' Set serial pace GOSUB Clear_Encoders ' Clear previous distances ' -----[ Main Routine ]---------------------------------------------------- counts = 288 : topSpeed = 200 ' Forward 288 @ 200 counts/sec GOSUB Travel ' Call Travel subroutine PAUSE 3000 ' 3 s to complete maneuver GOSUB Get_Distance ' Get distances counts = -93 : topSpeed = 200 ' Left 93 @ 200 counts/sec GOSUB Turn ' Call Turn subroutine PAUSE 3000 ' 3 s to complete maneuver GOSUB Get_Distance ' Get distances counts = 186 : topSpeed = 100 ' Right 186 @ 100 counts/sec GOSUB Turn ' Call Turn subroutine PAUSE 4000 ' 4 s to complete maneuver GOSUB Get_Distance ' Get distances counts = -93 : topSpeed = 32 ' Left 93 @ 32 counts/sec GOSUB Turn ' Call Turn subroutine PAUSE 5000 ' 4 s to complete maneuver GOSUB Get_Distance ' Get distances counts = -288 : topSpeed = 200 ' Backward 288 @ 200 counts/sec GOSUB Travel ' Call Travel subroutine PAUSE 3000 ' 3 s to complete maneuver GOSUB Get_Distance ' Get distances END ' -----[ Subroutine - Set_Ser_Pace ]--------------------------------------- ' Pace setting not required if Test BOE and DHB-10 Communication.bs2 has ' already been run (successfully). Adds 1 ms delay before serial reply ' and with each space so BS2 can set up SDEC inputs at 19200. Set_Ser_Pace: n = n + 1 ' Increment message index DEBUG "PACE 1", CR ' Set serial pace for BS2 SEROUT 13, 32800, ["PACE 1", CR] ' Set serial pace for BS2 SERIN 13, 32800, 50, ERR, [WAIT(CR)] ' Get DHB-10's confirmation RETURN ' -----[ Subroutine - Clear_Encoders ]------------------------------------- Clear_Encoders: n = n + 1 ' Increment message index DEBUG "RST", CR ' Display message to DHB-10 SEROUT Tx, 32800, ["RST", CR] ' Send reset encoders message SERIN Rx, 32800, 50, ERR, [WAIT(CR)] ' Get DHB-10's reply RETURN ' -----[ Subroutine - Travel ]--------------------------------------------- Travel: n = n + 1 ' Increment message index DEBUG "TRVL ", SDEC counts, " ", ' Display message to DHB-10 SDEC topSpeed, CR SEROUT Tx, 32800, ["TRVL ", SDEC counts, ' Travel command with arguments " ", SDEC topSpeed, CR] SERIN Rx, 32800, 50, ERR, [WAIT(CR)] ' Get DHB-10's reply RETURN ' -----[ Subroutine - Turn ]----------------------------------------------- Turn: n = n + 1 ' Increment message index DEBUG "TURN ", SDEC counts, ' Display message to DHB-10 " ", SDEC topSpeed, CR SEROUT Tx, 32800, ["TURN", " ", ' Turn command with arguments SDEC counts, " ", SDEC topSpeed, CR] SERIN Rx, 32800, 50, ERR, [WAIT(CR)] ' Get DHB-10's reply RETURN ' -----[ Subroutine - Get_Distance ]--------------------------------------- Get_Distance: countsL = 0 ' Clear counts variables countsR = 0 n = n + 1 ' Increment message index DEBUG "DIST ", CR ' Display message to DHB-10 SEROUT Tx, 32800, ["DIST", CR] ' Distance command SERIN Rx, 32800, 100, ERR, [SDEC countsL, ' Get DHB-10's reply SDEC countsR] DEBUG "countsL = ", SDEC countsL, " countsR = ", SDEC countsR, CR RETURN ' -----[ Routine - ERR ]--------------------------------------------------- ERR: ' Error routine DEBUG "ERROR, no reply from DHB-10.", CR ' Error message DEBUG "n = ", DEC n ' Index of exchange that failed STOP ' End program
Serial Speed Control
If your robot is using sensors to navigate, maybe you just want your program to make the Arlo go forward until it finds an obstacle. Instead of specifying a particular distance and setting aside a certain amount of time, your program can set each wheel speed and go until the sensors have detected a condition your program is looking for.
This next example program just goes forward at a certain speed for 2 seconds, then backward at that same speed for 2 seconds. Note that the left and right values are positive for forward and negative for backward. Keep in mind that the values don’t have to match. For example, you could change the left and right values to 150 and 50 to make it draw an arc, or even -150 and -50 to reverse the direction of the arc.
- Set power like this: MAIN (on), MOTORS (off), BOE (position-2)
- Run Serial Speed Maneuvers.bs2.
- Disregard the communication error message.
- Turn all power off, unplug programming cable, and take to your Arlo navigation area.
- Turn MAIN and MOTORS power on.
- Set the BOE power switch to 2.
- Verify that the Arlo goes forward, and then backwards.
- Try changing the left = 144 : right = 144 to left = 144 : right = 44.
- Change the left = -144 : right = -144 to left = -144 : right = -44.
' -----[ Title ]----------------------------------------------------------- ' Arlo - Serial Speed Maneuvers.bs2 ' Examples that use serial communication to make the arlo travel in certain ' straight-line and turn-in-place distances. ' {$STAMP BS2} ' {$PBASIC 2.5} ' -----[ I/O Definitions ]------------------------------------------------- Tx PIN 13 Rx PIN 13 ' -----[ Variables ]------------------------------------------------------- left VAR Word ' L & R speeds & distances right VAR Word n VAR Byte ' Stores index of serial exchange ' -----[ Initialization ]------------------------------------------------- FREQOUT 4, 2000, 3000 ' Beep -> program starting DEBUG "Program running...", CR, CR ' Display program running GOSUB Set_Ser_Pace ' Set serial pace GOSUB Clear_Encoders ' Clear previous distances ' -----[ Main Routine ]---------------------------------------------------- left = 144 : right = 144 ' Left & right 144 counts/sec GOSUB Go_Speed ' Call Go_Speed subroutine PAUSE 3000 ' Go for 3 seconds left = 0 : right = 0 ' Left & right 0 counts/sec GOSUB Go_Speed ' Call Go_Speed subroutine PAUSE 1500 ' Time to stop GOSUB Get_Distance ' Get distances left = -144 : right = -144 ' Left & right -144 counts/sec GOSUB Go_Speed ' Call Go_Speed subroutine PAUSE 3000 ' Go for 3 seconds left = 0 : right = 0 ' Left & right 0 counts/sec GOSUB Go_Speed ' Call Go_Speed subroutine PAUSE 1500 ' Time to stop GOSUB Get_Distance ' Get distances END ' -----[ Subroutine - Set_Ser_Pace ]--------------------------------------- ' Pace setting not required if Test BOE and DHB-10 Communication.bs2 has ' already been run (successfully). Adds 1 ms delay before serial reply ' and with each space so BS2 can set up SDEC inputs at 19200. Set_Ser_Pace: n = n + 1 ' Increment message index DEBUG "PACE 1", CR ' Set serial pace for BS2 SEROUT 13, 32800, ["PACE 1", CR] ' Set serial pace for BS2 SERIN 13, 32800, 50, ERR, [WAIT(CR)] ' Get DHB-10's confirmation RETURN ' -----[ Subroutine - Clear_Encoders ]------------------------------------- Clear_Encoders: n = n + 1 ' Increment message index DEBUG "RST", CR ' Display message to DHB-10 SEROUT Tx, 32800, ["RST", CR] ' Send reset encoders message SERIN Rx, 32800, 50, ERR, [WAIT(CR)] ' Get DHB-10's reply RETURN ' -----[ Subroutine - Go_Speed ]------------------------------------------- Go_Speed: n = n + 1 ' Increment message index DEBUG "GOSPD ", SDEC left, ' Display message to DHB-10 " ", SDEC right, CR SEROUT Tx, 32800, ["GOSPD", " ", ' GOSPD command with arguments SDEC left, " ", SDEC right, CR] SERIN Rx, 32800, 50, ERR, [WAIT(CR)] ' Get DHB-10's reply RETURN ' -----[ Subroutine - Get_Distance ]--------------------------------------- Get_Distance: n = n + 1 ' Increment message index left = 0 ' Clear counts variables right = 0 DEBUG "DIST ", CR ' Display message to DHB-10 SEROUT Tx, 32800, ["DIST", CR] ' Distance command SERIN Rx, 32800, 100, ERR, [SDEC left, ' Get DHB-10's reply SDEC right] DEBUG "left = ", SDEC left, ' Display distances " right = ", SDEC right, CR RETURN ' -----[ Routine - ERR ]--------------------------------------------------- ERR: ' Error routine DEBUG "ERROR, no reply from DHB-10.", CR ' Error message DEBUG "n = ", DEC n ' Index of exchange that failed STOP ' End program