Question
#include #include // include Seeed Studio ultrasonic ranger library #include // header file needed for I2C commands DualVNH5019MotorShield md; // md is a class of
#include
#include
DualVNH5019MotorShield md; // md is a class of the motor drive
#define LeftMotor md.setM1Speed
#define RightMotor md.setM2Speed
#define leftrangerpin 5
#define rightrangerpin 3
#define rearrangerpin 11
// All these trigger level values should be try & error to fine tune as differenct robot has vary performance.
#define IR_SENSOR_OBJECT_TRIGGER_LEVEL 20
#define LEFT_ULTRA_SENSOR_OBJECT_TRIGGER_LEVEL 15
#define RIGHT_ULTRA_SENSOR_OBJECT_TRIGGER_LEVEL 25
#define REAR_ULTRA_SENSOR_OBJECT_TRIGGER_LEVEL 20
Ultrasonic ultrasonic_left(leftrangerpin);
Ultrasonic ultrasonic_right(rightrangerpin); Ultrasonic ultrasonic_rear(rearrangerpin);
// Parameter and variables declaration.
int IRpin = 3; // analog pin for reading the IR sensor [ADC]
const int DrvPinCW = 3 ; // pin3 attached to the pin A of L293 const int DrvPinAW = 9 ; // pin9 attached to the pin B of L293 const int DrvPinEN = 4 ; // pin4 attached to the Enable pin of L293
const int encA = 21 ;
// IR sensor variables and parameter. int IRsensor_detected_Range = 0;
void setup()
{
// Serial Monitor setup.
Serial.begin(115200);
Serial.println("Differential Drive control");
// Motor driver setup. // initialise the motor driver. md.init();
// initialize the DrvPinCW and DrvPinAW as output:
pinMode(DrvPinCW, OUTPUT ); pinMode(DrvPinAW, OUTPUT );
pinMode(DrvPinEN, OUTPUT);
}
void TestingLoop()
{
while (true)
{
int sensorDistance; int leftranger; int rightranger;
int rearranger;
leftranger = ultrasonic_left.MeasureInCentimeters(); rightranger = ultrasonic_right.MeasureInCentimeters(); rearranger = ultrasonic_rear.MeasureInCentimeters();
Serial.print(" left/right/rear Sensor value is ");
Serial.print(leftranger);
Serial.print("/");
Serial.print(rightranger);
Serial.print("/");
Serial.println(rearranger);
sensorDistance = Get_IR_SensorRange();
Serial.print("IR Sensor value is "); Serial.println(sensorDistance);
delay(500);
}
}
void loop()
{
bool obstacle_Infront = false; bool obstacle_left = false; bool obstacle_right = false; float left_ultrasonic_dist = Get_left_UltraSonic_SensorRange(); float right_ultrasonic_dist = Get_right_UltraSonic_SensorRange();
// Check any obtacles in front of the robot
// Left turn 90 degree or Right turn 90 degree, recheck the robot front, // Move forward if no obtacle in front.
//TestingLoop(); // This is only testing loop, comment it or block it if you want to run your real code.
obstacle_Infront = Is_IR_Sense_Obstacle_Infront(); obstacle_left = Is_Left_UltraSensor_detect_Obstacle(); obstacle_right = Is_Right_UltraSensor_detect_Obstacle();
// for checking sensor status
Serial.print(obstacle_left);
Serial.print(" ");
Serial.print(obstacle_Infront);
Serial.print(" ");
Serial.println(obstacle_right) ;
if (obstacle_left)
{
TurnRight_90Deg();
}
MoveForward();
Serial.println("forward");
}
bool Is_Left_UltraSensor_detect_Obstacle()
{
bool ans = false;
if( Get_left_UltraSonic_SensorRange() < LEFT_ULTRA_SENSOR_OBJECT_TRIGGER_LEVEL )
{
ans = true;
}
return(ans);
}
bool Is_Right_UltraSensor_detect_Obstacle()
{
bool ans = false;
if( Get_right_UltraSonic_SensorRange() < RIGHT_ULTRA_SENSOR_OBJECT_TRIGGER_LEVEL )
{
ans = true;
}
return(ans);
}
bool Is_Rear_UltraSensor_detect_Obstacle()
{
bool ans = false;
if( Get_rear_UltraSonic_SensorRange() < REAR_ULTRA_SENSOR_OBJECT_TRIGGER_LEVEL )
{
ans = true;
}
return(ans);
}
bool Is_IR_Sense_Obstacle_Infront()
{
bool ans = false;
float IRsensor_detected_Range;
IRsensor_detected_Range = Get_IR_SensorRange();
if( IRsensor_detected_Range < IR_SENSOR_OBJECT_TRIGGER_LEVEL) // There is obstacle in front of the robot.
{
ans = true;
}
return (ans);
}
void TurnLeft_90Deg()
{
LeftMotor(200); RightMotor(200);
delay(200);
}
void TurnRight_90Deg()
{
LeftMotor(-200); RightMotor(-200);
delay(200);
}
void MoveForward()
{
LeftMotor(-200);
RightMotor(200);
}
void MoveBack()
{
LeftMotor(200);
RightMotor(-200);
}
void MoveRight()
{
LeftMotor(-200);
RightMotor(-200);
}
void MoveLeft()
{
LeftMotor(200);
RightMotor(200);
}
void justStop()
{
LeftMotor(0);
RightMotor(0);
}
void moveOnSpot()
{
LeftMotor(200);
RightMotor(200);
}
float Get_IR_SensorRange()
{
float volts = analogRead(IRpin) * 0.0048828125; // why multiply 0.0048828125? (5v/1024) float distance = 29.988 * pow(volts, -1.173); // for model 2Y0A21. return (distance);
}
float Get_left_UltraSonic_SensorRange()
{
int leftranger;
leftranger = ultrasonic_left.MeasureInCentimeters();
return (leftranger);
}
float Get_right_UltraSonic_SensorRange()
{
int rightranger;
rightranger = ultrasonic_right.MeasureInCentimeters();
return (rightranger);
}
float Get_rear_UltraSonic_SensorRange()
{
int rearranger;
rearranger = ultrasonic_rear.MeasureInCentimeters();
return (rearranger);
}
Can help me to fix this code i don't know why i keep getting errors every time.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started