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

Last change on this file since 370 was 364, checked in by Sanahuja Guillaume, 4 years ago

modifs

File size: 4.7 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 int src_id;
40
41 while (!ToBeStopped()) {
42 if(vrpnobjects.size()>0) {
43 //wait for last one to be sure all are up to date
44 //but if object is not tracked we send nothing... send it unsynchronized???
45 if(WaitUpdate(vrpnobjects.at(vrpnobjects.size()-1),100000000)) {
46 SendObjects();
47 }
48 dataSocketTimeout=TIME_NONBLOCK;
49 } else {
50 dataSocketTimeout=100000000;
51 }
52
53 ssize_t rcv=dataSocket->RecvMessage(msg,sizeof(msg),dataSocketTimeout,NULL,NULL,&src_id);
54 if(rcv>0) {
55 string object=msg;
56 uint16_t id;
57 memcpy(&id,&msg[rcv-2],sizeof(id));
58 dataSocket->HostToNetwork((char*)&id,sizeof(id));
59
60 if(src_id==vrpnobjects_list.size()) {
61 vector<VrpnObject*> vect;
62 vrpnobjects_list.push_back(vect);
63 Printf("add vect %i\n",src_id);
64 }
65 //assume we receive it in the good order
66 if(id==vrpnobjects_list.at(src_id).size()) {
67 Printf("adding object %s with id %i from %i\n",object.c_str(),id,src_id);
68 VrpnObject* vrpnobject = new VrpnObject(object,vrpnclient->GetTabWidget());
69 vrpnobjects.push_back(vrpnobject);
70 vrpnobjects_list.at(src_id).push_back(vrpnobject);
71 }else {
72 Err("adding object %s failed, expected id %i, got %i\n",object.c_str(),vrpnobjects.size(),id);
73 }
74 }
75 }
76}
77
78void VrpnLite::SendObjects(void) const{
79
80 int16_t position[3];
81 int16_t quaternion[4];
82 Time time;
83
84 //for (vector<vector<VrpnObject*>>::const_iterator connection= vrpnobjects_list.begin();connection < vrpnobjects_list.end(); connection++) {
85 for (int i=0;i<vrpnobjects_list.size();i++) {
86 char datas[vrpnobjects_list.at(i).size()*(sizeof(position)+sizeof(quaternion))+ sizeof(time)];
87 char *datasPtr=datas;
88 for (vector<VrpnObject*>::const_iterator it = vrpnobjects_list.at(i).begin();it < vrpnobjects_list.at(i).end(); it++) {
89 Vector3Df objectPosition;
90 Quaternion objectQuaternion;
91 const VrpnObject* vrpnobject=*it;
92
93 vrpnobject->GetPosition(objectPosition);
94 vrpnobject->GetQuaternion(objectQuaternion);
95 time=vrpnobject->GetLastPacketTime();
96
97 position[0]=ConvertPosition(objectPosition.x);
98 position[1]=ConvertPosition(objectPosition.y);
99 position[2]=ConvertPosition(objectPosition.z);
100 quaternion[0]=ConvertQuaternion(objectQuaternion.q0);
101 quaternion[1]=ConvertQuaternion(objectQuaternion.q1);
102 quaternion[2]=ConvertQuaternion(objectQuaternion.q2);
103 quaternion[3]=ConvertQuaternion(objectQuaternion.q3);
104
105 for(int i=0;i<3;i++) dataSocket->HostToNetwork((char*)(&position[i]),sizeof(position[i]));
106 for(int i=0;i<4;i++) dataSocket->HostToNetwork((char*)(&quaternion[i]),sizeof(quaternion[i]));
107
108 memcpy(datasPtr,position, sizeof(position));
109 datasPtr+=sizeof(position);
110 memcpy(datasPtr,quaternion, sizeof(quaternion));
111 datasPtr+=sizeof(quaternion);
112 }
113 dataSocket->HostToNetwork((char*)(&time),sizeof(Time));
114 memcpy(datasPtr,&time, sizeof(time));//only one time for all VrpnObject; suppose it is the same!
115 dataSocket->SendMessage(datas,sizeof(datas),i);
116 }
117}
118
119int16_t VrpnLite::ConvertQuaternion(float value) const{
120 int16_t tmp;
121 tmp=value*32767.;
122 if(value<-1) {
123 tmp=-32767;
124 Warn("position value is %f, saturating it to %i\n",value,tmp);
125 }
126 if(value>1) {
127 tmp=32767;
128 Warn("position value is %f, saturating it to %i\n",value,tmp);
129 }
130 return tmp;
131}
132
133int16_t VrpnLite::ConvertPosition(float value) const{
134 int16_t tmp;
135 tmp=value*1000;
136 if(value<-32.768) {
137 tmp=-32768;
138 Warn("position value is %f, saturating it to %i\n",value,tmp);
139 }
140 if(value>32.767) {
141 tmp=32767;
142 Warn("position value is %f, saturating it to %i\n",value,tmp);
143 }
144 return tmp;
145}
Note: See TracBrowser for help on using the repository browser.