[examples] Add Field2d to RamseteController example (#3371)

This commit is contained in:
Dalton Smith
2021-05-22 01:28:29 -04:00
committed by GitHub
parent 4c562a4457
commit 49b06beedf
2 changed files with 29 additions and 0 deletions

View File

@@ -6,6 +6,8 @@
#include <frc/TimedRobot.h>
#include <frc/XboxController.h>
#include <frc/controller/RamseteController.h>
#include <frc/smartdashboard/Field2d.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include <frc/trajectory/TrajectoryGenerator.h>
#include <frc2/Timer.h>
@@ -17,14 +19,23 @@ class Robot : public frc::TimedRobot {
// Start the timer.
m_timer.Start();
// Send Field2d to SmartDashboard.
frc::SmartDashboard::PutData(&m_field);
// Reset the drivetrain's odometry to the starting pose of the trajectory.
m_drive.ResetOdometry(m_trajectory.InitialPose());
// Send our generated trajectory to Field2d.
m_field.GetObject("traj")->SetTrajectory(m_trajectory);
}
void AutonomousPeriodic() override {
// Update odometry.
m_drive.UpdateOdometry();
// Update robot position on Field2d.
m_field.SetRobotPose(m_drive.GetPose());
if (m_timer.Get() < m_trajectory.TotalTime()) {
// Get the desired pose from the trajectory.
auto desiredPose = m_trajectory.Sample(m_timer.Get());
@@ -79,6 +90,9 @@ class Robot : public frc::TimedRobot {
// The timer to use during the autonomous period.
frc2::Timer m_timer;
// Create Field2d for robot and trajectory visualizations.
frc::Field2d m_field;
};
#ifndef RUNNING_FRC_TESTS