Force_sensorHandler.cpp
Go to the documentation of this file.
00001 
00002 #include <pluginlib/class_list_macros.h>
00003 
00004 #include <force_sensor_handler/Force_sensorHandler.h>
00005 #include <v_repLib.h>
00006 #include <vrep_ros_plugin/access.h>
00007 
00008 #include <geometry_msgs/WrenchStamped.h>
00009 #include <Eigen/Geometry>
00010 
00011 #include <vrep_ros_plugin/ConsoleHandler.h>
00012 
00013 
00014 Force_sensorHandler::Force_sensorHandler() : GenericObjectHandler(),
00015     //_handleOfForceSensor(-1),
00016     _lastPublishedForceSensor(0.0),
00017     _acquisitionFrequency(-1.0)
00018   {
00019 }
00020 
00021 Force_sensorHandler::~Force_sensorHandler(){
00022 }
00023 
00024 unsigned int Force_sensorHandler::getObjectType() const {
00025     return CustomDataHeaders::FORCE_SENSOR_DATA_MAIN;
00026 }
00027 
00028 void Force_sensorHandler::synchronize(){
00029 
00030 
00031        // Remove # chars for compatibility with ROS
00032     _associatedObjectName = simGetObjectName(_associatedObjectID);
00033     std::string objectName(_associatedObjectName);
00034     std::replace( objectName.begin(), objectName.end(), '#', '_');
00035     _pub = _nh.advertise<geometry_msgs::WrenchStamped>(objectName+"/data", 1000);
00036 
00037 }
00038 
00039 void Force_sensorHandler::handleSimulation(){
00040     // called when the main script calls: simHandleModule
00041     if(!_initialized){
00042         _initialize();
00043     }
00044 
00045     ros::Time now = ros::Time::now();
00046 
00047     const simFloat currentSimulationTime = simGetSimulationTime();
00048 
00049 
00050     if ( ((currentSimulationTime - _lastPublishedForceSensor) >= 1.0/_acquisitionFrequency) ){
00051 
00052 
00053         simFloat force[3];
00054         simFloat torque[3];
00055 
00056         int read_check = -1;
00057 
00058         read_check = simReadForceSensor(_associatedObjectID, force, torque);
00059 
00060                 if(read_check > 0){
00061 
00062                         //std::cout <<"- [" << _associatedObjectName << "]: Reading data from Force Sensor" << std::endl;
00063 
00064                         // Fill the force sensor msg
00065                         geometry_msgs::WrenchStamped msg;
00066                         msg.header.stamp = ros::Time::now();
00067                         msg.wrench.force.x = force[0];
00068                         msg.wrench.force.y = force[1];
00069                         msg.wrench.force.z = force[2];
00070                         msg.wrench.torque.x = torque[0];
00071                         msg.wrench.torque.y = torque[1];
00072                         msg.wrench.torque.z = torque[2];
00073                         _pub.publish(msg); // Publish the Force sensor message in ROS
00074 
00075                         _lastPublishedForceSensor = currentSimulationTime;
00076                 }
00077 
00078                 else
00079                 {
00080                         std::cout <<"- [" << _associatedObjectName << "]:  Cannot read the data from the force sensor" << std::endl;
00081 
00082                 }
00083 
00084 
00085     }
00086 
00087 }
00088 
00089 
00090 void Force_sensorHandler::_initialize(){
00091     if (_initialized)
00092         return;
00093 
00094     // get some data from the main object
00095     std::vector<unsigned char> developerCustomData;
00096     getDeveloperCustomData(developerCustomData);
00097 
00098 
00099     // 2. From that retrieved data, try to extract sub-data with the IMU_DATA_MAIN tag:
00100     std::vector<unsigned char> tempMainData;
00101     std::stringstream ss;
00102 
00103     if (CAccess::extractSerializationData(developerCustomData, CustomDataHeaders::FORCE_SENSOR_DATA_MAIN,tempMainData)){
00104         _acquisitionFrequency=CAccess::pop_float(tempMainData);
00105         if (_acquisitionFrequency > 0.0){
00106             ss << "- [" << _associatedObjectName << "] Frequency publisher: " << _acquisitionFrequency << "." << std::endl;
00107         } else {
00108             ss << "- [" << _associatedObjectName << "] Frequency publisher: simulation frequency."  << std::endl;
00109         }
00110     } else {
00111         ss << "- [" << _associatedObjectName << "] Force Sensor frequency publisher not specified. using simulation frequency as default."  << std::endl;
00112     }
00113 
00114 
00115 
00116 //    // search associated objects
00117 //    std::vector<int> toExplore;
00118 //    toExplore.push_back(_associatedObjectID); // We start exploration with the base of the quadrotor-tree
00119 //    while (toExplore.size()!=0)
00120 //    {
00121 //        int objHandle=toExplore[toExplore.size()-1];
00122 //        toExplore.pop_back();
00123 //        // 1. Add this object's children to the list to explore:
00124 //        int index=0;
00125 //        int childHandle=simGetObjectChild(objHandle,index++);
00126 //        while (childHandle!=-1) {
00127 //            toExplore.push_back(childHandle);
00129 //            childHandle=simGetObjectChild(objHandle,index++);
00130 //        }
00131 //        // 2. Now check if this object has one of the tags we are looking for:
00132 //        // a. Get all the developer data attached to this scene object (this is custom data added by the developer):
00133 //        int buffSize=simGetObjectCustomDataLength(objHandle, CustomDataHeaders::DEVELOPER_DATA_HEADER);
00134 //        if (buffSize!=0) { // Yes there is some custom data written by us (the developer with the DEVELOPER_DATA_HEADER header)
00135 //            char* datBuff=new char[buffSize];
00136 //            simGetObjectCustomData(objHandle, CustomDataHeaders::DEVELOPER_DATA_HEADER,datBuff);
00137 //            std::vector<unsigned char> developerCustomData(datBuff,datBuff+buffSize);
00138 //            delete[] datBuff;
00139 //            // b. From that retrieved data, try to extract sub-data with the searched tags:
00140 //            std::vector<unsigned char> tempMainData;
00141 //
00142 //            if (CAccess::extractSerializationData(developerCustomData, CustomDataHeaders::IMU_DATA_MASS,tempMainData)){
00143 //                _handleOfMass=objHandle;
00144 //                simGetObjectFloatParameter(_handleOfMass, 3005, &_mass);
00145 //
00146 //            } else if (CAccess::extractSerializationData(developerCustomData,CustomDataHeaders::IMU_DATA_FORCE,tempMainData)){
00147 //                _handleOfForceSensor=objHandle; // We found the IMU_DATA_FORCE tag. This is the Force sensor!
00148 //                ss << "- [" << _associatedObjectName << "] Found ForceSensor. The acceleration will be computed and published." << std::endl;
00149 //            }
00150 //
00151 //        }
00152 //    }
00153 
00154 
00155 
00156     ConsoleHandler::printInConsole(ss);
00157 
00158     _lastPublishedForceSensor = -1.0;
00159 
00160     _initialized=true;
00161 }
00162 
00163 bool Force_sensorHandler::endOfSimulation(){
00164 
00165     _initialized=false;
00166     return(false); // We don't want this object automatically destroyed at the end of simulation
00167 }
00168 
00169 PLUGINLIB_EXPORT_CLASS(Force_sensorHandler, GenericObjectHandler)


force_sensor_handler
Author(s): Giovanni Claudio
autogenerated on Sat Jun 8 2019 20:22:49