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
00026
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
00033 std::vector<unsigned char> tempMainData;
00034
00035 if (CAccess::extractSerializationData(developerCustomData,CustomDataHeaders::SET_OBJ_TWIST_DATA_MAIN,tempMainData)){
00036
00037
00038 _associatedObjectName = simGetObjectName(_associatedObjectID);
00039 std::string objectName(_associatedObjectName);
00040 std::replace( objectName.begin(), objectName.end(), '#', '_');
00041
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
00056 if(!_initialized){
00057 _initialize();
00058 }
00059
00060 Eigen::Quaternion < simFloat > orientation;
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
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
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
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
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
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
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
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