FRC1076 / 2019-Competition

Code for 2019 Competition
2 stars 1 forks source link

Learn about networktables and write some simple code to connect some robot info to the driverstation #52

Open mcolinj opened 5 years ago

mcolinj commented 5 years ago

Check out this guide.

https://robotpy.readthedocs.io/en/latest/guide/nt.html#networktables-guide

Look at some sample code, and see if you can create some way for use to enter some data on the driverstation that is used by the robot.

mcolinj commented 5 years ago

Doing putThing creates a new table entry/widget in shuffleboard.

Need to get an entry and figure out the API for setting attributes in the Entry

getEntry(key: str) -> networktables.entry.NetworkTableEntry from builtins.type Gets the entry for the specified key.
:param key: the key name

This code reads correctly, I think and then creates an additional entry:

greet = SmartDashboard.getString("Greeting", "What do YOU want?") self.logger.info("got greeting: "+greet) SmartDashboard.putString("Greeting", "Hello, World")

mcolinj commented 5 years ago

I installed shuffleboard (it is a .jar thing). I launched it with the Mac jar launcher and it creates an editting environment (as well as an active smart dashboard). You can run it with the simulator, although it make my computer labor pretty hard. Set localhost in preferences.

You can instantiate some Widget, give it a name (right click -> properties), get a handle to it using the name as a key SD.getEntry("theNameYouGaveIt"), and then use the appropriate interface to get at the elements. (check out the help(networktables.entry.NetworkTableEntry) to see what you can do to get at elements of a PIDController, for example. We'll need some of those in order to tune our PID algorithms.

mcolinj commented 5 years ago

I was able to get this working with a minimal bit of code. Seems that the key is to put the data of the PIDController to the SmartDashboard, and then get a handle to it to use for picking up the updates. ElevatorAttendent needs to create a PIDController (we want to the constants in the SmartDashboard so we can tune them).

    #  Add option logger = None argument to ctor and in the ctor call
    self.pid = wpilib.PIDController(kP, kI, kD, source=encoder, output=self)

    # Create widget in SD and maintain reference to it so we can get tuning values
    wpilib.SmartDashboard.putData("Elevator", self.pid)
    self.sdWidget = wpilib.SmartDashboard.getData("Elevator")
    if self.logger is not None:
        self.logger.info("SD.Elevetor getData %s", "FAILED" if self.sdWidget is None else "SUCCEEDED")
# Then any time we are in the move() function, we can pull widget values
# in to the controller from the SD widget
def move(self):
        if self.sdWidget is not None:
            self.pid.setP(self.sdWidget.getP())
            self.pid.setI(self.sdWidget.getI())
            self.pid.setD(self.sdWidget.getD())
        else:
            self.sdWidget = wpilib.SmartDashboard.getData("Elevator")
        if self.logger is not None:
            self.logger.info("Trying with P %f I %f D %f", self.pid.getP(), self.pid.getI(), self.pid.getD())
        self.pid.enable()