In: Computer Science
Write a C program to control an autonomous robot. It runs on two wheels, each wheel connected to a DC motor. If both wheels run at the same speed, robot moves forward. If the right wheel runs slower, robot makes a right turn. There are two sensors mounted on the robot that can detect an obstacle, one in the front, one on the right.
Once the robot is turned on, it moves forward at 80% of the maximum possible speed. When an obstacle is detected in front, if there is nothing blocking on the right side; it slows down to 40% of maximum speed and then it makes a right turn. Unless there is an obstacle on the right side too, then the robot will stop.
You must use PWM to control the wheels. The sensor outputs are digital, the mbed board will detect low when no obstacle and will detect high, when there is an obstacle. This is pin connections for this robot.
Device LPC1768 mbed pin
Right wheel pin 21
Left wheel pin 22
Front sensor pin 10
Right sensor pin 11
Use a flowchart to show the algorithm of your program.
Above is the flowchart for the
question and below is the c program for the same problem
#include <stdio.h>
int right_wh = 0; //value can be 0 to 255 (where 0 is 0% speed and
255 is 100% speed)
int left_wh = 0; //value can be 0 to 255 (where 0 is 0% speed and
255 is 100% speed)
char front_sen = 'L'; //value can be L or H (where L is low and H
is high)
char right_sen = 'L'; //value can be L or H (where L is low and H
is high)
int main()
{
while (front_sen !='H' && right_sen !='H'){
right_wh=left_wh=255*.80; //increasing speed to 80%
if (front_sen =='H'){
if (right_wh=='H'){
break;
}
else{
right_wh=left_wh=255*.40; //reducing speed to 40%
right_wh=255*.20 //reducing speed of right wheel to make a right
turn
break;
}
}
}
return 0;
}