SetTwistHandler.cpp
Go to the documentation of this file.
00001 
00002 #include <pluginlib/class_list_macros.h>
00003 
00004 #include <rigid_body_handler/SetTwistHandler.h>
00005 #include <v_repLib.h>
00006 #include <vrep_ros_plugin/access.h>
00007 
00008 
00009 #include <Eigen/Geometry>
00010 
00011 #include <vrep_ros_plugin/ConsoleHandler.h>
00012 
00013 
00014 SetTwistHandler::SetTwistHandler() : GenericObjectHandler(){
00015 }
00016 
00017 SetTwistHandler::~SetTwistHandler(){
00018 }
00019 
00020 unsigned int SetTwistHandler::getObjectType() const {
00021     return CustomDataHeaders::SET_OBJ_TWIST_DATA_MAIN;
00022 }
00023 
00024 void SetTwistHandler::synchronize(){
00025     // We update SetTwistHandler's data from its associated scene object custom data:
00026     // 1. Get all the developer data attached to the associated scene object (this is custom data added by the developer):
00027     int buffSize = simGetObjectCustomDataLength(_associatedObjectID, CustomDataHeaders::DEVELOPER_DATA_HEADER);
00028     char* datBuff=new char[buffSize];
00029     simGetObjectCustomData(_associatedObjectID, CustomDataHeaders::DEVELOPER_DATA_HEADER,datBuff);
00030     std::vector<unsigned char> developerCustomData(datBuff,datBuff+buffSize);
00031     delete[] datBuff;
00032     // 2. From that retrieved data, try to extract sub-data with the SET_OBJ_TWIST_DATA_MAIN tag:
00033     std::vector<unsigned char> tempMainData;
00034 
00035     if (CAccess::extractSerializationData(developerCustomData,CustomDataHeaders::SET_OBJ_TWIST_DATA_MAIN,tempMainData)){ // Yes, the tag is there. For now we only have to synchronize _maxVelocity:
00036 
00037         // Remove # chars for compatibility with ROS
00038                 _associatedObjectName = simGetObjectName(_associatedObjectID);
00039                 std::string objectName(_associatedObjectName);
00040                 std::replace( objectName.begin(), objectName.end(), '#', '_');
00041                 //_pub = _nh.advertise<geometry_msgs::TwistStamped>(objectName+"/twist", 1);
00042                 std::stringstream streamtemp;
00043 
00044 
00045                 streamtemp << "- [Set Twist Target] in '" << _associatedObjectName << "'. Subscribed on the topic "<< objectName<<"/SetTwist"<< std::endl;
00046                 ConsoleHandler::printInConsole(streamtemp);
00047 
00048     }
00049 
00050 
00051 
00052 }
00053 
00054 void SetTwistHandler::handleSimulation(){
00055     // called when the main script calls: simHandleModule
00056     if(!_initialized){
00057         _initialize();
00058     }
00059 
00060     Eigen::Quaternion < simFloat > orientation; //(x,y,z,w)
00061     Eigen::Matrix< simFloat, 3, 1> linVelocity((simFloat)_twistCommands.twist.linear.x,
00062                 (simFloat)_twistCommands.twist.linear.y,
00063                 (simFloat)_twistCommands.twist.linear.z);
00064     Eigen::Matrix< simFloat, 3, 1> angVelocity((simFloat)_twistCommands.twist.angular.x,
00065                 (simFloat)_twistCommands.twist.angular.y,
00066                 (simFloat)_twistCommands.twist.angular.z);
00067 
00068     // Input velocity is expected to be in body frame but V-Rep expects it to be in world frame.
00069     if(simGetObjectQuaternion(_associatedObjectID, -1, orientation.coeffs().data())!=-1){
00070         linVelocity = orientation*linVelocity;
00071             angVelocity = orientation*angVelocity;
00072     } else {
00073         std::stringstream ss;
00074         ss << "- [" << _associatedObjectName << "] Error getting orientation. " << std::endl;
00075         ConsoleHandler::printInConsole(ss);
00076     }
00077 
00078     simResetDynamicObject(_associatedObjectID);
00079 
00080         //Set object velocity
00081     if (_isStatic){
00082         Eigen::Matrix<simFloat, 3, 1> position;
00083         simGetObjectPosition(_associatedObjectID, -1, position.data());
00084         const simFloat timeStep = simGetSimulationTimeStep();
00085         position = position + timeStep*linVelocity;
00086         simSetObjectPosition(_associatedObjectID, -1, position.data());
00087         const simFloat angle = timeStep*angVelocity.norm();
00088         if(angle > 1e-6){
00089                 orientation = Eigen::Quaternion< simFloat >(Eigen::AngleAxis< simFloat >(timeStep*angVelocity.norm(), angVelocity/angVelocity.norm()))*orientation;
00090         }
00091         simSetObjectQuaternion(_associatedObjectID, -1, orientation.coeffs().data());
00092     } else {
00093                 // Apply the linear velocity to the object
00094                 if(simSetObjectFloatParameter(_associatedObjectID, 3000, linVelocity[0])
00095                         && simSetObjectFloatParameter(_associatedObjectID, 3001, linVelocity[1])
00096                         && simSetObjectFloatParameter(_associatedObjectID, 3002, linVelocity[2])==-1) {
00097                         std::stringstream ss;
00098                         ss << "- [" << _associatedObjectName << "] Error setting linear velocity. " << std::endl;;
00099                         ConsoleHandler::printInConsole(ss);
00100                 }
00101                 // Apply the angular velocity to the object
00102                 if(simSetObjectFloatParameter(_associatedObjectID, 3020, angVelocity[0])
00103                         && simSetObjectFloatParameter(_associatedObjectID, 3021, angVelocity[1])
00104                         && simSetObjectFloatParameter(_associatedObjectID, 3022, angVelocity[2])==-1) {
00105                         std::stringstream ss;
00106                         ss << "- [" << _associatedObjectName << "] Error setting angular velocity. " << std::endl;;
00107                         ConsoleHandler::printInConsole(ss);
00108                 }
00109     }
00110 
00111 }
00112 
00113 
00114 void SetTwistHandler::_initialize(){
00115     if (_initialized)
00116         return;
00117 
00118 
00119 //    std::vector<int> toExplore;
00120 //    toExplore.push_back(_associatedObjectID); // We start exploration with the base of the quadrotor-tree
00121 //    while (toExplore.size()!=0)
00122 //    {
00123 //        int objHandle=toExplore[toExplore.size()-1];
00124 //        toExplore.pop_back();
00125 //        // 1. Add this object's children to the list to explore:
00126 //        int index=0;
00127 //        int childHandle=simGetObjectChild(objHandle,index++);
00128 //        while (childHandle!=-1) {
00129 //            toExplore.push_back(childHandle);
00131 //            childHandle=simGetObjectChild(objHandle,index++);
00132 //        }
00133 //        // 2. Now check if this object has one of the tags we are looking for:
00134 //        // a. Get all the developer data attached to this scene object (this is custom data added by the developer):
00135 //        int buffSize=simGetObjectCustomDataLength(objHandle,CustomDataHeaders::DEVELOPER_DATA_HEADER);
00136 //        if (buffSize!=0) { // Yes there is some custom data written by us (the developer with the DEVELOPER_DATA_HEADER header)
00137 //            char* datBuff=new char[buffSize];
00138 //            simGetObjectCustomData(objHandle,CustomDataHeaders::DEVELOPER_DATA_HEADER,datBuff);
00139 //            std::vector<unsigned char> developerCustomData(datBuff,datBuff+buffSize);
00140 //            delete[] datBuff;
00141 //            // b. From that retrieved data, try to extract sub-data with the searched tags:
00142 //            std::vector<unsigned char> quadrotorTagData;
00143 //
00144 //
00145 //        }
00146 //    }
00147 
00148     std::string objectName(_associatedObjectName);
00149     std::replace( objectName.begin(), objectName.end(), '#', '_');
00150     _sub = _nh.subscribe(objectName+"/SetTwist", 1, &SetTwistHandler::TwistCommandCallback, this);
00151 
00152     //Check if the object is static
00153     simGetObjectIntParameter(_associatedObjectID, 3003, &_isStatic);
00154     if(_isStatic){
00155                 std::stringstream ss;
00156                 ss << "- [" << _associatedObjectName << "] WARNING: setting velocity of a static object might give inaccurate results." << std::endl;;
00157                 ConsoleHandler::printInConsole(ss);
00158     }
00159 
00160     _initialized=true;
00161 }
00162 
00163 
00164 
00165 void SetTwistHandler::TwistCommandCallback(const geometry_msgs::TwistStamped& msg){
00166 
00167         _twistCommands = msg;
00168 
00169 }
00170 
00171 
00172 PLUGINLIB_EXPORT_CLASS(SetTwistHandler, GenericObjectHandler)
00173 


rigid_body_handler
Author(s): Riccardo Spica , Giovanni Claudio
autogenerated on Wed Sep 9 2015 18:55:00