source: flair-src/trunk/lib/FlairSensorActuator/src/VrpnObject_impl.cpp @ 135

Last change on this file since 135 was 135, checked in by Sanahuja Guillaume, 5 years ago

VrpnObject? no longer outputs Euler (ony Quaternion): warning, output matrix has changed!

File size: 5.7 KB
RevLine 
[3]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[3]4// %flair:license}
5//  created:    2013/04/03
6//  filename:   VrpnObject.cpp
7//
[122]8//  author:     César Richard, Guillaume Sanahuja
[3]9//              Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11//  version:    $Id: $
12//
13//  purpose:    objet vrpn
14//
15//
16/*********************************************************************/
17#include "VrpnObject_impl.h"
18#include "VrpnObject.h"
19#include "VrpnClient.h"
20#include "VrpnClient_impl.h"
[15]21#include <vrpn_Tracker.h>
[3]22#include <fcntl.h>
23#include <errno.h>
24#include <string.h>
25#include <unistd.h>
26#include <vrpn_Connection.h>
27#include <cvmatrix.h>
28#include <Tab.h>
29#include <TabWidget.h>
30#include <DataPlot1D.h>
31#include <OneAxisRotation.h>
32#include <Vector3D.h>
33#include <Euler.h>
34#include <math.h>
35
36using std::string;
37using namespace flair::core;
38using namespace flair::gui;
39using namespace flair::sensor;
40
[122]41VrpnObject_impl::VrpnObject_impl(VrpnObject *self,
[15]42                                 string name, int id, const TabWidget *tab) {
[122]43  parent = GetVrpnClient();
[15]44  this->self = self;
[3]45
[122]46        if(parent==NULL) {
47                self->Err("VrpnClient must be instanced before creating VrpnObject\n");
48                return;
49        }
[15]50  if (id == -1 && parent->UseXbee()) {
51    self->Err("erreur aucun identifiant specifie pour la connexion Xbee\n");
52  }
53  if (id != -1 && !parent->UseXbee()) {
54    self->Warn(
55        "identifiant pour la connexion Xbee ignore car pas en mode Xbee\n");
56  }
[3]57
[15]58  if (parent->UseXbee()) {
59    parent->pimpl_->AddTrackable(this, id);
60    tracker = NULL;
61  } else {
62    parent->pimpl_->AddTrackable(self);
63    tracker = new vrpn_Tracker_Remote(name.c_str(), parent->pimpl_->connection);
64    tracker->register_change_handler(this, handle_pos);
65    tracker->shutup = true;
66  }
[3]67
[15]68  // state
[135]69  cvmatrix_descriptor *desc = new cvmatrix_descriptor(7, 1);
70  desc->SetElementName(0, 0, "q0");
71  desc->SetElementName(1, 0, "q1");
72  desc->SetElementName(2, 0, "q2");
73  desc->SetElementName(3, 0, "q3");
74  desc->SetElementName(4, 0, "x");
75  desc->SetElementName(5, 0, "y");
76  desc->SetElementName(6, 0, "z");
[15]77  output = new cvmatrix(self, desc, floatType);
78
79  desc = new cvmatrix_descriptor(3, 1);
80  desc->SetElementName(0, 0, "roll");
81  desc->SetElementName(1, 0, "pitch");
82  desc->SetElementName(2, 0, "yaw");
83  state = new cvmatrix(self, desc, floatType);
84
85  // ui
86  plot_tab = new Tab(tab, "Mesures " + name);
87  x_plot = new DataPlot1D(plot_tab->NewRow(), "x", -10, 10);
[135]88  x_plot->AddCurve(output->Element(4));
[15]89  y_plot = new DataPlot1D(plot_tab->LastRowLastCol(), "y", -10, 10);
[135]90  y_plot->AddCurve(output->Element(5));
[15]91  z_plot = new DataPlot1D(plot_tab->LastRowLastCol(), "z", -2, 0);
[135]92  z_plot->AddCurve(output->Element(6));
[3]93}
[15]94
95VrpnObject_impl::~VrpnObject_impl(void) {
96  if (tracker != NULL) // normal
97  {
98    parent->pimpl_->RemoveTrackable(self);
99    tracker->unregister_change_handler(this, handle_pos);
100    delete tracker;
101  } else // xbee
102  {
103    parent->pimpl_->RemoveTrackable(this);
104  }
105  delete plot_tab;
[3]106}
107
[15]108void VrpnObject_impl::mainloop(void) { tracker->mainloop(); }
[3]109
[15]110bool VrpnObject_impl::IsTracked(unsigned int timeout_ms) {
111  output->GetMutex();
112  Time a = GetTime();
113  Time dt = a - output->DataTime();
114  output->ReleaseMutex();
115
116  if (dt > (Time)(timeout_ms * 1000000)) {
117    // self->Printf("%lld %lld %lld
118    // %lld\n",a,output->DataTime(),dt,(Time)(timeout_ms*1000000));
119    return false;
120  } else {
121    return true;
122  }
[3]123}
124
125void VrpnObject_impl::GetQuaternion(Quaternion &quaternion) {
[15]126  output->GetMutex();
[135]127  quaternion.q0 = output->ValueNoMutex(0, 0);
128  quaternion.q1 = output->ValueNoMutex(1, 0);
129  quaternion.q2 = output->ValueNoMutex(2, 0);
130  quaternion.q3 = output->ValueNoMutex(3, 0);
[15]131  output->ReleaseMutex();
132}
[3]133
[15]134void VrpnObject_impl::GetPosition(Vector3D &point) {
135  output->GetMutex();
[135]136  point.x = output->ValueNoMutex(4, 0);
137  point.y = output->ValueNoMutex(5, 0);
138  point.z = output->ValueNoMutex(6, 0);
[15]139  output->ReleaseMutex();
140}
[3]141
[15]142void VRPN_CALLBACK
143VrpnObject_impl::handle_pos(void *userdata, const vrpn_TRACKERCB t) {
144  bool is_nan = false;
145  VrpnObject_impl *caller = reinterpret_cast<VrpnObject_impl *>(userdata);
146  Time time = GetTime();
[3]147
[15]148  // check if something is nan
149  for (int i = 0; i < 3; i++) {
150    if (isnan(t.pos[i]) == true)
151      is_nan = true;
152  }
153  for (int i = 0; i < 4; i++) {
154    if (isnan(t.quat[i]) == true)
155      is_nan = true;
156  }
157  if (is_nan == true) {
158    caller->self->Warn("data is nan, skipping it (time %lld)\n", time);
159    return;
160  }
[3]161
[122]162  // on prend une fois pour toute le mutex et on fait des accès directs
[15]163  caller->output->GetMutex();
[3]164
[15]165  // warning: t.quat is defined as (qx,qy,qz,qw), which is different from
166  // flair::core::Quaternion
[135]167        Quaternion quaternion(t.quat[3],t.quat[0],t.quat[1],t.quat[2]);
[15]168  Vector3D pos((float)t.pos[0], (float)t.pos[1], (float)t.pos[2]);
[3]169
[15]170  // on effectue les rotation
171  caller->parent->pimpl_->ComputeRotations(pos);
[135]172  caller->parent->pimpl_->ComputeRotations(quaternion);
[3]173
[135]174  caller->output->SetValueNoMutex(0, 0, quaternion.q0);
175  caller->output->SetValueNoMutex(1, 0, quaternion.q1);
176  caller->output->SetValueNoMutex(2, 0, quaternion.q2);
177  caller->output->SetValueNoMutex(3, 0, quaternion.q3);
178  caller->output->SetValueNoMutex(4, 0, pos.x);
179  caller->output->SetValueNoMutex(5, 0, pos.y);
180  caller->output->SetValueNoMutex(6, 0, pos.z);
[3]181
[15]182  caller->output->SetDataTime(time);
183  caller->output->ReleaseMutex();
[3]184
[135]185        Euler euler=quaternion.ToEuler();
[15]186  caller->state->GetMutex();
187  caller->state->SetValueNoMutex(0, 0, Euler::ToDegree(euler.roll));
188  caller->state->SetValueNoMutex(1, 0, Euler::ToDegree(euler.pitch));
189  caller->state->SetValueNoMutex(2, 0, Euler::ToDegree(euler.yaw));
190  caller->state->ReleaseMutex();
191
192  caller->self->ProcessUpdate(caller->output);
[3]193}
Note: See TracBrowser for help on using the repository browser.