Robotics at Home with Raspberry Pi Pico by Danny Staple
Author:Danny Staple
Language: eng
Format: epub
Publisher: Packt Publishing Limited
Published: 2023-02-21T00:00:00+00:00
Distance sensor wall avoider code
The code for this uses the distance sensors from the robot library. Put this in avoid_walls.py. Letâs start with familiar imports and by setting the sensor config:
import robot import time robot.left_distance.distance_mode = 1 robot.right_distance.distance_mode = 1
Weâll leave sensors on the default timing budget. We then have some configurations for our avoider:
too_close_cm = 30 speed = 0.9
The too_close_cm variable has a threshold for when the robot should turn to avoid a wall. We can set the overall robot speed for this behavior in the speed variable. We can tune these two variables to ensure the robot avoids a wall in time. Letâs start the sensors ranging:
robot.left_distance.start_ranging() robot.right_distance.start_ranging()
We are going to start the robot moving; however, we want to ensure that the robot stops moving and stops the sensors ranging if there are any problems, so we wrap the main loop in try:
try: robot.set_left(speed) robot.set_right(speed) while True:
A finally statement to accompany that try will come below the main loop. Next, we check whether there is sensor data ready:
if robot.left_distance.data_ready and robot.right_distance.data_ready: left_dist = robot.left_distance.distance right_dist = robot.right_distance.distance
We store the read distances so that we can use them throughout the handling. Note that while robot.left_distance.distance looks like a variable, it is a property that actively reads the sensor when we use it.
Since we have two values, we should check if one side is too close. Note that a timeout will result in a 0 value, so we should check values are above this too. By favoring a side, we slightly bias the robot to that side, and this should stop the robot from being indecisive if both sensors detect a close obstacle:
if 0 < right_dist < too_close_cm: print("Obstacle detected - Left: {} cm, Right: {} cm".format(left_dist, right_dist)) robot.set_left(-speed)
We check whether the distance on the right sensor is closer than the threshold. If so, we print a line of debug, showing that weâve detected an obstacle and the two sensor readings. We then set the left motor to go backward, which will cause the robot to swerve left, away from the obstacle.
We can now handle what happens otherwise:
else: robot.set_left(speed) if 0 < left_dist < too_close_cm: print("Obstacle detected - Left: {} cm, Right: {} cm".format(left_dist, right_dist)) robot.set_right(-speed) else: robot.set_right(speed)
If weâve not turned left, we ensure the left motor is going forward. We then check the left distance sensor, and if this is too close, we turn right. Finally, we set the right motor forward, so both motors will be going forward if it detects nothing too close.
Putting the left distance check in the else means that the robot will not set both motors backward and will favor turning left in front of an obstacle directly in front.
We then need to finish the loop by clearing the interrupts (so that the sensors are ranging again), and we leave a little time for them to sense again:
robot.left_distance.clear_interrupt() robot.right_distance.clear_interrupt() time.sleep(0.1)
Now, we need to handle any errors that happened. If you recall, we wrapped this code in a try block. The finally block stops and cleans everything up:
finally: robot.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Linux Device Driver Development Cookbook by Rodolfo Giometti(3933)
Embedded Programming with Modern C++ Cookbook by Igor Viarheichyk(3615)
Implementing Cellular IoT Solutions for Digital Transformation by Dennis McCain(3550)
Embedded Linux Development Using Yocto Project - Third Edition by Otavio Salvador & Daiane Angolini(3380)
TinyML Cookbook by Gian Marco Iodice(3317)
Simplifying 3D Printing with OpenSCAD by Colin Dow(2842)
TinyML Cookbook by Gian Marco Iodice & Ronan Naughton(2603)
Fusion 360 for Makers by Lydia Sloan Cline(2219)
Networking A Beginner's Guide by Bruce Hallberg(2209)
Hands-On Linux for Architects by Denis Salamanca(2051)
But How Do It Know? by J. Clark Scott(2030)
Computers For Seniors For Dummies by Nancy C. Muir(1996)
Raspberry Pi and MQTT Essentials by Dhairya Parikh(1953)
Arduino Project Handbook, Volume 2: 25 Simple Electronics Projects for Beginners by Geddes Mark(1947)
9781803246888-ENHANCING DEEP LEARNING WITH BAYESIAN INFERENCE by Unknown(1901)
Hack and HHVM by Owen Yamauchi(1884)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1860)
MicroPython Projects by Jacob Beningo(1736)
Hands-On Internet of Things with MQTT by Tim Pulver(1707)
