Log in

View Full Version : Read/Write Preferences in C++


rod@3711
05-10-2015, 19:51
The 2015 SmartDashboard description gives a code example to set Preferences, then display them by adding a Robot Preferences widget.

Last year we made program changes for some timing and positioning variables that we would have preferred to set via the SmartDashboard and have them preserved.

I added the code to a Sample Robot project and can not get it to work.

The lines we added have // %rod comments in the following snippet.

////////////////////////////////////////////////////////////////////
class Robot: public SampleRobot
{
RobotDrive myRobot; // robot drive system
Joystick stick; // only joystick

Preferences *prefs; // %rod

public:
Robot() :
myRobot(0, 1), // these must be initialized in the same order
stick(0) // as they are declared above.
{
myRobot.SetExpiration(0.1);
prefs = Preferences::GetInstance(); // %rod
}

void RobotInit(){ // all new %rod
double x;
double y;
SmartDashboard::PutString("Version","1.1");
x = prefs->GetDouble("x Pref",33);
y = prefs->GetDouble("y Pref",66);
}
//////////////////////////////////////////////////////

The SmartDashboard will display the version 1.1 data, but when we add the Robot Preferences widget, it is blank.

I then spent several hours trying prefs->PutDouble, prefs->Save..., etc. and have now given up.

Is there a working example of C++ using Preferences?

rod@3711
13-10-2015, 22:07
Finally got back to looking at Preferences and found my mistake.

The string names for the Keys can not have spaces.

x = prefs->GetDouble("x Pref",33);

changed "x Pref" to "xPref" and everything worked as expected.