forked from Team254/FRC-2019-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kinematics.java
55 lines (47 loc) · 2.39 KB
/
Kinematics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.team254.frc2019;
import com.team254.lib.geometry.Pose2d;
import com.team254.lib.geometry.Rotation2d;
import com.team254.lib.geometry.Twist2d;
import com.team254.lib.util.DriveSignal;
/**
* Provides forward and inverse kinematics equations for the robot modeling the wheelbase as a differential drive (with
* a corrective factor to account for skidding).
*/
public class Kinematics {
private static final double kEpsilon = 1E-9;
/**
* Forward kinematics using only encoders, rotation is implicit (less accurate than below, but useful for predicting
* motion)
*/
public static Twist2d forwardKinematics(double left_wheel_delta, double right_wheel_delta) {
double delta_rotation = (right_wheel_delta - left_wheel_delta) / (Constants.kDriveWheelTrackWidthInches * Constants.kTrackScrubFactor);
return forwardKinematics(left_wheel_delta, right_wheel_delta, delta_rotation);
}
public static Twist2d forwardKinematics(double left_wheel_delta, double right_wheel_delta, double delta_rotation_rads) {
final double dx = (left_wheel_delta + right_wheel_delta) / 2.0;
return new Twist2d(dx, 0.0, delta_rotation_rads);
}
public static Twist2d forwardKinematics(Rotation2d prev_heading, double left_wheel_delta, double right_wheel_delta,
Rotation2d current_heading) {
final double dx = (left_wheel_delta + right_wheel_delta) / 2.0;
final double dy = 0.0;
return new Twist2d(dx, dy, prev_heading.inverse().rotateBy(current_heading).getRadians());
}
/**
* For convenience, integrate forward kinematics with a Twist2d and previous rotation.
*/
public static Pose2d integrateForwardKinematics(Pose2d current_pose,
Twist2d forward_kinematics) {
return current_pose.transformBy(Pose2d.exp(forward_kinematics));
}
/**
* Uses inverse kinematics to convert a Twist2d into left and right wheel velocities
*/
public static DriveSignal inverseKinematics(Twist2d velocity) {
if (Math.abs(velocity.dtheta) < kEpsilon) {
return new DriveSignal(velocity.dx, velocity.dx);
}
double delta_v = Constants.kDriveWheelTrackWidthInches * velocity.dtheta / (2 * Constants.kTrackScrubFactor);
return new DriveSignal(velocity.dx - delta_v, velocity.dx + delta_v);
}
}