As part of my final project, I was working on a challenging project: building a wall-following robot using the Mbot and Python. This blog post reflects on my experience throughout the project, highlighting my choices, implementation process, and key takeaways.
The project was how the name sounds – following a wall. I implemented a bang-bang controller to leverage the Lidar sensor data and adjust the robot’s direction based on its distance from the wall. This involved functions like find_min_dist to identify the nearest obstacle and cross_product to calculate the required correction vector. Tuning the setpoint and control gain was crucial for achieving smooth and stable wall following.
The following is the pseudocode of how I implemented the find_min_dist and the cross_product.
Import necessary modules: time, numpy, MBot API, signal, sys
Initialize variable CTRL_C_Pressed as False
Define signal_handler function:
Set global variable CTRL_C_Pressed to True
Print message indicating CTRL+C has been pressed
Link the signal_handler function to the SIGINT signal (CTRL+C)
Define function find_min_dist with parameters ranges and thetas:
Initialize min_dist to infinity and min_angle to None
For each distance and its index in ranges:
If distance is not 0 and less than min_dist:
Update min_dist to this distance
Update min_angle to the corresponding angle from thetas
If min_dist remains infinity, return None, None
Otherwise, return min_dist, min_angle
Define function cross_product with parameters v1, v2:
Compute and return the cross product of v1 and v2
Create an MBot object named robot
Set a variable setpoint (desired distance) to 0.35
Begin a try block:
Start an infinite loop:
Read lidar data into ranges and thetas
Get min_dist and min_angle using find_min_dist
If a valid minimum distance and angle are found:
Calculate x and y using min_dist and min_angle
Define vectors v1 and v2
Compute crossP as the cross product of v1 and v2
Calculate correction vector based on min_dist, min_angle, and setpoint
Drive the robot using values from crossP and correction
If CTRL_C_Pressed is True:
Print a stopping message
Break the loop
Else:
Print a message indicating no valid minimum distance found
Pause for 0.1 seconds
Catch any exceptions, print the error message
Finally:
Stop the robot
Print a robot stopped message
Exit the program
Challenges and Solutions
I felt a great sense of accomplishment when my robot finished two full laps of the course with perfect navigation. This success was a result of the numerous hours spent testing, debugging, and improving the code.
One major problem was that the robot would sometimes detect two minimum distances at the same time, which resulted in a state of uncertainty and erratic movements. This was an especially difficult problem because it fundamentally tested the robot’s ability to navigate. I added a ‘correction vector’ to the code after doing extensive testing. This calculated addition turned out to be a game-changer because it allowed the robot to recognize its path with accuracy even in situations where distance readings were inconsistent. To ensure smoother, more consistent navigation, the correction vector adjusted the robot’s trajectory based on the most trustworthy distance data.
Previous projects had been instrumental in shaping my approach to this challenge. They taught me the nuances of the MBot’s API and the importance of real-time data visualization on the console. The most significant takeaway was the refinement of the correction vector, a concept I first encountered during the Wall Following project
Robot Following the wall
Teammates
Bishal Timalsina
Ali Ramazani