source: flair-src/trunk/lib/FlairSensorActuator/src/VrpnClient_impl.cpp@ 442

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

cosmetic changes

File size: 10.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: VrpnClient_impl.cpp
7//
8// author: César Richard, Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: objet se connectant au serveur vrpn
14//
15//
16/*********************************************************************/
17
18#include "VrpnClient.h"
19#include "VrpnClient_impl.h"
20#include "VrpnObject.h"
21#include "VrpnObject_impl.h"
22#include <SerialPort.h>
23#include <vrpn_Connection.h>
24#include <FrameworkManager.h>
25#include <TabWidget.h>
26#include <Tab.h>
27#include <GridLayout.h>
28#include <OneAxisRotation.h>
29#include <Vector3D.h>
30#include <Quaternion.h>
31#include <Mutex.h>
[309]32#include <UdpSocket.h>
[3]33#include <math.h>
34
35using std::string;
36using std::vector;
37using namespace flair::core;
38using namespace flair::gui;
39using namespace flair::sensor;
40
[309]41VrpnClient_impl::VrpnClient_impl(VrpnClient *self, string name,
[330]42 std::string address,flair::sensor::VrpnClient::ConnectionType_t connectionType) {
[15]43 this->self = self;
[294]44 this->address = address;
[330]45 this->connectionType = connectionType;
[309]46 isConnected=false;
[330]47
48 if(connectionType==VrpnClient::Vrpn) {
49 connection = vrpn_get_connection_by_name(address.c_str());
50 Printf("Connecting to VRPN server on %s\n",address.c_str());
51 } else if(connectionType==VrpnClient::VrpnLite) {
52 dataSocket =new UdpSocket(getFrameworkManager(), "data_socket", address);
53 Printf("Connecting to VRPN-lite server on %s\n",address.c_str());
54 } else {
55 self->Err("Bad connection type, try using naother constructor\n");
56 }
[3]57
[309]58 CommonConstructor(name);
[3]59}
60
[309]61
62
63VrpnClient_impl::VrpnClient_impl(VrpnClient *self, string name,
[15]64 SerialPort *serialport, uint16_t us_period) {
65 this->us_period = us_period;
66 this->self = self;
67 this->serialport = serialport;
[309]68 connectionType=VrpnClient::Xbee;
[3]69
[15]70 serialport->SetBaudrate(111111);
71 serialport->SetRxTimeout(us_period * 1000);
[3]72
[309]73 CommonConstructor(name);
74
75 Printf("Connecting to VRPN server through xbee on %s\n",serialport->ObjectName().c_str());
76}
77
78void VrpnClient_impl::CommonConstructor(std::string name) {
79 mutex = new Mutex(self, name);
80
[15]81 // station sol
82 main_tab = new Tab(getFrameworkManager()->GetTabWidget(), name);
83 tab = new TabWidget(main_tab->NewRow(), name);
84 setup_tab = new Tab(tab, "Reglages");
[3]85
[187]86 rotation_1 = new OneAxisRotation(setup_tab->NewRow(), "post rotation 1",OneAxisRotation::PreRotation);
87 rotation_2 = new OneAxisRotation(setup_tab->NewRow(), "post rotation 2",OneAxisRotation::PreRotation);
[3]88}
89
[15]90VrpnClient_impl::~VrpnClient_impl() {
[332]91 if (connectionType==VrpnClient::Vrpn|| connectionType==VrpnClient::VrpnLite) {
[140]92 // on fait une copie car le delete touche a trackables_copy via
[15]93 // RemoveTrackable
[140]94 vector<VrpnObject_impl *> trackables_copy = trackables;
[15]95 for (unsigned int i = 0; i < trackables_copy.size(); i++)
[140]96 delete trackables_copy.at(i)->self;
[15]97 // trackables.clear();
[332]98 } else if(connectionType==VrpnClient::Xbee ) {
[15]99 // on fait une copie car le delete touche a xbee_objects_copy via
100 // RemoveTrackable
[332]101 vector<xbeeObject_t> xbeeObjects_copy = xbeeObjects;
102 for (unsigned int i = 0; i < xbeeObjects_copy.size(); i++)
103 delete xbeeObjects_copy.at(i).vrpnobject->self;
[15]104 }
[3]105
[15]106 delete main_tab;
[3]107
[309]108 if (connectionType==VrpnClient::Vrpn) {
[15]109 // it will automatically delete connection
110 connection->removeReference();
111 }
[3]112}
113
[167]114void VrpnClient_impl::ComputeRotations(Vector3Df &point) {
[15]115 rotation_1->ComputeRotation(point);
116 rotation_2->ComputeRotation(point);
[3]117}
118
[15]119void VrpnClient_impl::ComputeRotations(Quaternion &quat) {
120 rotation_1->ComputeRotation(quat);
121 rotation_2->ComputeRotation(quat);
[3]122}
123
[140]124void VrpnClient_impl::AddTrackable(VrpnObject_impl *obj) {
[309]125 if (connectionType==VrpnClient::Vrpn) {
126 mutex->GetMutex();
127 trackables.push_back(obj);
128 mutex->ReleaseMutex();
[330]129 } else if (connectionType==VrpnClient::VrpnLite) {
[332]130 if(trackables.size()<0xffff) {
131 uint16_t id=trackables.size();
[330]132 mutex->GetMutex();
[332]133 trackables.push_back(obj);
[330]134 mutex->ReleaseMutex();
135
[331]136 char char_array[obj->self->ObjectName().length() + sizeof(id)];
137 strcpy(char_array, obj->self->ObjectName().c_str());
138 dataSocket->HostToNetwork((char*)(&id),sizeof(id));
139 memcpy(&char_array[obj->self->ObjectName().length()],&id,sizeof(id));
140 dataSocket->SendMessage(char_array,obj->self->ObjectName().length() + sizeof(id));
[330]141 }else {
142 self->Warn("too much trackables for vrpnlite connection, not adding %s\n",obj->self->ObjectName().c_str());
143 }
[309]144 } else {
[330]145 self->Warn("AddTrackable called but not in vrpn mode nor in vrpnlite mode\n");
[309]146 }
[3]147}
148
[309]149void VrpnClient_impl::AddTrackable(VrpnObject_impl *obj, uint8_t id) {
[332]150 if (connectionType==VrpnClient::Xbee) {
151 xbeeObject_t tmp;
[309]152 tmp.vrpnobject = obj;
153 tmp.id = id;
154 mutex->GetMutex();
[332]155 xbeeObjects.push_back(tmp);
[309]156 mutex->ReleaseMutex();
157 } else {
[330]158 self->Warn("AddTrackable called but not in xbee mode\n");
[309]159 }
160}
161
[140]162void VrpnClient_impl::RemoveTrackable(VrpnObject_impl *obj) {
[15]163 mutex->GetMutex();
[332]164 if (connectionType==VrpnClient::Vrpn || connectionType==VrpnClient::VrpnLite) {
[330]165 for (vector<VrpnObject_impl *>::iterator it = trackables.begin();it < trackables.end(); it++) {
[309]166 if (*it == obj) {
167 trackables.erase(it);
168 break;
169 }
[3]170 }
[15]171 }
[332]172 if ( connectionType==VrpnClient::Xbee) {
173 for (vector<xbeeObject_t>::iterator it = xbeeObjects.begin();it < xbeeObjects.end(); it++) {
[309]174 if ((*it).vrpnobject == obj) {
[332]175 xbeeObjects.erase(it);
[309]176 break;
177 }
[140]178 }
179 }
[15]180 mutex->ReleaseMutex();
[3]181}
182
[15]183void VrpnClient_impl::Run(void) {
[136]184 struct timeval timeout;
185 timeout.tv_sec=0;
186 timeout.tv_usec=100000;
187
[15]188 while (!self->ToBeStopped()) {
[309]189 if (connectionType==VrpnClient::Xbee) {
[15]190 ssize_t read = 0;
191 uint8_t response[38] = {0};
[3]192
[15]193 read = serialport->Read(response, sizeof(response));
194 if (read > 0 && read != sizeof(response))
195 read += serialport->Read(&response[read], sizeof(response) - read);
196 // int temps=(float)self->GetTime()/(1000*1000);
197 // self->Printf("%i %i %i\n",temps-last,temps,last);
198 // last=temps;
199 if (read < 0) {
200 // self->Warn("erreur rt_dev_read (%s)\n",strerror(-read));
201 } else if (read != sizeof(response)) {
[294]202 self->Warn("serial read error %i/%i\n", read, sizeof(response));
[15]203 } else {
204 // for(ssize_t i=0;i<read;i++) printf("%x ",response[i]);
205 // printf("\n");
206 uint8_t checksum = 0;
207 for (ssize_t i = 3; i < read; i++)
208 checksum += response[i];
209 if (checksum != 255) {
210 self->Err("checksum error\n");
211 } else {
212 vrpn_TRACKERCB t;
213 float pos[3];
214 float quat[4];
215 uint8_t id = response[8];
[3]216
[15]217 mutex->GetMutex();
[332]218 if (id < xbeeObjects.size()) {
[15]219 memcpy(pos, &response[9], sizeof(pos));
220 memcpy(quat, &response[9] + sizeof(pos), sizeof(quat));
221 for (int i = 0; i < 3; i++)
222 t.pos[i] = pos[i];
223 for (int i = 0; i < 4; i++)
224 t.quat[i] = quat[i];
225 if (fabs(pos[0] > 10) || fabs(pos[1] > 10) || fabs(pos[2] > 10)) {
[136]226 Printf("prob pos %f %f %f\n", pos[0], pos[1], pos[2]);
[15]227 } else {
228 // self->Printf("%i %f %f %f
229 // %f\n",id,pos[0],pos[1],pos[2],(float)self->GetTime()/(1000*1000));
[332]230 VrpnObject_impl::handle_pos(xbeeObjects.at(id).vrpnobject, t);
[3]231 }
[15]232 }
233 mutex->ReleaseMutex();
[3]234 }
[15]235 }
[309]236 } else if(connectionType==VrpnClient::Vrpn) {
[139]237 if(connection->connected()==vrpn_true && !isConnected) {
238 isConnected=true;
[294]239 Printf("VRPN connected to %s\n",address.c_str());
[139]240 }
241 if(connection->connected()==vrpn_false && isConnected) {
242 isConnected=false;
[294]243 Printf("VRPN disconnected to %s\n",address.c_str());
[139]244 }
245 //timeout in mainloop is not well handled if not connected...
246 if(isConnected) {
247 //this is when trackables callbacks are called:
248 if(connection->mainloop(&timeout)!=0) {
[294]249 self->Warn("connection dropped from\n",address.c_str());
[139]250 }
251 //printf("%lld\n",GetTime()/(1000*1000));
252 mutex->GetMutex();
[330]253 for (unsigned int i = 0; i < trackables.size(); i++) {
254 // Printf("tracker %i\n",i);
255 trackables.at(i)->tracker->mainloop();
256 //Printf("tracker %i ok\n",i);
257 }
[139]258 mutex->ReleaseMutex();
259 } else {
260 connection->mainloop();
261 self->SleepMS(10);
262 }
[309]263 }else if(connectionType==VrpnClient::VrpnLite) {
[330]264 mutex->GetMutex();
265
266 int16_t pos[3];
267 int16_t quat[4];
[309]268 Time time;
[332]269 char datas[trackables.size()*(sizeof(pos)+sizeof(quat))+ sizeof(time)];
[330]270 char *datasPtr=datas;
271
[309]272 int rcv=dataSocket->RecvMessage(datas,sizeof(datas),50*1000*1000);
[330]273 if(rcv!=sizeof(datas)) {
274 if(rcv>0) Printf("discarding message (size %i/%i)\n",rcv,sizeof(datas));
275 mutex->ReleaseMutex();
276 continue;
277 }
[309]278
[330]279 memcpy(&time, datasPtr+sizeof(datas)-sizeof(time), sizeof(time));
[309]280 dataSocket->NetworkToHost((char*)(&time),sizeof(time));
[330]281
[332]282 for (vector<VrpnObject_impl*>::iterator it = trackables.begin();it < trackables.end(); it++) {
[330]283 memcpy(pos, datasPtr, sizeof(pos));
284 datasPtr+=sizeof(pos);
285 memcpy(quat,datasPtr, sizeof(quat));
286 datasPtr+=sizeof(quat);
287
288 for(int i=0;i<3;i++) dataSocket->NetworkToHost((char*)(&pos[i]),sizeof(pos[i]));
289 for(int i=0;i<4;i++) dataSocket->NetworkToHost((char*)(&quat[i]),sizeof(quat[i]));
290
291 vrpn_TRACKERCB t;
292 for (int i = 0; i < 3; i++) t.pos[i] = ConvertPosition(pos[i]);
[309]293 // warning: t.quat is defined as (qx,qy,qz,qw), which is different from
294 // flair::core::Quaternion
[330]295 t.quat[0] = ConvertQuaternion(quat[1]);
296 t.quat[1] = ConvertQuaternion(quat[2]);
297 t.quat[2] = ConvertQuaternion(quat[3]);
298 t.quat[3] = ConvertQuaternion(quat[0]);
[309]299 t.msg_time.tv_sec=time/((Time)1000000000);
300 t.msg_time.tv_usec=(time%((Time)1000000000))/((Time)1000);
301 //Printf("%i %lld %lld %lld\n",id,time,t.msg_time.tv_sec,t.msg_time.tv_usec);
[332]302 VrpnObject_impl::handle_pos((void*)(*it), t);
[309]303 }
[330]304
[309]305 mutex->ReleaseMutex();
[3]306 }
[15]307 }
[3]308}
[330]309
310float VrpnClient_impl::ConvertPosition(int16_t value) const {
311 return (float)value/1000.;
312}
313
314float VrpnClient_impl::ConvertQuaternion(int16_t value) const {
315 return (float)value/32767.;
316}
Note: See TracBrowser for help on using the repository browser.