@@ -88,6 +88,7 @@ private enum ConnectionType {
88
88
private final double trackWidth ;
89
89
90
90
MovePilot mPilot = null ;
91
+ Chassis chassis = null ;
91
92
92
93
private final BluetoothCom blueCom = new BluetoothComImpl ();
93
94
@@ -132,14 +133,14 @@ public Hal(Configuration brickConfiguration, Set<UsedSensor> usedSensors) {
132
133
this .brickConfiguration .getActorOnPort (brickConfiguration .getRightMotorPort ()).getRotationDirection () == DriveDirection .BACKWARD ;
133
134
Wheel leftWheel = WheeledChassis .modelWheel (leftRegulatedMotor , this .wheelDiameter ).offset (this .trackWidth / 2. ).invert (isLeftActorInverse );
134
135
Wheel rightWeel = WheeledChassis .modelWheel (rightRegulatedMotor , this .wheelDiameter ).offset (-this .trackWidth / 2. ).invert (isRightActorInverse );
135
- Chassis chassis =
136
+ this . chassis =
136
137
new WheeledChassis (
137
138
new Wheel [] {
138
139
leftWheel ,
139
140
rightWeel
140
141
},
141
142
WheeledChassis .TYPE_DIFFERENTIAL );
142
- this .mPilot = new MovePilot (chassis );
143
+ this .mPilot = new MovePilot (this . chassis );
143
144
144
145
} catch ( DbcException e ) {
145
146
// 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) {
792
793
* @param speedPercent of motor power
793
794
*/
794
795
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 );
807
799
}
808
800
809
801
/**
@@ -819,58 +811,54 @@ public void regulatedDrive(DriveDirection direction, float speedPercent) {
819
811
* @param distance that the robot should travel
820
812
*/
821
813
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
+ }
823
819
this .mPilot .setLinearSpeed (this .mPilot .getMaxLinearSpeed () * speedPercent / 100.0 );
824
820
switch ( direction ) {
825
821
case FOREWARD :
826
- this .mPilot .travel (distance );
822
+ this .mPilot .travel (direct * distance );
827
823
break ;
828
824
case BACKWARD :
829
- this .mPilot .travel (-distance );
825
+ this .mPilot .travel (direct * -distance );
830
826
break ;
831
827
default :
832
828
throw new DbcException ("incorrect DriveAction" );
833
829
}
834
830
}
835
831
836
832
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 );
852
840
}
853
841
854
842
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 );
858
845
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 ;
860
851
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 );
869
857
} 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 );
872
861
}
873
-
874
862
}
875
863
876
864
private float calculateRadius (float speedLeft , float speedRight ) {
@@ -879,7 +867,7 @@ private float calculateRadius(float speedLeft, float speedRight) {
879
867
}
880
868
881
869
private float calculateSpeedDriveInCurve (float speedLeft , float speedRight ) {
882
- return (Math . abs ( speedLeft ) + Math . abs ( speedRight ) ) / 2.0f ;
870
+ return (speedLeft + speedRight ) / 2.0f ;
883
871
}
884
872
885
873
/**
@@ -903,19 +891,9 @@ public void stopRegulatedDrive() {
903
891
* @param speedPercent of motor power
904
892
*/
905
893
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 );
919
897
}
920
898
921
899
/**
0 commit comments