power-play/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/kinematics/AccelLimiter.java

49 lines
1.5 KiB
Java
Raw Normal View History

2023-01-25 23:27:08 -06:00
package org.firstinspires.ftc.teamcode.kinematics;
import com.acmerobotics.dashboard.config.Config;
import com.qualcomm.robotcore.util.Range;
2023-01-25 23:39:29 -06:00
import org.firstinspires.ftc.teamcode.Hardware;
2023-01-25 23:27:08 -06:00
@Config
public class AccelLimiter {
2023-01-25 23:39:29 -06:00
private final Hardware hardware;
2023-01-25 23:27:08 -06:00
private double posPower;
private double yawPower;
private double lastUpdateTime;
public static double MAX_POS_ACCELERATION = 13;
public static double MAX_YAW_ACCELERATION = 1;
private static final double NANOS_PER_SEC = 1000 * 1000 * 1000;
2023-01-25 23:39:29 -06:00
public AccelLimiter(Hardware hardware) {
this.hardware = hardware;
2023-01-25 23:27:08 -06:00
this.posPower = 0;
this.yawPower = 0;
}
public void move(double powerX, double powerY, double turn, double speed) {
double hypot = Math.hypot(powerX, powerY);
double time = System.nanoTime() / NANOS_PER_SEC;
double dt = time - this.lastUpdateTime;
double posLimit = MAX_POS_ACCELERATION * dt;
double yawLimit = MAX_YAW_ACCELERATION * dt;
this.posPower += Range.clip(hypot - this.posPower, -posLimit, posLimit);
this.yawPower += Range.clip(turn - this.yawPower, -yawLimit, yawLimit);
2023-01-25 23:39:29 -06:00
this.hardware.move(powerX / hypot * this.posPower, powerY / hypot * this.posPower, this.yawPower, speed);
2023-01-25 23:27:08 -06:00
this.lastUpdateTime = time;
}
public void stop() {
// yes, yes, i know this isnt good, but for now its only called when power values are low anyway
2023-01-25 23:39:29 -06:00
this.hardware.stop();
2023-01-25 23:27:08 -06:00
}
}