Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mechanism2d example. #52

Merged
merged 5 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions mechanism2d/robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import wpilib

"""
This sample program shows how to use Mechanism2d - a visual representation of arms, elevators,
and other mechanisms on dashboards; driven by a node-based API.

<p>Ligaments are based on other ligaments or roots, and roots are contained in the base
BerkeSinanYetkin marked this conversation as resolved.
Show resolved Hide resolved
Mechanism2d object.
"""


class MyRobot(wpilib.TimedRobot):

kMetersPerPulse = 0.01
kElevatorMinimumLength = 0.5

def robotInit(self):
self.elevatorMotor = wpilib.PWMSparkMax(0)
self.wristMotor = wpilib.PWMSparkMax(1)
self.wristPot = wpilib.AnalogPotentiometer(1, 90)
self.elevatorEncoder = wpilib.Encoder(0, 1)
self.joystick = wpilib.Joystick(0)

self.elevatorEncoder.setDistancePerPulse(self.kMetersPerPulse)

# the main mechanism object
self.mech = wpilib.Mechanism2d(3, 3)
# the mechanism root node
self.root = self.mech.getRoot("climber", 2, 0)

# MechanismLigament2d objects represent each "section"/"stage" of the mechanism, and are based
# off the root node or another ligament object
self.elevator = self.root.appendLigament(
"elevator", self.kElevatorMinimumLength, 90
)
self.wrist = self.elevator.appendLigament(
"wrist", 0.5, 90, 6, wpilib.Color8Bit(wpilib.Color.kPurple)
)

# post the mechanism to the dashboard
wpilib.SmartDashboard.putData("Mech2d", self.mech)

def robotPeriodic(self):
# update the dashboard mechanism's state
self.elevator.setLength(
self.kElevatorMinimumLength + self.elevatorEncoder.getDistance()
)
self.wrist.setAngle(self.wristPot.get())

def teleopPeriodic(self):
self.elevatorMotor.set(self.joystick.getRawAxis(0))
self.wristMotor.set(self.joystick.getRawAxis(1))


if __name__ == "__main__":
wpilib.run(MyRobot)
1 change: 1 addition & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ BASE_TESTS="
gyro
mecanum-drive
mecanum-driveXbox
mechanism2d
motor-control
physics/src
physics-4wheel/src
Expand Down