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