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 |
|
---|
23 | using namespace std;
|
---|
24 | using namespace flair::core;
|
---|
25 | using namespace flair::sensor;
|
---|
26 |
|
---|
27 | VrpnLite::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 |
|
---|
33 | VrpnLite::~VrpnLite() {
|
---|
34 | }
|
---|
35 |
|
---|
36 | void 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 | //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);
|
---|
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 | //assume we receive it in the good order
|
---|
60 | if(id==vrpnobjects.size()) {
|
---|
61 | Printf("adding object %s with id %i\n",object.c_str(),id);
|
---|
62 | VrpnObject* vrpnobject = new VrpnObject(object,vrpnclient->GetTabWidget());
|
---|
63 | vrpnobjects.push_back(vrpnobject);
|
---|
64 | }else {
|
---|
65 | Err("adding object %s failed, expected id %i, got %i\n",object.c_str(),vrpnobjects.size(),id);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | void VrpnLite::SendObjects(void) const{
|
---|
72 | int16_t position[3];
|
---|
73 | int16_t quaternion[4];
|
---|
74 | Time time;
|
---|
75 | char datas[vrpnobjects.size()*(sizeof(position)+sizeof(quaternion))+ sizeof(time)];
|
---|
76 | char *datasPtr=datas;
|
---|
77 |
|
---|
78 | for (vector<VrpnObject*>::const_iterator it = vrpnobjects.begin();it < vrpnobjects.end(); it++) {
|
---|
79 | Vector3Df objectPosition;
|
---|
80 | Quaternion objectQuaternion;
|
---|
81 | const VrpnObject* vrpnobject=*it;
|
---|
82 |
|
---|
83 | vrpnobject->GetPosition(objectPosition);
|
---|
84 | vrpnobject->GetQuaternion(objectQuaternion);
|
---|
85 | time=vrpnobject->GetLastPacketTime();
|
---|
86 |
|
---|
87 | position[0]=ConvertPosition(objectPosition.x);
|
---|
88 | position[1]=ConvertPosition(objectPosition.y);
|
---|
89 | position[2]=ConvertPosition(objectPosition.z);
|
---|
90 | quaternion[0]=ConvertQuaternion(objectQuaternion.q0);
|
---|
91 | quaternion[1]=ConvertQuaternion(objectQuaternion.q1);
|
---|
92 | quaternion[2]=ConvertQuaternion(objectQuaternion.q2);
|
---|
93 | quaternion[3]=ConvertQuaternion(objectQuaternion.q3);
|
---|
94 |
|
---|
95 | for(int i=0;i<3;i++) dataSocket->HostToNetwork((char*)(&position[i]),sizeof(position[i]));
|
---|
96 | for(int i=0;i<4;i++) dataSocket->HostToNetwork((char*)(&quaternion[i]),sizeof(quaternion[i]));
|
---|
97 |
|
---|
98 | memcpy(datasPtr,position, sizeof(position));
|
---|
99 | datasPtr+=sizeof(position);
|
---|
100 | memcpy(datasPtr,quaternion, sizeof(quaternion));
|
---|
101 | datasPtr+=sizeof(quaternion);
|
---|
102 | }
|
---|
103 |
|
---|
104 | dataSocket->HostToNetwork((char*)(&time),sizeof(Time));
|
---|
105 | memcpy(datasPtr,&time, sizeof(time));//only one time for all VrpnObject; suppose it is the same!
|
---|
106 | dataSocket->SendMessage(datas,sizeof(datas));
|
---|
107 | }
|
---|
108 |
|
---|
109 | int16_t VrpnLite::ConvertQuaternion(float value) const{
|
---|
110 | int16_t tmp;
|
---|
111 | tmp=value*32767.;
|
---|
112 | if(value<-1) {
|
---|
113 | tmp=-32767;
|
---|
114 | Warn("position value is %f, saturating it to %i\n",value,tmp);
|
---|
115 | }
|
---|
116 | if(value>1) {
|
---|
117 | tmp=32767;
|
---|
118 | Warn("position value is %f, saturating it to %i\n",value,tmp);
|
---|
119 | }
|
---|
120 | return tmp;
|
---|
121 | }
|
---|
122 |
|
---|
123 | int16_t VrpnLite::ConvertPosition(float value) const{
|
---|
124 | int16_t tmp;
|
---|
125 | tmp=value*1000;
|
---|
126 | if(value<-32.768) {
|
---|
127 | tmp=-32768;
|
---|
128 | Warn("position value is %f, saturating it to %i\n",value,tmp);
|
---|
129 | }
|
---|
130 | if(value>32.767) {
|
---|
131 | tmp=32767;
|
---|
132 | Warn("position value is %f, saturating it to %i\n",value,tmp);
|
---|
133 | }
|
---|
134 | return tmp;
|
---|
135 | } |
---|