[286] | 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: 2018/12/11 |
---|
| 6 | // filename: UavVrpnObject_impl.cpp |
---|
| 7 | // |
---|
| 8 | // author: Guillaume Sanahuja |
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253 |
---|
| 10 | // |
---|
| 11 | // version: $Id: $ |
---|
| 12 | // |
---|
| 13 | // purpose: uav vrpn object, can display a real vrpn object in a simulated environment |
---|
| 14 | // |
---|
| 15 | /*********************************************************************/ |
---|
| 16 | #ifdef GL |
---|
| 17 | |
---|
| 18 | #include "UavVrpnObject_impl.h" |
---|
| 19 | #include "UavVrpnObject.h" |
---|
| 20 | #include <TabWidget.h> |
---|
| 21 | #include <Tab.h> |
---|
| 22 | #include <DoubleSpinBox.h> |
---|
| 23 | #include "Simulator.h" |
---|
| 24 | #include <ISceneManager.h> |
---|
| 25 | #include <IVideoDriver.h> |
---|
| 26 | #include "Blade.h" |
---|
| 27 | #include "MeshSceneNode.h" |
---|
| 28 | #include "Gui.h" |
---|
| 29 | #include "FollowMeCamera.h" |
---|
| 30 | #include <Matrix.h> |
---|
| 31 | #include <Quaternion.h> |
---|
| 32 | #include <Euler.h> |
---|
| 33 | |
---|
| 34 | #define MOTOR_SPEED 100 |
---|
| 35 | |
---|
| 36 | using namespace irr::video; |
---|
| 37 | using namespace irr::scene; |
---|
| 38 | using namespace irr::core; |
---|
| 39 | using namespace flair::core; |
---|
| 40 | using namespace flair::simulator; |
---|
| 41 | using namespace flair::gui; |
---|
| 42 | |
---|
| 43 | UavVrpnObject_impl::UavVrpnObject_impl(UavVrpnObject *self,std::string name) |
---|
| 44 | : IODevice(self,name), |
---|
| 45 | ISceneNode(getGui()->getSceneManager()->getRootSceneNode(), getGui()->getSceneManager(), -1) { |
---|
| 46 | this->self=self; |
---|
| 47 | |
---|
| 48 | // init user interface |
---|
| 49 | Tab *tab = new Tab(getSimulator()->GetTabWidget(), ObjectName()); |
---|
| 50 | arm_length = new DoubleSpinBox(tab->LastRowLastCol(), "arm length (m):",0, 2, 0.1); |
---|
| 51 | |
---|
| 52 | // camera |
---|
| 53 | FollowMeCamera* camera = new FollowMeCamera(this,name); |
---|
| 54 | |
---|
| 55 | Draw(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | UavVrpnObject_impl::~UavVrpnObject_impl() { |
---|
| 59 | // les objets irrlicht seront automatiquement detruits (moteurs, helices, |
---|
| 60 | // pales) par parenté |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void UavVrpnObject_impl::UpdateFrom(const io_data *data) { |
---|
| 64 | const Matrix* input = dynamic_cast<const Matrix*>(data); |
---|
| 65 | if (!input) { |
---|
| 66 | self->Warn("casting %s to Matrix failed\n",data->ObjectName().c_str()); |
---|
| 67 | return; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | Vector3Df vrpnPosition; |
---|
| 71 | Quaternion vrpnQuaternion; |
---|
| 72 | input->GetMutex(); |
---|
| 73 | vrpnQuaternion.q0=input->ValueNoMutex(0, 0); |
---|
| 74 | vrpnQuaternion.q1=input->ValueNoMutex(1, 0); |
---|
| 75 | vrpnQuaternion.q2=input->ValueNoMutex(2, 0); |
---|
| 76 | vrpnQuaternion.q3=input->ValueNoMutex(3, 0); |
---|
| 77 | vrpnPosition.x=input->ValueNoMutex(4, 0); |
---|
| 78 | vrpnPosition.y=input->ValueNoMutex(5, 0); |
---|
| 79 | vrpnPosition.z=input->ValueNoMutex(6, 0); |
---|
| 80 | input->ReleaseMutex(); |
---|
| 81 | |
---|
| 82 | vector3df nodePosition; |
---|
| 83 | Quaternion nodeQuaternion; |
---|
| 84 | Euler nodeEuler; |
---|
| 85 | |
---|
| 86 | nodePosition = ToIrrlichtCoordinates(vrpnPosition); |
---|
| 87 | setPosition(nodePosition); |
---|
| 88 | nodeQuaternion = ToIrrlichtOrientation(vrpnQuaternion); |
---|
| 89 | nodeQuaternion.ToEuler(nodeEuler); |
---|
| 90 | setRotation(Euler::ToDegree(1) * vector3df(nodeEuler.roll,nodeEuler.pitch, nodeEuler.yaw)); |
---|
| 91 | |
---|
| 92 | if (arm_length->ValueChanged() == true) { |
---|
| 93 | setScale(vector3df(arm_length->Value(), arm_length->Value(), arm_length->Value())); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void UavVrpnObject_impl::render(void) { |
---|
| 98 | IVideoDriver *driver = SceneManager->getVideoDriver(); |
---|
| 99 | driver->setTransform(ETS_WORLD, AbsoluteTransformation); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void UavVrpnObject_impl::OnRegisterSceneNode(void) { |
---|
| 103 | if (IsVisible) |
---|
| 104 | SceneManager->registerNodeForRendering(this); |
---|
| 105 | |
---|
| 106 | ISceneNode::OnRegisterSceneNode(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void UavVrpnObject_impl::Draw(void) { |
---|
| 110 | // create unite (1m=100cm) UAV; scale will be adapted according to arm_length |
---|
| 111 | // parameter |
---|
| 112 | // note that the frame used is irrlicht one: |
---|
| 113 | // left handed, North East Up |
---|
| 114 | const IGeometryCreator *geo; |
---|
| 115 | geo = getGui()->getSceneManager()->getGeometryCreator(); |
---|
| 116 | |
---|
| 117 | // cylinders are aligned with y axis |
---|
| 118 | IMesh *red_arm = geo->createCylinderMesh(2.5, 100, 16, SColor(0, 255, 0, 0)); |
---|
| 119 | IMesh *black_arm = geo->createCylinderMesh(2.5, 100, 16, SColor(0, 128, 128, 128)); |
---|
| 120 | IMesh *motor = geo->createCylinderMesh(7.5, 15, 16); //,SColor(0, 128, 128, 128)); |
---|
| 121 | // geo->drop(); |
---|
| 122 | |
---|
| 123 | ITexture *texture = getGui()->getTexture("carbone.jpg"); |
---|
| 124 | MeshSceneNode *fl_arm = new MeshSceneNode(this, red_arm, vector3df(0, 0, 0), |
---|
| 125 | vector3df(0, 0, -135)); |
---|
| 126 | MeshSceneNode *fr_arm = new MeshSceneNode(this, red_arm, vector3df(0, 0, 0), |
---|
| 127 | vector3df(0, 0, -45)); |
---|
| 128 | MeshSceneNode *rl_arm = new MeshSceneNode(this, black_arm, vector3df(0, 0, 0), |
---|
| 129 | vector3df(0, 0, 135), texture); |
---|
| 130 | MeshSceneNode *rr_arm = new MeshSceneNode(this, black_arm, vector3df(0, 0, 0), |
---|
| 131 | vector3df(0, 0, 45), texture); |
---|
| 132 | |
---|
| 133 | texture = getGui()->getTexture("metal047.jpg"); |
---|
| 134 | MeshSceneNode *fl_motor = new MeshSceneNode(this, motor, vector3df(70.71, -70.71, 2.5), |
---|
| 135 | vector3df(90, 0, 0), texture); |
---|
| 136 | MeshSceneNode *fr_motor = new MeshSceneNode(this, motor, vector3df(70.71, 70.71, 2.5), |
---|
| 137 | vector3df(90, 0, 0), texture); |
---|
| 138 | MeshSceneNode *rl_motor = new MeshSceneNode(this, motor, vector3df(-70.71, -70.71, 2.5), |
---|
| 139 | vector3df(90, 0, 0), texture); |
---|
| 140 | MeshSceneNode *rr_motor = new MeshSceneNode(this, motor, vector3df(-70.71, 70.71, 2.5), |
---|
| 141 | vector3df(90, 0, 0), texture); |
---|
| 142 | |
---|
| 143 | fl_blade = new Blade(this, vector3df(70.71, -70.71, 17.5)); |
---|
| 144 | fr_blade = new Blade(this, vector3df(70.71, 70.71, 17.5), true); |
---|
| 145 | rl_blade = new Blade(this, vector3df(-70.71, -70.71, 17.5), true); |
---|
| 146 | rr_blade = new Blade(this, vector3df(-70.71, 70.71, 17.5)); |
---|
| 147 | |
---|
| 148 | fl_blade->SetRotationSpeed(MOTOR_SPEED); |
---|
| 149 | fr_blade->SetRotationSpeed(-MOTOR_SPEED); |
---|
| 150 | rl_blade->SetRotationSpeed(-MOTOR_SPEED); |
---|
| 151 | rr_blade->SetRotationSpeed(MOTOR_SPEED); |
---|
| 152 | |
---|
| 153 | setScale(vector3df(arm_length->Value(), arm_length->Value(), arm_length->Value())); |
---|
| 154 | } |
---|
| 155 | #endif // GL |
---|