From 6c6ffee36d9800248f567e739031a46132f73adb Mon Sep 17 00:00:00 2001 From: BerkeSinanYetkin Date: Sat, 1 Oct 2022 23:59:59 +0300 Subject: [PATCH 1/5] added mechanism2d example --- mechanism2d/robot.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 mechanism2d/robot.py diff --git a/mechanism2d/robot.py b/mechanism2d/robot.py new file mode 100644 index 0000000..8daeafa --- /dev/null +++ b/mechanism2d/robot.py @@ -0,0 +1,57 @@ +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. + +#

Ligaments are based on other ligaments or roots, and roots are contained in the base +# 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.elevator = wpilib.MechanismLigament2d + self.wrist = wpilib.MechanismLigament2d + + 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) From 62d631dd865218bdb83184ec4484fd58c09dc2d6 Mon Sep 17 00:00:00 2001 From: BerkeSinanYetkin Date: Sun, 2 Oct 2022 00:06:02 +0300 Subject: [PATCH 2/5] added mechanism2d to run_tests.sh --- run_tests.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/run_tests.sh b/run_tests.sh index 622aca4..3154f3f 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -17,6 +17,7 @@ BASE_TESTS=" gyro mecanum-drive mecanum-driveXbox + mechanism2d motor-control physics/src physics-4wheel/src From 13d4ff5109cd8eb388ab68c55fe2dc3bb716a820 Mon Sep 17 00:00:00 2001 From: BerkeSinanYetkin Date: Sun, 2 Oct 2022 09:40:35 +0300 Subject: [PATCH 3/5] deleted 2 lines of unneccesary code, changed the comments on the top of the program to be in docstring. --- mechanism2d/robot.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/mechanism2d/robot.py b/mechanism2d/robot.py index 8daeafa..c70a352 100644 --- a/mechanism2d/robot.py +++ b/mechanism2d/robot.py @@ -1,10 +1,12 @@ 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. +""" +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. -#

Ligaments are based on other ligaments or roots, and roots are contained in the base -# Mechanism2d object. +

Ligaments are based on other ligaments or roots, and roots are contained in the base +Mechanism2d object. +""" class MyRobot(wpilib.TimedRobot): @@ -19,9 +21,6 @@ def robotInit(self): self.elevatorEncoder = wpilib.Encoder(0, 1) self.joystick = wpilib.Joystick(0) - self.elevator = wpilib.MechanismLigament2d - self.wrist = wpilib.MechanismLigament2d - self.elevatorEncoder.setDistancePerPulse(self.kMetersPerPulse) # the main mechanism object From 59123faaee215a64030c9f51ac091a6418b986fb Mon Sep 17 00:00:00 2001 From: BerkeSinanYetkin <71657012+BerkeSinanYetkin@users.noreply.github.com> Date: Sun, 2 Oct 2022 13:20:30 +0300 Subject: [PATCH 4/5] Update the docstring Co-authored-by: David Vo --- mechanism2d/robot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mechanism2d/robot.py b/mechanism2d/robot.py index c70a352..ca6567c 100644 --- a/mechanism2d/robot.py +++ b/mechanism2d/robot.py @@ -4,7 +4,7 @@ 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. -

Ligaments are based on other ligaments or roots, and roots are contained in the base +Ligaments are based on other ligaments or roots, and roots are contained in the base Mechanism2d object. """ From 946cf7ebf589a5cdbc8eb8fb6994ae86b8f65bf5 Mon Sep 17 00:00:00 2001 From: David Vo Date: Sun, 2 Oct 2022 22:26:07 +1100 Subject: [PATCH 5/5] Fix docstring to be docstring --- mechanism2d/robot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mechanism2d/robot.py b/mechanism2d/robot.py index ca6567c..378dedf 100644 --- a/mechanism2d/robot.py +++ b/mechanism2d/robot.py @@ -1,5 +1,3 @@ -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. @@ -8,6 +6,8 @@ Mechanism2d object. """ +import wpilib + class MyRobot(wpilib.TimedRobot):