Skip to content

Commit

Permalink
Merge pull request #44 from robotpy/follow-sw
Browse files Browse the repository at this point in the history
Add configXXXLimitSwitchSource to simulation
  • Loading branch information
virtuald committed Feb 10, 2018
2 parents cc61c0b + f573eee commit e118ce5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 67 deletions.
49 changes: 0 additions & 49 deletions ctre/talonsrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,3 @@ class TalonSRX(BaseMotorController):
def __init__(self, deviceNumber):
super().__init__(deviceNumber | 0x02040000)
hal.report(hal.UsageReporting.kResourceType_CANTalonSRX, deviceNumber + 1)

def configForwardLimitSwitchSource(self, type, normalOpenOrClose, timeoutMs: int):
"""
Configures a limit switch for a local/remote source.
For example, a CAN motor controller may need to monitor the Limit-R pin
of another Talon, CANifier, or local Gadgeteer feedback connector.
If the sensor is remote, a device ID of zero is assumed.
If that's not desired, use the four parameter version of this function.
:param type:
Limit switch source.
User can choose between the feedback connector, remote Talon SRX, CANifier, or deactivate the feature.
:param normalOpenOrClose:
Setting for normally open, normally closed, or disabled. This setting
matches the web-based configuration drop down.
:param timeoutMs:
Timeout value in ms. If nonzero, function will wait for
config success and report an error if it times out.
If zero, no blocking or checking is performed.
:returns: Error Code generated by function. 0 indicates no error.
"""
return super().configForwardLimitSwitchSource(type, normalOpenOrClose, 0x00000000, timeoutMs)

def configReverseLimitSwitchSource(self, type, normalOpenOrClose, timeoutMs: int):
"""
Configures a limit switch for a local/remote source.
For example, a CAN motor controller may need to monitor the Limit-R pin
of another Talon, CANifier, or local Gadgeteer feedback connector.
If the sensor is remote, a device ID of zero is assumed. If that's not
desired, use the four parameter version of this function.
:param type:
Limit switch source. See :class:`.LimitSwitchSource` User can choose
between the feedback connector, remote Talon SRX, CANifier, or
deactivate the feature.
:param normalOpenOrClose:
Setting for normally open, normally closed, or disabled. This
setting matches the web-based configuration drop down.
:param timeoutMs:
Timeout value in ms. If nonzero, function will wait for config
success and report an error if it times out. If zero, no
blocking or checking is performed.
:returns: Error Code generated by function. 0 indicates no error.
"""
return super().configReverseLimitSwitchSource(type, normalOpenOrClose, 0x00000000, timeoutMs)
34 changes: 26 additions & 8 deletions gen/MotController_data.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
sim_extra: |
from .ctre_sim_enums import ControlMode, FeedbackDevice
from .ctre_sim_enums import ControlMode, FeedbackDevice, LimitSwitchSource
from wpilib import Notifier
_feedback2key = {
Expand Down Expand Up @@ -90,6 +90,12 @@ c_MotController_Create1:
'limit_switch_closed_for': False,
'limit_switch_closed_rev': False,
# Remote limit switches?
'limit_switch_remote_for': LimitSwitchSource.Deactivated,
'limit_switch_remote_rev': LimitSwitchSource.Deactivated,
'limit_switch_follow_for': deviceId,
'limit_switch_follow_rev': deviceId,
# These two values are set when the set()/setDemand() functions are called
'control_mode': 0,
'value': 0.0, # percent throttle
Expand Down Expand Up @@ -564,7 +570,8 @@ c_MotController_ConfigVelocityMeasurementWindow:
:returns: Error Code generated by function. 0 indicates no error.
c_MotController_ConfigForwardLimitSwitchSource:
code: |
raise NotImplementedError
self.hal_data['limit_switch_remote_for'] = type
self.hal_data['limit_switch_follow_for'] = deviceID
doc: |
Configures the forward limit switch for a remote source. For example, a
CAN motor controller may need to monitor the Limit-F pin of another Talon
Expand All @@ -585,7 +592,8 @@ c_MotController_ConfigForwardLimitSwitchSource:
:returns: Error Code generated by function. 0 indicates no error.
c_MotController_ConfigReverseLimitSwitchSource:
code: |
raise NotImplementedError
self.hal_data['limit_switch_remote_rev'] = type
self.hal_data['limit_switch_follow_rev'] = deviceID
doc: |
Configures the reverse limit switch for a remote source. For example, a
CAN motor controller may need to monitor the Limit-R pin of another Talon
Expand Down Expand Up @@ -1334,19 +1342,24 @@ c_MotController_GetPinStateQuadIdx:
get: quad_pin_idx
c_MotController_IsFwdLimitSwitchClosed:
code: |
retval = 1 if self.hal_data['limit_switch_closed_for'] else 0
data = self.hal_data
if self.hal_data['limit_switch_remote_for'] != LimitSwitchSource.Deactivated:
data = hal_data['CAN'][self.hal_data['limit_switch_follow_for']]
retval = 1 if data['limit_switch_closed_for'] else 0
c_MotController_IsRevLimitSwitchClosed:
code: |
retval = 1 if self.hal_data['limit_switch_closed_rev'] else 0
data = self.hal_data
if self.hal_data['limit_switch_remote_rev'] != LimitSwitchSource.Deactivated:
data = hal_data['CAN'][self.hal_data['limit_switch_follow_rev']]
retval = 1 if data['limit_switch_closed_rev'] else 0
c_MotController_GetFaults:
code: |
raise NotImplementedError
c_MotController_GetStickyFaults:
code: |
raise NotImplementedError
c_MotController_ClearStickyFaults:
code: |
raise NotImplementedError
code: pass
doc: |
Clears all sticky faults.
Expand Down Expand Up @@ -1394,7 +1407,12 @@ c_MotController_GetQuadPinStates:
self.hal_data['quad_pin_idx'])
c_MotController_GetLimitSwitchState:
code: |
retval = self.hal_data['limit_switch_closed_for'], self.hal_data['limit_switch_closed_rev']
fwd_data = rev_data = self.hal_data
if self.hal_data['limit_switch_remote_for'] != LimitSwitchSource.Deactivated:
fwd_data = hal_data['CAN'][self.hal_data['limit_switch_follow_for']]
if self.hal_data['limit_switch_remote_rev'] != LimitSwitchSource.Deactivated:
rev_data = hal_data['CAN'][self.hal_data['limit_switch_follow_rev']]
retval = fwd_data['limit_switch_closed_for'], rev_data['limit_switch_closed_rev']
doc: |
:returns: Forward Limit switch closed, Reverse Limit switch closed
c_MotController_GetClosedLoopTarget:
Expand Down
45 changes: 35 additions & 10 deletions tests/test_talonsrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ def test_talon_initSendable(talon, sendablebuilder):
assert talon.get() == 3


@pytest.mark.xfail(raises=NotImplementedError)
def test_talon_configForwardLimitSwitchSource(talon):
talon.configForwardLimitSwitchSource(1, 2, 3)


@pytest.mark.xfail(raises=NotImplementedError)
def test_talon_configReverseLimitSwitchSource(talon):
talon.configReverseLimitSwitchSource(1, 2, 3)


def test_talon_configPeakCurrentLimit(talon):
talon.configPeakCurrentLimit(1, 2)

Expand Down Expand Up @@ -314,6 +304,41 @@ def test_basemotorcontroller_getErrorDerivative(talon, cdata):
cdata['pid1_errorDerivative'] = 50.
assert talon.getErrorDerivative(1) == 50.

def test_basemotorcontroller_limitSwitches(ctre, hal_data, talon, cdata):

# local
for v in (True, False):
cdata['limit_switch_closed_for'] = v
cdata['limit_switch_closed_rev'] = not v
assert talon.isFwdLimitSwitchClosed() == v
assert talon.isRevLimitSwitchClosed() == (not v)

assert talon.getLimitSwitchState() == (v, (not v))

# remote
talon.configForwardLimitSwitchSource(talon.LimitSwitchSource.RemoteTalonSRX,
talon.LimitSwitchNormal.NormallyOpen, 2, 0)
talon.configReverseLimitSwitchSource(talon.LimitSwitchSource.RemoteTalonSRX,
talon.LimitSwitchNormal.NormallyOpen, 2, 0)

# The above should work without us creating the remote talon?
talon2 = ctre.WPI_TalonSRX(2)
cdata2 = hal_data['CAN'][2]

for v in (True, False):
cdata['limit_switch_closed_for'] = not v
cdata2['limit_switch_closed_for'] = v
cdata['limit_switch_closed_rev'] = v
cdata2['limit_switch_closed_rev'] = not v

assert talon.isFwdLimitSwitchClosed() == v
assert talon.isRevLimitSwitchClosed() == (not v)
assert talon2.isFwdLimitSwitchClosed() == v
assert talon2.isRevLimitSwitchClosed() == (not v)

assert talon.getLimitSwitchState() == (v, (not v))
assert talon2.getLimitSwitchState() == (v, (not v))


@pytest.mark.xfail(raises=NotImplementedError)
def test_basemotorcontroller_getFaults(talon, ctre):
Expand Down

0 comments on commit e118ce5

Please sign in to comment.