From 56ad47b2176a607978b5792c8295840dd6447c1e Mon Sep 17 00:00:00 2001 From: Inspirol <91295122+Inspirol@users.noreply.github.com> Date: Sun, 22 Jan 2023 16:11:17 -0500 Subject: [PATCH 1/3] SparkMax Breakout Board Encoders Adds the Absolute and Alternate Encoder Objects in the Wrapper Class --- robotpy_toolkit_7407/motors/rev_motors.py | 24 ++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/robotpy_toolkit_7407/motors/rev_motors.py b/robotpy_toolkit_7407/motors/rev_motors.py index 6a638d6..7f6f4c1 100644 --- a/robotpy_toolkit_7407/motors/rev_motors.py +++ b/robotpy_toolkit_7407/motors/rev_motors.py @@ -2,7 +2,7 @@ from dataclasses import dataclass from typing import Optional -from rev import CANSparkMax, SparkMaxPIDController, SparkMaxRelativeEncoder, SparkMaxAlternateEncoder +from rev import CANSparkMax, SparkMaxPIDController, SparkMaxRelativeEncoder, SparkMaxAlternateEncoder, _rev from robotpy_toolkit_7407.motor import PIDMotor from robotpy_toolkit_7407.utils.units import rev, minute, radians, radians_per_second, rad, s, rotations_per_second, \ @@ -135,6 +135,28 @@ def get_sensor_velocity(self) -> rotations_per_second: """ return self._encoder.getVelocity() + def absolute_encoder(self, encoderType: _rev.SparkMaxAbsoluteEncoder.Type = _rev.SparkMaxAbsoluteEncoder.Type.kDutyCycle): + """ + Gets an object the absolute encoder of the motor controller + + + + Returns: + (object): An object for interfacing with the absolute encoder + """ + return self._motor.getAbsoluteEncoder(encoderType) + + def alternate_encoder(self, encoderType: _rev.SparkMaxAlternateEncoder.Type = _rev.SparkMaxAlternateEncoder.Type.kQuadrature): + """ + Gets an object the alternate encoder of the motor controller + + Returns: + (object): An object for interfacing with the alternate encoder + """ + return self._motor.getAlternateEncoder(encoderType) + + + def _set_config(self, config: SparkMaxConfig): if config is None: return From fcc962d97fb858b5c355b2a664cb2aef78990973 Mon Sep 17 00:00:00 2001 From: Inspirol <91295122+Inspirol@users.noreply.github.com> Date: Sun, 22 Jan 2023 16:49:23 -0500 Subject: [PATCH 2/3] Follow Leader Motor Enables the usage of following a main motor --- robotpy_toolkit_7407/motors/rev_motors.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/robotpy_toolkit_7407/motors/rev_motors.py b/robotpy_toolkit_7407/motors/rev_motors.py index 7f6f4c1..b2beee1 100644 --- a/robotpy_toolkit_7407/motors/rev_motors.py +++ b/robotpy_toolkit_7407/motors/rev_motors.py @@ -99,6 +99,15 @@ def set_target_position(self, pos: rotations): """ self.__pid_controller.setReference(pos, CANSparkMax.ControlType.kPosition) + def follow(self, id, invert: bool = False): + """ + Follows another motor controller + + Args: + motor (SparkMax): The motor controller to follow + """ + self._motor.follow(id, invert) + def set_target_velocity(self, vel: rotations_per_second): """ Sets the target velocity of the motor controller in rotations per second From 15d2b39c82e17e0b6b9dc11bee99e3c07373055e Mon Sep 17 00:00:00 2001 From: Inspirol <91295122+Inspirol@users.noreply.github.com> Date: Thu, 26 Jan 2023 17:15:23 -0500 Subject: [PATCH 3/3] Updated Absolute encoder and added forward and reverse limits --- robotpy_toolkit_7407/motors/rev_motors.py | 43 +++++++++++++++++++---- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/robotpy_toolkit_7407/motors/rev_motors.py b/robotpy_toolkit_7407/motors/rev_motors.py index b2beee1..48c85a3 100644 --- a/robotpy_toolkit_7407/motors/rev_motors.py +++ b/robotpy_toolkit_7407/motors/rev_motors.py @@ -144,26 +144,57 @@ def get_sensor_velocity(self) -> rotations_per_second: """ return self._encoder.getVelocity() - def absolute_encoder(self, encoderType: _rev.SparkMaxAbsoluteEncoder.Type = _rev.SparkMaxAbsoluteEncoder.Type.kDutyCycle): + def absolute_encoder(self): """ Gets an object the absolute encoder of the motor controller - - Returns: (object): An object for interfacing with the absolute encoder """ - return self._motor.getAbsoluteEncoder(encoderType) + return self._motor.getAnalog() - def alternate_encoder(self, encoderType: _rev.SparkMaxAlternateEncoder.Type = _rev.SparkMaxAlternateEncoder.Type.kQuadrature): + def alternate_encoder(self): """ Gets an object the alternate encoder of the motor controller + + WARNING -- WILL DISABLE FORWARD AND REVERSE LIMITS Returns: (object): An object for interfacing with the alternate encoder """ - return self._motor.getAlternateEncoder(encoderType) + return self._motor.getAlternateEncoder(6) + + def forward_limit(self, polarity: bool = True): + """ + Gets an object the forward limit of the motor controller + WARNING -- WILL DISABLE ALTERNATE ENCODER + + Returns: + (object): An object for interfacing with the forward limit + """ + type: rev.LimitSwitchPolarity + if polarity: + type = rev.limitSwitchPolarity.kNormallyOpen + else: + type = rev.limitSwitchPolarity.kNormallyClosed + return self._motor.getForwardLimitSwitch(type) + + def reverse_limit(self, polarity: bool = True): + """ + Gets an object the reverse limit of the motor controller + + WARNING -- WILL DISABLE ALTERNATE ENCODER + + Returns: + (object): An object for interfacing with the reverse limit + """ + type: rev.LimitSwitchPolarity + if polarity: + type = rev.limitSwitchPolarity.kNormallyOpen + else: + type = rev.limitSwitchPolarity.kNormallyClosed + return self._motor.getReverseLimitSwitch(type) def _set_config(self, config: SparkMaxConfig):