Note: This is a reposted thread, but I needed to fix a bunch of things and CD wouldn’t let me edit posts, so I apologize if you are seeing this for a second time, feel free to ignore it.
Hello all,
Recently my team has decided to make the switch to CTRE Mag encoders. Based on their ability to be both a Quad and a Pulse encoder, and their excellent system of binding to the Talon SRX, along with teams like The Cheesy Poofs beautifully integrating them, it seemed like a no-brainer. However, when I pulled out some Versaplanetary Encoder Stages with CTRE Mags built it, I began to run into some problems. First thing I did was I plugged the encoders into the Mag port of the Talon, and then I wrote a very short iterative program to tell me the readout RPM. This is where I first started to see problems. The program first checked for the existence of the Mag encoder (a system in which I learned from reading 254’s 2016 code), which looked like this
talon = new CANTalon(0);
talon.setFeedbackDevice(CANTalon.FeedbackDevice.CtreMagEncoder_Relative);
talon.changeControlMode(CANTalon.TalonControlMode.Position);
if (talon.isSensorPresent(
CANTalon.FeedbackDevice.CtreMagEncoder_Absolute) !=
CANTalon.FeedbackDeviceStatus.FeedbackStatusPresent) {
DriverStation.reportError("Encoder not found", false);
}
For whatever reason, even when plugged in with a green light, this function always returned false. I then tried swapping _Relative with _Absolute and still got no luck. Just out of curiosity, I tried pulling the velocity and position of the encoders, despite the Talons telling me they weren’t there. I used this code;
boolean isSensorPresent(){
if (talon.isSensorPresent(CANTalon.FeedbackDevice.CtreMagEncoder_Relative) ==
CANTalon.FeedbackDeviceStatus.FeedbackStatusPresent){
return true;
}
else{
return false;
}
}
boolean isTalonPresent(){
if (talon.getDeviceID() == 0){
return true;
}
else{
return false;
}
}
@Override
public void teleopPeriodic() {
SmartDashboard.putBoolean("Encoder Present", isSensorPresent());
SmartDashboard.putBoolean("TalonPresent", isTalonPresent());
SmartDashboard.putDouble("Encoder Position", talon.getEncPosition());
SmartDashboard.putDouble("Encoder Velocity", talon.getEncVelocity());
SmartDashboard.putDouble("Speed", talon.getSpeed());
SmartDashboard.putDouble("Position", talon.getPosition());
}
From this I got some interesting results. my “Speed” and “Position” readouts on the dashboard were erratic and seemingly meaningless. They seemed to return garbage info that didn’t change when I unplugged the encoder altogether, so those were useless. As for “Encoder Velocity”, sometimes it would move a little, and never moved when the encoder was unplugged, but it didn’t seem to hold any meaning either. However, the “Encoder Position” readout had something interesting going on. Whenever the encoder’s shaft was rotating, the value would shift from 0 to -1 at a rapid rate, that rate speeding up and slowing down based on how quickly I was rotating the shaft. This was of course, pretty useless, but interesting nonetheless. At this point I tried modifying the code to tell the Talon to work with the Encoder to set the speed of the motor I had connected to it. I set the encoder’s mode to “Speed” and added a setPID loop.
talon.changeControlMode(CANTalon.TalonControlMode.Speed);
talon.setPID(.12, 0, .5, .014, (int) (1023/.12), 0, 0);
I also added a rpm setter at the base of my teleopPeriodic method;
talon.set(speed);
Now, when I did this and adjusted speed up and down, the speed of the motor moved with it, however not at the right speed. At what was supposed to be 60RPM, it moved at about 6RPM, and when set to 600RPM, it moved at about 75RPM. Sure, my PID wasn’t tuned, but it should have eventually got close to the right speed had the encoder been working properly. Besides, the SmartDashboard values for the encoder speed weren’t even sensible, so it wouldn’t have made a difference. Eventually I got paranoid and directly imported 254’s Flywheel class from 2016 and ran that directly and got the same results. I tried to do this same process with Position mode, and a setPosition method and got the same result. The motor would move, but it would only go in one direction and the Talon would never indicate a change in direction, even after it had long passed it’s “goal position”, along with the info from the SmartDashboard, I was able to conclude yet another encoder failure.
At this point I tried switching the encoder to Quad mode, in which behaved in the exact same way with one exception; Instead of “Encoder Position” switching between -1 and 0, it would osculate around about the 195 mark whenever the encoder shaft was spinning. This behavior carried over to “Pulse Width” mode as well.
I then did this entire process on two other brand new encoders, which eliminates the “bad encoder” theory. I then did this entire process with a new Talon as well.
The question I’m truly asking is, can anybody make sense of this? Have you seen this before? Can you shine some light on this situation?
Any ideas, comments, or suggestions are greatly appreciated. Thanks in advance.