Skip to content

Milkybeans - More human-friendly rewrite of Climber.java - #6

Open
FriedLongJohns wants to merge 29 commits into
devfrom
milkybeans
Open

Milkybeans - More human-friendly rewrite of Climber.java#6
FriedLongJohns wants to merge 29 commits into
devfrom
milkybeans

Conversation

@FriedLongJohns

Copy link
Copy Markdown

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.

beansbeansbeansyes and others added 8 commits September 8, 2022 19:29
-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
Comment thread src/main/java/org/team199/robot2022/subsystems/Climber.java Outdated
Comment thread src/main/java/org/team199/robot2022/subsystems/Climber.java Outdated
Comment thread src/main/java/org/team199/robot2022/subsystems/Climber.java Outdated
FriedLongJohns and others added 3 commits September 18, 2022 21:13
Co-authored-by: Kedas <m@yarn.network>

@CoolSpy3 CoolSpy3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good. I just have a few comments.

Comment on lines +131 to +134
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)));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

climber.leftMotor
climber.rightMotor

Static members should be accessed in a static manner
Climber.leftMotor
Climber.rightMotor

Comment on lines -15 to -28
super(
new FunctionalCommand(
() -> {},
climber::extendLeft,
climber::stopLeft,
climber::isLeftExtended
),
new FunctionalCommand(
() -> {},
climber::extendRight,
climber::stopRight,
climber::isRightExtended
)
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 26 to 28
public void end(){
climber.stopMotors(climber.bothMotors);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function end takes one argument boolean interrupted

import edu.wpi.first.wpilibj2.command.CommandBase;

public class RetractClimber extends ParallelCommandGroup {
public class RetractClimber extends CommandBase {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments on ExtendClimber

Comment on lines +35 to +41

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/main/java/org/team199/robot2022/subsystems/Climber.java Outdated
Comment thread src/main/java/org/team199/robot2022/subsystems/Climber.java Outdated

public class ExtendClimber extends ParallelCommandGroup {
public class ExtendClimber extends CommandBase {
private Climber climber;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be final

Comment on lines +54 to +80

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");
},
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid raw types. Use DoubleConsumer instead. This should remove the need for a cast to double.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, neat.
Didn't know that was a class.

Comment on lines +124 to +128
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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "Stopped"

@CoolSpy3

Copy link
Copy Markdown
Member

I missed this earlier, but please change the pull request to merge into dev instead of master.

@CoolSpy3

CoolSpy3 commented Sep 21, 2022 via email

Copy link
Copy Markdown
Member

@FriedLongJohns
FriedLongJohns changed the base branch from master to dev September 22, 2022 00:32
@FriedLongJohns

FriedLongJohns commented Sep 22, 2022

Copy link
Copy Markdown
Author

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@FriedLongJohns FriedLongJohns Sep 29, 2022

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +18 to +20
() -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.rightMotor);},
(interrupted) -> {climber.stopMotors(climber.rightMotor);},
() -> {return climber.isMotorExtended(climber.rightMotor);}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
() -> {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)

Comment on lines +24 to +26
() -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.leftMotor);},
(interrupted) -> {climber.stopMotors(climber.leftMotor);},
() -> {return climber.isMotorExtended(climber.leftMotor);}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
() -> {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)

Comment on lines +18 to +20
() -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.rightMotor);},
(interrupted) -> {climber.stopMotors(climber.rightMotor);},
() -> {return climber.isMotorExtended(climber.rightMotor);}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
() -> {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)

Comment on lines +24 to +26
() -> {climber.moveMotors(Climber.MotorSpeed.extend,climber.leftMotor);},
(interrupted) -> {climber.stopMotors(climber.leftMotor);},
() -> {return climber.isMotorExtended(climber.leftMotor);}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
() -> {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){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (motor>-1){
if (motor!=leftMotor){

Readability

Comment on lines +141 to +144
if (motor<1 && leftEncoder.getPosition() < EncoderPos.extendLeft.value){
return false;
}
if (motor>-1 && rightEncoder.getPosition() < EncoderPos.extendRight.value){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +151 to +154
if (motor<1 && leftEncoder.getPosition() > EncoderPos.retractLeft.value){
return false;
}
if (motor>-1 && rightEncoder.getPosition() > EncoderPos.retractRight.value){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +30 to +31
private static final double kSlowDesiredRetractSpeedInps = 1;
private static final double kSlowDesiredExtendSpeedInps = 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +175 to +178
extendLeft(-5.317),
extendRight(5.315),
retractLeft(-1.151),
retractRight(-1.172),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 CoolSpy3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@FriedLongJohns

Copy link
Copy Markdown
Author

I think this should be it...?

Remove unnecessary imports.
Revert kDesiredExtendSpeedInps to its original value
Switch motor indexes to 0 1 2

@CoolSpy3 CoolSpy3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants