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

Last change on this file since 442 was 370, checked in by Sanahuja Guillaume, 4 years ago

add abilitiy to change x4 and x8 arm color from ground station

File size: 16.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: 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 <SimuBldc.h>
19#include <TabWidget.h>
20#include <Tab.h>
21#include <DoubleSpinBox.h>
22#include <SpinBox.h>
23#include <GroupBox.h>
24#include <math.h>
25#ifdef GL
26#include <ISceneManager.h>
27#include <IMeshManipulator.h>
28#include "Blade.h"
29#include "MeshSceneNode.h"
30#include "Gui.h"
31#include <Mutex.h>
32#endif
33
34#define K_MOT 0.4f // blade animation
35#define G (float)9.81 // gravity ( N/(m/s²) )
36
37#ifdef GL
38using namespace irr::video;
39using namespace irr::scene;
40using namespace irr::core;
41#endif
42using namespace flair::core;
43using namespace flair::gui;
44using namespace flair::actuator;
45
46namespace flair {
47namespace simulator {
48
49X8::X8(std::string name, uint32_t modelId): Model( name,modelId) {
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 motorTimeout = new SpinBox(setup_tab->NewRow(), "motor timeout:","ms", 0, 1000, 100,100);
87
88 Tab *visual_tab = new Tab(GetTabWidget(), "visual");
89 armColorR = new SpinBox(visual_tab->NewRow(), "arm color (R):", 0, 255, 1,255);
90 armColorG = new SpinBox(visual_tab->LastRowLastCol(), "arm color (G):", 0, 255, 1,0);
91 armColorB = new SpinBox(visual_tab->LastRowLastCol(), "arm color (B):", 0, 255, 1,0);
92
93 motors = new SimuBldc(this, name, 8, modelId,0);
94
95 SetIsReady(true);
96}
97
98void X8::Draw() {
99#ifdef GL
100
101 // create unite (1m=100cm) UAV; scale will be adapted according to arm_length
102 // parameter
103 // note that the frame used is irrlicht one:
104 // left handed, North East Up
105
106 const IGeometryCreator *geo;
107 geo = getGui()->getSceneManager()->getGeometryCreator();
108
109 // cylinders are aligned with y axis
110 colored_arm = geo->createCylinderMesh(2.5, 100, 16, SColor(0, armColorR->Value(), armColorG->Value(), armColorB->Value()));
111 IMesh *black_arm = geo->createCylinderMesh(2.5, 100, 16, SColor(0, 128, 128, 128));
112 IMesh *motor = geo->createCylinderMesh(7.5, 15, 16); //,SColor(0, 128, 128, 128));
113 // geo->drop();
114
115 ITexture *texture = getGui()->getTexture("carbone.jpg");
116 MeshSceneNode *fl_arm = new MeshSceneNode(this, colored_arm, vector3df(0, 0, 0),
117 vector3df(0, 0, -135));
118 MeshSceneNode *fr_arm = new MeshSceneNode(this, colored_arm, vector3df(0, 0, 0),
119 vector3df(0, 0, -45));
120 MeshSceneNode *rl_arm = new MeshSceneNode(this, black_arm, vector3df(0, 0, 0),
121 vector3df(0, 0, 135), texture);
122 MeshSceneNode *rr_arm = new MeshSceneNode(this, black_arm, vector3df(0, 0, 0),
123 vector3df(0, 0, 45), texture);
124
125 texture = getGui()->getTexture("metal047.jpg");
126 MeshSceneNode *tfl_motor = new MeshSceneNode(this, motor, vector3df(70.71, -70.71, 2.5),
127 vector3df(90, 0, 0), texture);
128 MeshSceneNode *tfr_motor = new MeshSceneNode(this, motor, vector3df(70.71, 70.71, 2.5),
129 vector3df(90, 0, 0), texture);
130 MeshSceneNode *trl_motor = new MeshSceneNode(this, motor, vector3df(-70.71, -70.71, 2.5),
131 vector3df(90, 0, 0), texture);
132 MeshSceneNode *trr_motor = new MeshSceneNode(this, motor, vector3df(-70.71, 70.71, 2.5),
133 vector3df(90, 0, 0), texture);
134
135 MeshSceneNode *bfl_motor = new MeshSceneNode(this, motor, vector3df(70.71, -70.71, -17.5),
136 vector3df(90, 0, 0), texture);
137 MeshSceneNode *bfr_motor = new MeshSceneNode(this, motor, vector3df(70.71, 70.71, -17.5),
138 vector3df(90, 0, 0), texture);
139 MeshSceneNode *brl_motor = new MeshSceneNode(this, motor, vector3df(-70.71, -70.71, -17.5),
140 vector3df(90, 0, 0), texture);
141 MeshSceneNode *brr_motor = new MeshSceneNode(this, motor, vector3df(-70.71, 70.71, -17.5),
142 vector3df(90, 0, 0), texture);
143
144 tfl_blade = new Blade(this, vector3df(70.71, -70.71, 17.5));
145 tfr_blade = new Blade(this, vector3df(70.71, 70.71, 17.5), vector3df(0, 0, 0), true);
146 trl_blade = new Blade(this, vector3df(-70.71, -70.71, 17.5), vector3df(0, 0, 0), true);
147 trr_blade = new Blade(this, vector3df(-70.71, 70.71, 17.5));
148
149 bfl_blade = new Blade(this, vector3df(70.71, -70.71, -17.5));
150 bfr_blade = new Blade(this, vector3df(70.71, 70.71, -17.5), vector3df(0, 0, 0), true);
151 brl_blade = new Blade(this, vector3df(-70.71, -70.71, -17.5), vector3df(0, 0, 0),true);
152 brr_blade = new Blade(this, vector3df(-70.71, 70.71, -17.5));
153
154 motor_speed_mutex = new Mutex(this);
155 for (int i = 0; i < 8; i++)
156 motor_speed[i] = 0;
157 ExtraDraw();
158#endif
159}
160
161X8::~X8() {
162 // les objets irrlicht seront automatiquement detruits (moteurs, helices,
163 // pales) par parenté
164}
165
166#ifdef GL
167void X8::AnimateModel(void) {
168 motor_speed_mutex->GetMutex();
169 tfl_blade->SetRotationSpeed(K_MOT * vector3df(0, 0,motor_speed[0]));
170 tfr_blade->SetRotationSpeed(-K_MOT * vector3df(0, 0,motor_speed[1]));
171 trl_blade->SetRotationSpeed(-K_MOT * vector3df(0, 0,motor_speed[2]));
172 trr_blade->SetRotationSpeed(K_MOT * vector3df(0, 0,motor_speed[3]));
173
174 bfl_blade->SetRotationSpeed(-K_MOT * vector3df(0, 0,motor_speed[4]));
175 bfr_blade->SetRotationSpeed(K_MOT * vector3df(0, 0,motor_speed[5]));
176 brl_blade->SetRotationSpeed(K_MOT * vector3df(0, 0,motor_speed[6]));
177 brr_blade->SetRotationSpeed(-K_MOT * vector3df(0, 0,motor_speed[7]));
178 motor_speed_mutex->ReleaseMutex();
179
180 if (armColorR->ValueChanged() == true || armColorG->ValueChanged() == true || armColorB->ValueChanged() == true) {
181 getGui()->getSceneManager()->getMeshManipulator()->setVertexColors(colored_arm, SColor(0,armColorR->Value(), armColorG->Value(), armColorB->Value()));
182 }
183
184
185 // adapt UAV size
186 if (arm_length->ValueChanged() == true) {
187 setScale(arm_length->Value());
188 }
189}
190
191size_t X8::dbtSize(void) const {
192 return 6 * sizeof(float) + 4 * sizeof(float); // 6ddl+4helices
193}
194
195void X8::WritedbtBuf(
196 char *dbtbuf) { /*
197 float *buf=(float*)dbtbuf;
198 vector3df vect=getPosition();
199 memcpy(buf,&vect.X,sizeof(float));
200 buf++;
201 memcpy(buf,&vect.Y,sizeof(float));
202 buf++;
203 memcpy(buf,&vect.Z,sizeof(float));
204 buf++;
205 vect=getRotation();
206 memcpy(buf,&vect.X,sizeof(float));
207 buf++;
208 memcpy(buf,&vect.Y,sizeof(float));
209 buf++;
210 memcpy(buf,&vect.Z,sizeof(float));
211 buf++;
212 memcpy(buf,&motors,sizeof(rtsimu_motors));*/
213}
214
215void X8::ReaddbtBuf(
216 char *dbtbuf) { /*
217 float *buf=(float*)dbtbuf;
218 vector3df vect;
219 memcpy(&vect.X,buf,sizeof(float));
220 buf++;
221 memcpy(&vect.Y,buf,sizeof(float));
222 buf++;
223 memcpy(&vect.Z,buf,sizeof(float));
224 buf++;
225 setPosition(vect);
226 memcpy(&vect.X,buf,sizeof(float));
227 buf++;
228 memcpy(&vect.Y,buf,sizeof(float));
229 buf++;
230 memcpy(&vect.Z,buf,sizeof(float));
231 buf++;
232 ((ISceneNode*)(this))->setRotation(vect);
233 memcpy(&motors,buf,sizeof(rtsimu_motors));
234 AnimateModele();*/
235}
236#endif // GL
237
238// states are computed on fixed frame NED
239// x north
240// y east
241// z down
242void X8::CalcModel(void) {
243 float tfl_speed, tfr_speed, trl_speed, trr_speed;
244 float bfl_speed, bfr_speed, brl_speed, brr_speed;
245 float u_roll, u_pitch, u_yaw, u_thrust;
246 float omega;
247 Time motorTime;
248#ifdef GL
249 motor_speed_mutex->GetMutex();
250#endif // GL
251 motors->GetSpeeds(motor_speed,&motorTime);
252 if((GetTime()-motorTime)/1000000>motorTimeout->Value()) {
253 for(int i=0;i<8;i++) {
254 if(motor_speed[i]!=0) {
255 //Printf("timout\n");
256 for(int i=0;i<8;i++) motor_speed[i]=0;
257 break;
258 }
259 }
260 }
261#ifdef GL
262 motor_speed_mutex->ReleaseMutex();
263#endif // GL
264 tfl_speed = motor_speed[0];
265 tfr_speed = motor_speed[1];
266 trl_speed = motor_speed[2];
267 trr_speed = motor_speed[3];
268 bfl_speed = motor_speed[4];
269 bfr_speed = motor_speed[5];
270 brl_speed = motor_speed[6];
271 brr_speed = motor_speed[7];
272
273 omega = tfl_speed + brl_speed + trr_speed + bfr_speed - bfl_speed -
274 trl_speed - brr_speed - tfr_speed;
275
276 /*
277 ** ===================================================================
278 ** u roll: roll torque
279 **
280 ** ===================================================================
281 */
282
283 u_roll = arm_length->Value() * k_mot->Value() *
284 (sigma->Value() * tfl_speed * tfl_speed + bfl_speed * bfl_speed +
285 sigma->Value() * trl_speed * trl_speed + brl_speed * brl_speed -
286 sigma->Value() * tfr_speed * tfr_speed - bfr_speed * bfr_speed -
287 sigma->Value() * trr_speed * trr_speed - brr_speed * brr_speed) *
288 sqrtf(2) / 2;
289
290 /// Classical Nonlinear model of a quadrotor ( This is the w_x angular speed
291 /// of the quadri in the body frame). It is a discrete integrator
292 // 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
293 // + u_roll) +state[-1].W.x;//Osamah
294 state[0].W.x =
295 (dT() / j_roll->Value()) *
296 ((j_pitch->Value() - j_yaw->Value()) * state[-1].W.y * state[-1].W.z -
297 j_r->Value() * state[-1].W.y * omega + u_roll) +
298 state[-1].W.x; // Majd
299
300 // 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;
301
302 /*
303 ** ===================================================================
304 ** u pitch : pitch torque
305 **
306 ** ===================================================================
307 */
308 u_pitch = arm_length->Value() * k_mot->Value() *
309 (sigma->Value() * tfl_speed * tfl_speed + bfl_speed * bfl_speed +
310 sigma->Value() * tfr_speed * tfr_speed + bfr_speed * bfr_speed -
311 sigma->Value() * trl_speed * trl_speed - brl_speed * brl_speed -
312 sigma->Value() * trr_speed * trr_speed - brr_speed * brr_speed) *
313 sqrtf(2) / 2;
314
315 /// Classical Nonlinear model of a quadrotor ( This is the w_y angular speed
316 /// of the quadri in the body frame). It is a discrete integrator
317 // 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
318 // + u_pitch)+state[-1].W.y;//Osamah
319 state[0].W.y =
320 (dT() / j_pitch->Value()) *
321 ((j_yaw->Value() - j_roll->Value()) * state[-1].W.x * state[-1].W.z -
322 j_r->Value() * state[-1].W.x * omega + u_pitch) +
323 state[-1].W.y; // Majd
324
325 // 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;
326
327 /*
328 ** ===================================================================
329 ** u yaw : yaw torque
330 **
331 ** ===================================================================
332 */
333 u_yaw = c_mot->Value() * (tfl_speed * tfl_speed - bfl_speed * bfl_speed +
334 trr_speed * trr_speed - brr_speed * brr_speed -
335 tfr_speed * tfr_speed + bfr_speed * bfr_speed -
336 trl_speed * trl_speed + brl_speed * brl_speed);
337
338 /// Classical Nonlinear model of a quadrotor ( This is the w_z angular speed
339 /// of the quadri in the body frame). It is a discrete integrator
340 // state[0].W.z=(dT()/j_yaw->Value())* u_yaw +state[-1].W.z;//Osamah
341 state[0].W.z =
342 (dT() / j_yaw->Value()) * ((j_roll->Value() - j_pitch->Value()) *
343 state[-1].W.x * state[-1].W.y +
344 u_yaw) +
345 state[-1].W.z; // Majd
346
347 // state[0].W.z=(dT()/j_yaw->Value())*(u_yaw-f_air_lat->Value()*state[-1].W.z)+state[-1].W.z;
348
349 // compute quaternion from W
350 // Quaternion derivative: dQ = 0.5*(Q*Qw)
351 Quaternion dQ = state[-1].Quat.GetDerivative(state[0].W);
352
353 // Quaternion integration
354 state[0].Quat = state[-1].Quat + dQ * dT();
355 state[0].Quat.Normalize();
356
357 // Calculation of the thrust from the reference speed of motors
358 u_thrust =
359 k_mot->Value() * S->Value() *
360 (sigma->Value() * tfl_speed * tfl_speed +
361 sigma->Value() * tfr_speed * tfr_speed +
362 sigma->Value() * trl_speed * trl_speed +
363 sigma->Value() * trr_speed * trr_speed + bfl_speed * bfl_speed +
364 bfr_speed * bfr_speed + brl_speed * brl_speed + brr_speed * brr_speed);
365 Vector3D<double> vect(0, 0, -u_thrust);
366 vect.Rotate(state[0].Quat);
367
368 /*
369 ** ===================================================================
370 ** x double integrator
371 **
372 ** ===================================================================
373 */
374 state[0].Pos.x =
375 (dT() * dT() / m->Value()) *
376 (vect.x -
377 f_air_lat->Value() * (state[-1].Pos.x - state[-2].Pos.x) / dT()) +
378 2 * state[-1].Pos.x - state[-2].Pos.x;
379 state[0].Vel.x = (state[0].Pos.x - state[-1].Pos.x) / dT();
380
381 /*
382 ** ===================================================================
383 ** y double integrator
384 **
385 ** ===================================================================
386 */
387 state[0].Pos.y =
388 (dT() * dT() / m->Value()) *
389 (vect.y -
390 f_air_lat->Value() * (state[-1].Pos.y - state[-2].Pos.y) / dT()) +
391 2 * state[-1].Pos.y - state[-2].Pos.y;
392 state[0].Vel.y = (state[0].Pos.y - state[-1].Pos.y) / dT();
393
394 /*
395 ** ===================================================================
396 ** z double integrator
397 **
398 ** ===================================================================
399 */
400 state[0].Pos.z =
401 (dT() * dT() / m->Value()) *
402 (vect.z +
403 f_air_vert->Value() * (state[-1].Pos.z - state[-2].Pos.z) / dT() +
404 m->Value() * G) +
405 2 * state[-1].Pos.z - state[-2].Pos.z;
406 state[0].Vel.z = (state[0].Pos.z - state[-1].Pos.z) / dT();
407
408#ifndef GL
409 if (state[0].Pos.z < 0)
410 state[0].Pos.z = 0;
411#endif
412}
413
414} // end namespace simulator
415} // end namespace flair
Note: See TracBrowser for help on using the repository browser.