Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the following code in done in WeBots and written in java. Can you imrpove the PID wallFollowing section. import com.cyberbotics.webots.controller.Motor; import com.cyberbotics.webots.controller.Supervisor; import com.cyberbotics.webots.controller.Node; public

the following code in done in WeBots and written in java. Can you imrpove the PID wallFollowing section. import com.cyberbotics.webots.controller.Motor;
import com.cyberbotics.webots.controller.Supervisor;
import com.cyberbotics.webots.controller.Node;
public class PioneerNav2{
public static enum MoveState {
STOP,
FORWARD,
ARC,
FOLLOW_WALL };
private Supervisor robot; // reference to the robot
private Node robot_node; // reference to the robot node
private Pose robot_pose; // the robots percieved pose
private Motor left_motor;
private Motor right_motor;
private double max_vel;
private double prev_error;
private double total_error;
private PioneerProxSensors1 prox_sensors;
private MoveState state;
private final double WHEEL_RADIUS =0.0957; // in meters - found using CONFIGURE
private final double AXEL_LENGTH =0.323; // in meters - found using CONFIGURE
//==================================================================================
// Constructor
//==================================================================================
public PioneerNav2(Supervisor robot, Pose init_pose,PioneerProxSensors1 ps){
this.prox_sensors = ps;
this.robot = robot;
this.robot_node = this.robot.getSelf(); // reference to the robot node
this.state = MoveState.STOP;
// enable motors
this.left_motor = robot.getMotor("left wheel");
this.right_motor = robot.getMotor("right wheel");
this.left_motor.setPosition(Double.POSITIVE_INFINITY);
this.right_motor.setPosition(Double.POSITIVE_INFINITY);
this.max_vel = this.left_motor.getMaxVelocity()-0.1; // Fudge: just under max vel
int timeStep =(int) Math.round(robot.getBasicTimeStep());
this.max_vel = this.left_motor.getMaxVelocity()-0.1; //
// set up pose
this.robot_pose = new Pose(init_pose.getX(), init_pose.getY(), init_pose.getTheta());
// Initialise motor velocity
this.left_motor.setVelocity(0.0);
this.right_motor.setVelocity(0.0);
this.prev_error =0;
this.total_error =0;
}
// The following method only works if in supervisor mode
public Pose get_real_pose(){
if (this.robot_node == null)
return new Pose(0,0,0);
double[] realPos = robot_node.getPosition();
double[] rot = this.robot_node.getOrientation(); //3x3 Rotation matrix as vector of length 9
double theta1= Math.atan2(-rot[0], rot[3]);
double halfPi = Math.PI/2;
double theta2= theta1+ halfPi;
if (theta1> halfPi)
theta2=-(3*halfPi)+theta1;
return new Pose(realPos[0], realPos[1], theta2);
}
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 MoveState getState(){
return this.state;
}
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);
}
private double pid(double error){
double kp =0.5; // prop

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions