Changeset 330 in flair-src for trunk/tools
- Timestamp:
- Sep 25, 2019, 3:29:26 PM (5 years ago)
- Location:
- trunk/tools/VrpnLite/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/VrpnLite/src/VrpnLite.cpp
r317 r330 25 25 using namespace flair::sensor; 26 26 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 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); 37 30 vrpnclient->Start(); 38 31 } … … 42 35 43 36 void VrpnLite::Run(void) { 37 Time dataSocketTimeout; 38 char msg[256]; 39 44 40 while (!ToBeStopped()) { 45 41 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; 52 50 } 53 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 } 54 66 } 55 67 } 56 68 57 void VrpnLite::SendObject(const VrpnObject* vrpnobject,uint8_t id) const{ 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++) { 58 77 Vector3Df objectPosition; 59 78 Quaternion objectQuaternion; 60 79 const VrpnObject* vrpnobject=*it; 80 61 81 vrpnobject->GetPosition(objectPosition); 62 82 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 75 93 for(int i=0;i<3;i++) dataSocket->HostToNetwork((char*)(&position[i]),sizeof(position[i])); 76 94 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 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)); 87 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 } -
trunk/tools/VrpnLite/src/VrpnLite.h
r308 r330 30 30 class VrpnLite: public flair::core::Thread { 31 31 public: 32 VrpnLite( std::string clientAddress,std::string serveurAddress);32 VrpnLite(int vrpnLitePort,std::string vrpnServerAddress); 33 33 ~VrpnLite(); 34 34 … … 38 38 std::vector<flair::sensor::VrpnObject*> vrpnobjects; 39 39 flair::core::UdpSocket* dataSocket; 40 void SendObject(const flair::sensor::VrpnObject* vrpnobject,uint8_t id) const; 41 40 void SendObjects(void) const; 41 int16_t ConvertPosition(float value) const; 42 int16_t ConvertQuaternion(float value) const; 42 43 }; 43 44 -
trunk/tools/VrpnLite/src/main.cpp
r308 r330 22 22 23 23 string xml_file; 24 string clientAddress,serverAddress;25 int port;24 string vrpnServerAddress; 25 int gcsPort,vrpnLitePort; 26 26 27 27 void parseOptions(int argc, char **argv); … … 32 32 FrameworkManager *manager; 33 33 manager = new FrameworkManager("vrpnforwarder"); 34 manager->SetupConnection("127.0.0.1", port);34 manager->SetupConnection("127.0.0.1", gcsPort); 35 35 manager->SetupUserInterface(xml_file); 36 36 37 VrpnLite* vrpnlite=new VrpnLite( clientAddress,serverAddress);37 VrpnLite* vrpnlite=new VrpnLite(vrpnLitePort,vrpnServerAddress); 38 38 39 39 vrpnlite->Start(); … … 47 47 CmdLine cmd("Command description message", ' ', "0.1"); 48 48 49 ValueArg< string> clientaddressArg("c", "caddress","client address", true,50 "127.0.0.1:3884", "string");51 cmd.add( clientaddressArg);49 ValueArg<int> vrpnLitePortArg("v", "vport","vrpn lite port", true, 50 3884, "int"); 51 cmd.add(vrpnLitePortArg); 52 52 53 53 ValueArg<string> serveraddressArg("s", "saddress","server address", true, … … 55 55 cmd.add(serveraddressArg); 56 56 57 ValueArg<int> portArg("p", "port","local port used to connect to the ground station",57 ValueArg<int> gcsPortArg("p", "port","local port used to connect to the ground station", 58 58 false, 9000, "int"); 59 cmd.add( portArg);59 cmd.add(gcsPortArg); 60 60 61 61 … … 66 66 cmd.parse(argc, argv); 67 67 68 clientAddress = clientaddressArg.getValue();69 serverAddress = serveraddressArg.getValue();70 port = portArg.getValue();68 vrpnLitePort = vrpnLitePortArg.getValue(); 69 vrpnServerAddress = serveraddressArg.getValue(); 70 gcsPort = gcsPortArg.getValue(); 71 71 xml_file = xmlArg.getValue(); 72 72
Note:
See TracChangeset
for help on using the changeset viewer.