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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 }
00044 return(-1);
00045 }
00046
00047 bool CAccess::extractSerializationData(std::vector<unsigned char>& buffer, const int dataName, std::vector<unsigned char>& data){
00048
00049 int dataSize;
00050 const int dataLocation = getDataLocationAndSize(buffer, dataName, dataSize);
00051 if (dataLocation!=-1)
00052 {
00053
00054
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);
00061 }
00062
00063 void CAccess::insertSerializationData(std::vector<unsigned char>& buffer,int dataName,const std::vector<unsigned char>& data){
00064
00065 std::vector<unsigned char> dummyBuffer;
00066
00067 extractSerializationData(buffer,dataName,dummyBuffer);
00068
00069
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
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 }
00093
00094 void CAccess::push_int(std::vector<unsigned char>& buffer, const int data){
00095
00096
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
00103
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
00116
00117
00118
00119
00120
00121
00122
00123
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
00135
00136
00137
00138
00139
00140
00141
00142
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
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
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
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
00191 simRegisterCustomLuaVariable("sim_ext_ros_bridge_force_sensor_data_main", (boost::lexical_cast<std::string>(int(FORCE_SENSOR_DATA_MAIN))).c_str());
00192
00193
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
00201 simRegisterCustomLuaVariable("sim_ext_ros_bridge_obj_pose_data_main", (boost::lexical_cast<std::string>(int(OBJ_POSE_DATA_MAIN))).c_str());
00202
00203
00204 simRegisterCustomLuaVariable("sim_ext_ros_bridge_obj_twist_data_main", (boost::lexical_cast<std::string>(int(OBJ_TWIST_DATA_MAIN))).c_str());
00205
00206
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
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
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