I have yet to find java code that will allow me to adjust a double variable on the robot through shuffleboard with a slider. Old posts claim solutions, but their is no code. Does this only work in test but not teleop?
You have to call
SmartDashboard.getNumber("VariableName"); in your code, assuming you publish that variable with
If you’re using the Shuffleboard API I’m not familiar with it but check the WPILib docs for the shuffleboard API and you should find an answer.
Sliders on the dashboard are really just a different type of UI widget, you don’t actually have to handle them any differently in the code.
For example, if you wanted the speed limit to be a slider, you could add this code to one of your initialization methods:
That makes the value “kSpeedLimitFactor” available to Shuffleboard and initializes the speed limit to 1.0. Then, you would drag a slider widget to your Shuffleboard canvas and drag and drop the “kSpeedLimitFactor” variable on top of the slider. To read this value in your code, you would use:
double speedLimitFactor = SmartDashboard.getNumber(“kSpeedLimitFactor”, 1.0);
Note that the “1.0” is just a default max if the value can’t be found on the dashboard. Also, this is just an example, you should really get those default values from a Constants file instead of hard-coding them throughout the code.
Thank you for your reply and your insight. I tried your idea. Unfortunately, setting the variable with .getNumber does not work with shuffleboard as the shuffleboard slider continually resets the variable to the default value, which is a required argument. This isn’t fixed by making the variable itself the default. The shuffleboard docs and sample code aren’t working as described. When I used the sample code, the variable would not update even with the Shuffleboard.update() method put into the .robotPeriodic section. Has ANYONE successfully used the shuffleboard number slider widget for 2023 to provide input to a robot? Sorry for the tone of desperation. This has been taking up a fair bit of time.
oops- I replied before I saw CoderMatt’s reply- I will try his suggestion…
I do have a question for Matt. I put my SmartDashboard.putNumber statements in my robotPeriodic to keep them updated. Is this my problem?
You only put the putNumber() call in your constructor. You use the getNumber() call in the code where you want the value. Note that in principle, “getNumber()” can return a different value each call, including within the same method (if you call it twice), so deal with it if that could be a problem.
Thank you all for you advice. I now have a working input slider!
This is what I have learned (mis-learned?)
-SmartDashboard.putNumber(“Name”, variable); statements for sensor data don’t update unless they are in robotPeriodic, but I need to put similar statements into robotInit if I want to make a slider for input variables, and putting an assignment statement into robotPeriodic with getNumber() does the double duty of changing my variable according to shuffleboard, and correctly displaying its current value.
I tried the SmartDashboard.updateValues(); method in robotPeriodic, but it did not update the variables on suffleboard.- that’s ok, I just won’t use this method.
You are correct. We used a DashboardHelper class where we defined all possible SmartDashboard variables with their default values. This covered off all of our SubSystems, Commands, Sensor readings, etc. We called this code from our RobotContainer() constructor which had the great benefit of showing all possible Shuffleboard data as soon as the robot was connected so we could immediately design the Shuffleboard pages/tabs. (You can save the Shuffleboard layout so you only need to design all the tabs / widget locations once.)
Throughout the code (in the periodic methods), we would use “SmartDashboard.put…()” methods to update the dashboard with current speeds, encoder data, gyro angles, etc. and because of the prior initialization part, the data would show up exactly where we had already defined it.
One thing we found out by accident is that if you put forward slashes in your dashboard key names (e.g., “DriveToDistanceCommand/Constants/kSpeedLimitFactor”), they automatically show up in a very nice hierarchical format in Shuffleboard! It makes everything so much better organized.