source: flair-src/trunk/lib/FlairSimulator/src/X8.cpp@ 157

Last change on this file since 157 was 157, checked in by Sanahuja Guillaume, 6 years ago

iadded isready to iodevice:
avoid problem of imu not ready in ardrone2

File size: 15.2 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: 2014/04/03
6// filename: X8.cpp
7//
8// author: Majd Saied, Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: classe definissant un X8
14//
15/*********************************************************************/
16
17#include "X8.h"
18#include "Simulator.h"
19#include <SimuBldc.h>
20#include <TabWidget.h>
21#include <Tab.h>
22#include <DoubleSpinBox.h>
23#include <GroupBox.h>
24#include <math.h>
25#ifdef GL
26#include <ISceneManager.h>
27#include "Blade.h"
28#include "MeshSceneNode.h"
29#include "Gui.h"
30#include <Mutex.h>
31#endif
32
33#define K_MOT 0.4f // blade animation
34#define G (float)9.81 // gravity ( N/(m/s²) )
35
36#ifdef GL
37using namespace irr::video;
38using namespace irr::scene;
39using namespace irr::core;
40#endif
41using namespace flair::core;
42using namespace flair::gui;
43using namespace flair::actuator;
44
45namespace flair {
46namespace simulator {
47
48X8::X8(const Simulator *parent, std::string name, int dev_id)
49 : Model(parent, name) {
50 Tab *setup_tab = new Tab(GetTabWidget(), "model");
51 m = new DoubleSpinBox(setup_tab->NewRow(), "mass (kg):", 0, 20, 0.1);
52 arm_length = new DoubleSpinBox(setup_tab->LastRowLastCol(), "arm length (m):",
53 0, 2, 0.1);
54 l_cg = new DoubleSpinBox(
55 setup_tab->LastRowLastCol(), "position G (m):", -0.5, 0.5,
56 0.02); // position du centre de gravité/centre de poussé
57 k_mot =
58 new DoubleSpinBox(setup_tab->NewRow(), "k_mot:", 0, 1, 0.001,
59 3); // vitesse rotation² (unité arbitraire) -> force (N)
60 c_mot = new DoubleSpinBox(
61 setup_tab->LastRowLastCol(), "c_mot:", 0, 1, 0.001,
62 3); // vitesse rotation moteur -> couple (N.m/unité arbitraire)
63 f_air_vert = new DoubleSpinBox(setup_tab->NewRow(), "f_air_vert:", 0, 10,
64 1); // frottements air depl. vertical, aussi
65 // utilisé pour les rotations ( N/(m/s) )
66 // (du aux helices en rotation)
67 f_air_lat =
68 new DoubleSpinBox(setup_tab->LastRowLastCol(), "f_air_lat:", 0, 10,
69 1); // frottements air deplacements lateraux ( N/(m/s) )
70 j_roll = new DoubleSpinBox(setup_tab->NewRow(), "j_roll:", 0, 1, 0.001,
71 5); // moment d'inertie d'un axe (N.m.s²/rad)
72 j_pitch =
73 new DoubleSpinBox(setup_tab->LastRowLastCol(), "j_pitch:", 0, 1, 0.001,
74 5); // moment d'inertie d'un axe (N.m.s²/rad)
75 j_yaw = new DoubleSpinBox(setup_tab->LastRowLastCol(), "j_yaw:", 0, 1, 0.001,
76 5); // moment d'inertie d'un axe (N.m.s²/rad)
77 j_r = new DoubleSpinBox(setup_tab->NewRow(), "j_r:", 0, 1,
78 0.001); // moment des helices (N.m.s²/rad)
79 sigma = new DoubleSpinBox(
80 setup_tab->LastRowLastCol(), "sigma:", 0, 1,
81 0.1); // coefficient de perte d efficacite aerodynamique (sans unite)
82 S = new DoubleSpinBox(
83 setup_tab->LastRowLastCol(), "S:", 1, 2,
84 0.1); // coefficient de forme des helices 1<S=1+Ss/Sprop<2 (sans unite)
85
86 motors = new SimuBldc(this, name, 8, dev_id);
87
88 SetIsReady(true);
89}
90
91void X8::Draw() {
92#ifdef GL
93
94 // create unite (1m=100cm) UAV; scale will be adapted according to arm_length
95 // parameter
96 // note that the frame used is irrlicht one:
97 // left handed, North East Up
98
99 const IGeometryCreator *geo;
100 geo = getGui()->getSceneManager()->getGeometryCreator();
101
102 // cylinders are aligned with y axis
103 red_arm = geo->createCylinderMesh(2.5, 100, 16, SColor(0, 255, 0, 0));
104 black_arm = geo->createCylinderMesh(2.5, 100, 16, SColor(0, 128, 128, 128));
105 motor = geo->createCylinderMesh(7.5, 15, 16); //,SColor(0, 128, 128, 128));
106 // geo->drop();
107
108 ITexture *texture = getGui()->getTexture("carbone.jpg");
109 fl_arm = new MeshSceneNode(this, red_arm, vector3df(0, 0, 0),
110 vector3df(0, 0, -135));
111 fr_arm = new MeshSceneNode(this, red_arm, vector3df(0, 0, 0),
112 vector3df(0, 0, -45));
113 rl_arm = new MeshSceneNode(this, black_arm, vector3df(0, 0, 0),
114 vector3df(0, 0, 135), texture);
115 rr_arm = new MeshSceneNode(this, black_arm, vector3df(0, 0, 0),
116 vector3df(0, 0, 45), texture);
117
118 texture = getGui()->getTexture("metal047.jpg");
119 tfl_motor = new MeshSceneNode(this, motor, vector3df(70.71, -70.71, 2.5),
120 vector3df(90, 0, 0), texture);
121 tfr_motor = new MeshSceneNode(this, motor, vector3df(70.71, 70.71, 2.5),
122 vector3df(90, 0, 0), texture);
123 trl_motor = new MeshSceneNode(this, motor, vector3df(-70.71, -70.71, 2.5),
124 vector3df(90, 0, 0), texture);
125 trr_motor = new MeshSceneNode(this, motor, vector3df(-70.71, 70.71, 2.5),
126 vector3df(90, 0, 0), texture);
127
128 bfl_motor = new MeshSceneNode(this, motor, vector3df(70.71, -70.71, -17.5),
129 vector3df(90, 0, 0), texture);
130 bfr_motor = new MeshSceneNode(this, motor, vector3df(70.71, 70.71, -17.5),
131 vector3df(90, 0, 0), texture);
132 brl_motor = new MeshSceneNode(this, motor, vector3df(-70.71, -70.71, -17.5),
133 vector3df(90, 0, 0), texture);
134 brr_motor = new MeshSceneNode(this, motor, vector3df(-70.71, 70.71, -17.5),
135 vector3df(90, 0, 0), texture);
136
137 tfl_blade = new Blade(this, vector3df(70.71, -70.71, 17.5));
138 tfr_blade = new Blade(this, vector3df(70.71, 70.71, 17.5), true);
139 trl_blade = new Blade(this, vector3df(-70.71, -70.71, 17.5), true);
140 trr_blade = new Blade(this, vector3df(-70.71, 70.71, 17.5));
141
142 bfl_blade = new Blade(this, vector3df(70.71, -70.71, -17.5));
143 bfr_blade = new Blade(this, vector3df(70.71, 70.71, -17.5), true);
144 brl_blade = new Blade(this, vector3df(-70.71, -70.71, -17.5), true);
145 brr_blade = new Blade(this, vector3df(-70.71, 70.71, -17.5));
146
147 motor_speed_mutex = new Mutex(this);
148 for (int i = 0; i < 8; i++)
149 motor_speed[i] = 0;
150 ExtraDraw();
151#endif
152}
153
154X8::~X8() {
155 // les objets irrlicht seront automatiquement detruits (moteurs, helices,
156 // pales) par parenté
157}
158
159#ifdef GL
160void X8::AnimateModel(void) {
161 motor_speed_mutex->GetMutex();
162 tfl_blade->SetRotationSpeed(K_MOT * motor_speed[0]);
163 tfr_blade->SetRotationSpeed(-K_MOT * motor_speed[1]);
164 trl_blade->SetRotationSpeed(-K_MOT * motor_speed[2]);
165 trr_blade->SetRotationSpeed(K_MOT * motor_speed[3]);
166
167 bfl_blade->SetRotationSpeed(-K_MOT * motor_speed[4]);
168 bfr_blade->SetRotationSpeed(K_MOT * motor_speed[5]);
169 brl_blade->SetRotationSpeed(K_MOT * motor_speed[6]);
170 brr_blade->SetRotationSpeed(-K_MOT * motor_speed[7]);
171 motor_speed_mutex->ReleaseMutex();
172
173 // adapt UAV size
174 if (arm_length->ValueChanged() == true) {
175 setScale(arm_length->Value());
176 }
177}
178
179size_t X8::dbtSize(void) const {
180 return 6 * sizeof(float) + 4 * sizeof(float); // 6ddl+4helices
181}
182
183void X8::WritedbtBuf(
184 char *dbtbuf) { /*
185 float *buf=(float*)dbtbuf;
186 vector3df vect=getPosition();
187 memcpy(buf,&vect.X,sizeof(float));
188 buf++;
189 memcpy(buf,&vect.Y,sizeof(float));
190 buf++;
191 memcpy(buf,&vect.Z,sizeof(float));
192 buf++;
193 vect=getRotation();
194 memcpy(buf,&vect.X,sizeof(float));
195 buf++;
196 memcpy(buf,&vect.Y,sizeof(float));
197 buf++;
198 memcpy(buf,&vect.Z,sizeof(float));
199 buf++;
200 memcpy(buf,&motors,sizeof(rtsimu_motors));*/
201}
202
203void X8::ReaddbtBuf(
204 char *dbtbuf) { /*
205 float *buf=(float*)dbtbuf;
206 vector3df vect;
207 memcpy(&vect.X,buf,sizeof(float));
208 buf++;
209 memcpy(&vect.Y,buf,sizeof(float));
210 buf++;
211 memcpy(&vect.Z,buf,sizeof(float));
212 buf++;
213 setPosition(vect);
214 memcpy(&vect.X,buf,sizeof(float));
215 buf++;
216 memcpy(&vect.Y,buf,sizeof(float));
217 buf++;
218 memcpy(&vect.Z,buf,sizeof(float));
219 buf++;
220 ((ISceneNode*)(this))->setRotation(vect);
221 memcpy(&motors,buf,sizeof(rtsimu_motors));
222 AnimateModele();*/
223}
224#endif // GL
225
226// states are computed on fixed frame NED
227// x north
228// y east
229// z down
230void X8::CalcModel(void) {
231 float tfl_speed, tfr_speed, trl_speed, trr_speed;
232 float bfl_speed, bfr_speed, brl_speed, brr_speed;
233 float u_roll, u_pitch, u_yaw, u_thrust;
234 float omega;
235#ifdef GL
236 motor_speed_mutex->GetMutex();
237#endif // GL
238 motors->GetSpeeds(motor_speed);
239#ifdef GL
240 motor_speed_mutex->ReleaseMutex();
241#endif // GL
242 tfl_speed = motor_speed[0];
243 tfr_speed = motor_speed[1];
244 trl_speed = motor_speed[2];
245 trr_speed = motor_speed[3];
246 bfl_speed = motor_speed[4];
247 bfr_speed = motor_speed[5];
248 brl_speed = motor_speed[6];
249 brr_speed = motor_speed[7];
250
251 omega = tfl_speed + brl_speed + trr_speed + bfr_speed - bfl_speed -
252 trl_speed - brr_speed - tfr_speed;
253
254 /*
255 ** ===================================================================
256 ** u roll: roll torque
257 **
258 ** ===================================================================
259 */
260
261 u_roll = arm_length->Value() * k_mot->Value() *
262 (sigma->Value() * tfl_speed * tfl_speed + bfl_speed * bfl_speed +
263 sigma->Value() * trl_speed * trl_speed + brl_speed * brl_speed -
264 sigma->Value() * tfr_speed * tfr_speed - bfr_speed * bfr_speed -
265 sigma->Value() * trr_speed * trr_speed - brr_speed * brr_speed) *
266 sqrtf(2) / 2;
267
268 /// Classical Nonlinear model of a quadrotor ( This is the w_x angular speed
269 /// of the quadri in the body frame). It is a discrete integrator
270 // state[0].W.x=(dT()/j_roll->Value())*((j_yaw->Value()-j_pitch->Value())*state[-1].W.y*state[-1].W.z-j_r->Value()*state[-1].W.y*omega
271 // + u_roll) +state[-1].W.x;//Osamah
272 state[0].W.x =
273 (dT() / j_roll->Value()) *
274 ((j_pitch->Value() - j_yaw->Value()) * state[-1].W.y * state[-1].W.z -
275 j_r->Value() * state[-1].W.y * omega + u_roll) +
276 state[-1].W.x; // Majd
277
278 // state[0].W.x=(dT()/j_roll->Value())*(u_roll-m->Value()*G*l_cg->Value()*sinf(state[-2].W.x)-f_air_vert->Value()*arm_length->Value()*arm_length->Value()*state[-1].W.x)+state[-1].W.x;
279
280 /*
281 ** ===================================================================
282 ** u pitch : pitch torque
283 **
284 ** ===================================================================
285 */
286 u_pitch = arm_length->Value() * k_mot->Value() *
287 (sigma->Value() * tfl_speed * tfl_speed + bfl_speed * bfl_speed +
288 sigma->Value() * tfr_speed * tfr_speed + bfr_speed * bfr_speed -
289 sigma->Value() * trl_speed * trl_speed - brl_speed * brl_speed -
290 sigma->Value() * trr_speed * trr_speed - brr_speed * brr_speed) *
291 sqrtf(2) / 2;
292
293 /// Classical Nonlinear model of a quadrotor ( This is the w_y angular speed
294 /// of the quadri in the body frame). It is a discrete integrator
295 // state[0].W.y=(dT()/j_pitch->Value())*((j_roll->Value()-j_yaw->Value())*state[-1].W.x*state[-1].W.z-j_r->Value()*state[-1].W.x*omega
296 // + u_pitch)+state[-1].W.y;//Osamah
297 state[0].W.y =
298 (dT() / j_pitch->Value()) *
299 ((j_yaw->Value() - j_roll->Value()) * state[-1].W.x * state[-1].W.z -
300 j_r->Value() * state[-1].W.x * omega + u_pitch) +
301 state[-1].W.y; // Majd
302
303 // state[0].W.y=(dT()/j_pitch->Value())*(u_pitch-m->Value()*G*l_cg->Value()*sinf(state[-2].W.y)-f_air_vert->Value()*arm_length->Value()*arm_length->Value()*state[-1].W.y)+state[-1].W.y;
304
305 /*
306 ** ===================================================================
307 ** u yaw : yaw torque
308 **
309 ** ===================================================================
310 */
311 u_yaw = c_mot->Value() * (tfl_speed * tfl_speed - bfl_speed * bfl_speed +
312 trr_speed * trr_speed - brr_speed * brr_speed -
313 tfr_speed * tfr_speed + bfr_speed * bfr_speed -
314 trl_speed * trl_speed + brl_speed * brl_speed);
315
316 /// Classical Nonlinear model of a quadrotor ( This is the w_z angular speed
317 /// of the quadri in the body frame). It is a discrete integrator
318 // state[0].W.z=(dT()/j_yaw->Value())* u_yaw +state[-1].W.z;//Osamah
319 state[0].W.z =
320 (dT() / j_yaw->Value()) * ((j_roll->Value() - j_pitch->Value()) *
321 state[-1].W.x * state[-1].W.y +
322 u_yaw) +
323 state[-1].W.z; // Majd
324
325 // state[0].W.z=(dT()/j_yaw->Value())*(u_yaw-f_air_lat->Value()*state[-1].W.z)+state[-1].W.z;
326
327 // compute quaternion from W
328 // Quaternion derivative: dQ = 0.5*(Q*Qw)
329 Quaternion dQ = state[-1].Quat.GetDerivative(state[0].W);
330
331 // Quaternion integration
332 state[0].Quat = state[-1].Quat + dQ * dT();
333 state[0].Quat.Normalize();
334
335 // Calculation of the thrust from the reference speed of motors
336 u_thrust =
337 k_mot->Value() * S->Value() *
338 (sigma->Value() * tfl_speed * tfl_speed +
339 sigma->Value() * tfr_speed * tfr_speed +
340 sigma->Value() * trl_speed * trl_speed +
341 sigma->Value() * trr_speed * trr_speed + bfl_speed * bfl_speed +
342 bfr_speed * bfr_speed + brl_speed * brl_speed + brr_speed * brr_speed);
343 Vector3D vect(0, 0, -u_thrust);
344 vect.Rotate(state[0].Quat);
345
346 /*
347 ** ===================================================================
348 ** x double integrator
349 **
350 ** ===================================================================
351 */
352 state[0].Pos.x =
353 (dT() * dT() / m->Value()) *
354 (vect.x -
355 f_air_lat->Value() * (state[-1].Pos.x - state[-2].Pos.x) / dT()) +
356 2 * state[-1].Pos.x - state[-2].Pos.x;
357 state[0].Vel.x = (state[0].Pos.x - state[-1].Pos.x) / dT();
358
359 /*
360 ** ===================================================================
361 ** y double integrator
362 **
363 ** ===================================================================
364 */
365 state[0].Pos.y =
366 (dT() * dT() / m->Value()) *
367 (vect.y -
368 f_air_lat->Value() * (state[-1].Pos.y - state[-2].Pos.y) / dT()) +
369 2 * state[-1].Pos.y - state[-2].Pos.y;
370 state[0].Vel.y = (state[0].Pos.y - state[-1].Pos.y) / dT();
371
372 /*
373 ** ===================================================================
374 ** z double integrator
375 **
376 ** ===================================================================
377 */
378 state[0].Pos.z =
379 (dT() * dT() / m->Value()) *
380 (vect.z +
381 f_air_vert->Value() * (state[-1].Pos.z - state[-2].Pos.z) / dT() +
382 m->Value() * G) +
383 2 * state[-1].Pos.z - state[-2].Pos.z;
384 state[0].Vel.z = (state[0].Pos.z - state[-1].Pos.z) / dT();
385
386#ifndef GL
387 if (state[0].Pos.z < 0)
388 state[0].Pos.z = 0;
389#endif
390}
391
392} // end namespace simulator
393} // end namespace flair
Note: See TracBrowser for help on using the repository browser.