[308] | 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 |
|
---|
[330] | 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);
|
---|
[308] | 30 | vrpnclient->Start();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | VrpnLite::~VrpnLite() {
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | void VrpnLite::Run(void) {
|
---|
[330] | 37 | Time dataSocketTimeout;
|
---|
| 38 | char msg[256];
|
---|
[363] | 39 | int src_id;
|
---|
[330] | 40 |
|
---|
[308] | 41 | while (!ToBeStopped()) {
|
---|
[330] | 42 | if(vrpnobjects.size()>0) {
|
---|
| 43 | //wait for last one to be sure all are up to date
|
---|
[334] | 44 | //but if object is not tracked we send nothing... send it unsynchronized???
|
---|
[330] | 45 | if(WaitUpdate(vrpnobjects.at(vrpnobjects.size()-1),100000000)) {
|
---|
| 46 | SendObjects();
|
---|
| 47 | }
|
---|
| 48 | dataSocketTimeout=TIME_NONBLOCK;
|
---|
| 49 | } else {
|
---|
| 50 | dataSocketTimeout=100000000;
|
---|
[308] | 51 | }
|
---|
| 52 |
|
---|
[363] | 53 | ssize_t rcv=dataSocket->RecvMessage(msg,sizeof(msg),dataSocketTimeout,NULL,NULL,&src_id);
|
---|
[330] | 54 | if(rcv>0) {
|
---|
| 55 | string object=msg;
|
---|
[331] | 56 | uint16_t id;
|
---|
| 57 | memcpy(&id,&msg[rcv-2],sizeof(id));
|
---|
| 58 | dataSocket->HostToNetwork((char*)&id,sizeof(id));
|
---|
[363] | 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 | }
|
---|
[330] | 65 | //assume we receive it in the good order
|
---|
[363] | 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);
|
---|
[330] | 68 | VrpnObject* vrpnobject = new VrpnObject(object,vrpnclient->GetTabWidget());
|
---|
| 69 | vrpnobjects.push_back(vrpnobject);
|
---|
[363] | 70 | vrpnobjects_list.at(src_id).push_back(vrpnobject);
|
---|
[330] | 71 | }else {
|
---|
[331] | 72 | Err("adding object %s failed, expected id %i, got %i\n",object.c_str(),vrpnobjects.size(),id);
|
---|
[330] | 73 | }
|
---|
| 74 | }
|
---|
[308] | 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[330] | 78 | void VrpnLite::SendObjects(void) const{
|
---|
[364] | 79 |
|
---|
| 80 | int16_t position[3];
|
---|
[330] | 81 | int16_t quaternion[4];
|
---|
| 82 | Time time;
|
---|
[363] | 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++) {
|
---|
[364] | 86 | char datas[vrpnobjects_list.at(i).size()*(sizeof(position)+sizeof(quaternion))+ sizeof(time)];
|
---|
| 87 | char *datasPtr=datas;
|
---|
[363] | 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;
|
---|
[330] | 92 |
|
---|
[363] | 93 | vrpnobject->GetPosition(objectPosition);
|
---|
| 94 | vrpnobject->GetQuaternion(objectQuaternion);
|
---|
| 95 | time=vrpnobject->GetLastPacketTime();
|
---|
[330] | 96 |
|
---|
[363] | 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);
|
---|
[330] | 104 |
|
---|
[363] | 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]));
|
---|
[330] | 107 |
|
---|
[363] | 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);
|
---|
[330] | 116 | }
|
---|
[317] | 117 | }
|
---|
[330] | 118 |
|
---|
| 119 | int16_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 |
|
---|
| 133 | int16_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 | } |
---|