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

Last change on this file since 16 was 15, checked in by Bayard Gildas, 8 years ago

sources reformatted with flair-format-dir script

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