amiro-os / components / Odometry.cpp @ a3c54343
History | View | Annotate | Download (8.309 KB)
1 | 58fe0e0b | Thomas Schöpping | #include <ch.hpp> |
---|---|---|---|
2 | #include <hal.h> |
||
3 | |||
4 | #include <qei.h> |
||
5 | |||
6 | #include <amiro/Odometry.h> |
||
7 | |||
8 | #include <math.h> // cos(), sin() |
||
9 | #include <Matrix.h> // Matrixoperations "Matrix::*" |
||
10 | #include <amiro/Constants.h> // Constants "constants::*" |
||
11 | #include <chprintf.h> |
||
12 | f8cf404d | Thomas Schöpping | #include <global.hpp> |
13 | 58fe0e0b | Thomas Schöpping | |
14 | using namespace chibios_rt; |
||
15 | using namespace amiro; |
||
16 | using namespace constants::DiWheelDrive; |
||
17 | |||
18 | f8cf404d | Thomas Schöpping | extern Global global;
|
19 | |||
20 | 58fe0e0b | Thomas Schöpping | |
21 | b4885314 | Thomas Schöpping | Odometry::Odometry(MotorIncrements* mi, L3G4200D* gyroscope) |
22 | f336542d | Timo Korthals | : BaseStaticThread<512>(),
|
23 | 58fe0e0b | Thomas Schöpping | motorIncrements(mi), |
24 | b4885314 | Thomas Schöpping | gyro(gyroscope), |
25 | 58fe0e0b | Thomas Schöpping | eventSource(), |
26 | period(50),
|
||
27 | incrementsPerRevolution(incrementsPerRevolution), |
||
28 | updatesPerMinute(constants::secondsPerMinute * constants::millisecondsPerSecond / this->period),
|
||
29 | wheelCircumference(wheelCircumferenceSI), |
||
30 | wheelBaseDistanceSI(wheelBaseDistanceSI) { |
||
31 | |||
32 | |||
33 | // this-> = constants::secondsPerMinute * constants::millisecondsPerSecond / this->period;
|
||
34 | // this->wheelCircumference = constants::wheelCircumferenceSI;
|
||
35 | // this->wheelBaseDistanceSI = constants::wheelBaseDistanceSI;
|
||
36 | |||
37 | this->distance[LEFT_WHEEL] = 0.0f; |
||
38 | this->distance[RIGHT_WHEEL] = 0.0f; |
||
39 | this->increment[LEFT_WHEEL] = 0; |
||
40 | this->increment[RIGHT_WHEEL] = 0; |
||
41 | this->incrementDifference[LEFT_WHEEL] = 0.0f; |
||
42 | this->incrementDifference[RIGHT_WHEEL] = 0.0f; |
||
43 | this->distance[LEFT_WHEEL] = 0.0f; |
||
44 | this->distance[RIGHT_WHEEL] = 0.0f; |
||
45 | |||
46 | this->wheelError[LEFT_WHEEL] = wheelErrorSI[LEFT_WHEEL];
|
||
47 | this->wheelError[RIGHT_WHEEL] = wheelErrorSI[RIGHT_WHEEL];
|
||
48 | |||
49 | this->resetPosition(); // Init position |
||
50 | |||
51 | this->resetError(); // Init error Cp |
||
52 | |||
53 | } |
||
54 | |||
55 | types::position Odometry::getPosition() { |
||
56 | types::position robotPosition; |
||
57 | const int32_t piScaled = int32_t(2 * M_PI * 1e6); |
||
58 | chSysLock(); |
||
59 | // Conversion from standard unit to µ unit
|
||
60 | robotPosition.x = this->pX * 1e6; |
||
61 | robotPosition.y = this->pY * 1e6; |
||
62 | robotPosition.f_z = (int32_t(this->pPhi * 1e6) % piScaled) + ((this->pPhi < 0) ? piScaled : 0); // Get only the postitve angel f_z in [0 .. 2 * pi] |
||
63 | chSysUnlock(); |
||
64 | f8cf404d | Thomas Schöpping | // chprintf((BaseSequentialStream*) &global.sercanmux1, "X:%d Y:%d Phi:%d", robotPosition.x,robotPosition.y, robotPosition.f_z);
|
65 | // chprintf((BaseSequentialStream*) &global.sercanmux1, "\r\n");
|
||
66 | // chprintf((BaseSequentialStream*) &global.sercanmux1, "X:%f Y:%f Phi:%f", this->pX,this->pY, this->pPhi);
|
||
67 | // chprintf((BaseSequentialStream*) &global.sercanmux1, "\r\n");
|
||
68 | 58fe0e0b | Thomas Schöpping | return robotPosition;
|
69 | } |
||
70 | |||
71 | void Odometry::setPosition(float pX, float pY, float pPhi) { |
||
72 | chSysLock(); |
||
73 | this->pX = pX;
|
||
74 | this->pY = pY;
|
||
75 | this->pPhi = pPhi;
|
||
76 | chSysUnlock(); |
||
77 | } |
||
78 | |||
79 | 9090cc7e | galberding | void Odometry::setPositionXY(float pX, float pY) { |
80 | chSysLock(); |
||
81 | this->pX = pX;
|
||
82 | this->pY = pY;
|
||
83 | chSysUnlock(); |
||
84 | } |
||
85 | |||
86 | 58fe0e0b | Thomas Schöpping | void Odometry::resetPosition() {
|
87 | this->setPosition(0.0f,0.0f,0.0f); |
||
88 | } |
||
89 | |||
90 | void Odometry::setError(float* Cp3x3) { |
||
91 | chSysLock(); |
||
92 | Matrix::copy<float>(Cp3x3,3,3, &(this->Cp3x3[0]),3,3); |
||
93 | chSysUnlock(); |
||
94 | } |
||
95 | |||
96 | void Odometry::resetError() {
|
||
97 | Matrix::init<float>(&(this->Cp3x3[0]),3,3,0.0f); |
||
98 | } |
||
99 | |||
100 | EvtSource* Odometry::getEventSource() { |
||
101 | return &this->eventSource; |
||
102 | } |
||
103 | |||
104 | msg_t Odometry::main(void) {
|
||
105 | systime_t time = System::getTime(); |
||
106 | this->setName("Odometry"); |
||
107 | |||
108 | while (!this->shouldTerminate()) { |
||
109 | time += MS2ST(this->period);
|
||
110 | |||
111 | f336542d | Timo Korthals | // Update the base distance, because it may have changed after a calibration
|
112 | 58fe0e0b | Thomas Schöpping | this->updateWheelBaseDistance();
|
113 | |||
114 | // Get the actual speed
|
||
115 | this->updateDistance();
|
||
116 | |||
117 | // Calculate the odometry
|
||
118 | this->updateOdometry();
|
||
119 | |||
120 | |||
121 | f8cf404d | Thomas Schöpping | // chprintf((BaseSequentialStream*) &global.sercanmux1, "X:%f Y:%f Phi:%f", this->pX,this->pY, this->pPhi);
|
122 | // chprintf((BaseSequentialStream*) &global.sercanmux1, "\r\n");
|
||
123 | // chprintf((BaseSequentialStream*) &global.sercanmux1, "distance_left:%f distance_right:%f", this->distance[0],this->distance[1]);
|
||
124 | // chprintf((BaseSequentialStream*) &global.sercanmux1, "\r\n");
|
||
125 | |||
126 | f336542d | Timo Korthals | if (time >= System::getTime()) {
|
127 | chThdSleepUntil(time); |
||
128 | b4885314 | Thomas Schöpping | } else {
|
129 | f336542d | Timo Korthals | chprintf((BaseSequentialStream*) &global.sercanmux1, "WARNING Odometry: Unable to keep track\r\n");
|
130 | } |
||
131 | 58fe0e0b | Thomas Schöpping | } |
132 | |||
133 | return true; |
||
134 | } |
||
135 | |||
136 | void Odometry::updateOdometry() {
|
||
137 | |||
138 | // Get the temporary position and error
|
||
139 | float Cp3x3[9]; |
||
140 | f336542d | Timo Korthals | int32_t angular_ud; |
141 | int32_t angularRate_udps; |
||
142 | 58fe0e0b | Thomas Schöpping | chSysLock(); |
143 | float pX = this->pX; |
||
144 | float pY = this->pY; |
||
145 | float pPhi = this->pPhi; |
||
146 | Matrix::copy<float>(this->Cp3x3,3,3,Cp3x3,3,3); |
||
147 | f336542d | Timo Korthals | // TODO Get the gyro (or gyro rate) information and do something with it
|
148 | // angular_ud = gyro->getAngular_ud(L3G4200D::AXIS_Z);
|
||
149 | // angularRate_udps = gyro->getAngularRate_udps(L3G4200D::AXIS_Z);
|
||
150 | 58fe0e0b | Thomas Schöpping | chSysUnlock(); |
151 | |||
152 | ////////////////
|
||
153 | // Temporary calculations
|
||
154 | ////////////////
|
||
155 | |||
156 | // TMP: Rotated angular
|
||
157 | f336542d | Timo Korthals | float dPhi = (this->distance[RIGHT_WHEEL] - this->distance[LEFT_WHEEL]) / this->wheelBaseDistanceSI; |
158 | // TODO Calculate the differential angel dPhi from either the angular (1. line) or angular rate (2.+3. line)
|
||
159 | // float dPhi = ((float(angular_ud * 1e-3) * M_PI ) * 1e-3) / 180.0f;
|
||
160 | // const float angular_md = float((angularRate_udps * this->period / constants::millisecondsPerSecond) * 1e-3);
|
||
161 | // float dPhi = ((angular_md * M_PI) * 1e-3) / 180.0f;
|
||
162 | 58fe0e0b | Thomas Schöpping | |
163 | // TMP: Moved distance
|
||
164 | float dDistance = (this->distance[RIGHT_WHEEL] + this->distance[LEFT_WHEEL]) / 2.0f; |
||
165 | |||
166 | // TMP: Argument for the trigonometric functions
|
||
167 | float trigArg = pPhi + dPhi / 2.0f; |
||
168 | |||
169 | // TMP: Trigonometric functions
|
||
170 | float cosArg = cos(trigArg);
|
||
171 | float sinArg = sin(trigArg);
|
||
172 | |||
173 | // TMP: Delta distance
|
||
174 | float dPX = dDistance * cosArg;
|
||
175 | float dPY = dDistance * sinArg;
|
||
176 | |||
177 | ////////////////
|
||
178 | // Position Update
|
||
179 | ////////////////
|
||
180 | |||
181 | // Update distance
|
||
182 | pX += dPX; |
||
183 | pY += dPY; |
||
184 | pPhi += dPhi; |
||
185 | |||
186 | ////////////////
|
||
187 | // Temporary error calculations
|
||
188 | ////////////////
|
||
189 | |||
190 | // position propagation error (3x3 matrix)
|
||
191 | float Fp3x3[9] = {1.0f, 0.0f, -dPY, |
||
192 | 0.0f, 1.0f, dPX, |
||
193 | 0.0f, 0.0f, 1.0f}; |
||
194 | // steering error (2x2 matrix)
|
||
195 | float Cs2x2[4] = {abs(this->distance[RIGHT_WHEEL])*wheelError[RIGHT_WHEEL],0.0f, |
||
196 | 0.0f, abs(this->distance[LEFT_WHEEL])*wheelError[LEFT_WHEEL]}; |
||
197 | // steering propagation error (3x2 matrix)
|
||
198 | float Fs3x2[6] = {(cosArg+dDistance*sinArg/this->wheelBaseDistanceSI)/2.0f, (sinArg+dDistance*cosArg/this->wheelBaseDistanceSI)/2.0f, |
||
199 | (sinArg-dDistance*cosArg/this->wheelBaseDistanceSI)/2.0f, (cosArg-dDistance*sinArg/this->wheelBaseDistanceSI)/2.0f, |
||
200 | -1.0f/this->wheelBaseDistanceSI , 1.0f/this->wheelBaseDistanceSI}; |
||
201 | |||
202 | ////////////////
|
||
203 | // Error calculations tmpCp = Fp*Cp*~Fp
|
||
204 | ////////////////
|
||
205 | // New position error
|
||
206 | float tmpCp3x3[9] = {0.0f}; |
||
207 | float tmpFpCp3x3[9] = {0.0f}; |
||
208 | // tmpFpCp = Fp*Cp
|
||
209 | Matrix::XdotY<float>(&(Fp3x3[0]),3,3,&(Cp3x3[0]),3,3,&(tmpFpCp3x3[0]),3,3); |
||
210 | // tmpCp = tmpFpCp*~Fp
|
||
211 | Matrix::XdotYtrans<float>(&(tmpFpCp3x3[0]),3,3,&(Fp3x3[0]),3,3,&(tmpCp3x3[0]),3,3); |
||
212 | |||
213 | ////////////////
|
||
214 | // Error calculations tmpCs = Fs*Cs*~Fs
|
||
215 | ////////////////
|
||
216 | // New steering error
|
||
217 | float tmpCs3x3[9] = {0.0f}; |
||
218 | float tmpFsCs3x2[6] = {0.0f}; |
||
219 | // tmpFsCs = Fs*Cs
|
||
220 | Matrix::XdotY<float>(&(Fs3x2[0]),3,2,&(Cs2x2[0]),2,2,&(tmpFsCs3x2[0]),3,2); |
||
221 | // tmpCs = tmpFsCs*~Fs
|
||
222 | Matrix::XdotYtrans<float>(&(tmpFsCs3x2[0]),3,2,&(Fs3x2[0]),3,2,&(tmpCs3x3[0]),3,3); |
||
223 | |||
224 | ////////////////
|
||
225 | // Error calculations Cp = Fp*Cp*~Fp + Fs*Cs*~Fs
|
||
226 | ////////////////
|
||
227 | Matrix::XplusY<float>(tmpCp3x3,3,3,tmpCs3x3,3,3,Cp3x3,3,3); |
||
228 | |||
229 | ////////////////
|
||
230 | // Write back
|
||
231 | ////////////////
|
||
232 | |||
233 | // Write back
|
||
234 | this->setPosition(pX,pY,pPhi);
|
||
235 | chSysLock(); |
||
236 | Matrix::copy<float>(Cp3x3,3,3,this->Cp3x3,3,3); |
||
237 | chSysUnlock(); |
||
238 | |||
239 | } |
||
240 | |||
241 | void Odometry::updateWheelBaseDistance() {
|
||
242 | this->wheelBaseDistanceSI = MotorControl::actualWheelBaseDistanceSI;
|
||
243 | } |
||
244 | |||
245 | void Odometry::updateDistance() {
|
||
246 | |||
247 | // Get the current increments of the QEI
|
||
248 | MotorControl::updateIncrements(this->motorIncrements, this->increment, this->incrementDifference); |
||
249 | //
|
||
250 | f8cf404d | Thomas Schöpping | // chprintf((BaseSequentialStream*) &global.sercanmux1, "\ni_right = %d \t i_left = %d", this->increment[RIGHT_WHEEL], this->increment[LEFT_WHEEL]);
|
251 | // chprintf((BaseSequentialStream*) &global.sercanmux1, "\niDiff_right = %d \t iDiff_left = %d", this->incrementDifference[RIGHT_WHEEL], this->incrementDifference[LEFT_WHEEL]);
|
||
252 | 58fe0e0b | Thomas Schöpping | |
253 | // Get the driven distance for each wheel
|
||
254 | MotorControl::updateDistance(this->incrementDifference, this->distance); |
||
255 | |||
256 | f8cf404d | Thomas Schöpping | // chprintf((BaseSequentialStream*) &global.sercanmux1, "\nx_right = %f \t x_left = %f", this->distance[RIGHT_WHEEL], this->distance[LEFT_WHEEL]);
|
257 | 58fe0e0b | Thomas Schöpping | } |