Eren and I chose to work as a team for Project 4, and our focus was to implement the ultrasonic sensors to work with the robot’s current systems to achieve a wall-following action. Our roles were clearly defined, with me serving as the navigator and Eren taking on the driver role. As the navigator, I ensured our code algorithm aligned with our initial plans. My focus also went alongside maintaining the stability and functionality of our robot, addressing any potential issues that could arise from our test runs. Attention to detail was essential, given our past challenges of having motors break down and wheels detach.
We opted to delve into the hardware aspect of Project 4, as we understood that it was constructed upon the foundations laid by Project 3’s hardware, which we both previously worked on. Building upon the information from Project 3, which provided the fundamental understanding of working with the ultrasonic sensors simultaneously with the robot, Project 4 expands the project by introducing the ultrasonic sensors as a new obstacle avoidance system on the robot. To begin the implementation, we reused Eren’s code snippet, specifically the function’ int get CM()’, and integrated it into the 1D wall follower. Of course, this involved modifications to the source code of the 1D wall follower. This process allows the ultrasonic sensors to work effectively. Here is the snippet of the code.
#include <wiringPi.h>
#define TRIG 21
#define ECH 22
int getCM() {
//Send trig pulse
digitalWrite(TRIG, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG, LOW);
//Wait for echo start
while(digitalRead(ECH) == LOW);
std:: cout<< "L \n";
//Wait for echo end
long startTime = micros();
while(digitalRead(ECH) == HIGH);
std:: cout<< "H \n";
long travelTime = micros() - startTime;
//Get distance in cm
int distance = travelTime / 58;
return distance;
}
In this set of codes, we added new alterations to the conditions of how the robot moves. We also integrated the Lidar and the sensors into the robot’s system. Unlike the previous conditions that relied only on Lidar’s conditions, our robot now accommodates the input from the sensors. This code expands on the prior Project 1 as the new sensors amplify the range of detectable obstacles, now accounting for the lower base of the robot that the Lidar cannot detect. It is important to note that the sensors operate on a centimeter scale, while the Liar measures distances in meters. Incorporating both systems in the conditions addresses the limitations of Lidar on identifying lower-end obstacles, and adding the sensors contributes to a more comprehensive and effective avoidance obstacle system for the robot. This code is for the 1D wall follower.
int distance1 = getCM();
std:: cout << "echo: " << distance1 << "\n";
std:: cout << "lidar: " << dist_to_wall << "\n";
if ( distance1 >= 35 && dist_to_wall >=0.35) robot.drive (0.3,0,0); // if the distance sensor and lidar both show a value bigger than 35cm, then drive forward.
else if (dist_to_wall > 0.23 && dist_to_wall <0.35 || distance1 > 20 && distance1 <35 ) robot.drive (0,0,0); // if just distance sensor or just the lidar show a value lower than threshold but higher than the low limit, then stop.
else if (dist_to_wall <= 0.23 || distance1 <= 20 ) robot.drive (-0.3,0,0); // if too close to the mbot, move back. Either the lidar or the distance sensor.
else robot.drive(0,0,0); //else stop the robot. not necessary but it's still there.
One significant challenge we encountered during the project stemmed from using the wiring pi library, as it frequently conflicted with the robot’s library operations. We observed these issues when the robot became stuck while moving forward, possibly due to its proximity to the Lidar system. This situation required either a manual shutdown or a complete program restart using specific commands. Initially, we utilized the ‘ps -ef | grep follow’ command to identify the error and subsequently executed ‘kill -9 <processnumber>’ to terminate the entire program. Following this, we had to rerun the program and stop the robot using Ctrl+C.