FRC Paradigm Shift #1259
Documentation for the 2021 Robot
RobotContainer.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #include "RobotContainer.h"
9 #include <frc2/command/button/NetworkButton.h>
10 
11 #include "AutoPaths.h"
12 
14  : m_gyro()
15  , m_drive(&m_gyro)
16  , m_vision()
17 {
18  m_fieldRelative = false;
19 
22 
23  m_chooser.SetDefaultOption("Example 1", AutoPath::kEx1);
24  m_chooser.AddOption("Example 2", AutoPath::kEx2);
25  m_chooser.AddOption("Example 3", AutoPath::kEx3);
26  m_chooser.AddOption("Example 4", AutoPath::kEx4);
27  frc::SmartDashboard::PutData("Auto Path", &m_chooser);
28 }
29 
31 
33 {
34  m_drive.SetDefaultCommand(frc2::RunCommand(
35  [this] {
36  // up is xbox joystick y pos
37  // left is xbox joystick x pos
39  auto xInput = Util::Deadzone(m_primaryController.GetY(frc::GenericHID::kLeftHand) * -1.0, OIConstants::kDeadzoneX);
40  auto yInput = Util::Deadzone(m_primaryController.GetX(frc::GenericHID::kLeftHand) * -1.0, OIConstants::kDeadzoneY);
41  if (Util::Deadzone(sqrt(pow(xInput, 2) + pow(yInput, 2)), OIConstants::kDeadzoneXY) == 0) {
42  xInput = 0;
43  yInput = 0;
44  }
45 
46  auto rotInput = Util::Deadzone(m_primaryController.GetX(frc::GenericHID::kRightHand) * -1.0, OIConstants::kDeadzoneRot);
47 
48  m_drive.Drive(units::meters_per_second_t(xInput * AutoConstants::kMaxSpeed),
49  units::meters_per_second_t(yInput * AutoConstants::kMaxSpeed),
50  units::angular_velocity::radians_per_second_t(rotInput * DriveConstants::kDriveAngularSpeed.to<double>()),
52  },
53  {&m_drive}
54  ));
55 }
56 
58 {
59  // Primary
60  // Triggers field relative driving
61  frc2::JoystickButton(&m_primaryController, (int)frc::XboxController::Button::kBumperLeft).WhenPressed(
62  frc2::InstantCommand(
63  [this] { m_fieldRelative = true; },
64  {}
65  )
66  );
67 
68  frc2::JoystickButton(&m_primaryController, (int)frc::XboxController::Button::kBumperLeft).WhenReleased(
69  frc2::InstantCommand(
70  [this] { m_fieldRelative = false; },
71  {}
72  )
73  );
74 
75  // Secondary
76  // Ex: Triggers Fire sequence
77  // frc2::JoystickButton(&m_secondaryController, (int)frc::XboxController::Button::kY).WhenPressed(
78  // Fire(&m_secondaryController, &m_flywheel, &m_turret, &m_hood, &m_intake, &m_cycler, &m_vision,
79  // &m_turretready, &m_firing, &m_finished)
80  // );
81 
82 }
83 
85 {
86  m_drive.Drive(units::meters_per_second_t(0.0),
87  units::meters_per_second_t(0.0),
88  units::radians_per_second_t(0.0), false);
89 }
90 
92 {
93  switch(path)
94  {
95  case kEx1:
96  return new frc2::SequentialCommandGroup(
97  // Fire(&m_secondaryController, &m_flywheel, &m_turret, &m_hood, &m_intake, &m_cycler, &m_vision, &m_turretready, &m_firing, &m_finished, 2.0),
98  frc2::ParallelRaceGroup(
99  // CyclerIntakeAgitation(&m_intake, &m_cycler, CyclerConstants::kTurnTableSpeed),
100  std::move(GetSwerveCommand(left3, sizeof(left3) / sizeof(left3[0]), true))
101  ),
102  frc2::InstantCommand(
103  [this]() {
104  ZeroDrive();
105  }, {}
106  )
107  // Fire(&m_secondaryController, &m_flywheel, &m_turret, &m_hood, &m_intake, &m_cycler, &m_vision, &m_turretready, &m_firing, &m_finished, 8.0)
108  );
109 
110  case kEx2:
111  return new frc2::SequentialCommandGroup(
112  std::move(GetSwerveCommand(mid0, sizeof(mid0) / sizeof(mid0[0]), true)),
113  frc2::InstantCommand(
114  [this]() {
115  ZeroDrive();
116  }, {}
117  )
118  // Fire(&m_secondaryController, &m_flywheel, &m_turret, &m_hood, &m_intake, &m_cycler, &m_vision, &m_turretready, &m_firing, &m_finished, 8.0)
119  );
120 
121  case kEx3:
122  return new frc2::SequentialCommandGroup(
123  // Fire(&m_secondaryController, &m_flywheel, &m_turret, &m_hood, &m_intake, &m_cycler, &m_vision, &m_turretready, &m_firing, &m_finished, 1.8),
124  frc2::ParallelRaceGroup(
125  // CyclerIntakeAgitation(&m_intake, &m_cycler, CyclerConstants::kTurnTableSpeed),
126  std::move(GetSwerveCommand(mid5, sizeof(mid5) / sizeof(mid5[0]), true))
127  ),
128  frc2::InstantCommand(
129  [this]() {
130  ZeroDrive();
131  }, {}
132  )
133  // Fire(&m_secondaryController, &m_flywheel, &m_turret, &m_hood, &m_intake, &m_cycler, &m_vision, &m_turretready, &m_firing, &m_finished, 8.0)
134  );
135 
136  case kEx4:
137  return new frc2::SequentialCommandGroup(
138  frc2::ParallelRaceGroup(
139  // CyclerIntakeAgitation(&m_intake, &m_cycler, CyclerConstants::kTurnTableSpeed),
140  std::move(GetSwerveCommand(right2, sizeof(right2) / sizeof(right2[0]), true))
141  ),
142  frc2::InstantCommand(
143  [this]() {
144  ZeroDrive();
145  }, {}
146  )
147  // Fire(&m_secondaryController, &m_flywheel, &m_turret, &m_hood, &m_intake, &m_cycler, &m_vision, &m_turretready, &m_firing, &m_finished, 8.0)
148  );
149 
150  default:
151  return new frc2::SequentialCommandGroup(
152  frc2::InstantCommand(
153  [this]() {
154  ZeroDrive();
155  }, {}
156  )
157  );
158  }
159 }
160 
162 {
163  frc::Trajectory exampleTrajectory = convertArrayToTrajectory(path, length);
164 
165  frc::ProfiledPIDController<units::radians> thetaController{
168 
169  thetaController.EnableContinuousInput(units::radian_t(-wpi::math::pi), units::radian_t(wpi::math::pi));
170 
172  exampleTrajectory, // frc::Trajectory
173  [this]() { return m_drive.GetPose(); }, // std::function<frc::Pose2d()>
174  m_drive.kDriveKinematics, // frc::SwerveDriveKinematics<NumModules>
175  frc2::PIDController(AutoConstants::kPXController, 0, AutoConstants::kDXController), // frc2::PIDController
176  frc2::PIDController(AutoConstants::kPYController, 0, AutoConstants::kDYController), // frc2::PIDController
177  thetaController, // frc::ProfiledPIDController<units::radians>
178  [this](auto moduleStates) { m_drive.SetModuleStates(moduleStates); }, // std::function< void(std::array<frc::SwerveModuleState, NumModules>)>
179  {&m_drive} // std::initializer_list<Subsystem*> requirements
180  );
181 
182  // Reset odometry to the starting pose of the trajectory
183  if(primaryPath)
184  m_drive.ResetOdometry(exampleTrajectory.InitialPose());
185 
186  return swerveControllerCommand;
187 }
188 
189 // t, v, a, X, Y, H
190 frc::Trajectory RobotContainer::convertArrayToTrajectory(double a[][6], int length)
191 {
192  std::vector<frc::Trajectory::State> states;
193  printf("Converting array...");
194  for (int i = 0; i < length; i++)
195  {
196  printf("looping through timestamps...");
197  states.push_back({
198  a[i][0] * 1_s, a[i][1] * 1_mps, a[i][2] * 1_mps_sq,
199  frc::Pose2d(a[i][3] * 1_m, a[i][4] * -1.0 * 1_m, a[i][5] * -1.0 * 1_deg), curvature_t(0)
200  });
201  }
202  printf("Finished looping, returning Trajectory");
203  return frc::Trajectory(states);
204 }
205 
206 void RobotContainer::PrintTrajectory(frc::Trajectory& trajectory)
207 {
208  for (auto &state:trajectory.States())
209  {
210  double time = state.t.to<double>();
211  double x = state.pose.X().to<double>();
212  double y = state.pose.Y().to<double>();
213  printf("%.3f, %.3f, %.3f\n", time, x, y);
214  }
215 }
void SetDefaultCommands()
void Drive(meters_per_second_t xSpeed, meters_per_second_t ySpeed, radians_per_second_t rot, bool fieldRelative)
void PrintTrajectory(frc::Trajectory &trajectory)
constexpr double kDThetaController
Definition: Constants.h:173
double mid5[][6]
Definition: AutoPaths.h:521
frc2::SwerveControllerCommand2< DriveConstants::kNumSwerveModules > GetSwerveCommand(double path[][6], int length, bool primaryPath)
constexpr double kDeadzoneXY
Definition: Constants.h:182
constexpr double kPYController
Definition: Constants.h:170
frc::XboxController m_primaryController
double right2[][6]
Definition: AutoPaths.h:830
constexpr double kDeadzoneY
Definition: Constants.h:181
double mid0[][6]
Definition: AutoPaths.h:291
const frc::TrapezoidProfile< units::radians >::Constraints kThetaControllerConstraints
Definition: Constants.cpp:13
frc::Trajectory convertArrayToTrajectory(double a[][6], int length)
frc2::Command * GetAutonomousCommand(AutoPath path)
constexpr double kDeadzoneX
Definition: Constants.h:180
frc::SendableChooser< AutoPath > m_chooser
constexpr double kPThetaController
Definition: Constants.h:172
DriveSubsystem m_drive
static double Deadzone(double inputValue, double deadzone)
If an inputValue is smaller than its deadzone, returns 0, otherwise returns the inputValue.
Definition: Util.h:39
void ResetOdometry(Pose2d pose)
constexpr auto kMaxSpeed
Definition: Constants.h:163
constexpr double kDXController
Definition: Constants.h:169
double left3[][6]
Definition: AutoPaths.h:2
constexpr double kDeadzoneRot
Definition: Constants.h:183
constexpr auto kDriveAngularSpeed
Definition: Constants.h:37
constexpr double kPXController
Definition: Constants.h:168
constexpr double kDYController
Definition: Constants.h:171
void SetModuleStates(SwerveModuleStates desiredStates)
Sets the drive SpeedControllers to a power from -1 to 1.
void ConfigureButtonBindings()
SwerveDriveKinematics< kNumSwerveModules > kDriveKinematics
The kinematics object converts inputs into 4 individual swerve module turn angle and wheel speeds...