EDJ 39103
INDUSTRIAL AUTOMATION & ROBOTICS
LABORATORY 2
ROBOT PROGRAMMING 2
(OBSTACLE AVOIDANCE ROBOT)
Name : PREETHIKA THIAGARAJAN
Matric No. : 212061369
Date : 5/09/2024
Faculty of Electrical Engineering & Technology
5.0 RESULTS
5.1 PROGRAM FLOWCHART
Infrared Detection
Run sensors and set initial
velocity
READ LEFT &
RIGHT SENSOR
VALUES
IS LEFT
OBSTACLE
YES
LEFT SPEED = 0.5
RIGHT SPEED = -0.5 NO
IS RIGHT
OBSTACLE
YES
LEFT SPEED = 0.5
RIGHT SPEED = -0.5
NO SET MOTOR
VELOCITIES
CONTINUE
LOOP
END
5.2 PROGRAMMING CODE
#include <webots/robot.h>
#include <webots/distance_sensor.h>
#include <webots/motor.h>
#define TIME_STEP 64
#define MAX_SPEED 6.28
int main(int argc, char **argv) {
wb_robot_init();
int i;
WbDeviceTag ps[8];
char ps_names[8][4] = {
"ps0", "ps1", "ps2", "ps3",
"ps4", "ps5", "ps6", "ps7"
};
for (i = 0; i < 8 ; i++) {
ps[i] = wb_robot_get_device(ps_names[i]);
wb_distance_sensor_enable(ps[i], TIME_STEP);
}
WbDeviceTag left_motor = wb_robot_get_device("left wheel motor");
WbDeviceTag right_motor = wb_robot_get_device("right wheel motor");
wb_motor_set_position(left_motor, INFINITY);
wb_motor_set_position(right_motor, INFINITY);
wb_motor_set_velocity(left_motor, 0.0);
wb_motor_set_velocity(right_motor, 0.0);
while (wb_robot_step(TIME_STEP) != -1) {
double ps_values[8];
for (i = 0; i < 8 ; i++)
ps_values[i] = wb_distance_sensor_get_value(ps[i]);
printf("Sensor 0 : %f\n", ps_values[0]);
printf("Sensor 1 : %f\n", ps_values[1]);
printf("Sensor 2 : %f\n", ps_values[2]);
printf("Sensor 3 : %f\n", ps_values[3]);
printf("Sensor 4 : %f\n", ps_values[4]);
printf("Sensor 5 : %f\n", ps_values[5]);
bool right_obstacle =
ps_values[0] > 80.0 ||
ps_values[1] > 80.0 ||
ps_values[2] > 80.0;
bool left_obstacle =
ps_values[5] > 80.0 ||
ps_values[6] > 80.0 ||
ps_values[7] > 80.0;
double left_speed = 0.5 * MAX_SPEED;
double right_speed = 0.5 * MAX_SPEED;
if (left_obstacle) {
left_speed = 0.5 * MAX_SPEED;
right_speed = -0.5 * MAX_SPEED;
}
else if (right_obstacle) {
left_speed = -0.5 * MAX_SPEED;
right_speed = 0.5 * MAX_SPEED;
}
wb_motor_set_velocity(left_motor, left_speed);
wb_motor_set_velocity(right_motor, right_speed);
}
wb_robot_cleanup();
return 0;
}
5.3 SENSORS DATA
No. Sensors Data Direction of Robot
Right Sensor LEFT Sensor
ps0 ps1 ps2 ps5 ps6 ps7
1 73.82 68.06 63 64.5 65 63 RIGHT
2 65.8 67.7 62.2 66.9 66.7 65.2 RIGHT
3 67.3 63.21 67.7 68.2 70.6 63 RIGHT
4 70.1 64.1 69.9 66.5 66.3 69.1 LEFT
5 62.8 69.6 76.9 67.2 66.5 73.6 FORWARD
6 66.9 66.3 69.3 66.4 69.7 72.1 LEFT
7 70.9 68.3 66.2 60.4 74.7 64.5 RIGHT
8 63.4 64.7 64.8 70.5 62.3 67.6 LEFT
6.0 DISCUSSION
This lab implemented an obstacle avoidance algorithm for a Webots robot using distance
sensors. The algorithm successfully turns the robot away from obstacles and moves forward
when no obstacles are detected. However, it has limitations, such as not always turning in the
optimal direction and relying on a simple threshold-based approach. Future improvements
could include using machine learning or computer vision for obstacle detection and
incorporating more advanced navigation strategies. Overall, this lab demonstrates the basic
principles of obstacle avoidance and provides a foundation for more advanced robotics
projects.
7.0 CONCLUSION
In conclusion, the obstacle avoidance algorithm implemented in this lab successfully enabled
the Webots robot to navigate around obstacles using distance sensors. While the algorithm
has its limitations, it provides a solid foundation for further development and improvement.
Future enhancements can focus on incorporating more advanced techniques to enhance the
robot's navigation capabilities and adaptability in complex environments.
APPENDIX
#include <webots/robot.h>
#include <webots/distance_sensor.h>
#include <webots/motor.h>
#include <stdio.h>
#define TIME_STEP 64
int main(int argc, char **argv)
{
wb_robot_init(); //necessary to initialize webots stuff
/*Declaration*/
WbDeviceTag motor_right = wb_robot_get_device("right wheel
motor"); WbDeviceTag motor_left = wb_robot_get_device("left wheel
motor");
WbDeviceTag S0 = wb_robot_get_device("ps0");
WbDeviceTag S1 = wb_robot_get_device("ps1");
/*Enable Sensors*/
wb_distance_sensor_enable(S0, TIME_STEP);
wb_distance_sensor_enable(S1, TIME_STEP);
/*Initial Position Motor to be use with motor_velocity*/
wb_motor_set_position(motor_right,INFINITY);
wb_motor_set_position(motor_left,INFINITY);
/* main loop Perform simulation steps of TIME_STEP milliseconds and leave the loop when
the simulation is over */
while (wb_robot_step(TIME_STEP) != -1)
{ /* Read the sensors */
const double Sens0 = wb_distance_sensor_get_value(S0);
const double Sens1 = wb_distance_sensor_get_value(S1);
/*Display sensors reading*/
printf("Right Sensor : %f\n", SensR);
printf("Left Sensor : %f\n", SensL);
/* GO Straight */
wb_motor_set_velocity(motor_right,6.0);
wb_motor_set_velocity(motor_left,6.0);
/* Turn LEFT */
wb_motor_set_velocity(motor_right,3.0);
wb_motor_set_velocity(motor_left,1.0);
/* Turn RIGHT */
wb_motor_set_velocity(motor_right,1.0);
wb_motor_set_velocity(motor_left,3.0);
}
wb_robot_cleanup(); //This is necessary to cleanup webots resources return 0;
}