Retrieve Limit Switch State on CAN Speed Controller

Similar to this post (Spark Limit Switches) from back in 2017, I’m curious if anything has changed now that many (probably most) teams are using CAN-controlled speed controllers.

It seems like it should be fairly simple for a TalonSRX / TalonFX / SparkMax to provide programmers a flag to be able to tell if a limit switch attached directly to their breakout has been tripped, however, I have not been able to find any information about if this is possible in their user docs.

There are many cases where this would be useful, IE, not only prevent a mechanism from overrunning its bounds, but also 0 the encoder if the bound is reached.

Something like _talon.getForwardLimitSwitch() as a boolean seems like it would make total sense to have.

Taking a quick peak at the Rev Robotics Library API it appears this does exist now.

There are getForwardLimitSwitch and getReverseLimitSwitch​ methods that return CANDigitalInput objects which have a boolean get method.

So you could write something like the following

sparkMax.getForwardLimitSwitch(CANDigitalInput.LimitSwitchPolarity.kNormallyOpen).get()

I don’t see anything immediately for the CTRE Pheonix Library, but I’ll take a deeper look in a bit.

2 Likes

For CTRE, its a bit more interesting, but it does appear you can get the limit switch values:

There is a method in the TalonSRX class called getSensorCollection which then has boolean isFwdLimitSwitchClosed and isRevLimitSwitchClosed methods.

So you could write something like the following

talonSRX.getSensorCollection().isFwdLimitSwitchClosed()
2 Likes

That is awesome, thank you!

I must not have dig deep enough to find it in there. Though honestly, I know a lot more about the electrical than I do about the code! :sweat_smile:

Worth noting that the Talon supports rezeroing the encoder in hardware when a limit switch is asserted. I don’t think the Spark Max does though.

Oh snap really? How do you configure that?

My thought was to just try to have a command triggered when the limit switch was hit that reset the encoder value. Or have something in periodic watch the value of the limit switch.

The CANDigitalInput class and the optional factory functions for it have existed since the library’s creation for the 2019 season. v1.5.4 deprecates the CANDigitalInput constructor.

configClearPositionOnLimitF/R will do the trick.

It’s also available in the configAll API if you’re using it already.

1 Like

Does any of that invalidate my answer? I didn’t test anything, just posted my findings from the API Docs.

I don’t know what existed when, I simply assumed based on the prior thread and context from the OP’s initial post that something must have not-existed at some point.

I don’t believe so.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.