Robot Movement and Navigation

20
Robot Movement and Robot Movement and Navigation Navigation CPSC 120 CPSC 120 Principles of Computer Principles of Computer Science Science February 22, 2012 February 22, 2012

description

CPSC 120 Principles of Computer Science February 22, 2012. Robot Movement and Navigation. Where we are now. We know how to use the Basic Editor, download code to BS2, blink LEDs, PULSOUT to turn wheels, etc. We have basic loops available such as DO…LOOP and FOR…NEXT - PowerPoint PPT Presentation

Transcript of Robot Movement and Navigation

Page 1: Robot Movement and Navigation

Robot Movement and NavigationRobot Movement and Navigation

CPSC 120CPSC 120

Principles of Computer SciencePrinciples of Computer Science

February 22, 2012February 22, 2012

Page 2: Robot Movement and Navigation

Where we are nowWhere we are now We know how to use the Basic Editor, We know how to use the Basic Editor,

download code to BS2, blink LEDs, download code to BS2, blink LEDs, PULSOUT to turn wheels, etc.PULSOUT to turn wheels, etc.

We have basic loops available such as We have basic loops available such as DO…LOOP and FOR…NEXTDO…LOOP and FOR…NEXT

We understand how variables are used to We understand how variables are used to assign/change/increment values.assign/change/increment values.

We understand how to use DEBUG to get We understand how to use DEBUG to get program feedback to check variable program feedback to check variable values.values.

Page 3: Robot Movement and Navigation

Moving Your RobotMoving Your Robot' Right servo turns clockwise three seconds, stops 1 second, ‘ then counterclockwise three seconds. ' {$STAMP BS2} ' {PBASIC 2.5} counter VAR Word ‘ Declare our loop control variable

DEBUG "Program Running!" ‘ Give the user notice we are running

FOR counter = 1 TO 122 ' Turn clockwise about 3 seconds. PULSOUT 12, 650 ‘ Send a 1300 us = 1.3 ms pulsePAUSE 20 ‘ Let the servo respond for 20 ms

NEXT ‘ Bottom of loop, repeat or leave loop FOR counter = 1 TO 40 ' Stop servo one second.

PULSOUT 12, 750 ‘ Send center value to servoPAUSE 20 ‘ Let the servo respond

NEXT ' Bottom of loop

FOR counter = 1 TO 122 ' Counterclockwise three seconds. PULSOUT 12, 850 ‘ Send 1.7 ms pulse to servo, reversePAUSE 20 ‘ Let it respond

NEXT ‘ Bottom of loopEND ‘ End of program

Page 4: Robot Movement and Navigation

Driving LessonsDriving Lessons ' Move robot forward, left, right, then backward for testing.' {$STAMP BS2} ' {$PBASIC 2.5}

DEBUG "Program Running!”

counter VAR Word ‘ Loop control counter

FOR counter = 1 TO 64 ' Drive forward PULSOUT 13, 850 PULSOUT 12, 650 PAUSE 20

NEXT PAUSE 200 ‘ Do nothing for 0.2 secondsFOR counter = 1 TO 24 ' Rotate left-about 1/4 turn

PULSOUT 13, 650PULSOUT 12, 650PAUSE 20

NEXT‘ Continued next page

Page 5: Robot Movement and Navigation

Driving Lessons (cont)Driving Lessons (cont) ‘Code continued from previous slide

PAUSE 200 ‘ Pause again for a bit

FOR counter = 1 TO 24 ' Rotate right-about 1/4 turnPULSOUT 13, 850PULSOUT 12, 850PAUSE 20

NEXT

PAUSE 200 ‘ Stop once more briefly

FOR counter = 1 TO 64 ’ Drive backwardPULSOUT 13, 650PULSOUT 12, 850PAUSE 20

NEXT

END

Page 6: Robot Movement and Navigation

Useful Facts to RememberUseful Facts to Remember Once a PBASIC program is downloaded to BOE-BOT, it Once a PBASIC program is downloaded to BOE-BOT, it

remains in RAM until the program is overwritten. Switch remains in RAM until the program is overwritten. Switch position off, 1, 2 does not remove the program.position off, 1, 2 does not remove the program.

When the Reset button on BOE-BOT is pressed, the When the Reset button on BOE-BOT is pressed, the current program in RAM is restarted at its beginning.current program in RAM is restarted at its beginning.

Once a program is downloaded, you can unplug the USB Once a program is downloaded, you can unplug the USB cable, put the robot on the floor, press Reset and watch cable, put the robot on the floor, press Reset and watch your program control your robot! Your robot is your program control your robot! Your robot is autonomousautonomous. Only your program controls it. Smarter . Only your program controls it. Smarter robots mean more complex PBASIC programs.robots mean more complex PBASIC programs.

Putting a PAUSE 2000 as the first real instruction in your Putting a PAUSE 2000 as the first real instruction in your program helps avoid physical starting issues such as program helps avoid physical starting issues such as catching your fingers on the moving robot board.catching your fingers on the moving robot board.

WATCH OUT FOR RUNAWAY ROBOTS!WATCH OUT FOR RUNAWAY ROBOTS!

Page 7: Robot Movement and Navigation

Where we are goingWhere we are going Build the robot and learn how to fully control our Build the robot and learn how to fully control our

robot using PULSOUT commands.robot using PULSOUT commands. Learn more PBASIC control structures so we Learn more PBASIC control structures so we

can react to infrared (IR) obstacle sensor can react to infrared (IR) obstacle sensor information to avoid obstacles.information to avoid obstacles.

Use subroutines and IR information to avoid Use subroutines and IR information to avoid obstacles and traverse a maze without using obstacles and traverse a maze without using dead reckoning (guess distances/code loops)dead reckoning (guess distances/code loops)

Add two more downward looking IR sensors to Add two more downward looking IR sensors to detect a white line on a dark floordetect a white line on a dark floor

Write and test code to follow a white line around Write and test code to follow a white line around a loopa loop

Add an obstacle or two to make it interestingAdd an obstacle or two to make it interesting

Page 8: Robot Movement and Navigation

Subroutines in PBASICSubroutines in PBASICallow out-of-order executionallow out-of-order execution

Page 9: Robot Movement and Navigation

Using Subroutines to MoveUsing Subroutines to Move

Page 10: Robot Movement and Navigation

Using Infrared to Detect ObstaclesUsing Infrared to Detect Obstacles

Page 11: Robot Movement and Navigation

IR Circuit SchematicIR Circuit Schematic

Note we can rapidly blink our LEDs using a FREQOUT command in PBASIC on pins P0 and P2 then read the signal (0 or 1) at pins P8 and P9 which

report any reflected IR signal.

Page 12: Robot Movement and Navigation

IR Circuit WiringIR Circuit Wiring

Note we need to be careful to be sure that the IR LEDs are inserted correctly, LEDs don't shine when reversed! Also notice that things are getting crowded

on the breadboard. Neatness is helpful.

Page 13: Robot Movement and Navigation

IR Testing ProgramIR Testing Program' IR testing code. See pages 235-242 in BOE manual.' Test two sided IR object detection circuits before using for navigation' {$STAMP BS2}' {$PBASIC 2.5}

irDetectLeft VAR Bit ' Variable to hold left detector valueirDetectRight VAR Bit ' Variable to hold right detector value

DO ' Repeat foreverFREQOUT 8, 1, 38500 ' Pulse Left IR LED for 1 msirDetectLeft = IN9 ' Read pin for IR bounce and store

FREQOUT 2, 1, 38500 ' Pulse Right IR LED for 1 msirDetectRight = IN0 ' Read pin for IR bounce and store

DEBUG CLS, "irDetectLeft = ", BIN1 irDetectLeft , "irDetectRight = ", BIN1 irDetectRight

PAUSE 1000 ' Wait one second to read the newsLOOPEND

Page 14: Robot Movement and Navigation

Navigation Using IR SensorsNavigation Using IR Sensors‘ Skeleton code for navigation to avoid obstacles using IRDO

FREQOUT 8, 1, 38500 ‘ Flash left IR LEDirDetectLeft = IN9 ‘ Record any IR bounceFREQOUT 2, 1, 38500 ‘ Flash right IR LEDirDetectRight = IN0 ‘ Record any IR bounce

IF (irDetectLeft = 0) THEN ‘ Look out on left, evasive actionGOSUB Turn_Right

ELSE GOSUB Forward_Pulse ‘ Clear on left, continue

ENDIF

IF (irDetectRight = 0) THEN ‘ Danger on right, swerve leftGOSUB Turn_Left

ELSE GOSUB Forward_Pulse ‘ Clear on right, continue

ENDIFLOOP

Page 15: Robot Movement and Navigation

More Complex Decisions: Logic Again!More Complex Decisions: Logic Again!

DO FREQOUT 8, 1, 38500 ‘ Get the IR data irDetectLeft = IN9 FREQOUT 2, 1, 38500 irDetectRight = IN0

IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN ‘ Blocked ahead!GOSUB Back_UpGOSUB Turn_LeftGOSUB Turn_Left

ELSEIF (irDetectLeft = 0) THEN ‘ Blocked only on leftGOSUB Turn_Right

ELSEIF (irDetectRight = 0) THEN ‘ Blocked only on rightGOSUB Turn_Right

ELSEGOSUB Forward_Pulse ‘ No obstacles, full speed ahead!

ENDIFLOOP

Page 16: Robot Movement and Navigation

Using QTI sensors to follow linesUsing QTI sensors to follow lines

Page 17: Robot Movement and Navigation

Using QTI Sensors to Follow LinesUsing QTI Sensors to Follow Lines

Here is the schematic for the QTI line following sensors. These Here is the schematic for the QTI line following sensors. These bounce IR off the floor which triggers a sensor. White tape bounce IR off the floor which triggers a sensor. White tape reflects more than black so we can detect being over a line.reflects more than black so we can detect being over a line.

Page 18: Robot Movement and Navigation

Wiring diagram for QTI sensorsWiring diagram for QTI sensors

We do not include the 10K resistors! Again, it will be crowded on We do not include the 10K resistors! Again, it will be crowded on the breadboard. You need to plan your wiring and pin use.the breadboard. You need to plan your wiring and pin use.

Page 19: Robot Movement and Navigation

Code for testing QTI sensorsCode for testing QTI sensors

' Boe-Bot detects white tape with 2 QTI modules. ' Boe-Bot detects white tape with 2 QTI modules. '{$STAMP BS2}'{$STAMP BS2}'{$PBASIC 2.5}'{$PBASIC 2.5}

‘ Notice we are using the colon : to put several‘ Notice we are using the colon : to put several‘ ‘ commands on a single line. This makes the code commands on a single line. This makes the code ‘ more compact but a bit harder to read.‘ more compact but a bit harder to read.

qtiLeft VAR BitqtiLeft VAR BitqtiRight VAR BitqtiRight VAR Bit

DODOHIGH 5: PAUSE 1: qtiRight = IN4: INPUT 5 HIGH 5: PAUSE 1: qtiRight = IN4: INPUT 5 HIGH 7: PAUSE 1: qtiLeft = IN6: INPUT 7 HIGH 7: PAUSE 1: qtiLeft = IN6: INPUT 7 DEBUG HOME, BIN1 qtiLeft, BIN1 qtiRight DEBUG HOME, BIN1 qtiLeft, BIN1 qtiRight PAUSE 100PAUSE 100

LOOP ENDLOOP END

Page 20: Robot Movement and Navigation

This is the maze/obstacle course your robot This is the maze/obstacle course your robot will need to traverse on Monday in lab. will need to traverse on Monday in lab.