How do I get if a limit switch is pressed with CTRE's mag encoder?

We will run two limit switches through our encoder: one for max height of the lift, one for min height. They will eventually prevent over-extending our lift so it doesn’t damage itself. The encoder is plugged into our lift talon.

I tried researching the topic, but I couldn’t understand CTRE’s documentation and I haven’t found anyone that has used it. I’ve already struggled this year trying to use the encoder itself after they made some big changes in regards to how it’s programmed.

So, how do I get when a limit switch is pressed? I don’t see any methods that do that.

The hardware connection is covered in section 4 of the encoder’s user guide. Adding normally open limit switches to a connected encoder will stop the motor in the direction which has reached the limit upon closing the switches, without further programming. You can programmatically disable each limit switch or change it to a normally closed configuration using the web config or programmatically as described in the software manual, section 4 (web) and 5 (programmatically).

If you want to get the state of the limit switches in your robot program, see Jacob C.'s post below. [STRIKE]to send to the dashboard or for an isFinished() method, you will have to check out the API docsto get the function name, as it is not in the “sample” list in section 5. It’s in the MotControllerJNI class, the methods are:

static int IsFwdLimitSwitchClosed(long handle)    
static int IsRevLimitSwitchClosed(long handle)  

Edit: and yes, it does seem strange to me that you get this from a static method rather than from something like one of these:

talon.getSensorCollection().getFwdLimitSwitch()
talon.getSensorCollection().isFwdLimitSwitchClosed()
talon.isFwdLimitSwitchClosed()

Edit2: It appears that you get the handle from talon.getHandle(), so the overall call would be:

MotControllerJNI.IsFwdLimitSwitchClosed(talon.getHandle())    
MotControllerJNI.IsRevLimitSwitchClosed(talon.getHandle())  

Note that these return ints, not Booleans.[/STRIKE]

So the limit switches will do what I want them to do without actually programming them?

If you wire the switches normally open, you haven’t reconfigured the limit switches, and you want to use them for the usual function of turning off the motor in the indicated direction when your actuator closes the limit switch, yes.

So the limit switches will do what I want them to do without actually programming them?

Basically, yes. GeeTwo covered most of the caveats in the post above. I would still read the section of our documentation on how to set up limit switches: GitHub - CrossTheRoadElec/Phoenix5-Documentation: Phoenix-Documentation

If you need to read the state of the limit switches in your code, these are the functions to use (and they return booleans):

_talon.getSensorCollection().isFwdLimitSwitchClosed();
_talon.getSensorCollection().isRevLimitSwitchClosed();

This is documented here: GitHub - CrossTheRoadElec/Phoenix5-Documentation: Phoenix-Documentation

The static functions that GeeTwo found are the JNI functions - they’re what actually call into our Common C Interface (CCI), which is the same low-level library that all languages use.
Those functions are meant to be used only by the library itself and are not intended to be directly called by users.

A related question:

We use mag encoders that connect directly to the Talon data port using the tiny 10-wire ribbon cable and the single data port connector. These work great and the encoders are virtually “plug and play”.

We now wish to hardwire some limit switches like the OP does.

Is there an easy way to wire the limit switches in along with the encoders? I would love for there to be a breakout board that plugs into the Talon, into which you plug in the encoder, and solder in the wires for the limit switches. As far as I can tell, the closest that exists to this is a breakout board where we would have to split out 4 conductors from the tiny ribbon cable and solder them to the board.

If I remember correctly (I’m not the person who wires), the limit switches get soldered on directly to designated leads on the encoder itself.


The encoders appear to have 4 solder pads on the back, which expose the limit switch connections of the Talon. See section 4.1 on the Magnetic Encoder User’s Guide for more info.

EDIT: Whoops, minisolarclown beat me to it. Look at the attached image on his post instead.

AHA!

Thanks guys!

What does talon.getSensorCollection() do?

According to this, it returns an object that can get/set individual RAW sensor values.

The documentation of the class of the object it returns is here. It appears to be able to return both raw and scaled sensor values:

int 	GetAnalogIn ()
ErrorCode 	SetAnalogPosition (int newPosition, int timeoutMs)
int 	GetAnalogInRaw ()
int 	GetAnalogInVel ()
int 	GetQuadraturePosition ()
ErrorCode 	SetQuadraturePosition (int newPosition, int timeoutMs)
int 	GetQuadratureVelocity ()
int 	GetPulseWidthPosition ()
ErrorCode 	SetPulseWidthPosition (int newPosition, int timeoutMs)
int 	GetPulseWidthVelocity ()
int 	GetPulseWidthRiseToFallUs ()
int 	GetPulseWidthRiseToRiseUs ()
int 	GetPinStateQuadA ()
int 	GetPinStateQuadB ()
int 	GetPinStateQuadIdx ()
int 	IsFwdLimitSwitchClosed ()
int 	IsRevLimitSwitchClosed ()

Another neat feature of this is that you can use it to monitor a sensor which may or may not have anything to do with the motor the SRX is controlling.

We want to look closer at this in the future, because it could greatly simplify our wiring.

Our intake contains two motors and one analog IR sensor. Because the intake can be elevated up to 84" in the air, there is a very long signal path back to the RoboRio. We mounted the Talon SRX’s on the intake, so the only wires going up there are 2 pairs of red/black power wires, 2 pairs of CAN bus wires (one going out, one coming back) and a set of analog input wires for the IR sensor. One of the concerns was the long wire could interfere with accurate voltage readings. It seems to work, but if we could have wired the IR sensor directly to one of the Talons, and queried it over CAN, it would make the wiring simpler and more reliable.

We had our last 2 hours of unbag time and tested some stuff with this limit switch setup. It does not work if you have the talons in master/slave mode. The master disables its output without letting the slave know, so we messed up our gear box a little bit because of it. I was able to get the states of the limit switches, but we decided to re-wire them and plug them into the rio.

Not true. The follower blindly follows the applied motor output of the master - that’s how it work. So if the master’s reported applied output is zero, so is the followers.

Use self-test to confirm your motor controllers are setup correctly. Contact [email protected] if you have specific questions.