[10] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[10] | 4 | // %flair:license}
|
---|
[8] | 5 | // created: 2013/03/25
|
---|
| 6 | // filename: Model_impl.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: classe definissant un modele a simuler
|
---|
| 14 | //
|
---|
| 15 | /*********************************************************************/
|
---|
| 16 |
|
---|
| 17 | #include "Model.h"
|
---|
| 18 | #include "Model_impl.h"
|
---|
| 19 | #include "Simulator.h"
|
---|
[158] | 20 | #include "Simulator_impl.h"
|
---|
[8] | 21 | #include "TabWidget.h"
|
---|
| 22 | #include "Tab.h"
|
---|
| 23 | #include "DoubleSpinBox.h"
|
---|
| 24 | #include "Vector3DSpinBox.h"
|
---|
| 25 | #include "SpinBox.h"
|
---|
| 26 | #include "CheckBox.h"
|
---|
| 27 | #include "cvmatrix.h"
|
---|
| 28 | #include "Euler.h"
|
---|
| 29 | #include <math.h>
|
---|
| 30 |
|
---|
| 31 | #ifdef GL
|
---|
| 32 | #include "ConditionVariable.h"
|
---|
| 33 | #include "Gui.h"
|
---|
| 34 | #include "Gui_impl.h"
|
---|
| 35 | #include <ISceneManager.h>
|
---|
| 36 | #include <ISceneNodeAnimatorCollisionResponse.h>
|
---|
| 37 | #include <IMetaTriangleSelector.h>
|
---|
| 38 | #include <IVideoDriver.h>
|
---|
| 39 | #include <ICameraSceneNode.h>
|
---|
[69] | 40 | #include "FollowMeCamera.h"
|
---|
| 41 |
|
---|
[8] | 42 | using namespace irr;
|
---|
| 43 | using namespace video;
|
---|
| 44 | using namespace scene;
|
---|
| 45 | using namespace core;
|
---|
| 46 | using namespace io;
|
---|
| 47 | #endif
|
---|
| 48 |
|
---|
| 49 | using namespace flair::core;
|
---|
| 50 | using namespace flair::gui;
|
---|
| 51 | using namespace flair::simulator;
|
---|
| 52 |
|
---|
[15] | 53 | #ifdef GL
|
---|
[158] | 54 | Model_impl::Model_impl(Model *self, std::string name,uint32_t modelId)
|
---|
| 55 | : ISceneNode(getGui()->getSceneManager()->getRootSceneNode(), getGui()->getSceneManager(), -1),
|
---|
[202] | 56 | Thread(self, name, 99), vrpn_Tracker(name.c_str(), getSimulator()->pimpl_)
|
---|
[8] | 57 |
|
---|
| 58 | #else
|
---|
[158] | 59 | Model_impl::Model_impl(Model *self, std::string name,uint32_t modelId)
|
---|
[202] | 60 | : Thread(self, name, 99), vrpn_Tracker(name.c_str(), getSimulator()->pimpl_)
|
---|
[15] | 61 | #endif
|
---|
[8] | 62 | {
|
---|
[15] | 63 | this->self = self;
|
---|
[158] | 64 | this->modelId=modelId;
|
---|
[8] | 65 |
|
---|
| 66 | #ifdef GL
|
---|
[15] | 67 | // for sync with gui
|
---|
| 68 | cond = new ConditionVariable(this, name);
|
---|
| 69 | sync_count = 0;
|
---|
[8] | 70 |
|
---|
[15] | 71 | // collisions
|
---|
| 72 | collision_mutex = new Mutex(this);
|
---|
| 73 | collision_occured = false;
|
---|
[8] | 74 |
|
---|
[15] | 75 | // selector for collisions
|
---|
| 76 | selector = getSceneManager()->createTriangleSelectorFromBoundingBox(this);
|
---|
| 77 | setTriangleSelector(selector);
|
---|
| 78 | meta_selector = getSceneManager()->createMetaTriangleSelector();
|
---|
[8] | 79 |
|
---|
[15] | 80 | anim = getSceneManager()->createCollisionResponseAnimator(
|
---|
| 81 | meta_selector, this, vector3df(1, 1, 1), vector3df(0, 0, 0),
|
---|
| 82 | vector3df(0, 0, 0));
|
---|
| 83 | addAnimator(anim);
|
---|
[8] | 84 |
|
---|
[15] | 85 | // camera
|
---|
[70] | 86 | camera = new FollowMeCamera(this,name);
|
---|
[8] | 87 |
|
---|
[15] | 88 | position_init = false;
|
---|
[8] | 89 | #endif
|
---|
| 90 |
|
---|
[15] | 91 | // init user interface
|
---|
| 92 | Tab *tab = new Tab(getSimulator()->GetTabWidget(), ObjectName());
|
---|
| 93 | tabwidget = new TabWidget(tab->NewRow(), "tabs");
|
---|
| 94 | Tab *sampl = new Tab(tabwidget, "sampling");
|
---|
| 95 | dT = new DoubleSpinBox(sampl->NewRow(), "Tech (s):", 0.001, 1, 0.001, 3);
|
---|
| 96 | Tab *layout = new Tab(tabwidget, "optitrack");
|
---|
| 97 | enable_opti = new CheckBox(layout->NewRow(), "enabled");
|
---|
| 98 | Tab *init = new Tab(tabwidget, "init");
|
---|
| 99 | pos_init = new Vector3DSpinBox(init->NewRow(), "position", -50, 50, 1);
|
---|
| 100 | yaw_init = new SpinBox(init->NewRow(), "yaw (deg):", -180, 180, 10);
|
---|
[8] | 101 |
|
---|
[15] | 102 | // modele
|
---|
| 103 | states_mutex = new Mutex(this);
|
---|
| 104 | self->state[0].Pos = pos_init->Value();
|
---|
| 105 | self->state[0].Vel.x = 0;
|
---|
| 106 | self->state[0].Vel.y = 0;
|
---|
| 107 | self->state[0].Vel.z = 0;
|
---|
| 108 | self->state[0].Quat = ComputeInitRotation(Quaternion(1, 0, 0, 0));
|
---|
| 109 | self->state[0].W.x = 0;
|
---|
| 110 | self->state[0].W.y = 0;
|
---|
| 111 | self->state[0].W.z = 0;
|
---|
[8] | 112 |
|
---|
[15] | 113 | self->state[-1] = self->state[0];
|
---|
| 114 | self->state[-2] = self->state[0];
|
---|
[8] | 115 |
|
---|
[186] | 116 | cvmatrix_descriptor *desc = new cvmatrix_descriptor(19, 1);
|
---|
[15] | 117 | desc->SetElementName(0, 0, "q0");
|
---|
| 118 | desc->SetElementName(1, 0, "q1");
|
---|
| 119 | desc->SetElementName(2, 0, "q2");
|
---|
| 120 | desc->SetElementName(3, 0, "q3");
|
---|
| 121 | desc->SetElementName(4, 0, "x");
|
---|
| 122 | desc->SetElementName(5, 0, "y");
|
---|
| 123 | desc->SetElementName(6, 0, "z");
|
---|
| 124 | desc->SetElementName(7, 0, "wx");
|
---|
| 125 | desc->SetElementName(8, 0, "wy");
|
---|
| 126 | desc->SetElementName(9, 0, "wz");
|
---|
| 127 | desc->SetElementName(10, 0, "vx");
|
---|
| 128 | desc->SetElementName(11, 0, "vy");
|
---|
| 129 | desc->SetElementName(12, 0, "vz");
|
---|
[186] | 130 | desc->SetElementName(13, 0, "ax");
|
---|
| 131 | desc->SetElementName(14, 0, "ay");
|
---|
| 132 | desc->SetElementName(15, 0, "az");
|
---|
| 133 | desc->SetElementName(16, 0, "mx");
|
---|
| 134 | desc->SetElementName(17, 0, "my");
|
---|
| 135 | desc->SetElementName(18, 0, "mz");
|
---|
[15] | 136 | output = new cvmatrix(this, desc, floatType, "state");
|
---|
[148] | 137 | delete desc;
|
---|
[8] | 138 |
|
---|
[15] | 139 | self->AddDataToLog(output);
|
---|
[158] | 140 |
|
---|
| 141 | getSimulator()->pimpl_->models.push_back(self);
|
---|
[8] | 142 | }
|
---|
| 143 |
|
---|
[15] | 144 | Model_impl::~Model_impl() {
|
---|
| 145 | SafeStop();
|
---|
| 146 | Join();
|
---|
[8] | 147 | #ifdef GL
|
---|
[15] | 148 | remove(); // remove ISceneNode
|
---|
[8] | 149 | #endif
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | Quaternion Model_impl::ComputeInitRotation(Quaternion quat_in) {
|
---|
[15] | 153 | Quaternion yaw_rot_quat;
|
---|
| 154 | Euler yaw_rot_euler(0, 0, Euler::ToRadian(yaw_init->Value()));
|
---|
| 155 | yaw_rot_euler.ToQuaternion(yaw_rot_quat);
|
---|
| 156 | return yaw_rot_quat * quat_in;
|
---|
[8] | 157 | }
|
---|
| 158 |
|
---|
[15] | 159 | void Model_impl::mainloop(void) {
|
---|
| 160 | if (enable_opti->Value() == false)
|
---|
| 161 | return;
|
---|
| 162 | vrpn_gettimeofday(&_timestamp, NULL);
|
---|
| 163 | vrpn_Tracker::timestamp = _timestamp;
|
---|
[8] | 164 |
|
---|
[15] | 165 | // change to vrpn reference
|
---|
| 166 | states_mutex->GetMutex();
|
---|
| 167 | Quaternion quat = getSimulator()->ToVRPNReference(self->state[0].Quat);
|
---|
[167] | 168 | Vector3D<double> position = getSimulator()->ToVRPNReference(self->state[0].Pos);
|
---|
[15] | 169 | states_mutex->ReleaseMutex();
|
---|
[8] | 170 |
|
---|
[15] | 171 | pos[0] = position.x;
|
---|
| 172 | pos[1] = position.y;
|
---|
| 173 | pos[2] = position.z;
|
---|
| 174 | // warning: d_quat is defined as (qx,qy,qz,qw), which is different from
|
---|
| 175 | // flair::core::Quaternion
|
---|
| 176 | d_quat[0] = quat.q1;
|
---|
| 177 | d_quat[1] = quat.q2;
|
---|
| 178 | d_quat[2] = quat.q3;
|
---|
| 179 | d_quat[3] = quat.q0;
|
---|
[8] | 180 |
|
---|
[15] | 181 | char msgbuf[1000];
|
---|
[8] | 182 |
|
---|
[15] | 183 | d_sensor = 0;
|
---|
[8] | 184 |
|
---|
[15] | 185 | int len = vrpn_Tracker::encode_to(msgbuf);
|
---|
[8] | 186 |
|
---|
[15] | 187 | if (d_connection->pack_message(len, _timestamp, position_m_id, d_sender_id,
|
---|
| 188 | msgbuf, vrpn_CONNECTION_LOW_LATENCY)) {
|
---|
| 189 | fprintf(stderr, "can't write message: tossing\n");
|
---|
| 190 | }
|
---|
[8] | 191 |
|
---|
[15] | 192 | server_mainloop();
|
---|
[8] | 193 | }
|
---|
| 194 |
|
---|
| 195 | #ifdef GL
|
---|
[15] | 196 | ITriangleSelector *Model_impl::TriangleSelector(void) { return selector; }
|
---|
[8] | 197 |
|
---|
[15] | 198 | IMetaTriangleSelector *Model_impl::MetaTriangleSelector(void) {
|
---|
| 199 | return meta_selector;
|
---|
[8] | 200 | }
|
---|
| 201 |
|
---|
[15] | 202 | void Model_impl::UpdatePos(void) {
|
---|
| 203 | vector3df nodePosition;
|
---|
| 204 | Quaternion nodeOrientation;
|
---|
| 205 | Euler euler;
|
---|
[8] | 206 |
|
---|
[15] | 207 | states_mutex->GetMutex();
|
---|
[163] | 208 | //self->state[0].Pos.x=0;
|
---|
| 209 | //self->state[0].Pos.y=0;
|
---|
[15] | 210 | nodePosition = ToIrrlichtCoordinates(self->state[0].Pos);
|
---|
| 211 | nodeOrientation = ToIrrlichtOrientation(self->state[0].Quat);
|
---|
| 212 | states_mutex->ReleaseMutex();
|
---|
[8] | 213 |
|
---|
[15] | 214 | setPosition(nodePosition);
|
---|
[8] | 215 |
|
---|
[15] | 216 | nodeOrientation.ToEuler(euler);
|
---|
[151] | 217 | ISceneNode::setRotation(Euler::ToDegree(1) * vector3df(euler.roll,euler.pitch, euler.yaw));
|
---|
[8] | 218 |
|
---|
[15] | 219 | if (position_init == false) {
|
---|
| 220 | anim->setTargetNode(this); // a faire pour se teleporter sans les collisions
|
---|
| 221 | position_init = true;
|
---|
| 222 | }
|
---|
[8] | 223 |
|
---|
[15] | 224 | self->AnimateModel();
|
---|
[8] | 225 | }
|
---|
| 226 |
|
---|
[15] | 227 | void Model_impl::CheckCollision(void) {
|
---|
| 228 | // TODO: setEllipsoidRadius should be called in Model::setScale
|
---|
| 229 | // but we need to call recalculateBoundingBox
|
---|
| 230 | anim->setEllipsoidRadius(getTransformedBoundingBox().getExtent());
|
---|
[8] | 231 |
|
---|
[15] | 232 | if (anim->collisionOccurred() == true) {
|
---|
| 233 | vector3df pos;
|
---|
| 234 | vector3df pos_rel;
|
---|
| 235 | vector3df nodePosition;
|
---|
| 236 | pos = anim->getCollisionPoint();
|
---|
| 237 | nodePosition = getPosition();
|
---|
| 238 | pos_rel = pos - nodePosition;
|
---|
| 239 | // printf("collision %f %f %f\n",pos.X,pos.Y,pos.Z);
|
---|
| 240 | // printf("drone %f %f %f\n",nodePosition.X,nodePosition.Y,nodePosition.Z);
|
---|
| 241 | // printf("rel %f %f %f\n",pos_rel.X,pos_rel.Z,pos_rel.Y);
|
---|
[8] | 242 |
|
---|
| 243 | collision_mutex->GetMutex();
|
---|
[15] | 244 | collision_occured = true;
|
---|
| 245 | collision_point = ToSimulatorCoordinates(nodePosition);
|
---|
[8] | 246 | collision_mutex->ReleaseMutex();
|
---|
[15] | 247 | }
|
---|
[8] | 248 | }
|
---|
[15] | 249 |
|
---|
| 250 | void Model_impl::CollisionHandler(void) {
|
---|
| 251 | collision_mutex->GetMutex();
|
---|
| 252 | if (collision_occured == true) {
|
---|
| 253 | collision_occured = false;
|
---|
| 254 | states_mutex->GetMutex();
|
---|
| 255 | self->state[0].Pos = collision_point;
|
---|
| 256 | self->state[-1].Pos = self->state[0].Pos;
|
---|
| 257 | self->state[-2].Pos = self->state[0].Pos;
|
---|
| 258 | states_mutex->ReleaseMutex();
|
---|
| 259 | }
|
---|
| 260 | collision_mutex->ReleaseMutex();
|
---|
[8] | 261 | }
|
---|
| 262 |
|
---|
[15] | 263 | void Model_impl::OnRegisterSceneNode(void) {
|
---|
| 264 | if (IsVisible)
|
---|
| 265 | SceneManager->registerNodeForRendering(this);
|
---|
[8] | 266 |
|
---|
[15] | 267 | ISceneNode::OnRegisterSceneNode();
|
---|
| 268 | }
|
---|
[8] | 269 |
|
---|
[15] | 270 | void Model_impl::render(void) {
|
---|
| 271 | IVideoDriver *driver = SceneManager->getVideoDriver();
|
---|
| 272 | driver->setTransform(ETS_WORLD, AbsoluteTransformation);
|
---|
[8] | 273 | }
|
---|
| 274 |
|
---|
[15] | 275 | // le premier arrive attend l'autre
|
---|
| 276 | void Model_impl::SynchronizationPoint() {
|
---|
| 277 | cond->GetMutex();
|
---|
| 278 | sync_count++;
|
---|
[8] | 279 |
|
---|
[15] | 280 | if (sync_count < 2) {
|
---|
| 281 | cond->CondWait();
|
---|
| 282 | } else {
|
---|
| 283 | cond->CondSignal();
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | cond->ReleaseMutex();
|
---|
| 287 | }
|
---|
| 288 | #endif // GL
|
---|
| 289 |
|
---|
| 290 | void Model_impl::Run(void) {
|
---|
| 291 | // Ask Xenomai to warn us upon switches to secondary mode.
|
---|
| 292 | WarnUponSwitches(true);
|
---|
| 293 |
|
---|
[8] | 294 | #ifdef GL
|
---|
[15] | 295 | // synchronize with gui
|
---|
| 296 | SynchronizationPoint();
|
---|
[8] | 297 | #endif
|
---|
| 298 |
|
---|
[15] | 299 | SetPeriodMS(dT->Value() * 1000.);
|
---|
[8] | 300 |
|
---|
[15] | 301 | while (!ToBeStopped()) {
|
---|
| 302 | if (dT->ValueChanged())
|
---|
| 303 | SetPeriodMS(dT->Value() * 1000.);
|
---|
| 304 | WaitPeriod();
|
---|
[8] | 305 |
|
---|
| 306 | #ifdef GL
|
---|
[15] | 307 | CollisionHandler();
|
---|
[8] | 308 | #endif
|
---|
[15] | 309 | states_mutex->GetMutex();
|
---|
| 310 | self->CalcModel();
|
---|
[8] | 311 |
|
---|
[15] | 312 | output->GetMutex();
|
---|
| 313 | output->SetValueNoMutex(0, 0, self->state[0].Quat.q0);
|
---|
| 314 | output->SetValueNoMutex(1, 0, self->state[0].Quat.q1);
|
---|
| 315 | output->SetValueNoMutex(2, 0, self->state[0].Quat.q2);
|
---|
| 316 | output->SetValueNoMutex(3, 0, self->state[0].Quat.q3);
|
---|
| 317 | output->SetValueNoMutex(4, 0, self->state[0].Pos.x);
|
---|
| 318 | output->SetValueNoMutex(5, 0, self->state[0].Pos.y);
|
---|
| 319 | output->SetValueNoMutex(6, 0, self->state[0].Pos.z);
|
---|
| 320 | output->SetValueNoMutex(7, 0, self->state[0].W.x);
|
---|
| 321 | output->SetValueNoMutex(8, 0, self->state[0].W.y);
|
---|
| 322 | output->SetValueNoMutex(9, 0, self->state[0].W.z);
|
---|
| 323 | output->SetValueNoMutex(10, 0, self->state[0].Vel.x);
|
---|
| 324 | output->SetValueNoMutex(11, 0, self->state[0].Vel.y);
|
---|
| 325 | output->SetValueNoMutex(12, 0, self->state[0].Vel.z);
|
---|
[186] | 326 | //todo: put acc and mag
|
---|
| 327 | output->SetValueNoMutex(13, 0, 0);
|
---|
| 328 | output->SetValueNoMutex(14, 0, 0);
|
---|
| 329 | output->SetValueNoMutex(15, 0, 0);
|
---|
| 330 | output->SetValueNoMutex(16, 0, 0);
|
---|
| 331 | output->SetValueNoMutex(17, 0, 0);
|
---|
| 332 | output->SetValueNoMutex(18, 0, 0);
|
---|
[15] | 333 | output->ReleaseMutex();
|
---|
| 334 | output->SetDataTime(GetTime());
|
---|
[8] | 335 |
|
---|
[15] | 336 | self->state.Update();
|
---|
[8] | 337 |
|
---|
[15] | 338 | if (pos_init->ValueChanged() || yaw_init->ValueChanged()) {
|
---|
| 339 | self->state[-1].Quat = ComputeInitRotation(Quaternion(1, 0, 0, 0));
|
---|
| 340 | self->state[-2].Quat = ComputeInitRotation(Quaternion(1, 0, 0, 0));
|
---|
| 341 | self->state[-1].Pos = pos_init->Value();
|
---|
| 342 | self->state[-2].Pos = self->state[-1].Pos;
|
---|
[8] | 343 | #ifdef GL
|
---|
[15] | 344 | position_init = false;
|
---|
[8] | 345 | #endif
|
---|
[15] | 346 | }
|
---|
[8] | 347 |
|
---|
[15] | 348 | states_mutex->ReleaseMutex();
|
---|
[8] | 349 |
|
---|
[15] | 350 | self->ProcessUpdate(output);
|
---|
| 351 | }
|
---|
[8] | 352 |
|
---|
[15] | 353 | WarnUponSwitches(false);
|
---|
[8] | 354 | }
|
---|