Skip to content

Commit ef0d1c0

Browse files
authored
Merge pull request #114 from robotpy/update-commands
Update commands examples
2 parents 86d7ba6 + a7543f1 commit ef0d1c0

File tree

16 files changed

+83
-88
lines changed

16 files changed

+83
-88
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
- name: Install deps
4949
run: |
5050
pip install -U pip
51-
pip install 'robotpy[commands2]<2025.0.0,>=2024.0.0b4.post1' numpy pytest
51+
pip install 'robotpy[commands2]<2025.0.0,>=2024.2.1.1' numpy pytest
5252
- name: Run tests
5353
run: bash run_tests.sh
5454
shell: bash

ArmBot/robotcontainer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self) -> None:
4545
-self.driver_controller.getLeftY(),
4646
-self.driver_controller.getRightX(),
4747
),
48-
[self.robot_drive],
48+
self.robot_drive,
4949
)
5050
)
5151

@@ -57,20 +57,20 @@ def configureButtonBindings(self) -> None:
5757
"""
5858

5959
# Move the arm to 2 radians above horizontal when the 'A' button is pressed.
60-
self.driver_controller.A().onTrue(
61-
commands2.cmd.run(lambda: self.moveArm(2), [self.robot_arm])
60+
self.driver_controller.a().onTrue(
61+
commands2.cmd.run(lambda: self.moveArm(2), self.robot_arm)
6262
)
6363

6464
# Move the arm to neutral position when the 'B' button is pressed
65-
self.driver_controller.B().onTrue(
65+
self.driver_controller.b().onTrue(
6666
commands2.cmd.run(
6767
lambda: self.moveArm(constants.ArmConstants.kArmOffsetRads),
68-
[self.robot_arm],
68+
self.robot_arm,
6969
)
7070
)
7171

7272
# Disable the arm controller when Y is pressed
73-
self.driver_controller.Y().onTrue(
73+
self.driver_controller.y().onTrue(
7474
commands2.cmd.runOnce(lambda: self.robot_arm.disable())
7575
)
7676

@@ -92,8 +92,8 @@ def getAutonomousCommand(self) -> commands2.Command:
9292
9393
:returns: the command to run in autonomous
9494
"""
95-
return commands2.cmd.nothing()
95+
return commands2.cmd.none()
9696

97-
def moveArm(self, radians: int) -> None:
97+
def moveArm(self, radians: float) -> None:
9898
self.robot_arm.setGoal(radians)
9999
self.robot_arm.enable()

ArmBot/subsystems/armsubsystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self) -> None:
4949
# Start arm at rest in neutral position
5050
self.setGoal(constants.ArmConstants.kArmOffsetRads)
5151

52-
def _useOutput(
52+
def useOutput(
5353
self, output: float, setpoint: wpimath.trajectory.TrapezoidProfile.State
5454
) -> None:
5555
# Calculate the feedforward from the setpoint
@@ -58,5 +58,5 @@ def _useOutput(
5858
# Add the feedforward to the PID output to get the motor output
5959
self.motor.setVoltage(output + feedforward)
6060

61-
def _getMeasurement(self) -> float:
61+
def getMeasurement(self) -> float:
6262
return self.encoder.getDistance() + constants.ArmConstants.kArmOffsetRads

ArmBotOffboard/robotcontainer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def configureButtonBindings(self) -> None:
5050
"""
5151

5252
# Move the arm to 2 radians above horizontal when the 'A' button is pressed.
53-
self.driverController.A().onTrue(self.robotArm.setArmGoalCommand(2))
53+
self.driverController.a().onTrue(self.robotArm.setArmGoalCommand(2))
5454

5555
# Move the arm to neutral position when the 'B' button is pressed.
56-
self.driverController.B().onTrue(
56+
self.driverController.b().onTrue(
5757
self.robotArm.setArmGoalCommand(constants.kArmOffsetRads)
5858
)
5959

@@ -66,4 +66,4 @@ def configureButtonBindings(self) -> None:
6666
)
6767

6868
def getAutonomousCommand(self) -> commands2.Command:
69-
return commands2.cmd.nothing()
69+
return commands2.cmd.none()

ArmBotOffboard/subsystems/armsubsystem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ def useState(self, setpoint: wpimath.trajectory.TrapezoidProfile.State) -> None:
4848
)
4949

5050
def setArmGoalCommand(self, kArmOffsetRads: float) -> commands2.Command:
51-
return commands2.cmd.runOnce(lambda: self.setGoal(kArmOffsetRads), [self])
51+
return commands2.cmd.runOnce(lambda: self.setGoal(kArmOffsetRads), self)

ArmBotOffboard/subsystems/drivesubsystem.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def arcadeDriveCommand(
6666
rot: Supplier for the commanded rotation
6767
"""
6868
return commands2.cmd.run(
69-
lambda: self.drive.arcadeDrive(fwd(), rot(), True), [self]
69+
lambda: self.drive.arcadeDrive(fwd(), rot(), True), self
7070
)
7171

7272
def resetEncoders(self) -> None:
@@ -104,6 +104,4 @@ def limitOutputCommand(self, max_output: float) -> commands2.Command:
104104
Args:
105105
maxoutput: The maximum output to which the drive will be constrained.
106106
"""
107-
return commands2.cmd.runOnce(
108-
lambda: self.drive.setMaxOutput(max_output), [self]
109-
)
107+
return commands2.cmd.runOnce(lambda: self.drive.setMaxOutput(max_output), self)

DriveDistanceOffboard/commands/drivedistanceprofiled.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ def __init__(
2929
wpimath.trajectory.TrapezoidProfile.Constraints(
3030
constants.DriveConstants.kMaxSpeedMetersPerSecond,
3131
constants.DriveConstants.kMaxAccelerationMetersPerSecondSquared,
32-
),
33-
# End at desired position in meters; implicitly starts at 0
34-
wpimath.trajectory.TrapezoidProfile.State(meters, 0),
32+
)
3533
),
3634
# Pipe the profile state to the drive
3735
lambda setpointState: drive.setDriveStates(setpointState, setpointState),
36+
# End at desired position in meters; implicitly starts at 0
37+
lambda: wpimath.trajectory.TrapezoidProfile.State(meters, 0),
38+
# Current position
39+
lambda: wpimath.trajectory.TrapezoidProfile.State(0, 0),
3840
# Require the drive
39-
[drive],
41+
drive,
4042
)
4143
# Reset drive encoders since we're starting at 0
4244
drive.resetEncoders()

DriveDistanceOffboard/robotcontainer.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def __init__(self):
2929

3030
# Retained command references
3131
self.driveFullSpeed = commands2.cmd.runOnce(
32-
lambda: self.robotDrive.setMaxOutput(1), [self.robotDrive]
32+
lambda: self.robotDrive.setMaxOutput(1), self.robotDrive
3333
)
3434
self.driveHalfSpeed = commands2.cmd.runOnce(
35-
lambda: self.robotDrive.setMaxOutput(0.5), [self.robotDrive]
35+
lambda: self.robotDrive.setMaxOutput(0.5), self.robotDrive
3636
)
3737

3838
# The driver's controller
@@ -53,7 +53,7 @@ def __init__(self):
5353
-self.driverController.getLeftY(),
5454
-self.driverController.getRightX(),
5555
),
56-
[self.robotDrive],
56+
self.robotDrive,
5757
)
5858
)
5959

@@ -74,31 +74,32 @@ def configureButtonBindings(self):
7474
)
7575

7676
# Drive forward by 3 meters when the 'A' button is pressed, with a timeout of 10 seconds
77-
self.driverController.A().onTrue(
77+
self.driverController.a().onTrue(
7878
commands.drivedistanceprofiled.DriveDistanceProfiled(
7979
3, self.robotDrive
8080
).withTimeout(10)
8181
)
8282

8383
# Do the same thing as above when the 'B' button is pressed, but defined inline
84-
self.driverController.B().onTrue(
84+
self.driverController.b().onTrue(
8585
commands2.TrapezoidProfileCommand(
8686
wpimath.trajectory.TrapezoidProfile(
8787
# Limit the max acceleration and velocity
8888
wpimath.trajectory.TrapezoidProfile.Constraints(
8989
constants.DriveConstants.kMaxSpeedMetersPerSecond,
9090
constants.DriveConstants.kMaxAccelerationMetersPerSecondSquared,
9191
),
92-
# End at desired position in meters; implicitly starts at 0
93-
wpimath.trajectory.TrapezoidProfile.State(3, 0),
9492
),
9593
# Pipe the profile state to the drive
9694
lambda setpointState: self.robotDrive.setDriveStates(
9795
setpointState, setpointState
9896
),
99-
[self.robotDrive],
97+
# End at desired position in meters; implicitly starts at 0
98+
lambda: wpimath.trajectory.TrapezoidProfile.State(3, 0),
99+
wpimath.trajectory.TrapezoidProfile.State,
100+
self.robotDrive,
100101
)
101-
.beforeStarting(self.robotDrive.resetEncoders())
102+
.beforeStarting(self.robotDrive.resetEncoders)
102103
.withTimeout(10)
103104
)
104105

@@ -108,4 +109,4 @@ def getAutonomousCommand(self) -> commands2.Command:
108109
109110
:returns: the command to run in autonomous
110111
"""
111-
return commands2.cmd.nothing()
112+
return commands2.cmd.none()

DriveDistanceOffboard/subsystems/drivesubsystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ def setDriveStates(
9696
self.feedforward.calculate(right.velocity),
9797
)
9898

99-
def getLeftEncoderDistance(self) -> wpilib.Encoder:
99+
def getLeftEncoderDistance(self) -> float:
100100
"""
101101
Returns the left drive encoder distance.
102102
103103
:returns: the left drive encoder distance
104104
"""
105105
return self.leftLeader.getEncoderDistance()
106106

107-
def getRightEncoderDistance(self) -> wpilib.Encoder:
107+
def getRightEncoderDistance(self) -> float:
108108
"""
109109
Returns the right drive encoder distance.
110110

FrisbeeBot/robotcontainer.py

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,30 @@ def __init__(self):
2626
self.robotDrive = subsystems.drivesubsystem.DriveSubsystem()
2727
self.shooter = subsystems.shootersubsystem.ShooterSubsystem()
2828

29-
self.spinUpShooter = commands2.cmd.runOnce(self.shooter.enable, [self.shooter])
30-
self.stopShooter = commands2.cmd.runOnce(self.shooter.disable, [self.shooter])
29+
self.spinUpShooter = commands2.cmd.runOnce(self.shooter.enable, self.shooter)
30+
self.stopShooter = commands2.cmd.runOnce(self.shooter.disable, self.shooter)
3131

3232
# An autonomous routine that shoots the loaded frisbees
3333
self.autonomousCommand = commands2.cmd.sequence(
34-
[
35-
# Start the command by spinning up the shooter...
36-
commands2.cmd.runOnce(self.shooter.enable, [self.shooter]),
37-
# Wait until the shooter is at speed before feeding the frisbees
38-
commands2.cmd.waitUntil(
39-
lambda: self.shooter.getController().atSetpoint()
40-
),
41-
# Start running the feeder
42-
commands2.cmd.runOnce(self.shooter.runFeeder, [self.shooter]),
43-
# Shoot for the specified time
44-
commands2.cmd.wait(constants.AutoConstants.kAutoShootTimeSeconds)
45-
# Add a timeout (will end the command if, for instance, the shooter
46-
# never gets up to speed)
47-
.withTimeout(constants.AutoConstants.kAutoTimeoutSeconds)
48-
# When the command ends, turn off the shooter and the feeder
49-
.andThen(
50-
commands2.cmd.runOnce(
51-
lambda: self.shooter.disable, [self.shooter]
52-
).andThen(
53-
commands2.cmd.runOnce(
54-
lambda: self.shooter.stopFeeder, [self.shooter]
55-
)
56-
)
57-
),
58-
]
34+
# Start the command by spinning up the shooter...
35+
commands2.cmd.runOnce(self.shooter.enable, self.shooter),
36+
# Wait until the shooter is at speed before feeding the frisbees
37+
commands2.cmd.waitUntil(lambda: self.shooter.getController().atSetpoint()),
38+
# Start running the feeder
39+
commands2.cmd.runOnce(self.shooter.runFeeder, self.shooter),
40+
# Shoot for the specified time
41+
commands2.cmd.waitSeconds(constants.AutoConstants.kAutoShootTimeSeconds)
42+
# Add a timeout (will end the command if, for instance, the shooter
43+
# never gets up to speed)
44+
.withTimeout(constants.AutoConstants.kAutoTimeoutSeconds)
45+
# When the command ends, turn off the shooter and the feeder
46+
.andThen(
47+
commands2.cmd.runOnce(
48+
lambda: self.shooter.disable, self.shooter
49+
).andThen(
50+
commands2.cmd.runOnce(lambda: self.shooter.stopFeeder, self.shooter)
51+
)
52+
),
5953
)
6054

6155
# The driver's controller
@@ -76,7 +70,7 @@ def __init__(self):
7670
-self.driverController.getLeftY(),
7771
-self.driverController.getRightX(),
7872
),
79-
[self.robotDrive],
73+
self.robotDrive,
8074
)
8175
)
8276

@@ -92,39 +86,39 @@ def configureButtonBindings(self):
9286
# We can bind commands while retaining references to them in RobotContainer
9387

9488
# Spin up the shooter when the 'A' button is pressed
95-
self.driverController.A().onTrue(self.spinUpShooter)
89+
self.driverController.a().onTrue(self.spinUpShooter)
9690

9791
# Turn off the shooter when the 'B' button is pressed
98-
self.driverController.B().onTrue(self.stopShooter)
92+
self.driverController.b().onTrue(self.stopShooter)
9993

10094
# We can also write them as temporary variables outside the bindings
10195

10296
# Shoots if the shooter wheel has reached the target speed
10397
shoot = commands2.cmd.either(
10498
# Run the feeder
105-
commands2.cmd.runOnce(self.shooter.runFeeder, [self.shooter]),
99+
commands2.cmd.runOnce(self.shooter.runFeeder, self.shooter),
106100
# Do nothing
107-
commands2.cmd.nothing(),
101+
commands2.cmd.none(),
108102
# Determine which of the above to do based on whether the shooter has reached the
109103
# desired speed
110104
lambda: self.shooter.getController().atSetpoint(),
111105
)
112106

113-
stopFeeder = commands2.cmd.runOnce(self.shooter.stopFeeder, [self.shooter])
107+
stopFeeder = commands2.cmd.runOnce(self.shooter.stopFeeder, self.shooter)
114108

115109
# Shoot when the 'X' button is pressed
116-
self.driverController.X().onTrue(shoot).onFalse(stopFeeder)
110+
self.driverController.x().onTrue(shoot).onFalse(stopFeeder)
117111

118112
# We can also define commands inline at the binding!
119113

120114
# While holding the shoulder button, drive at half speed
121115
self.driverController.rightBumper().onTrue(
122116
commands2.cmd.runOnce(
123-
lambda: self.robotDrive.setMaxOutput(0.5), [self.robotDrive]
117+
lambda: self.robotDrive.setMaxOutput(0.5), self.robotDrive
124118
)
125119
).onFalse(
126120
commands2.cmd.runOnce(
127-
lambda: self.robotDrive.setMaxOutput(1), [self.robotDrive]
121+
lambda: self.robotDrive.setMaxOutput(1), self.robotDrive
128122
)
129123
)
130124

0 commit comments

Comments
 (0)