Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please implement a obstacle avoidance algorithm here. navigator class public static enum MoveState { STOP, FORWARD, ARC, CONFIGURE, FOLLOW _ WALL } / / more

please implement a obstacle avoidance algorithm here.
navigator class
public static enum MoveState {
STOP,
FORWARD,
ARC,
CONFIGURE,
FOLLOW_WALL }
// more private class
private PioneerProxSensors1 prox_sensors;
//////////////////////////////
more code her
/////////////////////////////
public int forward(double target_dist, double robot_linearvelocity){
double wheel_av =(robot_linearvelocity/this.WHEEL_RADIUS);
double target_time = target_dist/robot_linearvelocity;
this.left_motor.setVelocity(wheel_av);
this.right_motor.setVelocity(wheel_av);
this.state = MoveState.FORWARD;
// return target_time as millisecs
return (int)(1000.0*target_time);
}
public int arc(double icr_angle, double icr_r, double icr_omega){
double target_time = icr_angle / icr_omega;
// Calculate each wheel velocity around ICR
double vl = icr_omega *(icr_r -(this.AXEL_LENGTH /2));
double vr = icr_omega *(icr_r +(this.AXEL_LENGTH /2));
double leftwheel_av =(vl/this.WHEEL_RADIUS);
double rightwheel_av =(vr/this.WHEEL_RADIUS);
this.left_motor.setVelocity(leftwheel_av);
this.right_motor.setVelocity(rightwheel_av);
this.state = MoveState.ARC;
// return target_time as millisecs
return (int)(1000.0*target_time);
}
public void stop(){
this.left_motor.setVelocity(0.0);
this.right_motor.setVelocity(0.0);
this.state = MoveState.STOP;
}
public void set_velocity(double base, double control){
// base gives the velocity of the wheels in m/s
// control is an adjustment on the main velocity
double base_av =(base/this.WHEEL_RADIUS);
double lv = base_av;
double rv = base_av;
if (control !=0){
double control_av =(control/this.WHEEL_RADIUS);
// Check if we exceed max velocity and compensate
double correction =1;
lv = base_av - control_av;
rv = base_av + control_av;
if (lv > this.max_vel){
correction = this.max_vel / lv;
lv = lv * correction;
rv = rv * correction;
}
if (rv > this.max_vel){
correction = this.max_vel / rv;
lv = lv * correction;
rv = rv * correction;
}
}
this.left_motor.setVelocity(lv);
this.right_motor.setVelocity(rv);
}
public MoveState getState(){
return this.state;
}
private double pid(double error){
double kp =0.475; // proportional weight (may need tuning)
double kd =3; // differential weight (may need tuning)
double ki =0.0475; // integral weight (may need tuning)
double prop = error;
double diff = error - this.prev_error;
this.total_error += error;
double control =(kp * prop)+(ki * this.total_error)+(kd * diff);
this.prev_error = error;
return control;
}
public void follow_wall(double robot_linearvelocity, double set_point, boolean right){
int direction_coeff =1;
double error;
double control;
double wall_dist;
if (right) direction_coeff =-1; // invert the values for the control
if (Math.min(this.prox_sensors.get_value(1),
Math.min(this.prox_sensors.get_value(2),
Math.min(this.prox_sensors.get_value(3),
Math.min(this.prox_sensors.get_value(4),
Math.min(this.prox_sensors.get_value(5),
this.prox_sensors.get_value(6))))))< set_point)
this.set_velocity(robot_linearvelocity/3,-0.2*direction_coeff);
else {
if (!right) wall_dist = Math.min(this.prox_sensors.get_value(1),
this.prox_sensors.get_value(0));
else wall_dist = Math.min(this.prox_sensors.get_value(7),
this.prox_sensors.get_value(8));
// Running aproximately parallel to the wall
if (wall_dist < this.prox_sensors.get_maxRange()){
error = wall_dist - set_point;
control = this.pid(error);
// adjust for right wall
this.set_velocity(robot_linearvelocity, control*direction_coeff);
} else {
// No wall, so turn
this.set_velocity(robot_linearvelocity, 0.08*direction_coeff);
}
}
this.state = MoveState.FOLLOW_WALL;
}
controller class
double time_elapsed =0;
double target_time =0;
double robot_velocity =0.3;
// define schedule
PioneerNav2.MoveState[] schedule ={ PioneerNav2.MoveState.FOLLOW_WALL};
int schedule_index =-1; // we increment before selecting the current action
PioneerNav2.MoveState state; // current state
// set up display
String display_action ="";
if (schedule[0]== PioneerNav2.MoveState.CONFIGURE){
schedule_index++;
nav.configure_initialise_parameters(2*Math.PI);
}
while (robot.step(timeStep)!=-1){
robot_pose = nav.get_real_pose();
prox_sensors.set_pose(robot_pose);
etc

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions