My team has bought the MK4i swerve and it comes with a cancoder, but unfortunately one of our members has broken the magnetic during the assembly of it and now we only have 3 cancoders working.
We were thinking if it’s possible to completely remove them and only use the encoders from neo, and if it’s possible, is there any place that I can find the code of it? or if there’s another way to solve it, plz help
The internal encoders of motors reset every time they lose power, because they are relative encoders. Cancoders are absolute encoders, which means they can remember their position even when power is lost. Because of this, absolute encoders are needed for swerve, because they can keep track of the swerve wheel direction even when the robot has been turned off.
If you don’t want to buy an entire new encoder, its possible to just buy the magnet/mount from CTRE
What many teams do is they use the absolute encoder to “zero” the swerve when their robot initializes, and then use the Neo encoders to run onboard PID from the Spark Max, which avoids the small delay of running PID from the Roborio.
Yes, you will have to use the steering motor’s encoder for PID control of steering. We do this normally in our code (and only use the absolute encoder for initialization). If your code is not already written to use the motor encoder for steering control, it is pretty easy to switch over. You just need to multiply it by the appropriate gear ratio and encoder per rev count to get it into angular degrees and then you can feed it into your control loop the same way you would an absolute encoder. You will also need to initialize it by setting it to a fixed angle rather than reading an encoder. Then you manually align the wheel to that angle before initialization.
Do you have any other options in your shop, or that you can get from a team nearby? Some options would be a CTRE SRX Mag Encoder, or a Thrifty Absolute Mag Encoder. In fact, if your CANCoder is OK but only the magnet is a problem, you can use the magnet from either of these other encoders with your CANCoder.
My team has been working on a design philosophy, and one of the tenets we have discussed is “don’t code around a mechanical problem.” I would look for creative sensor solutions before I would resort to workarounds in the code.
However if you broke the magnet in the shaft, you can just make sure that your wheel is facing forwards when you turn on the robot, and zero the NEO encoder on boot.