Changeset 309 in flair-src
- Timestamp:
- Mar 13, 2019, 4:40:18 PM (6 years ago)
- Location:
- trunk/lib/FlairSensorActuator/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FlairSensorActuator/src/VrpnClient.cpp
r294 r309 44 44 singleton = this; 45 45 } 46 46 47 47 pimpl_ = new VrpnClient_impl(this, name, address); 48 48 } 49 49 50 51 VrpnClient::VrpnClient(std::string name, 52 uint16_t port, uint8_t priority) 53 : Thread(getFrameworkManager(), name, priority) { 54 if (singleton != NULL) { 55 SimpleWarn("VrpnClient should be instanced only one time!\n"); 56 SimpleWarn("Next calls to GetVrpnClient() will return the first created VrpnClient (%s)\n",singleton->ObjectName().c_str()); 57 } else { 58 singleton = this; 59 } 60 61 pimpl_ = new VrpnClient_impl(this, name, port); 62 } 63 50 64 VrpnClient::VrpnClient(string name, 51 65 SerialPort *serialport, uint16_t us_period, … … 69 83 } 70 84 85 VrpnClient::ConnectionType_t VrpnClient::ConnectionType(void) const { 86 return pimpl_->connectionType; 87 } 88 71 89 Layout *VrpnClient::GetLayout(void) const { 72 90 return (Layout *)(pimpl_->setup_tab); … … 75 93 TabWidget *VrpnClient::GetTabWidget(void) const { return pimpl_->tab; } 76 94 77 bool VrpnClient::UseXbee(void) const { return pimpl_->UseXbee(); }78 79 95 void VrpnClient::Run(void) { pimpl_->Run(); } 80 96 -
trunk/lib/FlairSensorActuator/src/VrpnClient.h
r136 r309 45 45 * \brief Constructor 46 46 * 47 * Construct a VrpnClient. Connection is done by IP .47 * Construct a VrpnClient. Connection is done by IP to a vrpn server. 48 48 * 49 49 * \param name name … … 54 54 std::string address, uint8_t priority); 55 55 56 /*! 57 * \brief Constructor 58 * 59 * Construct a VrpnClient. Connection is done by IP to a vrpn-lite server (see tools/VrpnLite in flair-src) 60 * 61 * \param name name 62 * \param port server port 63 * \param priority priority of the Thread 64 */ 65 VrpnClient(std::string name, 66 uint16_t port, uint8_t priority); 67 56 68 /*! 57 69 * \brief Constructor … … 87 99 */ 88 100 gui::TabWidget *GetTabWidget(void) const; 89 90 /*! 91 * \brief Is XBee used? 92 * 93 * \return true if connection is based on XBee modem 94 */ 95 bool UseXbee(void) const; 101 102 typedef enum { Vrpn, VrpnLite, Xbee } ConnectionType_t; 103 104 ConnectionType_t ConnectionType(void) const; 96 105 97 106 private: -
trunk/lib/FlairSensorActuator/src/VrpnClient_impl.cpp
r294 r309 30 30 #include <Quaternion.h> 31 31 #include <Mutex.h> 32 #include <UdpSocket.h> 32 33 #include <math.h> 33 34 … … 38 39 using namespace flair::sensor; 39 40 40 VrpnClient_impl::VrpnClient_impl(VrpnClient *self, st d::string name,41 VrpnClient_impl::VrpnClient_impl(VrpnClient *self, string name, 41 42 std::string address) { 42 43 this->self = self; 43 44 this->address = address; 44 serialport = NULL; 45 isConnected=false; 46 connectionType=VrpnClient::Vrpn; 47 48 connection = vrpn_get_connection_by_name(address.c_str()); 49 50 CommonConstructor(name); 51 52 Printf("Connecting to VRPN server on %s\n",address.c_str()); 53 } 54 55 VrpnClient_impl::VrpnClient_impl(VrpnClient *self, string name, 56 uint16_t port) { 57 this->self = self; 45 58 isConnected=false; 46 59 connectionType=VrpnClient::VrpnLite; 60 61 dataSocket =new UdpSocket(getFrameworkManager(), "data_socket", port); 62 63 CommonConstructor(name); 64 65 Printf("Connecting to VRPN-lite server on port %i\n",port); 66 } 67 68 VrpnClient_impl::VrpnClient_impl(VrpnClient *self, string name, 69 SerialPort *serialport, uint16_t us_period) { 70 this->us_period = us_period; 71 this->self = self; 72 this->serialport = serialport; 73 connectionType=VrpnClient::Xbee; 74 75 serialport->SetBaudrate(111111); 76 serialport->SetRxTimeout(us_period * 1000); 77 78 CommonConstructor(name); 79 80 Printf("Connecting to VRPN server through xbee on %s\n",serialport->ObjectName().c_str()); 81 } 82 83 void VrpnClient_impl::CommonConstructor(std::string name) { 47 84 mutex = new Mutex(self, name); 48 49 connection = vrpn_get_connection_by_name(address.c_str()); 50 85 51 86 // station sol 52 87 main_tab = new Tab(getFrameworkManager()->GetTabWidget(), name); … … 56 91 rotation_1 = new OneAxisRotation(setup_tab->NewRow(), "post rotation 1",OneAxisRotation::PreRotation); 57 92 rotation_2 = new OneAxisRotation(setup_tab->NewRow(), "post rotation 2",OneAxisRotation::PreRotation); 58 59 Printf("Connecting to VRPN server on %s\n",address.c_str());60 }61 62 VrpnClient_impl::VrpnClient_impl(VrpnClient *self, std::string name,63 SerialPort *serialport, uint16_t us_period) {64 this->us_period = us_period;65 this->self = self;66 this->serialport = serialport;67 connection = NULL;68 mutex = new Mutex(self, name);69 70 serialport->SetBaudrate(111111);71 serialport->SetRxTimeout(us_period * 1000);72 73 // station sol74 main_tab = new Tab(getFrameworkManager()->GetTabWidget(), name);75 tab = new TabWidget(main_tab->NewRow(), name);76 setup_tab = new Tab(tab, "Reglages");77 78 rotation_1 = new OneAxisRotation(setup_tab->NewRow(), "post rotation 1",OneAxisRotation::PreRotation);79 rotation_2 = new OneAxisRotation(setup_tab->NewRow(), "post rotation 2",OneAxisRotation::PreRotation);80 81 Printf("Connecting to VRPN server on %s\n",serialport->ObjectName().c_str());82 93 } 83 94 84 95 VrpnClient_impl::~VrpnClient_impl() { 85 if ( !UseXbee()) {96 if (connectionType==VrpnClient::Vrpn) { 86 97 // on fait une copie car le delete touche a trackables_copy via 87 98 // RemoveTrackable … … 90 101 delete trackables_copy.at(i)->self; 91 102 // trackables.clear(); 92 } else {103 } else if(connectionType==VrpnClient::Xbee || connectionType==VrpnClient::VrpnLite) { 93 104 // on fait une copie car le delete touche a xbee_objects_copy via 94 105 // RemoveTrackable 95 vector< xbee_object> xbee_objects_copy = xbee_objects;96 for (unsigned int i = 0; i < xbee_objects_copy.size(); i++)97 delete xbee_objects_copy.at(i).vrpnobject->self;106 vector<liteObject_t> liteObjects_copy = liteObjects; 107 for (unsigned int i = 0; i < liteObjects_copy.size(); i++) 108 delete liteObjects_copy.at(i).vrpnobject->self; 98 109 } 99 110 100 111 delete main_tab; 101 112 102 if ( !UseXbee()) {113 if (connectionType==VrpnClient::Vrpn) { 103 114 // it will automatically delete connection 104 115 connection->removeReference(); … … 117 128 118 129 void VrpnClient_impl::AddTrackable(VrpnObject_impl *obj) { 119 mutex->GetMutex(); 120 trackables.push_back(obj); 121 mutex->ReleaseMutex(); 130 if (connectionType==VrpnClient::Vrpn) { 131 mutex->GetMutex(); 132 trackables.push_back(obj); 133 mutex->ReleaseMutex(); 134 } else { 135 self->Warn("AddTrackable called but not in vrpn mode\n"); 136 } 137 } 138 139 void VrpnClient_impl::AddTrackable(VrpnObject_impl *obj, uint8_t id) { 140 if (connectionType==VrpnClient::VrpnLite || connectionType==VrpnClient::Xbee) { 141 liteObject_t tmp; 142 tmp.vrpnobject = obj; 143 tmp.id = id; 144 mutex->GetMutex(); 145 liteObjects.push_back(tmp); 146 mutex->ReleaseMutex(); 147 } else { 148 self->Warn("AddTrackable called but not in vrpnlite nor in xbee mode\n"); 149 } 122 150 } 123 151 124 152 void VrpnClient_impl::RemoveTrackable(VrpnObject_impl *obj) { 125 153 mutex->GetMutex(); 126 for (vector<VrpnObject_impl *>::iterator it = trackables.begin(); 127 it < trackables.end(); it++) { 128 if (*it == obj) { 129 trackables.erase(it); 130 break; 154 if (connectionType==VrpnClient::Vrpn) { 155 for (vector<VrpnObject_impl *>::iterator it = trackables.begin(); 156 it < trackables.end(); it++) { 157 if (*it == obj) { 158 trackables.erase(it); 159 break; 160 } 131 161 } 132 162 } 133 for (vector<xbee_object>::iterator it = xbee_objects.begin(); 134 it < xbee_objects.end(); it++) { 135 if ((*it).vrpnobject == obj) { 136 xbee_objects.erase(it); 137 break; 163 if (connectionType==VrpnClient::VrpnLite || connectionType==VrpnClient::Xbee) { 164 for (vector<liteObject_t>::iterator it = liteObjects.begin(); 165 it < liteObjects.end(); it++) { 166 if ((*it).vrpnobject == obj) { 167 liteObjects.erase(it); 168 break; 169 } 138 170 } 139 171 } 140 172 mutex->ReleaseMutex(); 141 }142 143 void VrpnClient_impl::AddTrackable(VrpnObject_impl *obj, uint8_t id) {144 xbee_object tmp;145 tmp.vrpnobject = obj;146 tmp.id = id;147 mutex->GetMutex();148 xbee_objects.push_back(tmp);149 mutex->ReleaseMutex();150 }151 152 bool VrpnClient_impl::UseXbee(void) {153 if (connection == NULL) {154 return true;155 } else {156 return false;157 }158 173 } 159 174 … … 164 179 165 180 while (!self->ToBeStopped()) { 166 if ( UseXbee()) {181 if (connectionType==VrpnClient::Xbee) { 167 182 ssize_t read = 0; 168 183 uint8_t response[38] = {0}; … … 193 208 194 209 mutex->GetMutex(); 195 if (id < xbee_objects.size()) {210 if (id < liteObjects.size()) { 196 211 memcpy(pos, &response[9], sizeof(pos)); 197 212 memcpy(quat, &response[9] + sizeof(pos), sizeof(quat)); … … 205 220 // self->Printf("%i %f %f %f 206 221 // %f\n",id,pos[0],pos[1],pos[2],(float)self->GetTime()/(1000*1000)); 207 VrpnObject_impl::handle_pos( xbee_objects.at(id).vrpnobject, t);222 VrpnObject_impl::handle_pos(liteObjects.at(id).vrpnobject, t); 208 223 } 209 224 } … … 211 226 } 212 227 } 213 } else {//!UseXbee()228 } else if(connectionType==VrpnClient::Vrpn) { 214 229 if(connection->connected()==vrpn_true && !isConnected) { 215 230 isConnected=true; … … 235 250 self->SleepMS(10); 236 251 } 252 }else if(connectionType==VrpnClient::VrpnLite) { 253 vrpn_TRACKERCB t; 254 float pos[3]; 255 float quat[4]; 256 Time time; 257 uint8_t id; 258 char datas[sizeof(id) + sizeof(pos)+sizeof(quat)+ sizeof(time)]; 259 int rcv=dataSocket->RecvMessage(datas,sizeof(datas),50*1000*1000); 260 if(rcv!=sizeof(datas)) continue; 261 id = datas[0]; 262 memcpy(pos, datas+sizeof(id), sizeof(pos)); 263 memcpy(quat, datas +sizeof(id)+ sizeof(pos), sizeof(quat)); 264 memcpy(&time, datas+sizeof(id) + sizeof(pos)+sizeof(quat), sizeof(time)); 265 266 for(int i=0;i<3;i++) dataSocket->NetworkToHost((char*)(&pos[i]),sizeof(pos[i])); 267 for(int i=0;i<4;i++) dataSocket->NetworkToHost((char*)(&quat[i]),sizeof(quat[i])); 268 dataSocket->NetworkToHost((char*)(&time),sizeof(time)); 269 270 mutex->GetMutex(); 271 if (id < liteObjects.size()) { 272 for (int i = 0; i < 3; i++) t.pos[i] = pos[i]; 273 // warning: t.quat is defined as (qx,qy,qz,qw), which is different from 274 // flair::core::Quaternion 275 t.quat[0] = quat[1]; 276 t.quat[1] = quat[2]; 277 t.quat[2] = quat[3]; 278 t.quat[3] = quat[0]; 279 t.msg_time.tv_sec=time/((Time)1000000000); 280 t.msg_time.tv_usec=(time%((Time)1000000000))/((Time)1000); 281 //Printf("%i %lld %lld %lld\n",id,time,t.msg_time.tv_sec,t.msg_time.tv_usec); 282 VrpnObject_impl::handle_pos(liteObjects.at(id).vrpnobject, t); 283 } 284 mutex->ReleaseMutex(); 237 285 } 238 286 } -
trunk/lib/FlairSensorActuator/src/VrpnObject.h
r286 r309 50 50 * \brief Constructor 51 51 * 52 * Construct a VrpnObject. Connection is done by IP. 52 * Construct a VrpnObject. Connection is done by IP. (vrpn) 53 53 * 54 54 * \param name VRPN object name, should be the same as defined in the server … … 62 62 * \brief Constructor 63 63 * 64 * Construct a VrpnObject. Connection is done by xbee .64 * Construct a VrpnObject. Connection is done by xbee or vrpnlite (see tools/VrpnLite in flair-src) 65 65 * 66 66 * \param name name 67 * \param id VRPN object id, should be the same as defined in the xbee bridge 67 * \param id VRPN object id, should be the same as defined in the xbee bridge or vrpnlite tool 68 68 * \param tab Tab for the user interface 69 69 * \param client VrpnClient of the connection, if unspecified, use the default one … … 88 88 * \brief Get Last Packet Time 89 89 * 90 * \return Time of last received packe 90 * \return Time of last received packet 91 91 */ 92 92 core::Time GetLastPacketTime(void) const; -
trunk/lib/FlairSensorActuator/src/VrpnObject_impl.cpp
r286 r309 48 48 return; 49 49 } 50 if (id == -1 && parent->UseXbee()) {50 if (id == -1 && GetVrpnClient()->ConnectionType()==VrpnClient::Xbee) { 51 51 self->Err("erreur aucun identifiant specifie pour la connexion Xbee\n"); 52 52 } 53 if (id != -1 && !parent->UseXbee()) { 54 self->Warn( 55 "identifiant pour la connexion Xbee ignore car pas en mode Xbee\n"); 53 if (id == -1 && GetVrpnClient()->ConnectionType()==VrpnClient::VrpnLite) { 54 self->Err("erreur aucun identifiant specifie pour la connexion VrpnLite\n"); 55 } 56 if (id != -1 && GetVrpnClient()->ConnectionType()==VrpnClient::Vrpn) { 57 self->Warn("identifiant pour la connexion ignore car inutile en mode Vrpn\n"); 56 58 } 57 59 … … 84 86 z_plot->AddCurve(output->Element(6)); 85 87 86 if (parent->UseXbee()) { 87 tracker = NULL; 88 if (GetVrpnClient()->ConnectionType()==VrpnClient::Xbee) { 88 89 parent->pimpl_->AddTrackable(this, id); 89 } else {90 } else if(GetVrpnClient()->ConnectionType()==VrpnClient::Vrpn){ 90 91 tracker = new vrpn_Tracker_Remote(name.c_str(), parent->pimpl_->connection); 91 92 tracker->register_change_handler(this, handle_pos); 92 93 tracker->shutup = true; 93 94 parent->pimpl_->AddTrackable(this); 95 } else if(GetVrpnClient()->ConnectionType()==VrpnClient::VrpnLite){ 96 parent->pimpl_->AddTrackable(this, id); 94 97 } 95 98 … … 99 102 VrpnObject_impl::~VrpnObject_impl(void) { 100 103 parent->pimpl_->RemoveTrackable(this); 101 if ( tracker != NULL) {// normal104 if (GetVrpnClient()->ConnectionType()==VrpnClient::Vrpn) { 102 105 tracker->unregister_change_handler(this, handle_pos); 103 106 delete tracker; -
trunk/lib/FlairSensorActuator/src/unexported/VrpnClient_impl.h
r294 r309 29 29 class Mutex; 30 30 class SerialPort; 31 class UdpSocket; 31 32 } 32 33 namespace gui { … … 50 51 VrpnClient_impl(flair::sensor::VrpnClient *self, std::string name, 51 52 flair::core::SerialPort *serialport, uint16_t us_period); 53 VrpnClient_impl(flair::sensor::VrpnClient *self, std::string name, 54 uint16_t port); 52 55 ~VrpnClient_impl(); 53 56 void AddTrackable(VrpnObject_impl *obj); // normal … … 56 59 void ComputeRotations(flair::core::Vector3Df &point); 57 60 void ComputeRotations(flair::core::Quaternion &quat); 58 bool UseXbee(void);59 61 void Run(void); 60 62 flair::gui::Tab *setup_tab; 61 63 flair::gui::TabWidget *tab; 62 64 vrpn_Connection *connection; 65 flair::sensor::VrpnClient::ConnectionType_t connectionType; 63 66 64 67 private: 68 void CommonConstructor(std::string name); 65 69 flair::sensor::VrpnClient *self; 66 70 flair::core::Mutex *mutex; 67 71 uint16_t us_period; 68 72 std::vector<VrpnObject_impl *> trackables; 69 typedef struct xbee_object {73 typedef struct liteObject_t { 70 74 VrpnObject_impl *vrpnobject; 71 75 uint8_t id; 72 } xbee_object;76 } liteObject_t; 73 77 74 std::vector< xbee_object> xbee_objects;78 std::vector<liteObject_t> liteObjects; 75 79 flair::gui::Tab *main_tab; 76 80 flair::core::OneAxisRotation *rotation_1, *rotation_2; … … 78 82 bool isConnected;//only for ip connection, not for xbee 79 83 std::string address; 84 flair::core::UdpSocket* dataSocket; 85 80 86 }; 81 87
Note:
See TracChangeset
for help on using the changeset viewer.