Skip to content

Commit 47b8298

Browse files
committed
OpenRoberta/robertalab-ev3lejos-v0#6 revise drive actions in hal
1 parent 7bb2377 commit 47b8298

File tree

1 file changed

+39
-61
lines changed
  • EV3Runtime/src/main/java/de/fhg/iais/roberta/runtime/ev3

1 file changed

+39
-61
lines changed

EV3Runtime/src/main/java/de/fhg/iais/roberta/runtime/ev3/Hal.java

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private enum ConnectionType {
8888
private final double trackWidth;
8989

9090
MovePilot mPilot = null;
91+
Chassis chassis = null;
9192

9293
private final BluetoothCom blueCom = new BluetoothComImpl();
9394

@@ -132,14 +133,14 @@ public Hal(Configuration brickConfiguration, Set<UsedSensor> usedSensors) {
132133
this.brickConfiguration.getActorOnPort(brickConfiguration.getRightMotorPort()).getRotationDirection() == DriveDirection.BACKWARD;
133134
Wheel leftWheel = WheeledChassis.modelWheel(leftRegulatedMotor, this.wheelDiameter).offset(this.trackWidth / 2.).invert(isLeftActorInverse);
134135
Wheel rightWeel = WheeledChassis.modelWheel(rightRegulatedMotor, this.wheelDiameter).offset(-this.trackWidth / 2.).invert(isRightActorInverse);
135-
Chassis chassis =
136+
this.chassis =
136137
new WheeledChassis(
137138
new Wheel[] {
138139
leftWheel,
139140
rightWeel
140141
},
141142
WheeledChassis.TYPE_DIFFERENTIAL);
142-
this.mPilot = new MovePilot(chassis);
143+
this.mPilot = new MovePilot(this.chassis);
143144

144145
} catch ( DbcException e ) {
145146
// do not instantiate because we do not need it (checked form code generation side)
@@ -792,18 +793,9 @@ public void stopUnregulatedMotor(ActorPort actorPort, MotorStopMode floating) {
792793
* @param speedPercent of motor power
793794
*/
794795
public void regulatedDrive(DriveDirection direction, float speedPercent) {
795-
speedPercent = speedPercent > 100 ? 100 : speedPercent;
796-
this.mPilot.setLinearSpeed(this.mPilot.getMaxLinearSpeed() * speedPercent / 100.0);
797-
switch ( direction ) {
798-
case FOREWARD:
799-
this.mPilot.forward();
800-
break;
801-
case BACKWARD:
802-
this.mPilot.backward();
803-
break;
804-
default:
805-
throw new DbcException("wrong DriveAction.Direction");
806-
}
796+
int direct = direction == DriveDirection.FOREWARD ? 1 : -1;
797+
speedPercent = (float) (this.mPilot.getMaxLinearSpeed() * speedPercent / 100.0);
798+
this.chassis.setVelocity(direct * speedPercent, 0);
807799
}
808800

809801
/**
@@ -819,58 +811,54 @@ public void regulatedDrive(DriveDirection direction, float speedPercent) {
819811
* @param distance that the robot should travel
820812
*/
821813
public void driveDistance(DriveDirection direction, float speedPercent, float distance) {
822-
speedPercent = speedPercent > 100 ? 100 : speedPercent;
814+
int direct = 1;
815+
if ( speedPercent < 0 ) {
816+
direct = -1;
817+
speedPercent = direct * speedPercent;
818+
}
823819
this.mPilot.setLinearSpeed(this.mPilot.getMaxLinearSpeed() * speedPercent / 100.0);
824820
switch ( direction ) {
825821
case FOREWARD:
826-
this.mPilot.travel(distance);
822+
this.mPilot.travel(direct * distance);
827823
break;
828824
case BACKWARD:
829-
this.mPilot.travel(-distance);
825+
this.mPilot.travel(direct * -distance);
830826
break;
831827
default:
832828
throw new DbcException("incorrect DriveAction");
833829
}
834830
}
835831

836832
public void driveInCurve(DriveDirection direction, float speedLeft, float speedRight) {
837-
speedLeft = speedLeft > 100 ? 100 : speedLeft;
838-
speedRight = speedRight > 100 ? 100 : speedRight;
839-
float robotSpeed = calculateSpeedDriveInCurve(speedLeft, speedRight);
840-
this.mPilot.setLinearSpeed(this.mPilot.getMaxLinearSpeed() * robotSpeed / 100.0);
841-
float radius = calculateRadius(speedLeft, speedRight);
842-
if ( speedLeft == speedRight ) {
843-
regulatedDrive(direction, speedLeft);
844-
return;
845-
}
846-
if ( direction == DriveDirection.FOREWARD ) {
847-
this.mPilot.arcForward(radius);
848-
} else {
849-
this.mPilot.arcBackward(radius);
850-
}
851-
833+
speedLeft = (float) (chassis.getMaxLinearSpeed() * speedLeft / 100.0);
834+
speedRight = (float) (chassis.getMaxLinearSpeed() * speedRight / 100.0);
835+
int direct = direction == DriveDirection.FOREWARD ? 1 : -1;
836+
double radius = calculateRadius(speedLeft, speedRight);
837+
double Lspeed = calculateSpeedDriveInCurve(speedLeft, speedRight);
838+
double Aspeed = Lspeed / radius * 180.0 / Math.PI;
839+
this.chassis.setVelocity(direct * Lspeed, direct * Aspeed);
852840
}
853841

854842
public void driveInCurve(DriveDirection direction, float speedLeft, float speedRight, float distance) {
855-
speedLeft = speedLeft > 100 ? 100 : speedLeft;
856-
speedRight = speedRight > 100 ? 100 : speedRight;
857-
float robotSpeed = calculateSpeedDriveInCurve(speedLeft, speedRight);
843+
speedLeft = (float) (mPilot.getMaxLinearSpeed() * speedLeft / 100.0);
844+
speedRight = (float) (mPilot.getMaxLinearSpeed() * speedRight / 100.0);
858845
int direct = direction == DriveDirection.FOREWARD ? 1 : -1;
859-
float radius = calculateRadius(speedLeft, speedRight);
846+
double radius = Math.abs(calculateRadius(speedLeft, speedRight));
847+
double robotSpeed = calculateSpeedDriveInCurve(speedLeft, speedRight);
848+
double Lspeed = Math.abs(robotSpeed);
849+
double Aspeed = Lspeed / radius * 180.0 / Math.PI;
850+
double angle = direct * Math.signum(robotSpeed) * 360.0 / (2 * Math.PI * radius) * distance;
860851
if ( speedLeft == speedRight ) {
861-
driveDistance(direction, speedLeft, distance);
862-
return;
863-
}
864-
if ( radius == 0 ) {
865-
double angle = distance / (Math.PI * this.trackWidth) * 360.0;
866-
// this.mPilot.setAngularSpeed(toDegPerSec(robotSpeed));
867-
this.mPilot.setAngularSpeed(this.mPilot.getMaxAngularSpeed() * robotSpeed / 100.);
868-
this.mPilot.rotate(direct * angle, false);
852+
this.mPilot.setLinearSpeed(Lspeed);
853+
this.mPilot.travel(direct * Math.signum(robotSpeed) * distance);
854+
} else if ( robotSpeed == 0 ) {
855+
this.mPilot.setAngularSpeed(speedLeft);
856+
this.mPilot.rotate(direct * 10000, false);
869857
} else {
870-
this.mPilot.setLinearSpeed(this.mPilot.getMaxLinearSpeed() * robotSpeed / 100.0);
871-
this.mPilot.travelArc(radius, direct * distance);
858+
this.mPilot.setAngularSpeed(Aspeed);
859+
this.mPilot.setLinearSpeed(Lspeed);
860+
this.mPilot.arc(radius, angle, false);
872861
}
873-
874862
}
875863

876864
private float calculateRadius(float speedLeft, float speedRight) {
@@ -879,7 +867,7 @@ private float calculateRadius(float speedLeft, float speedRight) {
879867
}
880868

881869
private float calculateSpeedDriveInCurve(float speedLeft, float speedRight) {
882-
return (Math.abs(speedLeft) + Math.abs(speedRight)) / 2.0f;
870+
return (speedLeft + speedRight) / 2.0f;
883871
}
884872

885873
/**
@@ -903,19 +891,9 @@ public void stopRegulatedDrive() {
903891
* @param speedPercent of motor power
904892
*/
905893
public void rotateDirectionRegulated(TurnDirection direction, float speedPercent) {
906-
this.mPilot.setAngularSpeed(this.mPilot.getMaxAngularSpeed() * speedPercent / 100.);
907-
System.out.println(this.mPilot.getMaxAngularSpeed());
908-
// this.mPilot.setAngularSpeed(toDegPerSec((int) speedPercent));
909-
switch ( direction ) {
910-
case RIGHT:
911-
this.mPilot.rotate(-10000, true);
912-
break;
913-
case LEFT:
914-
this.mPilot.rotate(10000, true);
915-
break;
916-
default:
917-
throw new DbcException("incorrect TurnAction");
918-
}
894+
float speed = (float) (chassis.getMaxAngularSpeed() * speedPercent / 100.0);
895+
int direct = direction == TurnDirection.LEFT ? 1 : -1;
896+
this.chassis.setVelocity(0, direct * speed);
919897
}
920898

921899
/**

0 commit comments

Comments
 (0)