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

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

cosmetic

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