00001 00028 #include <ros/ros.h> 00029 00030 #include <cob_lookat_controller/cob_lookat_driver.h> 00031 00032 00033 bool CobLookatDriver::initialize() 00034 { 00035 // JointNames 00036 if(!nh_.getParam("joint_names", joints_)) 00037 { 00038 ROS_ERROR("Parameter 'joint_names' not set"); 00039 return false; 00040 } 00041 dof_ = joints_.size(); 00042 00043 00044 current_pos_.resize(dof_, 0.0); 00045 current_pos_[0] = 1.0; //this is in order to see the frame 00046 current_vel_.resize(dof_, 0.0); 00047 00048 if (nh_.hasParam("update_rate")) 00049 { nh_.getParam("update_rate", update_rate_); } 00050 else 00051 { update_rate_ = 68.0; } 00052 00053 command_vel_sub_ = nh_.subscribe("command_vel", 1, &CobLookatDriver::command_vel_cb, this); 00054 jointstate_pub_ = nh_.advertise<sensor_msgs::JointState> ("joint_states", 1); 00055 00056 ROS_INFO("...initialized!"); 00057 return true; 00058 } 00059 00060 void CobLookatDriver::run() 00061 { 00062 ros::Rate r(update_rate_); 00063 while(ros::ok()) 00064 { 00065 update_state(); 00066 publish_state(); 00067 00068 ros::spinOnce(); 00069 r.sleep(); 00070 } 00071 } 00072 00073 void CobLookatDriver::update_state() 00074 { 00075 double dt = (ros::Time::now() - last_update_).toSec(); 00076 00077 for(unsigned int i=0; i<dof_; i++) 00078 current_pos_[i] += current_vel_[i]*dt; 00079 } 00080 00081 00082 00083 void CobLookatDriver::publish_state() 00084 { 00085 sensor_msgs::JointState msg; 00086 msg.header.stamp = ros::Time::now(); 00087 for(unsigned int i=0; i<dof_; i++) 00088 { 00089 msg.name.push_back(joints_[i]); 00090 msg.position.push_back(current_pos_[i]); 00091 msg.velocity.push_back(current_vel_[i]); 00092 msg.effort.push_back(0.0); 00093 } 00094 00095 jointstate_pub_.publish(msg); 00096 } 00097 00098 void CobLookatDriver::command_vel_cb(const std_msgs::Float64MultiArray::ConstPtr& msg) 00099 { 00101 if(msg->data.size() != dof_) 00102 { 00103 ROS_ERROR("DoF do not match! Stopping!"); 00104 current_vel_.assign(dof_, 0.0); 00105 return; 00106 } 00107 00108 for(unsigned int i=0; i<dof_; i++) 00109 { 00110 current_vel_[i]=msg->data[i]; 00111 } 00112 00113 last_update_ = ros::Time::now(); 00114 } 00115