Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SparkMax Breakout Board Encoders #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion robotpy_toolkit_7407/motors/rev_motors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -135,6 +144,59 @@ def get_sensor_velocity(self) -> rotations_per_second:
"""
return self._encoder.getVelocity()

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.getAnalog()

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(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):
if config is None:
return
Expand Down