Robotics at Home with Raspberry Pi Pico by Danny Staple

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



Copyright Disclaimer:
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.