I can’t get my MK4i swerve modules to return to the desired position when starting autonomous mode and teleoperator, can anyone help?
I assume by position you mean angle they are pointing?
Are you using an encoder on the top of the module to track rotation?
i dont know how to use the top encoder
and yes, im talking about the angle
What swerve code are you using? In your swerve code, have you set absolute encoder offset values for each module? Typically, you need to physically align your wheels to straight forward, read the values of the absolute encoder on each module, and then put those four values into the code. This lets the code know what straight forward is for each wheel so it can then send the correct signals to turn the wheels to desired rotational positions.
Here’s how we made our swerve modules do an X pattern FRC2023/src/main/java/frc/robot/subsystems/swerve/Swerve.java at a5df0a4eaa5d5c560e4a8de5e19fa593f98beb76 · Frc5572/FRC2023 · GitHub
Depending on clearances with the chassis my team uses a long straight edge between the front wheel and back wheel (do left and right sides independently) or a jig/jam block between each wheel and chassis.
The wheel orientation has to be the same every time so pick if you are going to use motor invert or not on a side. You might pick some landmark such as the motor gear is always on the outside of the robot (use whatever makes sense for your swerve module).
What encoder are you using and how is it connected to the RIO? This will determine how you use it.
In order for the robot to keep track of where the modules are through power cycles you will need to record offsets. As others have said you will need to point all your modules in the same direction and record what values the encoders are at. You can hardcode these in your program or save them in Preferences.
Once you have the offset you will need to reference it on boot. You will need to read the value of the encoder and set the steer motor internal encoder value to it.
This is an example of a method we call on each module on boot. You may need to convert the measurements units depending on how your code currently works and what value your sensor returns.
public void resetToAbsolute() {
m_angle_offset = Preferences.getDouble("Module" + m_encoder_id, 0);
double absolutePosition = m_analogEncoder.getAbsolutePosition() - m_angle_offset;
m_steerMotor.setPosition(absolutePosition);
}
FYI, for those that have WCP Swerve Xi as we do, we noticed that the modules have a top plate hole above a threaded hole in the gear that lets you align the wheels dead nuts straight with a 2" #10-32 bolt. Other modules might have similar, I dunno.
You didn’t say what encoder you are using. Here’s one possible way to setup the offset for a CANCoder. The setup
method is ours to handle error/restart conditions so don’t be concerned with that initially.
public void setupMagnetOffset(double magnetOffset)
{
MagnetSensorConfigs magnetSensorConfigs = new MagnetSensorConfigs();
setup(() -> cancoder.getConfigurator().refresh(magnetSensorConfigs), "Refresh MagnetOffset_" + magnetOffset);
magnetSensorConfigs.MagnetOffset = magnetOffset;
setup(() -> cancoder.getConfigurator().apply(magnetSensorConfigs), "Setup MagnetOffset_" + magnetOffset);
}