1516X Push Back 1.0
1516X's robot code for the 2025-2026 VEX Robotics Competition
Loading...
Searching...
No Matches
velocityController.cpp
Go to the documentation of this file.
1#include "velocityController.h"
2#include <cmath>
3
4int VoltageController::sign(double x) {
5 return (x > 0) - (x < 0); // returns 1, 0, or -1
6}
7
8VoltageController::VoltageController(double kv, double kaStraight, double kaTurn, double ksStraight, double ksTurn, double kp,
9 double ki, double integralThreshold, double trackWidth)
10 : kV(kv), kaStraight(kaStraight), kaTurn(kaTurn), ksStraight(ksStraight), ksTurn(ksTurn), kP(kp), kI(ki),
11 integralThreshold(integralThreshold), trackWidth(trackWidth) {}
12
13DrivetrainVoltages VoltageController::update(double targetLinearVelocity, double targetAngularVelocity, double measuredLeftVelocity,
14 double measuredRightVelocity) {
15
16 double deltaW = (targetAngularVelocity - prevAngularVelocity) / 0.01;
17
18 double deltaV = (targetLinearVelocity - prevLinearVelocity) / 0.01;
19
20 prevAngularVelocity = targetAngularVelocity;
21 prevLinearVelocity = targetLinearVelocity;
22
23 double leftVelocity = targetLinearVelocity - targetAngularVelocity * (trackWidth / 2.0);
24 double rightVelocity = targetLinearVelocity + targetAngularVelocity * (trackWidth / 2.0);
25
26 double leftError = leftVelocity - measuredLeftVelocity;
27 double rightError = rightVelocity - measuredRightVelocity;
28
29 if ((leftError < 0) != (prevLeftError < 0)) {
30 leftIntegral = 0;
31 }
32 if (std::abs(leftError) < integralThreshold) {
33 leftIntegral += leftError * 0.01;
34 }
35 if ((rightError < 0) != (prevRightError < 0)) {
36 rightIntegral = 0;
37 }
38 if (std::abs(rightError) < integralThreshold) {
39 rightIntegral += rightError * 0.01;
40 }
41
42 double kaLeft = (kaStraight * deltaV) - (kaTurn * deltaW);
43 double kaRight = (kaStraight * deltaV) + (kaTurn * deltaW);
44
45 double ksLeft = (ksStraight * sign(leftVelocity)) - (ksTurn * sign(targetAngularVelocity));
46 double ksRight = (ksStraight * sign(rightVelocity)) + (ksTurn * sign(targetAngularVelocity));
47
48 double leftVoltage =
49 (kV * leftVelocity) + (kaLeft) + (ksLeft) + (kP * leftError) + (kI * leftIntegral);
50 double rightVoltage =
51 (kV * rightVelocity) + (kaRight) + (ksRight) + (kP * rightError) + (kI * rightIntegral);
52 double ratio = std::max(fabs(leftVoltage), fabs(rightVoltage)) / 12000.0;
53 if (ratio > 1) {
54 leftVoltage /= ratio;
55 rightVoltage /= ratio;
56 }
57
58 return {leftVoltage, rightVoltage};
59}