Skip to content

Commit de3a7a8

Browse files
Added mechanism2d example. (#52)
* added mechanism2d example * added mechanism2d to run_tests.sh * deleted 2 lines of unneccesary code, changed the comments on the top of the program to be in docstring. * Update the docstring Co-authored-by: David Vo <[email protected]> * Fix docstring to be docstring Co-authored-by: David Vo <[email protected]>
1 parent 3162258 commit de3a7a8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

mechanism2d/robot.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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)

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ BASE_TESTS="
1717
gyro
1818
mecanum-drive
1919
mecanum-driveXbox
20+
mechanism2d
2021
motor-control
2122
physics/src
2223
physics-4wheel/src

0 commit comments

Comments
 (0)