DEV Community

Suave101
Suave101

Posted on

How to use NtCore with PyRobot for FRC?

As of 1/8/2023 the PyRobot documentation page has not released the documentation for NtCore or NetworkTables version 4. Therefore, the techniques used here may change between versions of RobotPy. In this case, we are using the following specifications:

pyntcore==2023.1.1.0
wpilib==2023.1.1.0
Enter fullscreen mode Exit fullscreen mode

The new NetworkTables 4.0 library implements a publisher and subscriber system where you have individual objects in your code that are read only or write only. Here is an example of publishing controller information to a network table:

import wpilib, ntcore


class MyRobot(wpilib.TimedRobot):
    def robotInit(self):
        # Network Table Server Configuration
        self.networkTablesServer = ntcore.NetworkTableInstance.getDefault()

        # Starting the Network Tables Server
        self.networkTablesServer.startServer()

        # Create a Network Table named "Controller Data"
        self.controllerDataNetworkTable = self.networkTablesServer.getTable("ControllerData")

        # Create Double (64 bit float) publishable topic on Controller Data Network Table
        self.networkTableLeftY = self.controllerDataNetworkTable.getDoubleTopic("Left-Y").publish()
        self.networkTableRightY = self.controllerDataNetworkTable.getDoubleTopic("Right-Y").publish()

        # Controller Configuration
        self.controller = wpilib.XboxController(0)

    def robotPeriodic(self):
        # Periodically set the Network Table value to Y Values
        self.networkTableLeftY.set(self.controller.getLeftY())
        self.networkTableRightY.set(self.controller.getRightY())


if __name__ == "__main__":
    wpilib.run(MyRobot)
Enter fullscreen mode Exit fullscreen mode

This shows the basic layout of a data publishing system but, what if we want to get user input? Then we would need to subscribe to a topic. Here is an example of user input changing our output:

import wpilib, ntcore


class MyRobot(wpilib.TimedRobot):
    def robotInit(self):
        # Network Table Server Configuration
        self.networkTablesServer = ntcore.NetworkTableInstance.getDefault()

        # Starting the Network Tables Server
        self.networkTablesServer.startServer()

        # Create a Network Table named "Motor Data"
        self.controllerDataNetworkTable = self.networkTablesServer.getTable("Motor-Data")

        # Create Double (64 bit float) publishable topic on Controller Data Network Table
        self.networkTableLeftY = self.controllerDataNetworkTable.getDoubleTopic("Left-Motor-Percent").publish()
        self.networkTableRightY = self.controllerDataNetworkTable.getDoubleTopic("Right-Motor-Percent").publish()

        # Create Motor Performance Object
        self._motorPerformance = self.controllerDataNetworkTable.getDoubleTopic("Motor Percentage").publish()
        self._motorPerformance.set(50)

        # Get Double (64 bit float) topic and subscribe
        self.motorPerformance = self.controllerDataNetworkTable.getDoubleTopic("Motor Percentage").subscribe(50)

        # Controller Configuration
        self.controller = wpilib.XboxController(0)

    def robotPeriodic(self):
        # Periodically set the Network Table value to Y Values
        if self.motorPerformance.get() > 100:
            self.networkTableLeftY.set(self.controller.getLeftY())
            self.networkTableRightY.set(self.controller.getRightY())
        else:
            self.networkTableLeftY.set(self.controller.getLeftY() * (self.motorPerformance.get()/100))
            self.networkTableRightY.set(self.controller.getRightY() * (self.motorPerformance.get()/100))


if __name__ == "__main__":
    wpilib.run(MyRobot)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)