How to use the wpilib.wpilib.sensorutil.SensorUtil function in wpilib

To help you get started, we’ve selected a few wpilib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github robotpy / robotpy-wpilib / wpilib / wpilib / powerdistributionpanel.py View on Github external
def initSendable(self, builder: SendableBuilder) -> None:
        builder.setSmartDashboardType("PowerDistributionPanel")
        for chan in range(SensorUtil.kPDPChannels):
            builder.addDoubleProperty(
                "Chan%s" % (chan,), partial(self.getCurrent, chan), None
            )
        builder.addDoubleProperty("Voltage", self.getVoltage, None)
        builder.addDoubleProperty("TotalCurrent", self.getTotalCurrent, None)
github robotpy / robotpy-wpilib / wpilib / wpilib / doublesolenoid.py View on Github external
raise ValueError(
                "don't know how to handle %d positional arguments" % len(args)
            )

        if moduleNumber is None:
            moduleNumber = SensorUtil.getDefaultSolenoidModule()
        if forwardChannel is None:
            raise ValueError("must specify forward channel")
        if reverseChannel is None:
            raise ValueError("must specify reverse channel")

        super().__init__(moduleNumber)

        self.valueEntry = None
        SensorUtil.checkSolenoidModule(moduleNumber)
        SensorUtil.checkSolenoidChannel(forwardChannel)
        SensorUtil.checkSolenoidChannel(reverseChannel)

        portHandle = hal.getPortWithModule(moduleNumber, forwardChannel)
        self.forwardHandle = hal.initializeSolenoidPort(portHandle)

        try:
            portHandle = hal.getPortWithModule(moduleNumber, reverseChannel)
            self.reverseHandle = hal.initializeSolenoidPort(portHandle)
        except Exception:
            # free the forward handle on exception, then rethrow
            hal.freeSolenoidPort(self.forwardHandle)
            self.forwardHandle = None
            self.reverseHandle = None
            raise

        self.forwardMask = 1 << forwardChannel
github robotpy / robotpy-wpilib / wpilib / wpilib / sensorutil.py View on Github external
def checkPDPModule(module: int) -> None:
        """Verify that the power distribution module number is within limits.
        Module numbers are 0-based.

        :param module: The module number to check.
        """
        if not hal.checkPDPModule(module):
            raise IndexError(
                "Requested PDP module number %d is out of range [0, %d)."
                % (module, SensorUtil.kPDPModules)
            )
github robotpy / robotpy-wpilib / wpilib / wpilib / digitalglitchfilter.py View on Github external
def setPeriodNanoSeconds(self, nanoseconds: float) -> None:
        """
        Sets the number of nanoseconds that the input must hold steady to pass
        through this glitch filter.

        :param nanoseconds: The number of nanoseconds.
        """
        fpga_cycles = int(
            nanoseconds * SensorUtil.kSystemClockTicksPerMicrosecond / 4 / 1000
        )
        self.setPeriodCycles(fpga_cycles)
github robotpy / robotpy-wpilib / wpilib / wpilib / pwm.py View on Github external
def __init__(self, channel: int) -> None:
        """Allocate a PWM given a channel.

        :param channel: The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
        """
        super().__init__()
        SendableBase.__init__(self)

        SensorUtil.checkPWMChannel(channel)
        self.channel = channel

        self.handle = hal.initializePWMPort(hal.getPort(channel))
        self.__finalizer = weakref.finalize(self, _freePWM, self.handle)

        self.setDisabled()

        hal.setPWMEliminateDeadband(self.handle, False)

        hal.report(hal.UsageReporting.kResourceType_PWM, channel)
        self.setName("PWM", channel)

        self.setSafetyEnabled(False)

        # Python-specific: Need this to free on unit test wpilib reset
        Resource._add_global_resource(self)
github robotpy / robotpy-wpilib / wpilib / wpilib / digitaloutput.py View on Github external
def __init__(self, channel: int) -> None:
        """Create an instance of a digital output.

        :param channel: the DIO channel for the digital output. 0-9 are on-board, 10-25 are on the MXP
        """

        super().__init__()
        self.pwmGenerator = None
        self._pwmGenerator_finalizer = None

        SensorUtil.checkDigitalChannel(channel)
        self.channel = channel

        self.handle = hal.initializeDIOPort(hal.getPort(channel), False)

        hal.report(hal.UsageReporting.kResourceType_DigitalOutput, channel)
        self.setName("DigitalOutput", channel)
github robotpy / robotpy-wpilib / wpilib / wpilib / digitalglitchfilter.py View on Github external
def getPeriodNanoSeconds(self) -> float:
        """
        Gets the number of nanoseconds that the input must hold steady to pass
        through this glitch filter.

        :returns: The number of nanoseconds.
        """
        fpga_cycles = self.getPeriodCycles()
        return fpga_cycles * 1000 / (SensorUtil.kSystemClockTicksPerMicrosecond / 4)
github robotpy / robotpy-wpilib / wpilib / wpilib / analogoutput.py View on Github external
from .resource import Resource
from .sendablebase import SendableBase
from .sensorutil import SensorUtil
from .sendablebuilder import SendableBuilder

__all__ = ["AnalogOutput"]


def _freeAnalogOutput(port: hal.AnalogOutputHandle) -> None:
    hal.freeAnalogOutputPort(port)


class AnalogOutput(SendableBase):
    """Analog output"""

    channels = Resource(SensorUtil.kAnalogOutputChannels)

    def __init__(self, channel: int) -> None:
        """Construct an analog output on a specified MXP channel.

        :param channel: The channel number to represent.
        """
        super().__init__()
        SensorUtil.checkAnalogOutputChannel(channel)

        self.channel = channel

        port = hal.getPort(channel)
        self.port = hal.initializeAnalogOutputPort(port)

        self.setName("AnalogOutput", channel)
        hal.report(hal.UsageReporting.kResourceType_AnalogChannel, channel, 1)
github robotpy / robotpy-wpilib / wpilib / wpilib / relay.py View on Github external
class Relay(SendableBase, MotorSafety):
    """Controls VEX Robotics Spike style relay outputs.

    Relays are intended to be connected to Spikes or similar relays. The relay
    channels controls a pair of channels that are either both off, one on, the
    other on, or both on. This translates into two Spike outputs at 0v, one at
    12v and one at 0v, one at 0v and the other at 12v, or two Spike outputs at
    12V. This allows off, full forward, or full reverse control of motors without
    variable speed. It also allows the two channels (forward and reverse) to
    be used independently for something that does not care about voltage
    polarity (like a solenoid).

    .. not_implemented: initRelay
    """

    relayChannels = Resource(SensorUtil.kRelayChannels * 2)

    class Value(enum.IntEnum):
        """The state to drive a Relay to."""

        #: Off
        kOff = 0

        #: On for relays with defined direction
        kOn = 1

        #: Forward
        kForward = 2

        #: Reverse
        kReverse = 3
github robotpy / robotpy-wpilib / wpilib / wpilib / sensorutil.py View on Github external
def checkPDPChannel(channel: int) -> None:
        """Verify that the power distribution channel number is within limits.
        Channel numbers are 0-based.

        :param channel: The channel number to check.
        """
        if not hal.checkPDPChannel(channel):
            raise IndexError(
                "Requested PDP channel number %d is out of range [0, %d)."
                % (channel, SensorUtil.kPDPChannels)
            )