Sendable Chooser Example for Enum

Greetings!

I’m trying to use the SendableChooser from Shuffleboard in a C++ program. I’m trying to use an enum like
enum pos { LEFT, CENTER, RIGHT };

Use the AddOption to add the Options as radio buttons and then when the driver selects the radio button, have that information go to the robot.

I’ve been fighting with the compiler all night and I’m raising the white flag and putting the question to the experts. Does anyone have an example of some C++ code that does something like this? Or, do I have to pass strings and then do string comparisons? Alternatively, maybe pass an Int instead?

Anyone with some sample code would be greatly appreciated.

TIA,

Mike

My C++ isn’t the best, but this should get the point across:

enum Pos { LEFT, CENTER, RIGHT };

frc::SendableChooser<Pos> m_chooser;

// In RobotInit()
m_chooser.AddOption("Left", Pos::LEFT);
m_chooser.AddOption("Right", Pos::RIGHT);
m_chooser.SetDefaultOption("Center", Pos::CENTER);

// AutomousInit()
Pos startPosition = m_chooser.GetSelected();

switch (startPosition) {
  case LEFT:
    // Start the left-side auto
  case RIGHT:
    // etc...
}

Excellent! I thought I tried that approach. But, it could have been complicated by something else in the file. I’ll give this a shot and let you know.

Thanks,

Mike

For completeness, here is a modification of the PDP-CAN C++ example with the working SendableChooser option. Thanks for the help, Sam!

/----------------------------------------------------------------------------/
/* Copyright © 2017-2018 FIRST. All Rights Reserved. /
/
Open Source Software - may be modified and shared by FRC teams. The code /
/
must be accompanied by the FIRST BSD license file in the root directory of /
/
the project. /
/
----------------------------------------------------------------------------*/

#include <frc/PowerDistributionPanel.h>
#include <frc/TimedRobot.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include <frc/smartdashboard/SendableChooser.h>
#include <frc/shuffleboard/Shuffleboard.h>
#include <frc/shuffleboard/ShuffleboardTab.h>

/**

  • This is a sample program showing how to retrieve information from the Power
  • Distribution Panel via CAN. The information will be displayed under variables
  • through the SmartDashboard.
    */
    class Robot : public frc::TimedRobot {
    public:

void RobotInit() override {
m_chooser.AddOption(“Left”, Pos::LEFT);
m_chooser.AddOption(“Center”, Pos::CENTER);
m_chooser.AddOption(“Right”, Pos::RIGHT);
m_chooser.SetDefaultOption(“Left”,Pos::LEFT);
frc::SmartDashboard::PutData(“Play”, &m_chooser);

}

void AutonomousInit() override {
Pos startPosition = m_chooser.GetSelected();
switch (startPosition) {
case LEFT:
printf(“Left Selected\n”);
break;
case CENTER:
printf(“Center Selected\n”);
break;
case RIGHT:
printf(“Right Selected\n”);
break;
default:
printf(“Default Selected\n”);
break;
}

}

void TeleopPeriodic() override {
/* Get the current going through channel 7, in Amperes. The PDP returns the
* current in increments of 0.125A. At low currents the current readings
* tend to be less accurate.
*/
frc::SmartDashboard::PutNumber(“Current Channel 7”, m_pdp.GetCurrent(7));

/* Get the voltage going into the PDP, in Volts. The PDP returns the voltage
 * in increments of 0.05 Volts.
 */
frc::SmartDashboard::PutNumber("Voltage", m_pdp.GetVoltage());

// Retrieves the temperature of the PDP, in degrees Celsius.
frc::SmartDashboard::PutNumber("Temperature", m_pdp.GetTemperature());

}

private:
// Object for dealing with the Power Distribution Panel (PDP).
frc::PowerDistributionPanel m_pdp;
enum Pos {LEFT, CENTER, RIGHT};
frc::SendableChooser m_chooser;

};

#ifndef RUNNING_FRC_TESTS
int main() { return frc::StartRobot(); }
#endif

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.