-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDriveTrain.cpp
More file actions
143 lines (120 loc) · 4.1 KB
/
DriveTrain.cpp
File metadata and controls
143 lines (120 loc) · 4.1 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "DriveTrain.h"
#define PI 3.14159265
DriveTrain::DriveTrain(uint32_t leftMasterDeviceID, uint32_t leftSlaveDeviceID, uint32_t rightMasterDeviceID, uint32_t rightSlaveDeviceID, Position *position_):
RobotDrive(leftMaster, rightMaster),
leftMaster(leftMasterDeviceID),
leftSlave(leftSlaveDeviceID),
rightMaster(rightMasterDeviceID),
rightSlave(rightSlaveDeviceID),
position(position_)
{
leftMaster.SetControlMode(CANTalon::ControlMode::kPercentVbus);
leftMaster.SetClosedLoopOutputDirection(true);
leftSlave.SetModeSelect(CanTalonSRX::kMode_SlaveFollower);
leftSlave.SetDemand(leftMasterDeviceID);
leftSlave.SetRevMotDuringCloseLoopEn(1);
rightMaster.SetControlMode(CANTalon::ControlMode::kPercentVbus);
rightSlave.SetControlMode(CANTalon::ControlMode::kFollower);
rightSlave.Set(Constants::driveRightMasterID);
}
void DriveTrain::Enable()
{
leftMaster.Enable();
rightMaster.Enable();
}
void DriveTrain::Disable()
{
leftMaster.Disable();
rightMaster.Disable();
}
//TODO: Take a sensitivity
void DriveTrain::TurnToAngle(float angle) //give angle in degrees
{
//angle *= -1;
angle = fmod(angle, 360.0);
angle = angle > 180 ? angle-360 : angle;
float currentAngle = position->GetAngleDegrees();
float k_p = SmartDashboard::GetNumber("PID_k_P", 0.75);
float k_i = SmartDashboard::GetNumber("PID_k_I", 1.00);
float p = 0;
float i = 0;
float delta_t = 0.1;
float errorDegrees = 0;
float errorScaled = 0;
float output = 0;
unsigned int failsafe = 0;
std::cout << "Running Loop";
while (abs(currentAngle - angle) > Constants::drivePIDepsilon && failsafe < 30)
{
std::cout << "PI Loop: " << std::endl;
std::cout << " P: " << p << std::endl;
std::cout << " I: " << i << std::endl;
std::cout << " Output: " << output << std::endl;
std::cout << " ErrorDegrees: " << errorDegrees << std::endl;
std::cout << " ErrorScaled: " << errorScaled << std::endl;
std::cout << " Target angle: " << angle << std::endl;
std::cout << " Current angle: " << currentAngle << std::endl;
std::cout << " Failsafe: " << failsafe << std::endl;
i = (i + k_i * errorScaled) * delta_t;
errorDegrees = currentAngle - angle;
if (abs(errorDegrees) > 180.0)
{
errorDegrees = errorDegrees - copysign(360.0, errorDegrees);
}
errorScaled = errorDegrees/180.0;
p = k_p * errorScaled;
output = p + i;
leftMaster.Set(output);
rightMaster.Set(-output);
currentAngle = position->GetAngleDegrees();
failsafe++;
Wait(delta_t);
}
leftMaster.Set(0);
rightMaster.Set(0);
}
void DriveTrain::TurnToRelativeAngle(float angle) {
TurnToAngle(angle + position->GetAngleDegrees());
}
void DriveTrain::DriveStraight(float speed, float fieldAngle, float timeInSeconds)
{
float currentAngle = position->GetAngle();
float k_p = Constants::driveK_P;
float k_i = Constants::driveK_I;
float p = 0;
float i = 0;
float delta_t = 0.01;
float error = 0;
float pidOutput = 0;
unsigned int failsafe = 0;
unsigned int failsafeMax = timeInSeconds / delta_t;
while (abs(currentAngle-fieldAngle) > Constants::drivePIDepsilon && failsafe < failsafeMax)
{
i = (i + k_i * error) * delta_t;
error = abs(fieldAngle - currentAngle);
if(error > 180.0)
error = error * -1;
error /= 180.0;
p = k_p * error;
pidOutput = p + i;
float combinedOutputLeft = tanh(speed + pidOutput);
float combinedOutputRight = tanh(speed - pidOutput);
combinedOutputLeft = combinedOutputLeft*0.9 + copysign(0.1, combinedOutputLeft);
combinedOutputRight = combinedOutputRight*0.9 + copysign(0.1, combinedOutputRight);
leftMaster.Set(combinedOutputLeft);
rightMaster.Set(combinedOutputRight);
currentAngle = position->GetAngleDegrees();
failsafe++;
Wait(delta_t);
}
leftMaster.Set(0);
rightMaster.Set(0);
}
void DriveTrain::MoveDistance(float distance, float speed) {
float xOffset = position->GetX();
float yOffset = position->GetY();
while (sqrt(pow(position->GetX() - xOffset, 2) + pow(position->GetY() - yOffset, 2)) < distance) {
leftMaster.Set(speed);
rightMaster.Set(speed);
}
}