示例#1
0
    def __init__(
        self,
        channel: Union[AnalogInput, int],
        center: Optional[int] = None,
        offset: Optional[float] = None,
    ) -> None:
        """Gyro constructor.

        Also initializes the gyro. Calibrate the gyro by running for a number
        of samples and computing the center value. Then use the
        center value as the Accumulator center value for subsequent
        measurements. It's important to make sure that the robot is not
        moving while the centering calculations are in progress, this is
        typically done when the robot is first turned on while it's sitting
        at rest before the competition starts.

        :param channel: The analog channel index or AnalogInput object that
            the gyro is connected to. Gyros can only be used on on-board channels 0-1.
        :param center: Preset uncalibrated value to use as the accumulator center value
        :param offset: Preset uncalibrated value to use as the gyro offset
        """
        super().__init__()

        if not hasattr(channel, "initAccumulator"):
            channel = AnalogInput(channel)
            self.channelAllocated = True
            self.addChild(channel)
        else:
            self.channelAllocated = False

        self.analog = channel

        self.gyroHandle = hal.initializeAnalogGyro(self.analog.port)

        self.setDeadband(0.0)

        hal.setupAnalogGyro(self.gyroHandle)

        hal.report(hal.UsageReporting.kResourceType_Gyro,
                   self.analog.getChannel())
        self.setName("AnalogGyro", self.analog.getChannel())

        if center is None or offset is None:
            self.calibrate()
        else:
            hal.setAnalogGyroParameters(self.gyroHandle,
                                        self.kDefaultVoltsPerDegreePerSecond,
                                        offset, center)
            self.reset()
示例#2
0
    def __init__(
        self,
        channel: Union[AnalogInput, int],
        center: Optional[int] = None,
        offset: Optional[float] = None,
    ) -> None:
        """Gyro constructor.

        Also initializes the gyro. Calibrate the gyro by running for a number
        of samples and computing the center value. Then use the
        center value as the Accumulator center value for subsequent
        measurements. It's important to make sure that the robot is not
        moving while the centering calculations are in progress, this is
        typically done when the robot is first turned on while it's sitting
        at rest before the competition starts.

        :param channel: The analog channel index or AnalogInput object that
            the gyro is connected to. Gyros can only be used on on-board channels 0-1.
        :param center: Preset uncalibrated value to use as the accumulator center value
        :param offset: Preset uncalibrated value to use as the gyro offset
        """
        super().__init__()

        if not hasattr(channel, "initAccumulator"):
            channel = AnalogInput(channel)
            self.channelAllocated = True
            self.addChild(channel)
        else:
            self.channelAllocated = False

        self.analog = channel

        self.gyroHandle = hal.initializeAnalogGyro(self.analog.port)

        self.setDeadband(0.0)

        hal.setupAnalogGyro(self.gyroHandle)

        hal.report(hal.UsageReporting.kResourceType_Gyro, self.analog.getChannel())
        self.setName("AnalogGyro", self.analog.getChannel())

        if center is None or offset is None:
            self.calibrate()
        else:
            hal.setAnalogGyroParameters(
                self.gyroHandle, self.kDefaultVoltsPerDegreePerSecond, offset, center
            )
            self.reset()