Changeset 330 in flair-src for trunk/tools/VrpnLite/src/VrpnLite.cpp


Ignore:
Timestamp:
Sep 25, 2019, 3:29:26 PM (5 years ago)
Author:
Sanahuja Guillaume
Message:

use less bandwidth in vprnlite

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/VrpnLite/src/VrpnLite.cpp

    r317 r330  
    2525using namespace flair::sensor;
    2626
    27 VrpnLite::VrpnLite(string clientAddress,string serveurAddress): Thread(getFrameworkManager(),"VrpnLite",90) {
    28   vrpnclient=new VrpnClient("vrpn", serveurAddress,80);
    29   VrpnObject* vrpnobject = new VrpnObject("x4_0",vrpnclient->GetTabWidget(),vrpnclient);
    30   //VrpnObject* vrpnobject2 = new VrpnObject("target",vrpnclient->GetTabWidget(),vrpnclient);
    31  
    32   vrpnobjects.push_back(vrpnobject);
    33   //vrpnobjects.push_back(vrpnobject2);
    34  
    35   dataSocket = new UdpSocket(this,"client socket",clientAddress);
    36  
     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);
    3730  vrpnclient->Start();
    3831}
     
    4235
    4336void VrpnLite::Run(void) {
     37  Time dataSocketTimeout;
     38  char msg[256];
     39 
    4440  while (!ToBeStopped()) {
    4541   
    46     WaitUpdate(vrpnobjects.at(0));//tood improve this wait, to be sure all vrpnobject are up to date; or one thread by object?
    47     //could be also lighter to send only one frame with all objects
    48     int i=0;
    49     for (vector<VrpnObject*>::iterator it = vrpnobjects.begin();it < vrpnobjects.end(); it++) {
    50       SendObject(*it,i);
    51       i++;
     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;
    5250    }
    5351   
     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    }
    5466  }
    5567}
    5668
    57 void VrpnLite::SendObject(const VrpnObject* vrpnobject,uint8_t id) const{
     69void 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++) {
    5877    Vector3Df objectPosition;
    5978    Quaternion objectQuaternion;
    60    
     79    const VrpnObject* vrpnobject=*it;
     80
    6181    vrpnobject->GetPosition(objectPosition);
    6282    vrpnobject->GetQuaternion(objectQuaternion);
    63     Time time=vrpnobject->GetLastPacketTime();
    64    
    65     float position[3];
    66     position[0]=objectPosition.x;
    67     position[1]=objectPosition.y;
    68     position[2]=objectPosition.z;
    69     float quaternion[4];
    70     quaternion[0]=objectQuaternion.q0;
    71     quaternion[1]=objectQuaternion.q1;
    72     quaternion[2]=objectQuaternion.q2;
    73     quaternion[3]=objectQuaternion.q3;
    74    
     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
    7593    for(int i=0;i<3;i++) dataSocket->HostToNetwork((char*)(&position[i]),sizeof(position[i]));
    7694    for(int i=0;i<4;i++) dataSocket->HostToNetwork((char*)(&quaternion[i]),sizeof(quaternion[i]));
    77     dataSocket->HostToNetwork((char*)(&time),sizeof(Time));
    78    
    79     char datas[sizeof(id) + sizeof(position)+sizeof(quaternion)+ sizeof(time)];
    80    
    81     datas[0]=id;
    82     memcpy(datas+sizeof(id),position, sizeof(position));
    83     memcpy(datas +sizeof(id)+ sizeof(position),quaternion, sizeof(quaternion));
    84     memcpy(datas+sizeof(id) + sizeof(position)+sizeof(quaternion),&time, sizeof(time));
    85    
    86     dataSocket->SendMessage(datas,sizeof(datas));
     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));
    87105}
     106
     107int16_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
     121int16_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}
Note: See TracChangeset for help on using the changeset viewer.