Skip to content

Commit

Permalink
Merge pull request #89 from robotpy/inverted
Browse files Browse the repository at this point in the history
Add new setInverted API
  • Loading branch information
virtuald committed Jan 14, 2019
2 parents 4e278cd + a555d8e commit e8fae5a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions ctre/_impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
RemoteSensorSource = _enum_module.RemoteSensorSource
FeedbackDevice = _enum_module.FeedbackDevice
FollowerType = _enum_module.FollowerType
InvertType = _enum_module.InvertType
StatusFrame = _enum_module.StatusFrame
StatusFrameEnhanced = _enum_module.StatusFrameEnhanced
VelocityMeasPeriod = _enum_module.VelocityMeasPeriod
Expand Down
1 change: 1 addition & 0 deletions ctre/_impl/ctre_roborio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef py::call_guard<py::gil_scoped_release> release_gil;
#include "ctre/phoenix/motorcontrol/DemandType.h"
#include "ctre/phoenix/motorcontrol/FeedbackDevice.h"
#include "ctre/phoenix/motorcontrol/FollowerType.h"
#include "ctre/phoenix/motorcontrol/InvertType.h"
#include "ctre/phoenix/motorcontrol/LimitSwitchType.h"
#include "ctre/phoenix/motorcontrol/NeutralMode.h"
#include "ctre/phoenix/motorcontrol/RemoteSensorSource.h"
Expand Down
14 changes: 10 additions & 4 deletions ctre/basemotorcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#  (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE
# ----------------------------------------------------------------------------

import typing

from .sensorcollection import SensorCollection
from .trajectorypoint import TrajectoryPoint
from ._impl import (
Expand All @@ -30,6 +32,7 @@
ErrorCode,
Faults,
FollowerType,
InvertType,
LimitSwitchNormal,
MotController,
MotionProfileStatus,
Expand All @@ -52,6 +55,7 @@ class BaseMotorController(MotController):

ControlMode = ControlMode
DemandType = DemandType
InvertType = InvertType
LimitSwitchNormal = LimitSwitchNormal
NeutralMode = NeutralMode
ParamEnum = ParamEnum
Expand Down Expand Up @@ -174,7 +178,7 @@ def neutralOutput(self):
"""Neutral the motor output by setting control mode to disabled."""
self.set(ControlMode.Disabled, 0, 0)

def setInverted(self, invert: bool):
def setInverted(self, invert: typing.Union[bool, InvertType]):
"""
Inverts the hbridge output of the motor controller.
Expand All @@ -190,12 +194,14 @@ def setInverted(self, invert: bool):
:param invert:
Invert state to set.
"""
self.invert = invert
super().setInverted(invert)
self.invert = int(invert)
super()._setInverted_2(self.invert)

def getInverted(self) -> bool:
""":returns: invert setting of motor output"""
return self.invert
if self.invert in (0, 1):
return bool(self.invert)
return super().getInverted()

def getMotorOutputVoltage(self) -> float:
""":returns: applied voltage to motor in volts"""
Expand Down
27 changes: 26 additions & 1 deletion gen/MotController_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,14 @@ c_MotController_SetSensorPhase:
:param PhaseSensor:
Indicates whether to invert the phase of the sensor.
c_MotController_SetInverted:
set: inverted
ignore: true
c_MotController_SetInverted_2:
internal: true
code: |
if invertType in (0, 1):
invertType = bool(invertType)
self.hal_data['inverted'] = invertType
c_MotController_ConfigOpenLoopRamp:
set: open_loop_ramp
doc: |
Expand Down Expand Up @@ -381,6 +388,24 @@ c_MotController_EnableVoltageCompensation:
:param enable:
Enable state of voltage compensation.
c_MotController_GetInverted:
code: |
invert_state = self.hal_data['inverted']
if isinstance(invert_state, bool):
retval = invert_state
else:
follow_target = self.hal_data['follow_target']
target_state = hal_data['CAN'][follow_target]['inverted']
if not isinstance(target_state, bool):
raise ValueError("Cannot follow a target that is following")
if invert_state == 2:
retval = target_state
elif invert_state == 3:
retval = not target_state
else:
raise ValueError("Invalid invert_state %s" % invert_state)
doc: |
:returns: invert setting of motor output.
c_MotController_GetBusVoltage:
get: bus_voltage
doc: |
Expand Down
1 change: 1 addition & 0 deletions gen/gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- ctre/phoenix/motorcontrol/DemandType.h
- ctre/phoenix/motorcontrol/FeedbackDevice.h
- ctre/phoenix/motorcontrol/FollowerType.h
- ctre/phoenix/motorcontrol/InvertType.h
- ctre/phoenix/motorcontrol/LimitSwitchType.h
- ctre/phoenix/motorcontrol/NeutralMode.h
- ctre/phoenix/motorcontrol/RemoteSensorSource.h
Expand Down
17 changes: 15 additions & 2 deletions tests/test_talonsrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,22 @@ def test_talon_stopMotor(talon, cdata):


def test_talon_setinverted(talon, cdata):
assert cdata["inverted"] == False
assert cdata["inverted"] is False
talon.setInverted(True)
assert cdata["inverted"] == True
assert cdata["inverted"] is True


def test_talon_setinverted_follow(ctre, talon, cdata):
master = ctre.WPI_TalonSRX(3)
talon.follow(master)
assert cdata["inverted"] is False
talon.setInverted(ctre.WPI_TalonSRX.InvertType.FollowMaster)
assert cdata["inverted"] is 2

master.setInverted(True)
assert talon.getInverted() is True
master.setInverted(False)
assert talon.getInverted() is False


def test_talon_initSendable(talon, sendablebuilder):
Expand Down

0 comments on commit e8fae5a

Please sign in to comment.