Robot preferences help

Hi

We’re trying to get the robot preferences to work. We followed the example on the screen steps page (http://wpilib.screenstepslive.com/s/4485/m/26401/l/255423-setting-robot-preferences-from-smartdashboard), but are confused with a number of things. We’re doing command based programming in Java.

  1. should this go in the robot init like in the example? Doesn’t that mean you need to reboot the Rio every time you change the file?
  2. we can’t seem to get anything to show up in the preferences. The things that I have found in the forums are a couple of years old so I’m not sure if it’s changed.
  3. if we create a key in the preferences table that doesn’t exist in the code does that have any impact?
  4. we trying declaring the variables as public in our robot.java but while in other commands or subsystems it doesn’t seem like we could use them. Is there something I’m missing there?

Thanks for any help!
Andrew

We typically read preferences either in disabled init or inline with where the data is used (based on how often we want to be able to updated it).

The code on screensteps doesn’t put any data to the preferences, just reads existing data. You can either add the data using SmartDashboard, or put it in your code. Generally, if our robot code reads a value and it doesn’t exist the code immediately puts a default value to preferences.

No.

You need to references them as Robot.value. Or you could just read the preferences in your command or subsystem.

Thanks for the suggestions! I like putting it into the disabledInit() function.

Still having some problems though.


public class Robot extends IterativeRobot {
	public static double speed;
	Preferences prefs;
        //Other stuff
}
    public void disabledInit(){

    	speed = prefs.getDouble("speed", .5);
    	
    }


And then later on in a different class


     value = Robot.speed;

However we’re having issues where we’re getting an unhandled exception for a null pointer in the disabledInit function, so the driver station is listing as “no robot code”

Any suggestions?

in addition to declaring

Preferences prefs;

are you initializing the ‘prefs’ variable before using it?

prefs = Preferences.getInstance();

Usually a NPE (NullPointerException) is caused by using a variable before initializing it.

d’oh! That was at least part of the problem.

Still having some issues that might be related to the scope or static-ness of the variable. For some reason in order to acces it in a different class, the code wants us to make it static.


public static double speed;

public void disabledInit(){
        prefs = Preferences.getInstance();
    	speed = prefs.getDouble("speed", 0.0);    
	SmartDashboard.putNumber("Pref speed", speed);
    }	

     
	SmartDashboard.putNumber("Pref speed in different class", Robot.speed);

In smart dashboard we’ll get
“Pref speed”: # (this updates appropriately when we disable)
“Pref speed in different class”: 0.0 (this stays at whatever we initially set it to no matter what this doesnt surprise me because it’s declared as static).

Thanks for all the help so far.

Alright, I think we have it figured out enough for how we’ll use it. I think the issue is that we were trying to send the value into a function in a command group for autonomous. When we try to just read it in our teleop mode, we don’t have the same problem. It’s weird, but I think we can work around it.

If anyone else has a similar issue, for our command based programming we did still have to declare are variable in Robot class as:
public static double speed;

My suspicion is that it has to do with being used in a command group. When a new command group was made for our sendableChooser, it must poll what that variable is at that point in time. Then when that command group is called later, it must not look at if the variable has changed.

Thanks!