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

Last change on this file since 140 was 137, checked in by Sanahuja Guillaume, 7 years ago

singleton manager

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