source: flair-src/trunk/lib/FlairSensorActuator/src/BlCtrlV2_x4_speed.cpp

Last change on this file was 318, checked in by Sanahuja Guillaume, 3 years ago
File size: 13.6 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2013/04/29
6// filename: BlCtrlV2_x4_speed.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: objet integrant les moteurs i2c, controle en vitesse
14//
15//
16/*********************************************************************/
17#ifdef ARMV7A
18
19#include "BlCtrlV2_x4_speed.h"
20#include "I2cPort.h"
21#include <TabWidget.h>
22#include <Tab.h>
23#include <GroupBox.h>
24#include <SpinBox.h>
25#include <DoubleSpinBox.h>
26#include <ComboBox.h>
27#include <PushButton.h>
28#include <Matrix.h>
29#include <Mutex.h>
30#include <FrameworkManager.h>
31#include <DataPlot1D.h>
32#include <math.h>
33#include <string.h>
34
35#define TAU_US 1000
36
37using std::string;
38using namespace flair::core;
39using namespace flair::gui;
40
41namespace flair {
42namespace actuator {
43BlCtrlV2_x4_speed::BlCtrlV2_x4_speed(string name,I2cPort *i2cport, uint8_t base_address,
44 uint8_t priority)
45 : Thread(getFrameworkManager(), name, priority), IODevice(getFrameworkManager(), name) {
46 this->i2cport = i2cport;
47 slave_address = base_address;
48 tested_motor = -1;
49 enabled = false;
50 int_av_g = 0;
51 int_av_d = 0;
52 int_ar_g = 0;
53 int_ar_d = 0;
54
55 // flight time
56 FILE *file;
57 file = fopen("/etc/flight_time", "r");
58 if (file == NULL) {
59 Printf("fichier d'info de vol vide\n");
60 time_sec = 0;
61 } else {
62 char ligne[32];
63 fgets(ligne, 32, file);
64 time_sec = atoi(ligne);
65 Printf("temps de vol total: %is = %imin = %ih\n", time_sec, time_sec / 60,
66 time_sec / 3600);
67 fclose(file);
68 }
69
70 // station sol
71 main_tab = new Tab(getFrameworkManager()->GetTabWidget(), name);
72 tab = new TabWidget(main_tab->NewRow(), name);
73 Tab *sensor_tab = new Tab(tab, "Reglages");
74 reglages_groupbox = new GroupBox(sensor_tab->NewRow(), name);
75 poles = new SpinBox(reglages_groupbox->NewRow(), "nb poles", 0, 255, 1);
76 kp = new DoubleSpinBox(reglages_groupbox->LastRowLastCol(), "kp", 0., 255,
77 0.001, 4);
78 ki = new DoubleSpinBox(reglages_groupbox->LastRowLastCol(), "ki", 0., 255,
79 0.001, 4);
80 min = new SpinBox(reglages_groupbox->NewRow(), "min pwm", 0., 2048, 1);
81 max =
82 new SpinBox(reglages_groupbox->LastRowLastCol(), "max pwm", 0., 2048, 1);
83 test = new SpinBox(reglages_groupbox->LastRowLastCol(), "test value", 0.,
84 2048, 1);
85 start_value = new SpinBox(reglages_groupbox->NewRow(), "valeur demarrage", 0,
86 10000, 10);
87 trim = new DoubleSpinBox(reglages_groupbox->LastRowLastCol(), "pas decollage",
88 0, 1000, .1);
89
90 av_g = new ComboBox(reglages_groupbox->NewRow(), "avant gauche");
91 av_g->AddItem("1");
92 av_g->AddItem("2");
93 av_g->AddItem("3");
94 av_g->AddItem("4");
95 button_avg = new PushButton(reglages_groupbox->LastRowLastCol(), "test avg");
96
97 av_d = new ComboBox(reglages_groupbox->LastRowLastCol(), "avant droite:");
98 av_d->AddItem("1");
99 av_d->AddItem("2");
100 av_d->AddItem("3");
101 av_d->AddItem("4");
102 button_avd = new PushButton(reglages_groupbox->LastRowLastCol(), "test avd");
103
104 ar_g = new ComboBox(reglages_groupbox->NewRow(), "arriere gauche:");
105 ar_g->AddItem("1");
106 ar_g->AddItem("2");
107 ar_g->AddItem("3");
108 ar_g->AddItem("4");
109 button_arg = new PushButton(reglages_groupbox->LastRowLastCol(), "test arg");
110
111 ar_d = new ComboBox(reglages_groupbox->LastRowLastCol(), "arriere droite:");
112 ar_d->AddItem("1");
113 ar_d->AddItem("2");
114 ar_d->AddItem("3");
115 ar_d->AddItem("4");
116 button_ard = new PushButton(reglages_groupbox->LastRowLastCol(), "test ard");
117
118 pas = new ComboBox(reglages_groupbox->NewRow(), "pas helice avant gauche:");
119 pas->AddItem("normal");
120 pas->AddItem("inverse");
121
122 input = new Matrix((IODevice *)this, 8, 1, floatType);
123
124 MatrixDescriptor *desc = new MatrixDescriptor(4, 2);
125 desc->SetElementName(0, 0, "avant gauche");
126 desc->SetElementName(1, 0, "arriere droite");
127 desc->SetElementName(2, 0, "avant droite");
128 desc->SetElementName(3, 0, "arriere gauche");
129
130 desc->SetElementName(0, 1, "cons avant gauche");
131 desc->SetElementName(1, 1, "cons arriere droite");
132 desc->SetElementName(2, 1, "cons avant droite");
133 desc->SetElementName(3, 1, "cons arriere gauche");
134 output = new Matrix((IODevice *)this, desc, floatType);
135 delete desc;
136
137 SetIsReady(true);
138
139 /*
140
141 //le 3ieme lu est la tension batteire
142 if(i2c_mutex!=NULL) i2c_mutex->GetMutex();
143 uint16_t pwm_moteur;
144 pwm_moteur=0;
145 ssize_t read;
146 uint8_t rx[8];
147 SetSlave(slave_address);
148
149 for(int j=0;j<10;j++)
150 {
151
152
153 WriteValue(pwm_moteur);
154
155
156 read = rt_dev_read(i2c_fd, rx, sizeof(rx));
157
158 if(read<0)
159 {
160 rt_printf("BlCtrlV2_x4_speed::BlCtrlV2_x4_speed: %s, erreur
161 rt_dev_read (%s)\n",IODevice::ObjectName().c_str(),strerror(-read));
162 }
163 else if (read != sizeof(rx))
164 {
165 rt_printf("BlCtrlV2_x4_speed::BlCtrlV2_x4_speed: %s, erreur
166 rt_dev_read %i/2\n",IODevice::ObjectName().c_str(),read);
167
168 }
169 for(int i=0;i<sizeof(rx);i++) printf("%i ",rx[i]);
170
171 printf("\n");
172
173 }
174
175 if(i2c_mutex!=NULL) i2c_mutex->ReleaseMutex();*/
176}
177
178BlCtrlV2_x4_speed::~BlCtrlV2_x4_speed(void) {
179 SafeStop();
180 Join();
181 delete main_tab;
182}
183
184void BlCtrlV2_x4_speed::UseDefaultPlot(void) {
185 Tab *plot_tab = new Tab(tab, "Mesures");
186 DataPlot1D *av_g_plot = new DataPlot1D(plot_tab->NewRow(), "avg", 0, 10000);
187 av_g_plot->AddCurve(output->Element(0, 0));
188 av_g_plot->AddCurve(output->Element(0, 1), DataPlot::Blue);
189 DataPlot1D *av_d_plot =
190 new DataPlot1D(plot_tab->LastRowLastCol(), "avd", 0, 10000);
191 av_d_plot->AddCurve(output->Element(2, 0));
192 av_d_plot->AddCurve(output->Element(2, 1), DataPlot::Blue);
193 DataPlot1D *ar_g_plot = new DataPlot1D(plot_tab->NewRow(), "arg", 0, 10000);
194 ar_g_plot->AddCurve(output->Element(3, 0));
195 ar_g_plot->AddCurve(output->Element(3, 1), DataPlot::Blue);
196 DataPlot1D *ar_d_plot =
197 new DataPlot1D(plot_tab->LastRowLastCol(), "ard", 0, 10000);
198 ar_d_plot->AddCurve(output->Element(1, 0));
199 ar_d_plot->AddCurve(output->Element(1, 1), DataPlot::Blue);
200}
201
202float BlCtrlV2_x4_speed::TrimValue(void) { return (float)trim->Value(); }
203
204int BlCtrlV2_x4_speed::StartValue(void) { return start_value->Value(); }
205
206void BlCtrlV2_x4_speed::Run(void) {
207 WarnUponSwitches(true);
208
209 SetPeriodUS(TAU_US);
210
211 while (!ToBeStopped()) {
212 WaitPeriod();
213
214 Update();
215 }
216
217 WarnUponSwitches(false);
218}
219
220void BlCtrlV2_x4_speed::Update(void) {
221 float u_roll, u_pitch, u_yaw, u_gaz;
222 float trim_roll, trim_pitch, trim_yaw;
223 float pwm[4];
224 uint16_t pwm_moteur[4];
225
226 // on prend une fois pour toute le mutex et on fait des accès directs
227 input->GetMutex();
228
229 u_roll = input->ValueNoMutex(0, 0);
230 u_pitch = input->ValueNoMutex(1, 0);
231 u_yaw = input->ValueNoMutex(2, 0);
232 u_gaz =
233 input->ValueNoMutex(3, 0) +
234 input->ValueNoMutex(7, 0) * input->ValueNoMutex(7, 0); // ugaz+trim*trim
235 trim_roll = input->ValueNoMutex(4, 0);
236 trim_pitch = input->ValueNoMutex(5, 0);
237 trim_yaw = input->ValueNoMutex(6, 0);
238
239 input->ReleaseMutex();
240
241 if (pas->CurrentIndex() == 1) {
242 trim_yaw = -trim_yaw;
243 u_yaw = -u_yaw;
244 }
245
246 // rt_printf("%f %f %f %f\n",u_roll,u_pitch,u_yaw,u_gaz);
247 // if(u_gaz!=0) rt_printf("gaz: %f\n",u_gaz);
248
249 // avant gauche
250 if (u_gaz + u_pitch + u_roll + u_yaw > 0) {
251 pwm[0] = trim_pitch + trim_roll + trim_yaw +
252 sqrtf(u_gaz + u_pitch + u_roll + u_yaw);
253 } else {
254 pwm[0] = trim_pitch + trim_roll + trim_yaw;
255 }
256
257 // arriere gauche
258 if (u_gaz - u_pitch + u_roll - u_yaw > 0) {
259 pwm[3] = -trim_pitch + trim_roll - trim_yaw +
260 sqrtf(u_gaz - u_pitch + u_roll - u_yaw);
261 } else {
262 pwm[3] = -trim_pitch + trim_roll - trim_yaw;
263 }
264
265 // arriere droit
266 if (u_gaz - u_pitch - u_roll + u_yaw > 0) {
267 pwm[1] = -trim_pitch - trim_roll + trim_yaw +
268 sqrtf(u_gaz - u_pitch - u_roll + u_yaw);
269 } else {
270 pwm[1] = -trim_pitch - trim_roll + trim_yaw;
271 }
272
273 // avant droit
274 if (u_gaz + u_pitch - u_roll - u_yaw > 0) {
275 pwm[2] = trim_pitch - trim_roll - trim_yaw +
276 sqrtf(u_gaz + u_pitch - u_roll - u_yaw);
277 } else {
278 pwm[2] = trim_pitch - trim_roll - trim_yaw;
279 }
280
281 int_av_g += ki->Value() * (pwm[0] - speed_av_g);
282 pwm[0] = kp->Value() * (pwm[0] - speed_av_g) + int_av_g;
283
284 int_ar_g += ki->Value() * (pwm[3] - speed_ar_g);
285 pwm[3] = kp->Value() * (pwm[3] - speed_ar_g) + int_ar_g;
286
287 int_ar_d += ki->Value() * (pwm[1] - speed_ar_d);
288 pwm[1] = kp->Value() * (pwm[1] - speed_ar_d) + int_ar_d;
289
290 int_av_d += ki->Value() * (pwm[2] - speed_av_d);
291 pwm[2] = kp->Value() * (pwm[2] - speed_av_d) + int_av_d;
292
293 // rt_printf("%f\n",pwm[0]);
294 for (int i = 0; i < 4; i++)
295 pwm_moteur[i] = SatPWM(pwm[i], min->Value(), max->Value());
296
297 if (button_avg->Clicked() == true) {
298 tested_motor = 0;
299 StartTest();
300 }
301 if (button_avd->Clicked() == true) {
302 tested_motor = 2;
303 StartTest();
304 }
305 if (button_arg->Clicked() == true) {
306 tested_motor = 3;
307 StartTest();
308 }
309 if (button_ard->Clicked() == true) {
310 tested_motor = 1;
311 StartTest();
312 }
313
314 if (tested_motor != -1) {
315 for (int i = 0; i < 4; i++) {
316 pwm_moteur[i] = 0;
317 }
318 pwm_moteur[tested_motor] = (uint16_t)test->Value();
319
320 if (GetTime() > (start_time + 2 * 1000000000))
321 StopTest();
322 }
323
324 i2cport->GetMutex();
325
326 if (enabled == true) {
327 i2cport->SetSlave(slave_address + av_g->CurrentIndex());
328 WriteValue(pwm_moteur[0]);
329
330 i2cport->SetSlave(slave_address + av_d->CurrentIndex());
331 WriteValue(pwm_moteur[2]);
332
333 i2cport->SetSlave(slave_address + ar_g->CurrentIndex());
334 WriteValue(pwm_moteur[3]);
335
336 i2cport->SetSlave(slave_address + ar_d->CurrentIndex());
337 WriteValue(pwm_moteur[1]);
338
339 } else {
340 for (int i = 0; i < 4; i++) {
341 i2cport->SetSlave(slave_address + i);
342 WriteValue(0);
343 }
344 int_av_g = 0;
345 int_av_d = 0;
346 int_ar_g = 0;
347 int_ar_d = 0;
348 }
349
350 i2cport->SetSlave(slave_address + av_g->CurrentIndex());
351 speed_av_g = GetSpeed();
352
353 i2cport->SetSlave(slave_address + av_d->CurrentIndex());
354 speed_av_d = GetSpeed();
355
356 i2cport->SetSlave(slave_address + ar_g->CurrentIndex());
357 speed_ar_g = GetSpeed();
358
359 i2cport->SetSlave(slave_address + ar_d->CurrentIndex());
360 speed_ar_d = GetSpeed();
361
362 i2cport->ReleaseMutex();
363
364 // on prend une fois pour toute le mutex et on fait des accès directs
365 output->GetMutex();
366 output->SetValueNoMutex(0, 0, speed_av_g);
367 output->SetValueNoMutex(1, 0, speed_ar_d);
368 output->SetValueNoMutex(2, 0, speed_av_d);
369 output->SetValueNoMutex(3, 0, speed_ar_g);
370 // rt_printf("%i %i %i
371 // %i\n",pwm_moteur[0],pwm_moteur[1],pwm_moteur[2],pwm_moteur[3]);
372 output->ReleaseMutex();
373
374 output->SetDataTime(GetTime());
375 ProcessUpdate(output);
376}
377
378void BlCtrlV2_x4_speed::StartTest(void) {
379 start_time = GetTime();
380 SetEnabled(true);
381}
382
383void BlCtrlV2_x4_speed::StopTest(void) {
384 SetEnabled(false);
385 tested_motor = -1;
386}
387
388uint16_t BlCtrlV2_x4_speed::SatPWM(float vel_cons, uint16_t min, uint16_t max) {
389 uint16_t sat_value = (uint16_t)vel_cons;
390
391 if (vel_cons > ((float)sat_value + 0.5))
392 sat_value++;
393
394 if (vel_cons < (float)min)
395 sat_value = min;
396 if (vel_cons > (float)max)
397 sat_value = max;
398
399 return sat_value;
400}
401
402void BlCtrlV2_x4_speed::LockUserInterface(void) {
403 reglages_groupbox->setEnabled(false);
404}
405
406void BlCtrlV2_x4_speed::UnlockUserInterface(void) {
407 reglages_groupbox->setEnabled(true);
408}
409
410void BlCtrlV2_x4_speed::SetEnabled(bool status) {
411 enabled = status;
412 if (enabled == true) {
413 LockUserInterface();
414
415 flight_start_time = GetTime();
416 } else {
417 UnlockUserInterface();
418
419 Time now = GetTime();
420 int t_sec;
421 FILE *file;
422 char ligne[32];
423
424 t_sec = (now - flight_start_time) / 1000000000;
425 time_sec += t_sec;
426
427 Printf("temps de vol: %is = %imin\n", t_sec, t_sec / 60);
428 Printf("temps de vol total: %is = %imin = %ih\n", time_sec, time_sec / 60,
429 time_sec / 3600);
430
431 file = fopen("/etc/flight_time", "w");
432 if (file == NULL) {
433 Thread::Err("Erreur a l'ouverture du fichier d'info vol\n");
434 } else {
435 sprintf(ligne, "%i", time_sec);
436 fputs(ligne, file);
437 fclose(file);
438 }
439 }
440}
441
442void BlCtrlV2_x4_speed::SetUroll(float value) { input->SetValue(0, 0, value); }
443
444void BlCtrlV2_x4_speed::SetUpitch(float value) { input->SetValue(1, 0, value); }
445
446void BlCtrlV2_x4_speed::SetUyaw(float value) { input->SetValue(2, 0, value); }
447
448void BlCtrlV2_x4_speed::SetUgaz(float value) { input->SetValue(3, 0, value); }
449
450void BlCtrlV2_x4_speed::SetRollTrim(float value) {
451 input->SetValue(4, 0, value);
452}
453
454void BlCtrlV2_x4_speed::SetPitchTrim(float value) {
455 input->SetValue(5, 0, value);
456}
457
458void BlCtrlV2_x4_speed::SetYawTrim(float value) {
459 input->SetValue(6, 0, value);
460}
461
462void BlCtrlV2_x4_speed::SetGazTrim(float value) {
463 input->SetValue(7, 0, value);
464}
465
466void BlCtrlV2_x4_speed::WriteValue(uint16_t value) {
467 unsigned char tx[2];
468 ssize_t written;
469
470 tx[0] = (unsigned char)(value >> 3); // msb
471 tx[1] = 16 + 8 + (value & 0x07); // 16+8 pour recuperer la vitesse
472 written = i2cport->Write(tx, 2);
473 if (written < 0) {
474 Thread::Err("erreur rt_dev_write (%s)\n", strerror(-written));
475 } else if (written != 2) {
476 Thread::Err("erreur rt_dev_write %i/2\n", written);
477 }
478}
479
480float BlCtrlV2_x4_speed::GetSpeed(void) {
481 ssize_t read;
482 uint8_t value;
483 read = i2cport->Read(&value, 1);
484
485 if (read < 0) {
486 Thread::Err("erreur rt_dev_read (%s)\n", strerror(-read));
487 } else if (read != 1) {
488 Thread::Err("erreur rt_dev_read %i/2\n", read);
489 }
490
491 return value * 780. / poles->Value();
492}
493
494} // end namespace actuator
495} // end namespace flair
496
497#endif
Note: See TracBrowser for help on using the repository browser.