Search in sources :

Example 1 with SwerveState

use of me.wobblyyyy.pathfinder2.kinematics.SwerveState in project Pathfinder2 by Wobblyyyy.

the class SwerveDriveKinematics method calculate.

/**
 * Calculate a {@link SwerveState}, given a translation and a center
 * of rotation.
 *
 * @param translation      the translation to calculate a swerve state
 *                         based on.
 * @param centerOfRotation the center of rotation. To be entirely honest,
 *                         I have absolutely no idea what this means.
 * @return create a new swerve drive.
 */
public SwerveState calculate(Translation translation, PointXY centerOfRotation) {
    if (!centerOfRotation.equals(previousCenterOfRotation)) {
        for (int i = 0; i < moduleCount; i++) {
            PointXY modulePosition = modulePositions[i];
            double x = modulePosition.x() - centerOfRotation.x();
            double y = -modulePosition.y() + centerOfRotation.y();
            inverseKinematics.setRow(i * 2 + 0, 0, 1, 0, y);
            inverseKinematics.setRow(i * 2 + 1, 0, 0, 1, x);
        }
        previousCenterOfRotation = centerOfRotation;
    }
    SimpleMatrix speedVector = new SimpleMatrix(3, 1);
    speedVector.setColumn(0, 0, translation.vx(), translation.vy(), translation.vz());
    SimpleMatrix moduleStateMatrix = inverseKinematics.mult(speedVector);
    SwerveModuleState[] states = new SwerveModuleState[moduleCount];
    for (int i = 0; i < moduleCount; i++) {
        double x = moduleStateMatrix.get(i * 2 + 0, 0);
        double y = moduleStateMatrix.get(i * 2 + 1, 0);
        double speed = Math.hypot(x, y);
        Angle angle = Angle.atan2(y, x);
        states[i] = new SwerveModuleState(speed, angle);
    }
    return new SwerveState(states[0], states[1], states[2], states[3]);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) Angle(me.wobblyyyy.pathfinder2.geometry.Angle) PointXY(me.wobblyyyy.pathfinder2.geometry.PointXY)

Example 2 with SwerveState

use of me.wobblyyyy.pathfinder2.kinematics.SwerveState in project Pathfinder2 by Wobblyyyy.

the class SwerveChassisOdometry method getRawPosition.

@Override
public PointXYZ getRawPosition() {
    double currentTimeMs = Time.ms();
    Angle gyroAngle = getGyroAngle.get();
    SwerveModuleState frontRightState = frontRightOdometry.getState();
    SwerveModuleState frontLeftState = frontLeftOdometry.getState();
    SwerveModuleState backRightState = backRightOdometry.getState();
    SwerveModuleState backLeftState = backLeftOdometry.getState();
    SwerveState state = new SwerveState(frontRightState, frontLeftState, backRightState, backLeftState);
    return odometry.updateWithTime(currentTimeMs, gyroAngle, state);
}
Also used : Angle(me.wobblyyyy.pathfinder2.geometry.Angle) SwerveState(me.wobblyyyy.pathfinder2.kinematics.SwerveState) SwerveModuleState(me.wobblyyyy.pathfinder2.kinematics.SwerveModuleState)

Aggregations

Angle (me.wobblyyyy.pathfinder2.geometry.Angle)2 PointXY (me.wobblyyyy.pathfinder2.geometry.PointXY)1 SwerveModuleState (me.wobblyyyy.pathfinder2.kinematics.SwerveModuleState)1 SwerveState (me.wobblyyyy.pathfinder2.kinematics.SwerveState)1 SimpleMatrix (org.ejml.simple.SimpleMatrix)1