How send ultrasonic sensor data to driver station?

Hi! We want to use some ultrasonic sensors so the driver can get an idea how close they are to the crates or other robots. My question is how can I send the data to the driver station from the RIO? Should I make a program that sends the data through TCP to the driver station computer? I am using Python and RobotPy but if you know how to do it in Java, I could try to change that to Python. Any help will be greatly appreciated!

Although Im not familiar with python, in Java you would do this:

distance = Ultrasonic.getRangeInches();
SmartDashboard.putNumber("UltraSonic Distance", distance);

however you might be better off (if you can) making to have some LED’s turn a certain colour on your robot when you are on target. Perhaps red for really far, yellow for partially on target and green for on target. This would allow the driver to keep his eyes on the robot apposed to him having to look down at the driver station constantly to see how far he is from the tote.

So there is a special kind of Driver Station called SmartDashboard. It is basically an ‘application’ that you can add (basically) anything to, including Ultrasonic values. To do this, create an analog input object (we’ll call this ultrasonic).

self.ultrasonic = wpilib.AnalogInput(PortNumber)

Now, we must add it to SmartDashboard.

wpilib.SmartDashboard.putNumber('Ultrasonic', self.ultrasonic.getVoltage())

The reason it is getVoltage() is because thats how the ultrasonic returns its values, so it’d be best to set up a test board, then hold something in front of it at measured distances, record these values, then use them in your code.

For more SmartDashboard help check out

So both of those lines you would put in the RIO’s code? Do I have to import anything else other than wpilib? Thanks for the detailed response!

Yup. Put both of these lines in roboInit and you should be good. And do you know how to start the smart dashboard?

No I don’t know how to start it up. Thanks again!

Ok, so first you want to make sure you have this folder on the driver station On the DS, go to wpilib -> tools -> smartdashboard.jar when you are connected to the robot. To test your code on your own computer, make sure you have the robotpy eclipse plugins Then, in the eclipse menu bar, click WPILib, then Run Simulation Smartdashboard. This will let you see the ultrasonic sensor change based on the value you change in the simulator.

The NetworkTables shared storage is implemented across all languages and all dashboards. So you can pretty easily add the display to either dashboard and publish on your robot.

Greg McKaskle

I wrote some code that would interpret the distance and then display a red, yellow or green light on the remote machine. I saw in the link you sent me(robotpy one), you could import networktables so I was wondering if I could use that in the program I wrote, not needing to go through the smartdashboard? In summary, I just want to use the info from the sensors to input in my wxpython program. Thanks!

Ok, if you just want to use the info from the sensors, then you just need to use self.ultrasonic.getVoltage(). You don’t have to go through SmartDashboard, but for getting the data of the ultrasonic (what the value is when the robot is 5 inches from the bin, for example), it would be wise to just put that value in SDB and write it down.

Instead of writing down the values read by the SmartDashboard, you can just use the csv.txt file. In the preferences menu, just click the checkbox labeled “Log to CSV” before you enable a run you want to record. The file records a timestamp and a key value pair every time the putNumber function is called.

But that wouldn’t tell you the distance of what you’re measuring. So you can just write 5ft, 4.34 volts

Ok thanks, I will try to set up the SmartDashboard tomorrow. Though it does sound quite complicated to set up. Thanks so much for your help!!!

team-4480, since you have a lot of questions, it may benefit you to go to and join the #robotpy channel. VirtualD and I are normally on there (he more than I) and it would be faster communication

WPILib has an ultrasonic sensor implementation that you may find useful:

Once you get the distance data, you can send it to SmartDashboard as mentioned earlier – wpilib.SmartDashboard.putNumber().

Running SmartDashboard/SFX is pretty simple, and is great for viewing the raw values that the robot sends back. However, if you want to do something with the values, you shouldn’t use SmartDashboard/SFX (unless you want to write a SmartDashboard/SFX extension in java).

If you want to write your own networktables client (for example, your wxpython program) to talk to the robot, and receive values from the robot, check out pynetworktables. What you would do is import networktables, get the SmartDashboard table, and ask for the values. You can also get notifications when new data values are sent by the robot, for easier usage.

There’s an example hereand here.