source: flair-src/trunk/tools/VrpnLite/src/VrpnLite.cpp@ 331

Last change on this file since 331 was 331, checked in by Sanahuja Guillaume, 2 years ago

resolve aligment trap bug

File size: 4.0 KB
Line 
1// created: 2019/03/12
2// filename: VrpnLite.cpp
3//
4// author: Guillaume Sanahuja
5// Copyright Heudiasyc UMR UTC/CNRS 7253
6//
7// version: $Id: $
8//
9// purpose: vrpnlite, to forward it to bth for exemple
10// usefull to reduce vrpn frame size
11//
12/*********************************************************************/
13
14#include "VrpnLite.h"
15#include <VrpnClient.h>
16#include <VrpnObject.h>
17#include <Thread.h>
18#include <UdpSocket.h>
19#include <FrameworkManager.h>
20#include <Quaternion.h>
21#include <string.h>
22
23using namespace std;
24using namespace flair::core;
25using namespace flair::sensor;
26
27VrpnLite::VrpnLite(int vrpnLitePort,string vrpnServerAddress): Thread(getFrameworkManager(),"VrpnLite",90) {
28 vrpnclient=new VrpnClient("vrpn", vrpnServerAddress,80);
29 dataSocket = new UdpSocket(this,"client socket",vrpnLitePort);
30 vrpnclient->Start();
31}
32
33VrpnLite::~VrpnLite() {
34}
35
36void VrpnLite::Run(void) {
37 Time dataSocketTimeout;
38 char msg[256];
39
40 while (!ToBeStopped()) {
41
42 if(vrpnobjects.size()>0) {
43 //wait for last one to be sure all are up to date
44 if(WaitUpdate(vrpnobjects.at(vrpnobjects.size()-1),100000000)) {
45 SendObjects();
46 }
47 dataSocketTimeout=TIME_NONBLOCK;
48 } else {
49 dataSocketTimeout=100000000;
50 }
51
52 ssize_t rcv=dataSocket->RecvMessage(msg,sizeof(msg),dataSocketTimeout);
53 if(rcv>0) {
54 string object=msg;
55 uint16_t id;
56 memcpy(&id,&msg[rcv-2],sizeof(id));
57 dataSocket->HostToNetwork((char*)&id,sizeof(id));
58 //assume we receive it in the good order
59 if(id==vrpnobjects.size()) {
60 Printf("adding object %s with id %i\n",object.c_str(),id);
61 VrpnObject* vrpnobject = new VrpnObject(object,vrpnclient->GetTabWidget());
62 vrpnobjects.push_back(vrpnobject);
63 }else {
64 Err("adding object %s failed, expected id %i, got %i\n",object.c_str(),vrpnobjects.size(),id);
65 }
66 }
67 }
68}
69
70void VrpnLite::SendObjects(void) const{
71 int16_t position[3];
72 int16_t quaternion[4];
73 Time time;
74 char datas[vrpnobjects.size()*(sizeof(position)+sizeof(quaternion))+ sizeof(time)];
75 char *datasPtr=datas;
76
77 for (vector<VrpnObject*>::const_iterator it = vrpnobjects.begin();it < vrpnobjects.end(); it++) {
78 Vector3Df objectPosition;
79 Quaternion objectQuaternion;
80 const VrpnObject* vrpnobject=*it;
81
82 vrpnobject->GetPosition(objectPosition);
83 vrpnobject->GetQuaternion(objectQuaternion);
84 time=vrpnobject->GetLastPacketTime();
85
86 position[0]=ConvertPosition(objectPosition.x);
87 position[1]=ConvertPosition(objectPosition.y);
88 position[2]=ConvertPosition(objectPosition.z);
89 quaternion[0]=ConvertQuaternion(objectQuaternion.q0);
90 quaternion[1]=ConvertQuaternion(objectQuaternion.q1);
91 quaternion[2]=ConvertQuaternion(objectQuaternion.q2);
92 quaternion[3]=ConvertQuaternion(objectQuaternion.q3);
93
94 for(int i=0;i<3;i++) dataSocket->HostToNetwork((char*)(&position[i]),sizeof(position[i]));
95 for(int i=0;i<4;i++) dataSocket->HostToNetwork((char*)(&quaternion[i]),sizeof(quaternion[i]));
96
97 memcpy(datasPtr,position, sizeof(position));
98 datasPtr+=sizeof(position);
99 memcpy(datasPtr,quaternion, sizeof(quaternion));
100 datasPtr+=sizeof(quaternion);
101 }
102
103 dataSocket->HostToNetwork((char*)(&time),sizeof(Time));
104 memcpy(datasPtr,&time, sizeof(time));//only one time for all VrpnObject; suppose it is the same!
105 dataSocket->SendMessage(datas,sizeof(datas));
106}
107
108int16_t VrpnLite::ConvertQuaternion(float value) const{
109 int16_t tmp;
110 tmp=value*32767.;
111 if(value<-1) {
112 tmp=-32767;
113 Warn("position value is %f, saturating it to %i\n",value,tmp);
114 }
115 if(value>1) {
116 tmp=32767;
117 Warn("position value is %f, saturating it to %i\n",value,tmp);
118 }
119 return tmp;
120}
121
122int16_t VrpnLite::ConvertPosition(float value) const{
123 int16_t tmp;
124 tmp=value*1000;
125 if(value<-32.768) {
126 tmp=-32768;
127 Warn("position value is %f, saturating it to %i\n",value,tmp);
128 }
129 if(value>32.767) {
130 tmp=32767;
131 Warn("position value is %f, saturating it to %i\n",value,tmp);
132 }
133 return tmp;
134}
Note: See TracBrowser for help on using the repository browser.