Shuffleboard Command Example in C++

Does anyone have an example of adding a command button to a shuffleboard layout in C++? Trying to use the below, but doesn’t work. Seems to blowup the whole layout.

  frc::Shuffleboard::GetTab("Subsystems").GetLayout("Drive").Add(
                                                                  "Reset Gyro",
                                                                  new frc2::InstantCommand(
                                                                                            [this] { m_drive.ZeroHeading(); },
                                                                                            {&m_drive}
                                                                                          )
                                                                  ).WithWidget(frc::BuiltInWidgets::kCommand)   // with Command Type Widget
                                                                   .WithSize(2, 1).WithPosition(0, 7);          // At this location in my layout.

There’s a bunch of fancy C++ Shuffleboard stuff here: Scrappy/src/main/cpp/subsystems/DriveSubsystem.cpp at 750edd85e16341ea15d5d05218793aa14f26a6be · Jagwires7443/Scrappy · GitHub.

Thank You for sharing! It is hard to find good C++ examples. Lots of good stuff in there, but none that specifically run a command on button press.

The way you are using the Chooser will probably be helpful for me in next…

1 Like

Well here is a work around using NetworkButton API… Not sure if there is a better way to do it.

in RobotContainer.h

  bool              m_resetGyroButton;

In RobotContainer.cpp

  /* Add a button to reset the gyro using a triggered network button    */
  /* command.  Note this clears the boolean after running the function  */
  /* to zero heading.                                                   */
  frc::Shuffleboard::GetTab("Subsystems").GetLayout("Drive").AddBoolean("Reset Gyro", [this]{ return(m_resetGyroButton); }).WithWidget(frc::BuiltInWidgets::kToggleButton).WithSize(2, 1).WithPosition(0, 7);
  frc2::NetworkButton(nt::NetworkTableInstance::GetDefault().GetBooleanTopic("/Shuffleboard/Subsystems/Drive/Reset Gyro")).OnTrue(frc2::cmd::RunOnce([this] { m_drive.ZeroHeading(); m_resetGyroButton = false; }, {&m_drive}));