00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "interactive_robot.h"
00042 #include <eigen_conversions/eigen_msg.h>
00043 #include <moveit/robot_state/conversions.h>
00044
00045
00046
00047 const Eigen::Affine3d InteractiveRobot::DEFAULT_WORLD_OBJECT_POSE_(Eigen::Affine3d(Eigen::Translation3d(0.25, -0.5, 0.5)));
00048
00049
00050 const double InteractiveRobot::WORLD_BOX_SIZE_ = 0.15;
00051
00052
00053 const ros::Duration InteractiveRobot::min_delay_(0.01);
00054
00055
00056
00057 InteractiveRobot::InteractiveRobot(
00058 const std::string& robot_description,
00059 const std::string& robot_topic,
00060 const std::string& marker_topic,
00061 const std::string& imarker_topic) :
00062
00063 nh_(),
00064
00065 robot_state_publisher_(nh_.advertise<moveit_msgs::DisplayRobotState>(robot_topic,1)),
00066 world_state_publisher_(nh_.advertise<visualization_msgs::Marker>(marker_topic,100)),
00067
00068 interactive_marker_server_(imarker_topic),
00069 imarker_robot_(0),
00070 imarker_world_(0),
00071
00072 rm_loader_(robot_description),
00073 group_(0),
00074 user_data_(0),
00075 user_callback_(0)
00076 {
00077
00078 robot_model_ = rm_loader_.getModel();
00079 if (!robot_model_) {
00080 ROS_ERROR("Could not load robot description");
00081 throw RobotLoadException();
00082 }
00083
00084
00085 robot_state_.reset(new robot_state::RobotState(robot_model_));
00086 if (!robot_state_) {
00087 ROS_ERROR("Could not get RobotState from Model");
00088 throw RobotLoadException();
00089 }
00090 robot_state_->setToDefaultValues();
00091
00092
00093 group_ = robot_state_->getJointStateGroup("right_arm");
00094 std::string end_link = group_->getJointModelGroup()->getLinkModelNames().back();
00095 desired_group_end_link_pose_ = robot_state_->getLinkState(end_link)->getGlobalLinkTransform();
00096
00097
00098 imarker_robot_ = new IMarker(
00099 interactive_marker_server_,
00100 "robot",
00101 desired_group_end_link_pose_,
00102 "/base_footprint",
00103 boost::bind(movedRobotMarkerCallback,this,_1),
00104 IMarker::BOTH),
00105
00106
00107 desired_world_object_pose_ = DEFAULT_WORLD_OBJECT_POSE_;
00108 imarker_world_ = new IMarker(
00109 interactive_marker_server_,
00110 "world",
00111 desired_world_object_pose_,
00112 "/base_footprint",
00113 boost::bind(movedWorldMarkerCallback,this,_1),
00114 IMarker::POS),
00115
00116
00117 init_time_ = ros::Time::now();
00118 last_callback_time_ = init_time_;
00119 average_callback_duration_ = min_delay_;
00120 schedule_request_count_ = 0;
00121 publish_timer_ = nh_.createTimer(average_callback_duration_, &InteractiveRobot::updateCallback, this, true);
00122
00123
00124 scheduleUpdate();
00125 }
00126
00127 InteractiveRobot::~InteractiveRobot()
00128 {
00129 delete imarker_world_;
00130 delete imarker_robot_;
00131 }
00132
00133
00134 void InteractiveRobot::movedRobotMarkerCallback(
00135 InteractiveRobot *robot,
00136 const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
00137 {
00138 Eigen::Affine3d pose;
00139 tf::poseMsgToEigen(feedback->pose, pose);
00140 robot->setGroupPose(pose);
00141 }
00142
00143
00144 void InteractiveRobot::movedWorldMarkerCallback(
00145 InteractiveRobot *robot,
00146 const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
00147 {
00148 Eigen::Affine3d pose;
00149 tf::poseMsgToEigen(feedback->pose, pose);
00150 robot->setWorldObjectPose(pose);
00151 }
00152
00153
00154
00155 bool InteractiveRobot::setCallbackTimer(bool new_update_request)
00156 {
00157 publish_timer_.stop();
00158
00159 const ros::Time now = ros::Time::now();
00160 const ros::Duration desired_delay = std::max(min_delay_, average_callback_duration_ * 1.2);
00161 ros::Duration sec_since_last_callback = now - last_callback_time_;
00162 ros::Duration sec_til_next_callback = desired_delay - sec_since_last_callback;
00163
00164 if (schedule_request_count_)
00165 {
00166
00167 schedule_request_count_ += new_update_request ? 1 : 0;
00168 if (sec_til_next_callback <= ros::Duration(0.0001))
00169 {
00170
00171 return true;
00172 }
00173 publish_timer_.setPeriod(sec_til_next_callback);
00174 publish_timer_.start();
00175 return false;
00176 }
00177 else if (new_update_request)
00178 {
00179 if (sec_til_next_callback < min_delay_) {
00180
00181
00182 sec_til_next_callback = min_delay_;
00183 sec_since_last_callback = desired_delay - sec_til_next_callback;
00184 last_callback_time_ = now - sec_since_last_callback;
00185 }
00186 publish_timer_.setPeriod(sec_til_next_callback);
00187 publish_timer_.start();
00188 return false;
00189 }
00190 else if (!init_time_.isZero())
00191 {
00192
00193
00194
00195 if ((now - init_time_).sec >= 8)
00196 {
00197 init_time_ = ros::Time(0,0);
00198 return false;
00199 }
00200 else
00201 {
00202 publish_timer_.setPeriod(std::max(ros::Duration(1.0), average_callback_duration_*2));
00203 publish_timer_.start();
00204 return false;
00205 }
00206 }
00207 else
00208 {
00209
00210 return false;
00211 }
00212 }
00213
00214
00215
00216 void InteractiveRobot::scheduleUpdate()
00217 {
00218
00219
00220 if (setCallbackTimer(true))
00221 updateCallback(ros::TimerEvent());
00222 }
00223
00224
00225
00226 void InteractiveRobot::updateCallback(const ros::TimerEvent& e)
00227 {
00228 ros::Time tbegin = ros::Time::now();
00229 publish_timer_.stop();
00230
00231
00232 updateAll();
00233
00234
00235 ros::Time tend = ros::Time::now();
00236 average_callback_duration_ = (average_callback_duration_ + (tend - tbegin)) * 0.5;
00237 last_callback_time_ = tend;
00238 schedule_request_count_ = 0;
00239
00240
00241 setCallbackTimer(false);
00242 }
00243
00244
00245 void InteractiveRobot::updateAll()
00246 {
00247 publishWorldState();
00248
00249 if (group_->setFromIK(desired_group_end_link_pose_, 10, 0.1))
00250 {
00251 publishRobotState();
00252
00253 if (user_callback_)
00254 user_callback_(*this);
00255 }
00256 }
00257
00258
00259 void InteractiveRobot::setGroup(const std::string& name)
00260 {
00261 robot_state::JointStateGroup* group = robot_state_->getJointStateGroup(name);
00262 if (!group)
00263 {
00264 ROS_ERROR_STREAM("No joint group named " << name);
00265 if (!group_)
00266 throw RobotLoadException();
00267 }
00268 group_ = group;
00269 std::string end_link = group_->getJointModelGroup()->getLinkModelNames().back();
00270 desired_group_end_link_pose_ = robot_state_->getLinkState(end_link)->getGlobalLinkTransform();
00271 if (imarker_robot_)
00272 {
00273 imarker_robot_->move(desired_group_end_link_pose_);
00274 }
00275 }
00276
00277
00278 const std::string& InteractiveRobot::getGroupName() const
00279 {
00280 return group_->getName();
00281 }
00282
00283
00284 bool InteractiveRobot::setGroupPose(const Eigen::Affine3d& pose)
00285 {
00286 desired_group_end_link_pose_ = pose;
00287 scheduleUpdate();
00288 }
00289
00290
00291 void InteractiveRobot::publishRobotState()
00292 {
00293 moveit_msgs::DisplayRobotState msg;
00294 robot_state::robotStateToRobotStateMsg(*robot_state_, msg.state);
00295 robot_state_publisher_.publish(msg);
00296 }
00297
00298
00299
00300 void InteractiveRobot::setWorldObjectPose(const Eigen::Affine3d& pose)
00301 {
00302 desired_world_object_pose_ = pose;
00303 scheduleUpdate();
00304 }
00305
00306
00307 void InteractiveRobot::publishWorldState()
00308 {
00309 visualization_msgs::Marker marker;
00310 marker.header.frame_id = "/base_footprint";
00311 marker.header.stamp = ros::Time::now();
00312 marker.ns = "world_box";
00313 marker.id = 0;
00314 marker.type = visualization_msgs::Marker::CUBE;
00315 marker.action = visualization_msgs::Marker::ADD;
00316 marker.scale.x = WORLD_BOX_SIZE_;
00317 marker.scale.y = WORLD_BOX_SIZE_;
00318 marker.scale.z = WORLD_BOX_SIZE_;
00319 marker.color.r = 1.0f;
00320 marker.color.g = 1.0f;
00321 marker.color.b = 0.0f;
00322 marker.color.a = 0.4f;
00323 marker.lifetime = ros::Duration();
00324 tf::poseEigenToMsg(desired_world_object_pose_, marker.pose);
00325 world_state_publisher_.publish(marker);
00326 }
00327
00328
00329 void InteractiveRobot::getWorldGeometry(Eigen::Affine3d& pose, double& size)
00330 {
00331 pose = desired_world_object_pose_;
00332 size = WORLD_BOX_SIZE_;
00333 }