Skip to content

Commit 75ed577

Browse files
committed
Initial Code Commit
1 parent 8fa1f3e commit 75ed577

76 files changed

Lines changed: 6991 additions & 140 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

TeamCode/src/main/java/org/firstinspires/ftc/blackice/DEVELOPER_NOTES.md

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.firstinspires.ftc.blackice;
2+
3+
import com.qualcomm.robotcore.hardware.DcMotorSimple;
4+
5+
import org.firstinspires.ftc.blackice.core.control.VelocityController;
6+
import org.firstinspires.ftc.blackice.core.hardware.localization.localizers.GoBildaPinpointDriver;
7+
import org.firstinspires.ftc.blackice.core.hardware.localization.localizers.LocalizerConfig;
8+
import org.firstinspires.ftc.blackice.core.hardware.localization.localizers.PinpointConfig;
9+
import org.firstinspires.ftc.blackice.core.hardware.drivetrain.DrivetrainConfig;
10+
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;
11+
import org.firstinspires.ftc.blackice.core.control.QuadraticDampedPIDController;
12+
import org.firstinspires.ftc.blackice.core.follower.FollowerConfig;
13+
import org.firstinspires.ftc.blackice.core.control.PIDFController;
14+
import org.firstinspires.ftc.blackice.core.paths.PathBehavior;
15+
import org.firstinspires.ftc.blackice.core.hardware.drivetrain.MecanumConfig;
16+
17+
/**
18+
* Set default configuration for all OpModes using the Follower.
19+
*/
20+
public final class FollowerConstants {
21+
public static LocalizerConfig localizerConfig = new PinpointConfig()
22+
.distanceUnit(DistanceUnit.INCH)
23+
.podDirection(
24+
GoBildaPinpointDriver.EncoderDirection.REVERSED,
25+
GoBildaPinpointDriver.EncoderDirection.REVERSED)
26+
.podOffset(-36, 0);
27+
28+
public static DrivetrainConfig drivetrainConfig = new MecanumConfig()
29+
.frontLeft("frontLeft", DcMotorSimple.Direction.REVERSE)
30+
.backLeft("backLeft", DcMotorSimple.Direction.REVERSE)
31+
.frontRight("frontRight", DcMotorSimple.Direction.REVERSE)
32+
.backRight("backRight", DcMotorSimple.Direction.FORWARD)
33+
.maxForwardSpeed(60)
34+
.maxStrafeSpeed(45);
35+
36+
public static PathBehavior defaultPathBehavior = new PathBehavior()
37+
.continueMomentumAtEnd()
38+
.setStuckTimeoutSeconds(3.0)
39+
.setPauseTimeoutSeconds(5.0)
40+
.setTimeoutSeconds(8.0);
41+
42+
public static FollowerConfig defaultFollowerConfig = new FollowerConfig()
43+
.localizerConfig(localizerConfig)
44+
.drivetrainConfig(drivetrainConfig)
45+
.defaultPathBehavior(defaultPathBehavior)
46+
.headingPID(new PIDFController(2, 0, 0.1, 0))
47+
.positionalPID(new QuadraticDampedPIDController(0.5, 0.07, 0.001))
48+
.driveVelocityController(new VelocityController(0.01, 0.0159, 0.04, 40)) // 34
49+
// only needed for following velocity profiles.
50+
.maxReversalBrakingPower(0.3)
51+
.centripetalFeedforward(0.005) // only needed for following curves
52+
.stopIfVoltageBelow(7);
53+
}
54+
55+
// ! https://visualizer.pedropathing.com
56+
57+
//public static VelocityToStoppingDistanceVectorModel BRAKING_DISPLACEMENT =
58+
// new VelocityToStoppingDistanceVectorModel(
59+
// //Drift, braking force
60+
// new QuadraticLinearBrakingModel(0.00112, 0.07316),
61+
// new QuadraticLinearBrakingModel(0.00165, 0.05054)
62+
// );
63+
64+
//
65+
//Heading 0 is the front side of the robot facing away from the starting wall
66+
//positive X-axis is forward for the robot with 0 heading (facing the center of the field)
67+
//positive Y-axis is strafing the robot to the left from 0 heading
68+
//
69+
//This is the axis with the robot's HEADING AT 0 DEGREES: The robot is touching the wall
70+
// ```
71+
// ^ +Y Axis (Robot Left Strafing)
72+
//│
73+
// │ ┌──────────┐
74+
// │ │ │
75+
// │ │ │--> FRONT OF ROBOT (FORWARD)
76+
//│ │ │
77+
// │ └──────────┘
78+
// └──────────────> +X Axis (Robot Forward)
79+
//```

TeamCode/src/main/java/org/firstinspires/ftc/blackice/README.md

Lines changed: 0 additions & 98 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.firstinspires.ftc.blackice.core.control;
2+
3+
/**
4+
* PIDController implements a basic Proportional-Integral-Derivative control algorithm.
5+
* It calculates an output value based on the error between a target and current value,
6+
* with optional integral and derivative terms.
7+
*/
8+
public class PIDController {
9+
double kP, kI, kD;
10+
double previousError, integralSum;
11+
12+
public PIDController(double kP, double kI, double kD) {
13+
this.kP = kP;
14+
this.kI = kI;
15+
this.kD = kD;
16+
}
17+
18+
public double run(double target, double current, double deltaTime) {
19+
double error = target - current;
20+
double errorRate = kD != 0 ? (error - previousError) / deltaTime : 0;
21+
return runFromError(error, errorRate);
22+
}
23+
24+
public double runFromError(double error, double derivative) {
25+
double output = kP * error;
26+
27+
if (kI != 0) {
28+
integralSum += error;
29+
output += kI * integralSum;
30+
}
31+
if (kD != 0) output += kD * derivative;
32+
33+
previousError = error;
34+
return output;
35+
}
36+
37+
public void reset() {
38+
previousError = 0;
39+
integralSum = 0;
40+
}
41+
42+
public PIDController setCoefficients(double kP, double kI, double kD) {
43+
this.kP = kP;
44+
this.kI = kI;
45+
this.kD = kD;
46+
return this;
47+
}
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.firstinspires.ftc.blackice.core.control;
2+
3+
4+
/**
5+
* Represents a PIDF (Proportional-Integral-Derivative-Feedforward) controller.
6+
*/
7+
public class PIDFController extends PIDController {
8+
@FunctionalInterface
9+
public interface Feedforward {
10+
double compute(double target);
11+
}
12+
13+
public Feedforward feedforward;
14+
15+
public PIDFController(double kP, double kI, double kD, double feedforwardGain) {
16+
this(kP, kI, kD, target -> target * feedforwardGain);
17+
}
18+
19+
public PIDFController(double kP, double kI, double kD, Feedforward feedforward) {
20+
super(kP, kI, kD);
21+
this.feedforward = feedforward;
22+
}
23+
24+
@Override
25+
public double run(double target, double current, double deltaTime) {
26+
return super.run(target, current, deltaTime) + feedforward.compute(target);
27+
}
28+
29+
public PIDFController setCoefficients(double kP, double kI, double kD, Feedforward feedforward) {
30+
this.kP = kP;
31+
this.kI = kI;
32+
this.kD = kD;
33+
this.feedforward = feedforward;
34+
return this;
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.firstinspires.ftc.blackice.core.control;
2+
3+
/**
4+
* A PD controller with quadratic damping **quadratic-damped PIDs** to model the non-linear effects of friction and momentum.but more empirical. Also has basically a
5+
* Derivative of
6+
* Derivative term or velocity^2
7+
* term to help stop more smoothly because EMF braking is not perfectly quadratic or linear.
8+
* Also is predictive because D term compensates for predictedPosition = position + velocity * deltaTime.
9+
*/
10+
public class QuadraticDampedPIDController extends PIDController {
11+
private double kBraking;
12+
private double kFriction;
13+
14+
public QuadraticDampedPIDController(double kP, double kLinearBraking,
15+
double kQuadraticFriction) {
16+
super(kP, 0, 0);
17+
this.kBraking = kLinearBraking;
18+
this.kFriction = kQuadraticFriction;
19+
}
20+
21+
@Override
22+
public double runFromError(double error, double derivative) {
23+
double velocity = -derivative;
24+
double predictedBrakingDisplacement = velocity * Math.abs(velocity) * kFriction + velocity *
25+
kBraking;
26+
return super.runFromError(
27+
error - predictedBrakingDisplacement,
28+
derivative
29+
);
30+
}
31+
32+
public QuadraticDampedPIDController setCoefficients(double kP, double kLinearBraking,
33+
double kQuadraticFriction) {
34+
this.kP = kP;
35+
this.kBraking = kLinearBraking;
36+
this.kFriction = kQuadraticFriction;
37+
return this;
38+
}
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.firstinspires.ftc.blackice.core.control;
2+
3+
4+
import org.firstinspires.ftc.blackice.util.Validator;
5+
6+
/**
7+
* A PIDF (Proportional-Integral-Derivative-Feedforward) controller that controls
8+
* velocity.
9+
* <p>
10+
* Does not use Integral or Derivative terms as they are usually unnecessary
11+
* for this controller and can often add instability unless tuned appropriately.
12+
* <p>
13+
* Uses feedforward based on {@code kA} and {@code kS} (velocity and static gain).
14+
* Uses momentum damping based off the robot's natural deceleration instead of {@code
15+
* kA} (acceleration gain).
16+
*/
17+
public class VelocityController extends PIDFController {
18+
public double naturalDeceleration;
19+
20+
/**
21+
* Creates a simple velocity controller of a proportional and feedforward based
22+
* on kV and kS (velocity and static gain).
23+
* <p>
24+
* Does not use Integral or Derivative terms as they are usually unnecessary
25+
* for this controller and can often add instability unless tuned appropriately.
26+
* Uses momentum damping based off the robot's natural deceleration instead of {@code
27+
* kA} (acceleration gain).
28+
*
29+
* @param kP the proportional gain, how much power the robot reacts to error.
30+
* Usually around 0.01.
31+
* @param kV the velocity gain, how much power the robot requires per
32+
* velocity. Should be around 1/maxVelocity or 1/65 ~= 0.015
33+
* @param kS the static gain, the minimum power required to start moving.
34+
* Should be determined experimentally. Should be around 0.05.
35+
* @param naturalDeceleration the natural deceleration of your robot (in/s^2).
36+
* Should be positive. Used for momentum damping.
37+
*/
38+
public VelocityController(double kP, double kV, double kS,
39+
double naturalDeceleration) {
40+
super(kP, 0, 0, target -> target * kV + kS);
41+
this.naturalDeceleration = Validator.ensurePositiveSign(naturalDeceleration);
42+
}
43+
44+
public double run(double targetVelocity, double currentVelocity,
45+
double feedforwardVelocity) {
46+
double error = targetVelocity - currentVelocity;
47+
return runFromError(error, 0) + feedforward.compute(feedforwardVelocity);
48+
}
49+
50+
public VelocityController setCoefficients(double kP, double kV, double kS, double naturalDeceleration) {
51+
this.kP = kP;
52+
this.feedforward = target -> target * kV + kS;
53+
this.naturalDeceleration = naturalDeceleration;
54+
return this;
55+
}
56+
}

0 commit comments

Comments
 (0)