Milkybeans - More human-friendly rewrite of Climber.java - #6
Milkybeans - More human-friendly rewrite of Climber.java#6FriedLongJohns wants to merge 29 commits into
Conversation
-Put all Encoder Positions and Motor speeds into enums -Replaced all encoder set and motor speed set funcs with resetEncodersTo() and moveMotors() which use list lambdas to go fast and not check if statements -replace all boolean funcs with isMotorExtended and isMotorRetracted for all funcs using int argument called "motor", -1 left, 0 both, and 1 right motor resetEncodersTo.*() -> resetEncodersTo(EncoderPos, int motor) (?:slow)?(extend|retract).*() -> moveMotors(MotorSpeed, int motor) stop.*() -> stopMotors(int motor) is(Right|Left)(?:Reset)?(Retracted|Extended) -> isMotorExtended(int motor, boolean reset) and isMotorRetracted(int motor, boolean reset)
+Motor selection tuple +getMotorPos now maps to a lambda array =Rework isMotor(Extended|Retracted) to not use reset boolean anymore and operate on a "true until false" basis
-Fix enum system in Climber.java to actually work as intended by using dictionaries. -Go fix all the uses of Climber.java to also work correctly -It builds now Probably still horribly buggy
Co-authored-by: Kedas <m@yarn.network>
Co-authored-by: Kedas <m@yarn.network>
Co-authored-by: Kedas <m@yarn.network>
CoolSpy3
left a comment
There was a problem hiding this comment.
Looks pretty good. I just have a few comments.
| new JoystickButton(rightJoy, Constants.OI.RightJoy.slowExtendLeftClimberPort).whileHeld(new InstantCommand(()->climber.moveMotors(Climber.MotorSpeed.slowExtend,climber.leftMotor))).whenReleased(new InstantCommand(()->climber.stopMotors(climber.leftMotor))); | ||
| new JoystickButton(rightJoy, Constants.OI.RightJoy.slowRetractLeftClimberPort).whileHeld(new InstantCommand(()->climber.moveMotors(Climber.MotorSpeed.slowRetract,climber.leftMotor))).whenReleased(new InstantCommand(()->climber.stopMotors(climber.leftMotor))); | ||
| new JoystickButton(rightJoy, Constants.OI.RightJoy.slowExtendRightClimberPort).whileHeld(new InstantCommand(()->climber.moveMotors(Climber.MotorSpeed.slowExtend,climber.rightMotor))).whenReleased(new InstantCommand(()->climber.stopMotors(climber.rightMotor))); | ||
| new JoystickButton(rightJoy, Constants.OI.RightJoy.slowRetractRightClimberPort).whileHeld(new InstantCommand(()->climber.moveMotors(Climber.MotorSpeed.slowRetract,climber.rightMotor))).whenReleased(new InstantCommand(()->climber.stopMotors(climber.rightMotor))); |
There was a problem hiding this comment.
climber.leftMotor
climber.rightMotor
Static members should be accessed in a static manner
Climber.leftMotor
Climber.rightMotor
| super( | ||
| new FunctionalCommand( | ||
| () -> {}, | ||
| climber::extendLeft, | ||
| climber::stopLeft, | ||
| climber::isLeftExtended | ||
| ), | ||
| new FunctionalCommand( | ||
| () -> {}, | ||
| climber::extendRight, | ||
| climber::stopRight, | ||
| climber::isRightExtended | ||
| ) | ||
| ); |
There was a problem hiding this comment.
The point of using a ParallelCommandGroup with individual commands for each arm is that the climber won't malfunction if the arms become unaligned (unlikely but possible). I recommend that you restore this behavior through the use of a ParallelCommandGroup or by checking and updating the state of each arm in execute.
| public void end(){ | ||
| climber.stopMotors(climber.bothMotors); | ||
| } |
There was a problem hiding this comment.
Function end takes one argument boolean interrupted
| import edu.wpi.first.wpilibj2.command.CommandBase; | ||
|
|
||
| public class RetractClimber extends ParallelCommandGroup { | ||
| public class RetractClimber extends CommandBase { |
|
|
||
| public static enum MotorSpeed{retract,extend,slowRetract,slowExtend;}; | ||
| private static Dictionary dMotorSpeed = new Hashtable();//given values in constructor | ||
|
|
||
|
|
||
| public static enum EncoderPos{extendLeft,extendRight,retractLeft,retractRight,zero;}; | ||
| private static Dictionary dEncoderPos = new Hashtable();//given values in constructor |
There was a problem hiding this comment.
I recommend assigning a member variable to these enums instead of using a dictionary. For an example, see Limelight.TurnDirection. Additionally, it's convention to define enums at the end of the file.
|
|
||
| public class ExtendClimber extends ParallelCommandGroup { | ||
| public class ExtendClimber extends CommandBase { | ||
| private Climber climber; |
|
|
||
| private final Consumer[] setEncoder = new Consumer[]{ // faster to array[](inp) than a bunch of if's in a func | ||
| (pos) -> leftEncoder.setPosition((double) pos), | ||
| (pos) -> { | ||
| leftEncoder.setPosition((double) pos); | ||
| rightEncoder.setPosition((double) pos); | ||
| }, | ||
| (pos) -> rightEncoder.setPosition((double) pos), | ||
| }; | ||
|
|
||
| private final Consumer[] setMotor = new Consumer[]{ | ||
| (speed) -> { | ||
| left.set((double) speed); | ||
| SmartDashboard.putString("Left Climber State", "Moving"); | ||
| }, | ||
| (speed) -> { | ||
| left.set((double) speed); | ||
| right.set((double) speed); | ||
| SmartDashboard.putString("Left Climber State", "Moving"); | ||
| SmartDashboard.putString("Right Climber State", "Moving"); | ||
| }, | ||
| (speed) -> { | ||
| right.set((double) speed); | ||
| SmartDashboard.putString("Right Climber State", "Moving"); | ||
| }, | ||
| }; | ||
|
|
There was a problem hiding this comment.
Please avoid raw types. Use DoubleConsumer instead. This should remove the need for a cast to double.
There was a problem hiding this comment.
Oh, neat.
Didn't know that was a class.
| SmartDashboard.putString("Left Climber State", "Stop"); | ||
| } | ||
| if (motor>-1){ | ||
| right.set(0); | ||
| SmartDashboard.putString("Right climber is", "Stopped"); | ||
| } | ||
|
|
||
| public void stop(boolean interrupted) { | ||
| stop(); | ||
| } | ||
|
|
||
| public void stopLeft(boolean interrupted) { | ||
| stopLeft(); | ||
| } | ||
|
|
||
| public void stopRight(boolean interrupted) { | ||
| stopRight(); | ||
| } | ||
|
|
||
| public double getLeftPosition() { | ||
| return leftEncoder.getPosition(); | ||
| } | ||
|
|
||
| public double getRightPosition() { | ||
| return rightEncoder.getPosition(); | ||
| } | ||
|
|
||
| public boolean isLeftExtended() { | ||
| return getLeftPosition() >= extendPositionLeft; | ||
| } | ||
|
|
||
| public boolean isRightExtended() { | ||
| return getRightPosition() >= extendPositionRight; | ||
| } | ||
|
|
||
| public boolean isLeftRetracted() { | ||
| return getLeftPosition() <= retractPositionLeft; | ||
| } | ||
|
|
||
| public boolean isRightRetracted() { | ||
| return getRightPosition() <= retractPositionRight; | ||
| } | ||
|
|
||
| public boolean isRightResetExtended() { | ||
| return getRightPosition() >= 0; | ||
| SmartDashboard.putString("Right Climber State", "Stop"); |
|
I missed this earlier, but please change the pull request to merge into |
|
I meant to define the enums at the end of the class definition.
…On Wed, Sep 21, 2022 at 8:40 AM FriedLongJohns ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/main/java/org/team199/robot2022/subsystems/Climber.java
<#6 (comment)>
:
> +
+ public static enum MotorSpeed{retract,extend,slowRetract,slowExtend;};
+ private static Dictionary dMotorSpeed = new Hashtable();//given values in constructor
+
+
+ public static enum EncoderPos{extendLeft,extendRight,retractLeft,retractRight,zero;};
+ private static Dictionary dEncoderPos = new Hashtable();//given values in constructor
Do you mean to define them last out of all class variables or literally at
the end of the java class?
—
Reply to this email directly, view it on GitHub
<#6 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANF6GTVMADXOR6ZBXMOFHA3V7MT47ANCNFSM6AAAAAAQPXKW6M>
.
You are receiving this because your review was requested.Message ID:
***@***.***>
|
mmm yes what a good push title
Reject Humanity, return to Monke
|
Finished all your suggestions (I hope), will fix the new conflicts later. |
| import edu.wpi.first.wpilibj2.command.ParallelCommandGroup; | ||
|
|
||
| public class ExtendClimber extends ParallelCommandGroup { | ||
| public final class ExtendClimber extends ParallelCommandGroup { |
There was a problem hiding this comment.
| public final class ExtendClimber extends ParallelCommandGroup { | |
| public class ExtendClimber extends ParallelCommandGroup { |
Sorry, my bad. The final comment was meant to refer to the member variable not the class. The commands do not have to be final.
There was a problem hiding this comment.
Java throws an error that you're not allowed to use the 'final' modifier on the "public (Retract|Extend)Climber(Climber climber)"
So then what is "the member variable?"
There was a problem hiding this comment.
I was talking about the line private Climber climber; and changing it to private final Climber climber; It looks like it was deleted in a previous commit, so no further changes are necessary.
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.rightMotor);}, | ||
| (interrupted) -> {climber.stopMotors(climber.rightMotor);}, | ||
| () -> {return climber.isMotorExtended(climber.rightMotor);} |
There was a problem hiding this comment.
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.rightMotor);}, | |
| (interrupted) -> {climber.stopMotors(climber.rightMotor);}, | |
| () -> {return climber.isMotorExtended(climber.rightMotor);} | |
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,Climber.rightMotor);}, | |
| (interrupted) -> {climber.stopMotors(Climber.rightMotor);}, | |
| () -> {return climber.isMotorExtended(Climber.rightMotor);} |
Static members should be accessed in a static manner. (i.e. use Climber instead of climber)
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.leftMotor);}, | ||
| (interrupted) -> {climber.stopMotors(climber.leftMotor);}, | ||
| () -> {return climber.isMotorExtended(climber.leftMotor);} |
There was a problem hiding this comment.
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.leftMotor);}, | |
| (interrupted) -> {climber.stopMotors(climber.leftMotor);}, | |
| () -> {return climber.isMotorExtended(climber.leftMotor);} | |
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,Climber.leftMotor);}, | |
| (interrupted) -> {climber.stopMotors(Climber.leftMotor);}, | |
| () -> {return climber.isMotorExtended(Climber.leftMotor);} |
Static members should be accessed in a static manner. (i.e. use Climber instead of climber)
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.rightMotor);}, | ||
| (interrupted) -> {climber.stopMotors(climber.rightMotor);}, | ||
| () -> {return climber.isMotorExtended(climber.rightMotor);} |
There was a problem hiding this comment.
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.rightMotor);}, | |
| (interrupted) -> {climber.stopMotors(climber.rightMotor);}, | |
| () -> {return climber.isMotorExtended(climber.rightMotor);} | |
| () -> climber.moveMotors(Climber.MotorSpeed.extend, climber.rightMotor), | |
| (interrupted) -> climber.stopMotors(climber.rightMotor), | |
| () -> climber.isMotorExtended(climber.rightMotor) |
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.leftMotor);}, | ||
| (interrupted) -> {climber.stopMotors(climber.leftMotor);}, | ||
| () -> {return climber.isMotorExtended(climber.leftMotor);} |
There was a problem hiding this comment.
| () -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.leftMotor);}, | |
| (interrupted) -> {climber.stopMotors(climber.leftMotor);}, | |
| () -> {return climber.isMotorExtended(climber.leftMotor);} | |
| () -> climber.moveMotors(Climber.MotorSpeed.extend, climber.leftMotor), | |
| (interrupted) -> climber.stopMotors(climber.leftMotor), | |
| () -> climber.isMotorExtended(climber.leftMotor) |
| left.set(0); | ||
| SmartDashboard.putString("Left Climber State", "Stopped"); | ||
| } | ||
| if (motor>-1){ |
There was a problem hiding this comment.
| if (motor>-1){ | |
| if (motor!=leftMotor){ |
Readability
| if (motor<1 && leftEncoder.getPosition() < EncoderPos.extendLeft.value){ | ||
| return false; | ||
| } | ||
| if (motor>-1 && rightEncoder.getPosition() < EncoderPos.extendRight.value){ |
There was a problem hiding this comment.
| if (motor<1 && leftEncoder.getPosition() < EncoderPos.extendLeft.value){ | |
| return false; | |
| } | |
| if (motor>-1 && rightEncoder.getPosition() < EncoderPos.extendRight.value){ | |
| if (motor!=rightMotor && leftEncoder.getPosition() < EncoderPos.extendLeft.value){ | |
| return false; | |
| } | |
| if (motor!=leftMotor && rightEncoder.getPosition() < EncoderPos.extendRight.value){ |
Readability
| if (motor<1 && leftEncoder.getPosition() > EncoderPos.retractLeft.value){ | ||
| return false; | ||
| } | ||
| if (motor>-1 && rightEncoder.getPosition() > EncoderPos.retractRight.value){ |
There was a problem hiding this comment.
| if (motor<1 && leftEncoder.getPosition() > EncoderPos.retractLeft.value){ | |
| return false; | |
| } | |
| if (motor>-1 && rightEncoder.getPosition() > EncoderPos.retractRight.value){ | |
| if (motor!=rightMotor && leftEncoder.getPosition() > EncoderPos.retractLeft.value){ | |
| return false; | |
| } | |
| if (motor!=leftMotor && rightEncoder.getPosition() > EncoderPos.retractRight.value){ |
Readability
| private static final double kSlowDesiredRetractSpeedInps = 1; | ||
| private static final double kSlowDesiredExtendSpeedInps = 1; |
There was a problem hiding this comment.
| private static final double kSlowDesiredRetractSpeedInps = 1; | |
| private static final double kSlowDesiredExtendSpeedInps = 1; | |
| private static final double kSlowDesiredRetractSpeedInps = 2; | |
| private static final double kSlowDesiredExtendSpeedInps = 2; |
These values should not be changed
| extendLeft(-5.317), | ||
| extendRight(5.315), | ||
| retractLeft(-1.151), | ||
| retractRight(-1.172), |
There was a problem hiding this comment.
| extendLeft(-5.317), | |
| extendRight(5.315), | |
| retractLeft(-1.151), | |
| retractRight(-1.172), | |
| extendLeft(6.315), | |
| extendRight(6.315), | |
| retractLeft(-0.6), | |
| retractRight(-0.6), |
These values should match the previous encoder constants
-change EncoderPos constants -change kSlowDesired(Retract|Extend)SpeedInps
CoolSpy3
left a comment
There was a problem hiding this comment.
It's almost there. There are still a couple outstanding comments from the previous review in Climber.java. Take a look at those and then I'll approve.
|
I think this should be it...? |
Remove unnecessary imports. Revert kDesiredExtendSpeedInps to its original value Switch motor indexes to 0 1 2
CoolSpy3
left a comment
There was a problem hiding this comment.
I went ahead and applied my final suggestions in d8ec3c5d4e48b26f61dbe9434c1d7b6dc2d7ee23. Make sure you're okay with the changes and, if so, you can merge after the code is tested (hopefully Monday).
Wow humans can use it now
And it doesn't look too bad
And it doesn't have ten billion no-argument functions
Also, milk.