Statistics
| Branch: | Tag: | Revision:

amiro-os / components / Odometry.cpp @ 2f3e64c4

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