access.cpp
Go to the documentation of this file.
00001 
00002 
00003 #include "vrep_ros_plugin/access.h"
00004 #include "v_repLib.h"
00005 
00006 #include <iostream>
00007 #include <string>
00008 #include <sstream>
00009 #include <iomanip>
00010 #include <stdio.h>
00011 #include <boost/lexical_cast.hpp>
00012 
00013 CAccess::CAccess(){
00014 }
00015 
00016 CAccess::~CAccess(){
00017 }
00018 
00019 int CAccess::getDataLocationAndSize(const std::vector<unsigned char>& buffer,int dataName,int& theSize){
00020         int p=0;
00021         while (p<=int(buffer.size()-2*sizeof(int)))
00022         {
00023                 const int currHead = *((int*)(buffer.data()+p));
00024                 const int currSize = *((int*)(buffer.data()+p)+1);
00025                 if (currHead==dataName){
00026                     theSize=currSize;
00027                     return p+2*sizeof(int);
00028                 }
00029                 p+=2*sizeof(int)+currSize;
00030 //              for (int i=0;i<4;i++)
00031 //                      ((unsigned char*)&n)[i]=buffer[p+i];
00032 //              for (int i=0;i<4;i++)
00033 //                      ((unsigned char*)&l)[i]=buffer[p+4+i];
00034 //              if (n==dataName){
00035 //                      if (l!=0)
00036 //                      {
00037 //                              theSize=l;
00038 //                              return(p+8);
00039 //                      }
00040 //                      return(-1); // shouldn't happen if data saved correctly
00041 //              }
00042 //              p+=8+l;
00043         }
00044         return(-1); // data not present
00045 }
00046 
00047 bool CAccess::extractSerializationData(std::vector<unsigned char>& buffer, const int dataName, std::vector<unsigned char>& data){
00048 //      data.clear();
00049         int dataSize;
00050         const int dataLocation = getDataLocationAndSize(buffer, dataName, dataSize);
00051         if (dataLocation!=-1)
00052         {
00053 //              for (int i=0;i<l;i++)
00054 //                      data.push_back(buffer[p+i]);
00055             data.resize(dataSize);
00056             memcpy(data.data(), buffer.data()+dataLocation,dataSize);
00057                 buffer.erase(buffer.begin()+dataLocation-2*sizeof(int),buffer.begin()+dataLocation+dataSize);
00058                 return(true);
00059         }
00060         return(false); // that data was not present!
00061 }
00062 
00063 void CAccess::insertSerializationData(std::vector<unsigned char>& buffer,int dataName,const std::vector<unsigned char>& data){
00064         // 1. We remove the old data:
00065         std::vector<unsigned char> dummyBuffer;
00066 
00067         extractSerializationData(buffer,dataName,dummyBuffer);
00068         // 2. and insert the new data:
00069         // 2.1 data name:
00070 
00071         const unsigned int prevBuffSize = buffer.size();
00072         const unsigned int dataSize = data.size();
00073         buffer.resize(prevBuffSize+2*sizeof(int)+dataSize);
00074         int* prevBuffEnd = (int *)(buffer.data()+prevBuffSize);
00075 
00076         prevBuffEnd[0] = dataName;
00077         prevBuffEnd[1] = dataSize;
00078         memcpy((unsigned char *)(prevBuffEnd+2),data.data(),dataSize);
00079 
00080 //      for (int i=0;i<4;i++)
00081 //              buffer.push_back(((unsigned char*)&dataName)[i]);
00082 //
00083 //      std::cout << "bufferSize: " << buffer.size() << std::endl;
00084 //
00085 //      // 2.2 Data length:
00086 //      int l=int(data.size());
00087 //      for (int i=0;i<4;i++)
00088 //              buffer.push_back(((unsigned char*)&l)[i]);
00089 //      // 2.3 The data itself:
00090 //      for (int i=0;i<l;i++)
00091 //              buffer.push_back(data[i]);
00092 }
00093 
00094 void CAccess::push_int(std::vector<unsigned char>& buffer, const int data){
00095 //      for (unsigned int i=0;i<sizeof(data);i++)
00096 //              buffer.push_back(((unsigned char*)&data)[i]);
00097         buffer.resize(buffer.size()+sizeof(int));
00098         *((int*)(&(*buffer.end()))-1) = data;
00099 }
00100 
00101 void CAccess::push_float(std::vector<unsigned char>& buffer, const float data){
00102 //      for (unsigned int i=0;i<sizeof(data);i++)
00103 //              buffer.push_back(((unsigned char*)&data)[i]);
00104     buffer.resize(buffer.size()+sizeof(float));
00105     *((float*)(&(*buffer.end()))-1) = data;
00106 }
00107 
00108 void CAccess::push_float(std::vector<unsigned char>& buffer, const std::vector<float> data){
00109     buffer.resize(buffer.size()+sizeof(float)*data.size());
00110         float * buf = ((float*)(&(*buffer.end()))) - data.size();
00111         memcpy(buf, data.data(), sizeof(float)*data.size());
00112 }
00113 
00114 int CAccess::pop_int(std::vector<unsigned char>& buffer){
00115 //      int retVal=0;
00116 //      if (buffer.size()<sizeof(retVal))
00117 //              return(retVal);
00118 //      for (int i=int(sizeof(retVal))-1;i>=0;i--)
00119 //      {
00120 //              ((unsigned char*)&retVal)[i]=buffer[buffer.size()-1];
00121 //              buffer.pop_back();
00122 //      }
00123 //      return(retVal);
00124     if (buffer.size()<sizeof(int)){
00125           return 0;
00126     } else {
00127         const float retVal=*((int*)(&(*buffer.end()))-1);
00128         buffer.resize(buffer.size()-sizeof(int));
00129         return retVal;
00130     }
00131 }
00132 
00133 float CAccess::pop_float(std::vector<unsigned char>& buffer){
00134 //      float retVal=0.0f;
00135 //      if (buffer.size()<sizeof(retVal))
00136 //              return(retVal);
00137 //      for (int i=int(sizeof(retVal))-1;i>=0;i--)
00138 //      {
00139 //              ((unsigned char*)&retVal)[i]=buffer[buffer.size()-1];
00140 //              buffer.pop_back();
00141 //      }
00142 //      return(retVal);
00143     if (buffer.size()<sizeof(float)){
00144           return 0.0;
00145     } else {
00146         const float retVal=*((float*)(&(*buffer.end()))-1);
00147         buffer.resize(buffer.size()-sizeof(float));
00148         return retVal;
00149     }
00150 }
00151 
00152 std::vector<float> CAccess::pop_float(std::vector<unsigned char>& buffer, const unsigned int n){
00153         std::vector<float> out;
00154         if (buffer.size()>=sizeof(float)*n){
00155                 out.resize(n);
00156                 const float * buf = ((const float*)(&(*buffer.end()))) - n;
00157                 memcpy(out.data(), buf, sizeof(float)*n);
00158                 buffer.resize(buffer.size()-sizeof(float)*n);
00159         }
00160         return out;
00161 }
00162 
00163 void CustomDataHeaders::registerCustomDataHeaders(){
00164 
00165 
00166     simRegisterCustomLuaVariable("sim_ext_ros_bridge_developer_data_header", (boost::lexical_cast<std::string>(int(DEVELOPER_DATA_HEADER))).c_str());
00167 
00168     // Quadrotor defines
00169     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_main", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_MAIN))).c_str());
00170     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_tk_data_main", (boost::lexical_cast<std::string>(int(QUADROTOR_TK_DATA_MAIN))).c_str());
00171     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_motor_0", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_MOTOR_0))).c_str());
00172     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_motor_1", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_MOTOR_1))).c_str());
00173     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_motor_2", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_MOTOR_2))).c_str());
00174     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_motor_3", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_MOTOR_3))).c_str());
00175     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_com", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_COM))).c_str());
00176     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_tf_ratio", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_TF_RATIO))).c_str());
00177     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_data_ctrl_mode", (boost::lexical_cast<std::string>(int(QUADROTOR_DATA_CTRL_MODE))).c_str());
00178 
00179     //Quadrotor ctrl modes
00180     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_ctrl_mode_direct", (boost::lexical_cast<std::string>(int(DIRECT))).c_str());
00181     simRegisterCustomLuaVariable("sim_ext_ros_bridge_quadrotor_ctrl_mode_internal", (boost::lexical_cast<std::string>(int(INTERNAL))).c_str());
00182 
00183     // IMU defines
00184     simRegisterCustomLuaVariable("sim_ext_ros_bridge_imu_data_main", (boost::lexical_cast<std::string>(int(IMU_DATA_MAIN))).c_str());
00185     simRegisterCustomLuaVariable("sim_ext_ros_bridge_imu_data_mass", (boost::lexical_cast<std::string>(int(IMU_DATA_MASS))).c_str());
00186     simRegisterCustomLuaVariable("sim_ext_ros_bridge_imu_data_force", (boost::lexical_cast<std::string>(int(IMU_DATA_FORCE))).c_str());
00187     simRegisterCustomLuaVariable("sim_ext_ros_bridge_imu_data_freq", (boost::lexical_cast<std::string>(int(IMU_DATA_FREQ))).c_str());
00188     simRegisterCustomLuaVariable("sim_ext_ros_bridge_imu_data_cutoff", (boost::lexical_cast<std::string>(int(IMU_DATA_CUTOFF))).c_str());
00189 
00190     // Force sensor defines
00191     simRegisterCustomLuaVariable("sim_ext_ros_bridge_force_sensor_data_main", (boost::lexical_cast<std::string>(int(FORCE_SENSOR_DATA_MAIN))).c_str());
00192 
00193     // Camera defines
00194     simRegisterCustomLuaVariable("sim_ext_ros_bridge_camera_data_main", (boost::lexical_cast<std::string>(int(CAMERA_DATA_MAIN))).c_str());
00195     simRegisterCustomLuaVariable("sim_ext_ros_bridge_camera_data_freq", (boost::lexical_cast<std::string>(int(CAMERA_DATA_FREQ))).c_str());
00196     simRegisterCustomLuaVariable("sim_ext_ros_bridge_camera_data_rgb", (boost::lexical_cast<std::string>(int(CAMERA_DATA_RGB))).c_str());
00197     simRegisterCustomLuaVariable("sim_ext_ros_bridge_camera_data_has_depth", (boost::lexical_cast<std::string>(int(CAMERA_DATA_HAS_DEPTH))).c_str());
00198     simRegisterCustomLuaVariable("sim_ext_ros_bridge_camera_data_use_float", (boost::lexical_cast<std::string>(int(CAMERA_DATA_USE_FLOAT))).c_str());
00199 
00200     // Object Pose defines
00201     simRegisterCustomLuaVariable("sim_ext_ros_bridge_obj_pose_data_main", (boost::lexical_cast<std::string>(int(OBJ_POSE_DATA_MAIN))).c_str());
00202 
00203     // Object twist defines
00204     simRegisterCustomLuaVariable("sim_ext_ros_bridge_obj_twist_data_main", (boost::lexical_cast<std::string>(int(OBJ_TWIST_DATA_MAIN))).c_str());
00205 
00206     // Manipulator defines
00207     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_data_main", (boost::lexical_cast<std::string>(int(MANIPULATOR_DATA_MAIN))).c_str());
00208     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_data_freq", (boost::lexical_cast<std::string>(int(MANIPULATOR_DATA_FREQ))).c_str());
00209     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_data_joint", (boost::lexical_cast<std::string>(int(MANIPULATOR_DATA_JOINT))).c_str());
00210     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_data_ctrl_mode", (boost::lexical_cast<std::string>(int(MANIPULATOR_DATA_CTRL_MODE))).c_str());
00211 
00212     // Manipulator ctrl modes
00213     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_ctrl_mode_TF_position", (boost::lexical_cast<std::string>(int(TF_POSITION))).c_str());
00214     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_ctrl_mode_TF_velocity", (boost::lexical_cast<std::string>(int(TF_VELOCITY))).c_str());
00215     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_ctrl_mode_TF_effort", (boost::lexical_cast<std::string>(int(TF_EFFORT))).c_str());
00216     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_ctrl_mode_MOT_velocity", (boost::lexical_cast<std::string>(int(MOT_VELOCITY))).c_str());
00217     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_ctrl_mode_Passive_mode", (boost::lexical_cast<std::string>(int(PASSIVE_MODE))).c_str());
00218     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_ctrl_mode_Passive_mode_velocity", (boost::lexical_cast<std::string>(int(PASSIVE_MODE_VELOCITY))).c_str());
00219     simRegisterCustomLuaVariable("sim_ext_ros_bridge_manipulator_ctrl_mode_Ignore_mode", (boost::lexical_cast<std::string>(int(IGNORE_MODE))).c_str());
00220 
00221     // Contact defines
00222     simRegisterCustomLuaVariable("sim_ext_ros_bridge_contact_data_main", (boost::lexical_cast<std::string>(int(CONTACT_DATA_MAIN))).c_str());
00223 
00224 }
00225 
00226 
00227 
00228 


vrep_ros_plugin
Author(s): Riccardo Spica , Giovanni Claudio
autogenerated on Sat Jun 8 2019 20:22:41