Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
fixed driving to coordinates
Browse files Browse the repository at this point in the history
by fixing the normalization method of the Position2D class
  • Loading branch information
RobotIGS-Building committed Sep 28, 2023
1 parent 30a46fb commit ec397a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TestCoordinateDriving extends BaseAutonomous {

@Override
public void initialize() {
navi = new FieldNavigation();
navi = new FieldNavigation(new Position2D(100,50), 0.0);
chassi = new MecanumChassi();
chassi.setRotationAxis(1);
chassi.populateMotorArray(hardwareMap);
Expand All @@ -29,7 +29,7 @@ public void initialize() {

@Override
public void run() {
robot.drive(new Position2D(100.0, 0.0));
robot.drive(new Position2D(0.0, 0.0), false);

while (opModeIsActive()) {
robot.step();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ else if (this.y == 0.0)

// normalize
double alpha = Math.atan(this.y / this.x);
boolean x = this.x > 0;
boolean y = this.y > 0;
return new Position2D((x?1:.1)*Math.cos(alpha),(y?1:-1)*Math.sin(alpha));
return new Position2D(
(this.x/Math.abs(this.x)) * Math.cos(alpha),
(this.x/Math.abs(this.x)) * Math.sin(alpha)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class FieldNavigation {
* @param position position of the robot in CM
* @param rotation rotation of the robot in Degrees
*/

// TODO remove rotation as it is part of chassi in the constructor
public FieldNavigation(Position2D position, double rotation) {
this.driving = false;
this.position = position;
Expand Down Expand Up @@ -128,45 +130,43 @@ public void drive_speed(double vx, double vy, double wz){
}

public void stop(){
drive_speed(0,0,0);
drive_speed(0.0,0.0,0.0);
}


public String debug() {
String ret = "--- FieldNavigation Debug ---\n";
ret += String.format("driving :: %s\ntarget position :: x=%+3.1f y=%+3.1f\n",
(this.driving?"True":"False"), target_position.getX(), target_position.getY());
ret += String.format("distance :: x=%+3.1f %+3.1f\n", this.distance.getX(), this.distance.getY());
ret += String.format("position :: x=%+3.1f y=%+3.1f\n",
position.getX(), position.getY());
ret += String.format("velocity :: x=%+1.2f y=%+1.2f wz=%+1.2f\n",
velocity.getVX(), velocity.getVY(), velocity.getWZ());

ret += String.format("DIST :: %f %f\n", distance.getX(), distance.getY());

return ret;
}

/**
* refresh
*/
public void step() {
Position2D distance;
double quad = 0;

distance = target_position.copy();
distance.subract(position);

if (driving) {
// calculate the distance to the target position
this.distance = target_position.copy();
this.distance.subract(position);

// test if in range of the target position (reached)
if (Math.abs(distance.getAbsolute()) <= this.driving_accuracy)
stop();

distance = distance.getNormalization();
else {
// calculate velocity for the chassi
Position2D distance = this.distance.getNormalization();
distance.rotate(-this.rotation);

distance.rotate(-this.rotation);
this.distance = distance;
velocity.set(distance.getX()*0.3, distance.getY()*0.3, 0.0);
} else {
velocity.set(0.0,0.0,0.0); // TODO remove this
// setting the velocity for the chassi
velocity.set(distance.getX() * 0.3, distance.getY() * 0.3, 0.0);
}
}
}
}

0 comments on commit ec397a9

Please sign in to comment.