|
| 1 | +""" |
| 2 | +This sample program shows how to use Mechanism2d - a visual representation of arms, elevators, |
| 3 | +and other mechanisms on dashboards; driven by a node-based API. |
| 4 | +
|
| 5 | +Ligaments are based on other ligaments or roots, and roots are contained in the base |
| 6 | +Mechanism2d object. |
| 7 | +""" |
| 8 | + |
| 9 | +import wpilib |
| 10 | + |
| 11 | + |
| 12 | +class MyRobot(wpilib.TimedRobot): |
| 13 | + |
| 14 | + kMetersPerPulse = 0.01 |
| 15 | + kElevatorMinimumLength = 0.5 |
| 16 | + |
| 17 | + def robotInit(self): |
| 18 | + self.elevatorMotor = wpilib.PWMSparkMax(0) |
| 19 | + self.wristMotor = wpilib.PWMSparkMax(1) |
| 20 | + self.wristPot = wpilib.AnalogPotentiometer(1, 90) |
| 21 | + self.elevatorEncoder = wpilib.Encoder(0, 1) |
| 22 | + self.joystick = wpilib.Joystick(0) |
| 23 | + |
| 24 | + self.elevatorEncoder.setDistancePerPulse(self.kMetersPerPulse) |
| 25 | + |
| 26 | + # the main mechanism object |
| 27 | + self.mech = wpilib.Mechanism2d(3, 3) |
| 28 | + # the mechanism root node |
| 29 | + self.root = self.mech.getRoot("climber", 2, 0) |
| 30 | + |
| 31 | + # MechanismLigament2d objects represent each "section"/"stage" of the mechanism, and are based |
| 32 | + # off the root node or another ligament object |
| 33 | + self.elevator = self.root.appendLigament( |
| 34 | + "elevator", self.kElevatorMinimumLength, 90 |
| 35 | + ) |
| 36 | + self.wrist = self.elevator.appendLigament( |
| 37 | + "wrist", 0.5, 90, 6, wpilib.Color8Bit(wpilib.Color.kPurple) |
| 38 | + ) |
| 39 | + |
| 40 | + # post the mechanism to the dashboard |
| 41 | + wpilib.SmartDashboard.putData("Mech2d", self.mech) |
| 42 | + |
| 43 | + def robotPeriodic(self): |
| 44 | + # update the dashboard mechanism's state |
| 45 | + self.elevator.setLength( |
| 46 | + self.kElevatorMinimumLength + self.elevatorEncoder.getDistance() |
| 47 | + ) |
| 48 | + self.wrist.setAngle(self.wristPot.get()) |
| 49 | + |
| 50 | + def teleopPeriodic(self): |
| 51 | + self.elevatorMotor.set(self.joystick.getRawAxis(0)) |
| 52 | + self.wristMotor.set(self.joystick.getRawAxis(1)) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + wpilib.run(MyRobot) |
0 commit comments