-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPositionControl.cpp
More file actions
232 lines (172 loc) · 6.39 KB
/
Copy pathPositionControl.cpp
File metadata and controls
232 lines (172 loc) · 6.39 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
#include "PositionControl.h"
PositionControl::PositionControl(){
}
PositionControl::~PositionControl(){
}
void PositionControl::moveForward(){
this->actionInProgress = true;
this->speed = DEFAULT_DRIVE_SPEED;
drive_rampStep(this->speed + this->speedCorrectionLeft, this->speed + this->speedCorrectionRight);
sprint(loggerBuffer, "K9 moving forward(speed left: %f, speed right: %f)", this->speed + this->speedCorrectionLeft, this->speed + this->speedCorrectionRight);
Logger::getInstance().log(loggerBuffer);
}
void PositionControl::moveBackward(){
this->actionInProgress = true;
this->speed = DEFAULT_DRIVE_SPEED;
drive_rampStep((this->speed + this->speedCorrectionLeft) * (-1), (this->speed + this->speedCorrectionRight) * (-1));
sprint(loggerBuffer, "K9 moving backwards(speed left: %f, speed right: %f)", (this->speed + this->speedCorrectionLeft) * (-1), (this->speed + this->speedCorrectionRight) * (-1));
Logger::getInstance().log(loggerBuffer);
}
void PositionControl::stop(){
// Start ramping to 0 speed
this->speed = 0;
drive_rampStep(this->speed, this->speed);
static int prevTicksLeft = -1;
static int prevTicksRight = -1;
// Get current ticks
int currentLeft;
int currentRight;
drive_getTicks(¤tLeft, ¤tRight);
// For initialisation of prevTicksLeft and prevTicksRight
if(prevTicksLeft == -1){
prevTicksLeft = currentLeft - 1; // - 1 for making sure the first pass in this function wont set the actionInProgress flag to false;
prevTicksRight = currentRight - 1; // - 1 for making sure the first pass in this function wont set the actionInProgress flag to false;
}
// Log entry
Logger::getInstance().log("K9 ramping down");
// Set actionInProgress flag to false when completely stopped
if(currentLeft - prevTicksLeft == 0 && currentRight - prevTicksRight == 0){
this->actionInProgress = false;
Logger::getInstance().log("K9 stopped");
}
// Set prev ticks current ticks
prevTicksLeft = currentLeft;
prevTicksRight = currentRight;
}
void PositionControl::turn(Direction direction){
}
void PositionControl::correct(float correctionLeft, float correctionRight){
this->speedCorrectionLeft = correctionLeft;
this->speedCorrectionRight = correctionRight;
sprint(loggerBuffer, "Updating PositionControl correction(left: %f, right: %f)", correctionLeft, correctionRight);
Logger::getInstance().log(loggerBuffer);
}
void PositionControl::correctAngle(int radials){
}
bool PositionControl::getMoveForwardFlag(){
return this->moveForwardFlag;
}
bool PositionControl::getMoveBackwardFlag(){
return this->moveBackwardFlag;
}
bool PositionControl::getStopFlag(){
return this->stopFlag;
}
CoordSet PositionControl::getCurrentPosition(){
return this->currentPosition;
}
void PositionControl::setCurrentPosition(CoordSet position){
this->currentPosition = position;
}
CoordSet PositionControl::getPrevPosition(){
return this->prevPosition;
}
void PositionControl::setPrevPosition(CoordSet position){
this->prevPosition = position;
}
CoordSet PositionControl::getTargetPosition(){
return this->targetPosition;
}
void PositionControl::setTargetPosition(CoordSet position){
this->targetPosition = position;
}
Direction PositionControl::getCurrentOrientation(){
return this->currentOrientation;
}
void PositionControl::setCurrentOrientation(Direction orientation){
this->currentOrientation = orientation;
}
Direction PositionControl::getTargetOrientation(){
return this->targetOrientation;
}
void PositionControl::setTargetOrientation(Direction orientation){
this->targetOrientation = orientation;
}
int PositionControl::getSpeed(){
return this->speed;
}
void PositionControl::setSpeed(int speed){
this->speed = speed;
}
int PositionControl::getCurrentTicks(){
return this->currentTicks;
}
void PositionControl::setCurrentTicks(int ticks){
this->currentTicks = ticks;
}
int PositionControl::getPrevTicks(){
}
void PositionControl::setPrevTicks(int ticks){
}
int PositionControl::getTargetTicks(){
return this->targetTicks;
}
void PositionControl::setTargetTicks(int ticks){
this->targetTicks = ticks;
}
bool PositionControl::isAtTargetPosition(){
if(currentPosition.x == targetPosition.x && currentPosition.y == targetPosition.y)
return true;
return false;
}
int PositionControl::calcSpeedFromTicks(){
}
void PositionControl::update(){
static int prevTicksLeft;
static int prevTicksRight;
if(this->actionInProgress){
// Get current ticks
int currentLeft,
currentRight;
drive_getTicks(¤tLeft, ¤tRight);
// For initialisation of prevTicksLeft and prevTicksRight(static int is always initialized to 0 by compiler!)
if(prevTicksLeft == 0 && prevTicksRight == 0){
prevTicksLeft = currentLeft;
prevTicksRight = currentRight;
}
////////////////////////////////////////////////////////
// TODO: Implement code to take corrections into account
////////////////////////////////////////////////////////
/*
// - Check if correction flag is true
// - Calculate difference between left and right
// - Calculate correction on local Y at the center of k9 based on difference between left and right. Use left as base for corection
// - Calculate correction on local X at center of k9
*/
float correctionX,
correctionY;
// Calulate ticks between previous and current ticks
int diffLeft = currentLeft - prevTicksLeft;
int diffRight = currentRight - prevTicksRight;
//////////////////////////////////////////////////////////////////
// TODO: Check if add/substaction of the corrections is done right
//////////////////////////////////////////////////////////////////
// Update current position based on the orientation
switch(this->currentOrientation){
case NORTH: this->currentPosition.x + correctionX;
this->currentPosition.y + (this->actionTicksLeft * TRAVEL_DISTANCE_PER_TICK) + correctionY;
break;
case EAST: this->currentPosition.x + (this->actionTicksLeft * TRAVEL_DISTANCE_PER_TICK) + correctionY;
this->currentPosition.y + correctionX;
break;
case SOUTH: this->currentPosition.x + correctionX;
this->currentPosition.y - (this->actionTicksLeft * TRAVEL_DISTANCE_PER_TICK) + correctionY;
break;
case WEST: this->currentPosition.x - (this->actionTicksLeft * TRAVEL_DISTANCE_PER_TICK) + correctionY;
this->currentPosition.y + correctionX;
break;
}
sprint(loggerBuffer, "Updating currentPosition(x: %f, y: %f)", this->currentPosition.x, this->currentPosition.y);
Logger::getInstance().log(loggerBuffer);
}
}