Any Gyro Sims Recommendations?

Currently working on an ideally ‘improved’ version of our ‘swerve drive sim’ in software.

Every time I go to make a gyro sim, I find myself basically creating a sim layer that simply follows what the rotation joystick does.

I’d like to instead maybe thing about some sort of model using the flywheel sim objects I have. But not sure how quite yet.

Anyone have any recommendations for how they have accomplished this? OR similar approaches?

If you have the module sims working, you can use the toTwist2d method on SwerveDriveKinematics to convert a set of module positions deltas into a Twist2d. This includes components for the position deltas in X, Y, and theta based purely on the modules. That method is what SwerveDriveOdometry uses internally, except it would normally just use the X and Y components since the rotation comes from the gyro. However, you can use the theta value to update a Rotation2d to track the rotation without a gyro. That rotation can then be passed to the normal odometry or pose estimator object. It would look something like this:

SwerveDriveKinematics kinematics = ...;
SwerveDriveOdometry odometry = ...;
Rotation2d simRotation = new Rotation2d();

periodic() {
    SwerveModulePosition[] wheelDeltas = ...;
    var twist = kinematics.toTwist2d(wheelDeltas);
    simRotation = simRotation.plus(new Rotation2d(twist.dtheta));
    odometry.update(simRotation, ...);
}
6 Likes

Woah learned something new, long while ago I wanted to get the rotation kinematics from the swerve module states and thought they only computed the XY position. Must have missed this method. Thanks!

That was new in 2023

1 Like

i am using this code and the robot just goes crazy when activated

this is the kinematic object

You need to give module deltas (change in position) to the toTwist2d method, not the current positions. So for each module, its delta is current position - last position

2 Likes

Can you please give an example using those functions from the pictures of my code?


I use this code now; but the robot loses it begining angle every time

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.