mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[glass, wpilib] Rewrite Mechanism2d (#3281)
Substantially improves Mechanism2d by moving it to NetworkTables and adding a robot API to create the mechanism elements, instead of requiring a JSON file. Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <frc/AnalogPotentiometer.h>
|
||||
#include <frc/Encoder.h>
|
||||
#include <frc/Joystick.h>
|
||||
#include <frc/PWMSparkMax.h>
|
||||
#include <frc/TimedRobot.h>
|
||||
#include <frc/simulation/SimHooks.h>
|
||||
#include <frc/smartdashboard/Mechanism2d.h>
|
||||
#include <frc/smartdashboard/MechanismLigament2d.h>
|
||||
#include <frc/smartdashboard/SmartDashboard.h>
|
||||
#include <units/angle.h>
|
||||
|
||||
/**
|
||||
* 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 Mechanism2d object. Use pointers for nodes, and beware not to let
|
||||
* the container out of scope - the appended nodes will be recursively
|
||||
* destructed!
|
||||
*/
|
||||
class Robot : public frc::TimedRobot {
|
||||
static constexpr double kMetersPerPulse = 0.01;
|
||||
static constexpr double kElevatorMinimumLength = 0.5;
|
||||
|
||||
public:
|
||||
void RobotInit() override {
|
||||
m_elevatorEncoder.SetDistancePerPulse(kMetersPerPulse);
|
||||
|
||||
// publish to dashboard
|
||||
frc::SmartDashboard::PutData("Mech2d", &m_mech);
|
||||
}
|
||||
|
||||
void RobotPeriodic() override {
|
||||
// update the dashboard mechanism's state
|
||||
m_elevator->SetLength(kElevatorMinimumLength +
|
||||
m_elevatorEncoder.GetDistance());
|
||||
m_wrist->SetAngle(units::degree_t(m_wristPotentiometer.Get()));
|
||||
}
|
||||
|
||||
void TeleopPeriodic() override {
|
||||
m_elevatorMotor.Set(m_joystick.GetRawAxis(0));
|
||||
m_wristMotor.Set(m_joystick.GetRawAxis(1));
|
||||
}
|
||||
|
||||
private:
|
||||
frc::PWMSparkMax m_elevatorMotor{0};
|
||||
frc::PWMSparkMax m_wristMotor{1};
|
||||
frc::Encoder m_elevatorEncoder{0, 1};
|
||||
frc::AnalogPotentiometer m_wristPotentiometer{1, 90};
|
||||
frc::Joystick m_joystick{0};
|
||||
|
||||
// the main mechanism object
|
||||
frc::Mechanism2d m_mech{3, 3};
|
||||
// the mechanism root node
|
||||
frc::MechanismRoot2d* m_root = m_mech.GetRoot("climber", 3, 3);
|
||||
// MechanismLigament2d objects represent each "section"/"stage" of the
|
||||
// mechanism, and are based off the root node or another ligament object
|
||||
frc::MechanismLigament2d* m_elevator =
|
||||
m_root->Append<frc::MechanismLigament2d>("elevator", 1, 90_deg);
|
||||
frc::MechanismLigament2d* m_wrist =
|
||||
m_elevator->Append<frc::MechanismLigament2d>("wrist", 0.5, 90_deg);
|
||||
};
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
return frc::StartRobot<Robot>();
|
||||
}
|
||||
#endif
|
||||
@@ -51,6 +51,14 @@
|
||||
"gradlebase": "cpp",
|
||||
"commandversion": 2
|
||||
},
|
||||
{
|
||||
"name":"Mechanism2d",
|
||||
"foldername":"Mechanism2d",
|
||||
"gradlebase":"cpp",
|
||||
"description":"An example usage of Mechanism2d to display mechanism states on a dashboard.",
|
||||
"tags":[],
|
||||
"commandversion": 2
|
||||
},
|
||||
{
|
||||
"name": "Solenoids",
|
||||
"description": "Demonstrate controlling a single and double solenoid from Joystick buttons.",
|
||||
|
||||
Reference in New Issue
Block a user