-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAccumulator.cpp
More file actions
312 lines (267 loc) · 12.4 KB
/
Accumulator.cpp
File metadata and controls
312 lines (267 loc) · 12.4 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Accumulator.cpp
*
* Created on: Feb 7, 2017
* Author: FRC3941
*/
#include "Accumulator.h"
DoubleDouble Accumulator::drive(bool hasBeenGoing, bool encoderPos, bool powerPos, bool goingPos, float target, float targetAngle, float targetError, float robotAngle)
{
//target
//target error
float leftus = leftProx->GetRangeInches();
float rightus = rightProx->GetRangeInches();
float usAverage = 0;
if (leftus == 0 && rightus != 0)
usAverage = rightus;
else if (rightus == 0 && leftus != 0)
usAverage = leftus;
else if (rightus != 0 && leftus != 0)
usAverage = (leftus + rightus) / 2.0;
float visualDistance = aimer->GetDistanceToGear();
SmartDashboard::PutNumber("Joe visual distance", visualDistance );
SmartDashboard::PutNumber("Joe us average", usAverage);
float currentAngleError = targetAngle - robotAngle + lastCameraAngle;
if (!hasBeenGoing)
{
encoderY->Reset();
float intitialAngleError = targetAngle - robotAngle;
reset(aimer->GetXDistanceToGear(usAverage, intitialAngleError), usAverage, robotAngle);
SmartDashboard::PutNumber("Joe start y position", getCurrentPosition().y);
lastLeftProxValue = leftProx->GetRangeInches();
lastRightProxValue = rightProx->GetRangeInches();
float cameraAngle = aimer->GetAngleToGear(usAverage);
if(fabs(cameraAngle)< 30){
lastCameraAngle = aimer->GetAngleToGear(usAverage);
}else{
lastCameraAngle = targetAngle;
}
SmartDashboard::PutNumber("Joe returned angle from aimer", lastCameraAngle);
updateUS(leftProx->GetRangeInches(), 0);
lastLeftProxValue = leftProx->GetRangeInches();
float adjustedAngle = targetAngle - robotAngle + lastCameraAngle;
float currentAngleError2 = aimer->ConvertToPlusMinus180(adjustedAngle);
if (fabs(robotAngle - targetAngle) <= 2)
updateX(robotAngle - targetAngle);
lastCameraAngle = aimer->GetAngleToGear(history.back().y);
}
if(fabs(lastLeftProxValue - leftProx->GetRangeInches()) > 1 && leftProx->GetRangeInches() != 0){
SmartDashboard::PutString("Joe Status", "new left ultrasonic");
updateUS(leftProx->GetRangeInches(), 0);
lastLeftProxValue = leftProx->GetRangeInches();
}
if(fabs(lastRightProxValue - rightProx->GetRangeInches()) > 1 && rightProx->GetRangeInches() != 0){
SmartDashboard::PutString("Joe Status", "new right ultrasoinic");
updateUS(rightProx->GetRangeInches(), 1);
lastRightProxValue = rightProx->GetRangeInches();
}
if (lastCameraAngle != aimer->GetAngleToGear(history.back().y) && fabs(getCurrentPosition().y) >= 24)
{
float gearAngle = aimer-> GetAngleToGear(history.back().y);
if(fabs(gearAngle) < 30){
SmartDashboard::PutNumber("Joe returned angle from aimer", gearAngle);
currentAngleError = aimer->ConvertToZeroTo360(gearAngle - robotAngle);
SmartDashboard::PutNumber("Joe angleError", currentAngleError);
SmartDashboard::PutString("Joe Status", "new camera angle");
updateX(robotAngle - targetAngle);
lastCameraAngle = aimer->GetAngleToGear(history.back().y);
}
}
currentAngleError = targetAngle - robotAngle + lastCameraAngle;
SmartDashboard::PutNumber("Joe angleError", currentAngleError);
SmartDashboard::PutNumber("joe test last camera angle", lastCameraAngle);
SmartDashboard::PutNumber("joe test get angle to gear", aimer->GetAngleToGear(history.back().y));
float robotAngleError = fabs(targetAngle - robotAngle);
float driveX = 0.0;
float driveY = 0.0;
float driveZ = 0.0;
float powerX = 0.0;
float power = 0.0;
if(true){
float basicXP = SmartDashboard::GetNumber("Joe XP", Constants::accumulatorXp);
float tempXPower = -basicXP * (getCurrentPosition().x + SmartDashboard::GetNumber("Joe x offset", 0)); //This is the power calculator for the x movement
//Fixing the power
if (tempXPower > 1)
powerX = 1.0;
else if (tempXPower < Constants::minStrafePower && tempXPower > 0.05)
powerX = Constants::minStrafePower;
else if (tempXPower > -Constants::minStrafePower && tempXPower < 0.05)
powerX = -Constants::minStrafePower;
else if (tempXPower < -1)
powerX = -1.0;
else
powerX = tempXPower;
if (fabs(getCurrentPosition().x + SmartDashboard::GetNumber("Joe x offset", 0)) < 1 || fabs(getCurrentPosition().y) < 36)
powerX = 0.0;
float yscaleFactor = (fabs(getCurrentPosition().x + SmartDashboard::GetNumber("Joe x offset", 0) )<4)? 1: (fabs(getCurrentPosition().y )<= 42 && fabs(getCurrentPosition().y) >= 36)? 0 : .5;
float distance = encoderY->GetDistance();
float lastDistanceTraveled = distance - lastEncoderDistance;
float currentY = getCurrentPosition().y;
float distanceError = currentY + (lastDistanceTraveled * ((goingPos) ? -1 : 1) * ((encoderPos) ? 1 : -1)) - target;
float joeP = SmartDashboard::GetNumber("Joe P-value", (Constants::accumulatorPower));
float tempPower = -fabs(joeP) * ((powerPos) ? 1 : -1) * distanceError * yscaleFactor;
if (tempPower > 1)
power = 1;
else if (tempPower < Constants::minForwardPower && tempPower > 0)
power = Constants::minForwardPower;
else if (tempPower > -Constants::minForwardPower && tempPower < 0)
power = -.13;
else if (tempPower < -1)
power = -1;
else
power = tempPower;
if (fabs(distanceError) < targetError)
power = 0;
update(lastDistanceTraveled, power, powerX, robotAngle);//Update all of the powers with the accumulating
//robotDrive.MecanumDrive_Cartesian(0,power,0);
SmartDashboard::PutNumber("Joe Motor Power", power);
SmartDashboard::PutNumber("Joe Distance", distance);
SmartDashboard::PutNumber("Joe last Distance", lastDistanceTraveled);
SmartDashboard::PutNumber("Joe Error", error);
SmartDashboard::PutNumber("Joe X error", errorX);
SmartDashboard::PutNumber("Joe current y position", currentY);
SmartDashboard::PutNumber("Joe current x position", getCurrentPosition().x + SmartDashboard::GetNumber("Joe x offset", 0));
}
SmartDashboard::PutBoolean("Joe in loop", true);
// float gearAngle = aimer->TwoCameraAngleFilter() + robotAngle;
// gearAngle = gearAngle < 360 ? gearAngle : gearAngle - 360; //scaling
// gearAngle = gearAngle > 0 ? gearAngle : gearAngle + 360;
// pid->resetPIDAngle(); //if loop is done reset values
robotAngle = robotAngle < 0 ? 360 + robotAngle : robotAngle;
float angleOutput = pid->PIDAngle(robotAngle, targetAngle);
//driveX = (fabs(robotAngle - targetAngle) > 2 && fabs(robotAngle - targetAngle - 360) < 358) ? 0 : powerX;
//driveY = (fabs(robotAngle - targetAngle) > 2 && fabs(robotAngle - targetAngle - 360) < 358) ? 0 : power;
driveX = powerX;
driveY = power;
driveZ = angleOutput;
lastEncoderDistance = encoderY->GetDistance();
SmartDashboard::PutNumber("robot angle - target angle", robotAngle - targetAngle);
SmartDashboard::PutNumber("robot angle - target angle - 360", robotAngle - targetAngle - 360);
//SmartDashboard::PutString("Joe Status", "in try loop");
//return DoubleDouble(0, 0, 0);
driveX = fabs(driveX) > .5 ? std::copysign(.5, driveX) : driveX; //if angleOutput is above max, set to max
driveY = fabs(driveY) > .6 ? std::copysign(.6, driveY) : driveY; //if angleOutput is above max, set to max
return DoubleDouble(driveX, driveY, driveZ);
//return DoubleDouble(driveX, 0, driveZ);
}
//PLEASE IGNORE THIS CRUD
DoubleDouble Accumulator::driveToTower(bool hasBeenGoing, bool encoderPos, bool powerPos, bool goingPos, float target, float targetAngle, float targetError, float robotAngle)
{
if (!(aimer->GetBoilerAngle() > 500) && !(aimer->GetBoilerDistance() > 500))
{
float boilerAngle = aimer->GetBoilerAngle();
float boilerDistance = aimer->GetBoilerDistance();
if (!hasBeenGoing)
{
encoderY->Reset();
encoderX->Reset();
float intitialAngleError = targetAngle - robotAngle;
reset(0, boilerDistance, boilerAngle);
lastRightProxValue = rightProx->GetRangeInches();
if(fabs(boilerAngle)< 30){
lastCameraAngle = boilerAngle;
}else{
lastCameraAngle = targetAngle;
}
SmartDashboard::PutNumber("Joe returned angle from aimer", lastCameraAngle);
updateUS(leftProx->GetRangeInches(), 0);
lastLeftProxValue = leftProx->GetRangeInches();
float adjustedAngle = targetAngle - robotAngle + lastCameraAngle;
float currentAngleError2 = aimer->ConvertToPlusMinus180(adjustedAngle);
updateX(robotAngle - targetAngle);
lastCameraAngle = aimer->GetAngleToGear(history.back().y);
}
return DoubleDouble(0,0,0);
}
else
return DoubleDouble(0,0,0);
}
void Accumulator::update(float encoder, float power, float powerX, float angle)
{
// history.push(DoubleDouble(0,average + history.back().y + error,0));
// prevPredicted = predict(power);
//
//
float lastY = history.back().y;
float lastDistancePower = Constants::yDistancePerSecond * power * Constants::teleopLoopTime;
SmartDashboard::PutNumber("Joe last power distance accum", lastDistancePower);
float averageDistance = (encoder + lastDistancePower)/2;
SmartDashboard::PutNumber("Joe average distance accum", averageDistance);
float lastX = history.back().x;
float xPowerDistance = Constants::xDistancePerSecond * powerX * Constants::teleopLoopTime;//Increments the accumulator for the x
SmartDashboard::PutNumber("joe X predicted Entry", xPowerDistance + lastX);
history.push_back(DoubleDouble(xPowerDistance + lastX, lastY + averageDistance, angle));
}
void Accumulator::updateUS(float ultrasonic, int id)
{
if (ultrasonic < 120)
{
float lastY = history[(history.size() + lastUpdated[id]) / 2].y;
error = ultrasonic - lastY;
SmartDashboard::PutNumber("Joe last us y accum", lastY);
//SmartDashboard::PutNumber("history size", history.size());
int intermediateLastUpdate = lastUpdated[id];
int historyLastIndex = history.size()-1;
if(intermediateLastUpdate < historyLastIndex && intermediateLastUpdate <= (history.size() -1)){
float yValueInHistory = history[intermediateLastUpdate].y;
float errorAtHistory = ultrasonic - yValueInHistory;
SmartDashboard::PutNumber("joe y history error", errorAtHistory);
//SmartDashboard::PutString("Joe updatedVis status", "updating");
float lasty = history.back().y;
for (int i = intermediateLastUpdate; i < history.size(); i++){
float yPosit = history[i].y;
history[i].y = yPosit + errorAtHistory;
}
float latestYValue = history.back().y;
SmartDashboard::PutNumber("joe lasty in ydistance", latestYValue);
//SmartDashboard::PutString("Joe updatedVis status", "updated History");
}else if(history.size() == 1){
history.back().y = ultrasonic;
//SmartDashboard::PutNumber("joe lastx in xdistance", history.back().x);
}
// }
lastUpdated[id] = history.size() - 1;
}
}
void Accumulator::updateX(float currentAngleError)//updates the x based on the camera angles
{
float lastX = history[lastUpdated.back()].x;
float dist = aimer->GetXDistanceToGear(history.back().y, currentAngleError);
SmartDashboard::PutNumber("Joe y in xUpdate", history.back().y);
SmartDashboard::PutNumber("Joe x update from last x", lastX);
if(dist < 140)
{
SmartDashboard::PutNumber("Joe x distance from gear", dist);
errorX = dist;// - lastX;
lastUpdated.back() = history.size() - 1;
int lastUpdatedIndex = lastUpdated.back();
int historyLastIndex = history.size()-1;
if(lastUpdatedIndex < historyLastIndex && lastUpdatedIndex <= (history.size() -1)){
float xValueInHistory = history[lastUpdatedIndex].x;
float errorAtHistory = dist - xValueInHistory;
SmartDashboard::PutNumber("joe x history error", errorAtHistory);
SmartDashboard::PutString("Joe updatedVis status", "updating");
float lastX = history.back().x;
for (int i = lastUpdatedIndex; i < history.size(); i++){
float xPosit = history[i].x;
history[i].x = xPosit + errorAtHistory;
}
float latestXValue = history.back().x + SmartDashboard::GetNumber("Joe x offset", 0);
SmartDashboard::PutNumber("joe lastx in xdistance", latestXValue);
SmartDashboard::PutString("Joe updatedVis status", "updated History");
}else if(history.size() == 1){
history.back().x = dist;
SmartDashboard::PutNumber("joe lastx in xdistance", history.back().x);
}
}
}
float Accumulator::GetXHistoryOffset(float targetAngle){
float result = 0;
return result;
}
void Accumulator::reset(float x, float y, float z)
{
history.clear();
prevPredicted = error = 0.0;
history.push_back(DoubleDouble((x > 100) ? 0 : x,y,z));
}