CamJam EduKit Robotics Worksheet Nine – Obstacle Avoidance camjam.me/edukit

CamJam EduKit Robotics - Obstacle Avoidance Project

Obstacle Avoidance

Description

You will learn how to stop your robot from bumping into things.

Equipment Required For this worksheet you will require: 

Your robot with the ultrasonic sensor attached to the front (see Worksheet 6).

How it Works In Worksheet 6 you learnt how to detect how far in front of your robot an object was using the ultrasonic sensor. In this worksheet you are going to get the robot to avoid driving into obstacles. Here is the logic for how the robot will work in this worksheet:   

The robot will drive forwards until it detects that there is an object a few centimetres in front of it. The robot will reverse and then turn right. The robot will then drive forwards again.

Code The code will be based on the code you completed at the end of Worksheet 7, so lets copy it: cd ~/EduKitRobotics/ cp 7-pwm2.py 9-avoidance.py nano 9-avoidance.py Add variables for the ultrasonic sensors’ GPIO pins to the section where the motor GPIO pins are defined: # Define GPIO pins to use on the Pi pinTrigger = 17 pinEcho = 18 Add variables to set how close to an obstacle the robot can go, how long to reverse for and how long to turn right for. You should change these variables to suit your robot. # Distance Variables HowNear = 15.0 ReverseTime = 0.5 TurnTime = 0.75 And add the GPIO setup line to the others: # Set pins as output and input GPIO.setup(pinTrigger, GPIO.OUT) GPIO.setup(pinEcho, GPIO.IN)

# Trigger # Echo

You’re going to add another function that will return whether the ultrasonic sensor sees an obstacle. # Return True if the ultrasonic sensor sees an obstacle def IsNearObstacle(localHowNear): Distance = Measure() print("IsNearObstacle: "+str(Distance))

Rev 1.02

Page 1 of 7

May 23, 2017

CamJam EduKit Robotics Worksheet Nine – Obstacle Avoidance camjam.me/edukit if Distance < localHowNear: return True else: return False In the code above, you pass in a variable to say how close you can get to an obstacle. It also uses another new function that does the measurement using the ultrasonic sensor. # Take a distance measurement def Measure(): GPIO.output(pinTrigger, True) time.sleep(0.00001) GPIO.output(pinTrigger, False) StartTime = time.time() StopTime = StartTime while GPIO.input(pinEcho)==0: StartTime = time.time() StopTime = StartTime while GPIO.input(pinEcho)==1: StopTime = time.time() # If the sensor is too close to an object, the Pi cannot # see the echo quickly enough, so it has to detect that # problem and say what has happened if StopTime-StartTime >= 0.04: print("Hold on there! You're too close for me to see.") StopTime = StartTime break ElapsedTime = StopTime - StartTime Distance = (ElapsedTime * 34326)/2 return Distance You also need to write a function that will make the robot avoid the obstacle by moving back a little, then turning right. # Move back a little, then turn right def AvoidObstacle(): # Back off a little print("Backwards") Backwards() time.sleep(ReverseTime) StopMotors() # Turn right print("Right") Right() time.sleep(TurnTime) StopMotors()

Rev 1.02

Page 2 of 7

May 23, 2017

CamJam EduKit Robotics Worksheet Nine – Obstacle Avoidance camjam.me/edukit The code that controls the robot then replaces everything between the comment # Your code to control the robot goes below this line and the end of the code. try: # Set trigger to False (Low) GPIO.output(pinTrigger, False) # Allow module to settle time.sleep(0.1) #repeat the next indented block forever while True: Forwards() time.sleep(0.1) if IsNearObstacle(HowNear): StopMotors() AvoidObstacle() # If you press CTRL+C, cleanup and stop except KeyboardInterrupt: GPIO.cleanup() This code runs the commands within the try: section until the Ctrl+C keys are pressed. Within the try: is a while True:. Since True is always true, the code within that while will run forever – or until Ctrl+C is pressed. Within the while True:, the code makes the robot go forward until it is HowNear cm from an obstacle. When it is, the robot reverses and turns right. Your code should now look like this: # CamJam EduKit 3 - Robotics # Worksheet 9 – Obstacle Avoidance import RPi.GPIO as GPIO # Import the GPIO Library import time # Import the Time library # Set the GPIO modes GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Set variables for the GPIO motor pins pinMotorAForwards = 10 pinMotorABackwards = 9 pinMotorBForwards = 8 pinMotorBBackwards = 7 # Define GPIO pins to use on the Pi pinTrigger = 17 pinEcho = 18 # How many times to turn the pin on and off each second Frequency = 20 Rev 1.02

Page 3 of 7

May 23, 2017

CamJam EduKit Robotics Worksheet Nine – Obstacle Avoidance camjam.me/edukit # How long the pin stays on each cycle, as a percent DutyCycleA = 30 DutyCycleB = 30 # Setting the duty cycle to 0 means the motors will not turn Stop = 0 # Set the GPIO Pin mode to be Output GPIO.setup(pinMotorAForwards, GPIO.OUT) GPIO.setup(pinMotorABackwards, GPIO.OUT) GPIO.setup(pinMotorBForwards, GPIO.OUT) GPIO.setup(pinMotorBBackwards, GPIO.OUT) # Set pins as output and input GPIO.setup(pinTrigger, GPIO.OUT) # Trigger GPIO.setup(pinEcho, GPIO.IN) # Echo # Distance Variables HowNear = 15.0 ReverseTime = 0.5 TurnTime = 0.75 # Set the GPIO to software PWM at 'Frequency' Hertz pwmMotorAForwards = GPIO.PWM(pinMotorAForwards, Frequency) pwmMotorABackwards = GPIO.PWM(pinMotorABackwards, Frequency) pwmMotorBForwards = GPIO.PWM(pinMotorBForwards, Frequency) pwmMotorBBackwards = GPIO.PWM(pinMotorBBackwards, Frequency) # Start the software PWM with a duty cycle of 0 (i.e. not moving) pwmMotorAForwards.start(Stop) pwmMotorABackwards.start(Stop) pwmMotorBForwards.start(Stop) pwmMotorBBackwards.start(Stop) # Turn all motors off def StopMotors(): pwmMotorAForwards.ChangeDutyCycle(Stop) pwmMotorABackwards.ChangeDutyCycle(Stop) pwmMotorBForwards.ChangeDutyCycle(Stop) pwmMotorBBackwards.ChangeDutyCycle(Stop) # Turn both motors forwards def Forwards(): pwmMotorAForwards.ChangeDutyCycle(DutyCycleA) pwmMotorABackwards.ChangeDutyCycle(Stop) pwmMotorBForwards.ChangeDutyCycle(DutyCycleB) pwmMotorBBackwards.ChangeDutyCycle(Stop)

Rev 1.02

Page 4 of 7

May 23, 2017

CamJam EduKit Robotics Worksheet Nine – Obstacle Avoidance camjam.me/edukit # Turn both motors backwards def Backwards(): pwmMotorAForwards.ChangeDutyCycle(Stop) pwmMotorABackwards.ChangeDutyCycle(DutyCycleA) pwmMotorBForwards.ChangeDutyCycle(Stop) pwmMotorBBackwards.ChangeDutyCycle(DutyCycleB) # Turn left def Left(): pwmMotorAForwards.ChangeDutyCycle(Stop) pwmMotorABackwards.ChangeDutyCycle(DutyCycleA) pwmMotorBForwards.ChangeDutyCycle(DutyCycleB) pwmMotorBBackwards.ChangeDutyCycle(Stop) # Turn Right def Right(): pwmMotorAForwards.ChangeDutyCycle(DutyCycleA) pwmMotorABackwards.ChangeDutyCycle(Stop) pwmMotorBForwards.ChangeDutyCycle(Stop) pwmMotorBBackwards.ChangeDutyCycle(DutyCycleB) # Take a distance measurement def Measure(): GPIO.output(pinTrigger, True) time.sleep(0.00001) GPIO.output(pinTrigger, False) StartTime = time.time() StopTime = StartTime while GPIO.input(pinEcho)==0: StartTime = time.time() StopTime = StartTime while GPIO.input(pinEcho)==1: StopTime = time.time() # If the sensor is too close to an object, the Pi cannot # see the echo quickly enough, so it has to detect that # problem and say what has happened if StopTime-StartTime >= 0.04: print("Hold on there! You're too close for me to see.") StopTime = StartTime break ElapsedTime = StopTime - StartTime Distance = (ElapsedTime * 34326)/2 return Distance

Rev 1.02

Page 5 of 7

May 23, 2017

CamJam EduKit Robotics Worksheet Nine – Obstacle Avoidance camjam.me/edukit # Return True if the ultrasonic sensor sees an obstacle def IsNearObstacle(localHowNear): Distance = Measure() print("IsNearObstacle: "+str(Distance)) if Distance < localHowNear: return True else: return False # Move back a little, then turn right def AvoidObstacle(): # Back off a little print("Backwards") Backwards() time.sleep(ReverseTime) StopMotors() # Turn right print("Right") Right() time.sleep(TurnTime) StopMotors() # Your code to control the robot goes below this line try: # Set trigger to False (Low) GPIO.output(pinTrigger, False) # Allow module to settle time.sleep(0.1) #repeat the next indented block forever while True: Forwards() time.sleep(0.1) if IsNearObstacle(HowNear): StopMotors() AvoidObstacle() # If you press CTRL+C, cleanup and stop except KeyboardInterrupt: GPIO.cleanup() Once you have typed all the code and checked it, save and exit the text editor with “Ctrl + x” then “y” then “enter”.

Rev 1.02

Page 6 of 7

May 23, 2017

CamJam EduKit Robotics Worksheet Nine – Obstacle Avoidance camjam.me/edukit

Running the Code Place your robot on the floor near a wall or other obstacles and start the program by typing the following into the terminal window: python3 9-avoidance.py The robot should then approach the wall until it gets close. It should then reverse and turn right.

Tuning the Robot As mentioned in previous worksheets, every robot is different. You should therefore experiment with the distance variables (HowNear, ReverseTime, and TurnTime), as well as the speed of the robot, if necessary (Frequency, DutyCycleA and DutyCycleB).

Challenge Instead of just reversing and going right, make your robot turn left, measure the distance to the next object, then turn right and do the same and drive in the direction that has an obstacle furthest away.

Rev 1.02

Page 7 of 7

May 23, 2017

CamJam EduKit Robotics - Obstacle Avoidance Equipment ... - GitHub

May 23, 2017 - In the code above, you pass in a variable to say how close you can get to an obstacle. It also uses another new function that does the ... problem and say what has happened if StopTime-StartTime >= 0.04: print("Hold on there! You're ... Move back a little, then turn right def AvoidObstacle():. # Back off a little.

454KB Sizes 0 Downloads 218 Views

Recommend Documents

CamJam EduKit Sensors Worksheet Two Equipment ... - GitHub
Jun 20, 2017 - LEDs and Buzzer camjam.me/edukit .... Next, push the buzzer into the breadboard with the buzzer itself straddling the centre of the board. The.

CamJam EduKit Robotics
Feb 2, 2016 - Description Set up your Raspberry Pi and run your first python program to print “Hello World” to the screen. You will not be connecting any of the contents of the CamJam EduKit Robotics kit ... A Raspberry Pi power supply. Setting u

CamJam EduKit 3 - Robotics Worksheet 8 - Obstacle Avoidance.pdf ...
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. CamJam EduKit ...

CamJam EduKit 3 - Robotics Worksheet 7 - Line Follower.pdf ...
Page 1 of 6. Rev 0.5 Page 1 of 6 November 08, 2015. CamJam EduKit Robotics. Worksheet Seven – Line Following. camjam.me/edukit. CamJam EduKit Robotics - Line Following. Project Line Following. Description You will learn how to get your robot to fol

Hand Path Priming in Manual Obstacle Avoidance
E-mail: [email protected] or dar12@psu. ...... template may have been completed in real time to produce the full hand path. .... Bulletin, 127, 342–357. Flash, T.

Reactive Inflight Obstacle Avoidance via Radar Feedback
Email: [email protected], [email protected] ... on automated highways has resulted in obstacle avoidance methods in quasi 1-D problems ...

Obstacle Avoidance Behavior for a Biologically-inspired ...
Using this robot for the sensor platform allowed larger objects to be ..... the IEEE International Conference on Robotics and Automation (ICRA), pp. 2412-2417 ...

A Robust Algorithm for Local Obstacle Avoidance
Jun 3, 2010 - Engineering, India. Index Terms— Gaussian ... Library along with Microsoft Visual Studio for programming. The robot is equipped with ...

Research paper: Hand path priming in manual obstacle avoidance ...
free targets that were removed from the last target tested. On the. basis of this observation, Jax and Rosenbaum (in press) argued that. their participants relied on ...

Extending Fitts' Law to manual obstacle avoidance
Received: 1 March 2007 / Accepted: 16 May 2007 / Published online: 12 June 2007. © Springer-Verlag 2007. Abstract ... Department of Psychology, Hamilton College,. Clinton, NY, USA. 123. Exp Brain .... were input to a computer program that governed t

External Localization System for Mobile Robotics - GitHub
... the most known external localization reference is GPS; however, it ... robots [8], [9], [10], [11]. .... segments, their area ratio, and a more complex circularity .... The user just places ..... localization,” in IEEE Workshop on Advanced Robo

Monocular Obstacle Detection
Ankur Kumar and Ashraf Mansur are students of Robot Learning Course at Cornell University, Ithaca, NY 14850. {ak364, aam243} ... for two labeled training datasets. This is used to train the. SVM. For the test dataset, features are ..... Graphics, 6(2

Reconciliation, submission and avoidance
Saitama, 351-0198, Japan (email: [email protected] or kutsu@ · darwin. .... from nine to 36 individuals in 2003 (median ¼ 19) and from eight to 38 individuals ...

Imperceptible Relaxation of Collision Avoidance ...
Figure 1: Hundreds of characters are causing collisions in these crowd scenes. They are however .... How to evaluate the employed believability trade-offs and validate their selection in ..... 23 to 50 years old (29, 7 ± 7.7) took part in this exper

Contingency Planning Over Probabilistic Obstacle ...
Feb 20, 2009 - even built in to the final application circuit eliminating the need for a .... It is called an Integrated Development Environment, or IDE, because it.

Beyond Interference Avoidance: On Transparent ...
paper, we call this interference-avoidance paradigm.1 Under ... a transparent (or “invisible”) way to primary nodes. We call this transparent-coexistence paradigm in this paper.2 .... have 3 DoFs remaining, which can be used for SM of 3 data.

Read PDF Probabilistic Robotics (Intelligent Robotics ...
Autonomous Agents series) - Best Seller Book - By Sebastian Thrun. Online PDF .... for anyone involved in robotic software development and scientific research.

WPI Robotics Library User's Guide - FIRST Robotics Resource Center
Jan 7, 2010 - will unwind the entire call stack and cause the whole robot program to quit, therefore, we caution teams on ... different purposes (though there is a way around it if necessary). ... Figure 3: WPILib Actuators section organization.

Embedded Robotics
... robots, six-legged walkers, biped android walkers, autonomous flying and underwater robots, as ..... 10 Walking Robots. 131. 10.1 Six-Legged Robot Design .

IoT Meets Robotics - EWSN
emerging applications mixing robotics with the Internet of. Things. Keywords. IoT, Robotics, Cloud Computing. 1 Introduction. The recent years have seen wide interest and innovation in the field of Internet of Things (IoT), triggered by techno- logic

Lightning Robotics -
Student Sign Up Instructions • Vol. 1 – Rev. E • 9/7/2016 •1/ 2. Lightning Robotics. Student Sign Up Instructions. The following items need to be completed to ...