Sign of Encoder Value Randomly Flips?

Hello everyone,
My name is Ankith Udupa, a programmer from Frog Force 503, and we are using a versaplanetary integrated encoder in our gearbox for our elevator. We haven’t had a problem with it all season but recently this past summer it seems that every time we worked on the gearbox and had to take out the encoder and put it back in, the sign of the values returned by the encoder would flip. Also, sometimes it seems like the sign will flip randomly with nothing(to my knowledge) actually changing other than the fact that the robot had been power cycled. Originally, we were able to fix the problem temporarily by reflashing the firmware of the talon, however eventually, even this didn’t solve the problem. We have since resorted to simply adding a negative sign in front of the value that we get in our code and removing the sign when it flips. Obviously, this is less than ideal and we would like to know what the root cause of this is. We are stumped and can’t seem to figure it out. What do you guys think?

Are you using this with an SRX, via breakout to the RIO, or some other configuration? If using a breakout, is there the possibility that the A and B channel signal wires are getting swapped?

Posting some code and/or a picture of your wiring may also help identify issues.

Hmmmm. I believe the talon does store settings over power cycle, usually. Is there any logic in your code that “flips” the sign of the encoder configured in the SRX ? Like, do you happen to be toggeling the sign every time the robot powers on or something like that? A code posting could help.

Is there any commonality between the events leading up to the flips? Can you trigger them with any action?

If the sign is truly changing with no physical or code or configuration modifications to the robot, it would lead me to suspect either the SRX itself, the cable, or the encoder.

Do you have a spare SRX you can swap your unit in with? If so, do it, and see if that mitigates the issue. If it does, it’s related to the behavior of the SRX itself.

If you have an oscilliscope, you may be able to view the signal generated by the encoder to see the flip event (which would mean a faulty cable or encoder).

Our encoder is plugged directly into a talon. We don’t use a breakout board. As for the code, we retrieve values from the sensor with


public double getEncoderCounts() {
		//9/13/2018 Swapped out the encoder yesterday and as a result we were forced to flip the sign 
		return -elevatorMotor.getSelectedSensorPosition(0);
}

public double getEncoderVelCounts() {
		//9/13/2018 Swapped out the encoder yesterday and as a result we were forced to flip the sign 
		return -elevatorMotor.getSelectedSensorVelocity(0);
}

the only change to this code since April was the negative sign and the rest of the code relating to the elevator hasn’t been changed since April. Is there anything relating to our setup or code that you see wrong? Thanks so much for your help on this issue!

Thanks for your reply! The flips happen whenever we switch the encoder but they also occur seemingly randomly. Sometimes in the middle of practice when we are switching a battery or something. The only thing in the code that we’ve changed since April is the addition of a negative sign in front of where we get the value when we saw that it was flipped. Is it possible that some of the configurations that we set for the talon aren’t getting set/persisting over power cycles? Here is the code that we use to configure the talon:


elevator motor = new TalonSRX(Robot.bot.elevatorID);
	
elevatorMotor.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Absolute, 0, 0);
elevatorMotor.setInverted(true);
elevatorMotor.setSensorPhase(true);
elevatorMotor.setNeutralMode(NeutralMode.Brake);

We can definitly try switching the talons if we can get our hands on one. We’ve switched the cable multiple times but that doesn’t seem prevent the problem from happening again.

Are you using this with an SRX, via breakout to the RIO, or some other configuration? If using a breakout, is there the possibility that the A and B channel signal wires are getting swapped?

Posting some code and/or a picture of your wiring may also help identify issues.

Thanks for your reply! We have the encoder wired straight to a talon. As for the code, we haven’t changed anything except adding the negative sign that I referred to since April. Here is how we get the value of the encoder.



public double getEncoderCounts(){
	return elevatorMotor.getSelectedSensorPosition(0);
}
public double getEncoderVelCounts(){
	Return elevatorMotor.getSelectedSensorVelocity(0);
}

Is there anything you can see in this that might cause the values to just flip as described? Thanks for your help!