19 #include <std_msgs/String.h> 20 #include <std_msgs/Float64MultiArray.h> 21 #include <sensor_msgs/JointState.h> 22 #include <control_msgs/JointTrajectoryControllerState.h> 24 #include <control_msgs/FollowJointTrajectoryAction.h> 28 #include <std_srvs/Trigger.h> 29 #include <cob_srvs/SetString.h> 30 #include <cob_srvs/SetFloat.h> 63 trajectory_msgs::JointTrajectory
traj_;
71 action_name_(
"follow_joint_trajectory")
73 joint_vel_pub_ = n_.
advertise<std_msgs::Float64MultiArray>(
"joint_group_velocity_controller/command", 1);
79 srvClient_SetOperationMode = n_.
serviceClient<cob_srvs::SetString>(
"driver/set_operation_mode");
80 while(!srvClient_SetOperationMode.
exists())
82 ROS_INFO(
"Waiting for operationmode service to become available");
90 current_operation_mode_ =
"undefined";
93 double maxError = 0.7;
94 double overlap_time = 0.4;
95 velocity_timeout_ = 2.0;
99 if (n_private_.
hasParam(
"joint_names"))
101 n_private_.
getParam(
"joint_names", JointNames_);
105 ROS_ERROR(
"Parameter 'joint_names' not set");
107 DOF = JointNames_.size();
111 n_private_.
getParam(
"ptp_vel", PTPvel);
115 n_private_.
getParam(
"ptp_acc", PTPacc);
117 if (n_private_.
hasParam(
"max_error"))
119 n_private_.
getParam(
"max_error", maxError);
121 if (n_private_.
hasParam(
"overlap_time"))
123 n_private_.
getParam(
"overlap_time", overlap_time);
125 if (n_private_.
hasParam(
"operation_mode"))
127 n_private_.
getParam(
"operation_mode", current_operation_mode_);
129 q_current.resize(DOF);
130 ROS_INFO(
"starting controller with DOF: %d PTPvel: %f PTPAcc: %f maxError %f", DOF, PTPvel, PTPacc, maxError);
131 traj_generator_ =
new genericArmCtrl(DOF, PTPvel, PTPacc, maxError);
140 if (n_private_.
hasParam(
"frequency"))
142 n_private_.
getParam(
"frequency", frequency);
143 ROS_INFO(
"Setting controller frequency to %f HZ", frequency);
148 ROS_WARN(
"Parameter 'frequency' not available, setting to default value: %f Hz", frequency);
155 ROS_INFO(
"Stopping trajectory controller.");
167 ROS_INFO(
"Setting velocity to %f", req.data);
175 ROS_INFO(
"Setting acceleration to %f", req.data);
183 current_operation_mode_ = message->data;
186 void state_callback(
const control_msgs::JointTrajectoryControllerStatePtr& message)
188 std::vector<double> positions = message->actual.positions;
189 for(
unsigned int i = 0; i < positions.size(); i++)
191 q_current[i] = positions[i];
197 if(!executing_ || preemted_)
200 cob_srvs::SetString opmode;
201 opmode.request.data =
"velocity";
202 srvClient_SetOperationMode.
call(opmode);
204 while(current_operation_mode_ !=
"velocity")
206 ROS_INFO(
"waiting for component to go to velocity mode");
216 std::vector<double> traj_start;
217 if(preemted_ ==
true)
219 ROS_INFO(
"There is a old trajectory currently running");
220 traj_start = traj_generator_->
last_q;
221 trajectory_msgs::JointTrajectory temp_traj;
222 temp_traj = trajectory;
224 trajectory_msgs::JointTrajectoryPoint p;
225 p.positions.resize(DOF);
226 p.velocities.resize(DOF);
227 p.accelerations.resize(DOF);
228 for(
int i = 0; i<
DOF; i++)
230 p.positions.at(i) = traj_start.at(i);
231 p.velocities.at(i) = 0.0;
232 p.accelerations.at(i) = 0.0;
234 std::vector<trajectory_msgs::JointTrajectoryPoint>::iterator it;
235 it = temp_traj.points.begin();
236 temp_traj.points.insert(it,p);
238 for(
int i = 0; i<
DOF; i++)
240 p.positions.at(i) = traj_generator_->
last_q1.at(i);
241 p.velocities.at(i) = 0.0;
242 p.accelerations.at(i) = 0.0;
244 it = temp_traj.points.begin();
245 temp_traj.points.insert(it,p);
246 for(
int i = 0; i<
DOF; i++)
248 p.positions.at(i) = traj_generator_->
last_q2.at(i);
249 p.velocities.at(i) = 0.0;
250 p.accelerations.at(i) = 0.0;
252 it = temp_traj.points.begin();
253 temp_traj.points.insert(it,p);
254 for(
int i = 0; i<
DOF; i++)
256 p.positions.at(i) = traj_generator_->
last_q3.at(i);
257 p.velocities.at(i) = 0.0;
258 p.accelerations.at(i) = 0.0;
260 it = temp_traj.points.begin();
261 temp_traj.points.insert(it,p);
262 for(
int i = 0; i<
DOF; i++)
264 p.positions.at(i) = q_current.at(i);
265 p.velocities.at(i) = 0.0;
266 p.accelerations.at(i) = 0.0;
268 it = temp_traj.points.begin();
269 temp_traj.points.insert(it,p);
276 trajectory_msgs::JointTrajectory temp_traj;
277 temp_traj = trajectory;
278 if(temp_traj.points.size() == 1)
281 traj_generator_->
moveThetas(temp_traj.points[0].positions, traj_start);
286 trajectory_msgs::JointTrajectoryPoint p;
287 p.positions.resize(DOF);
288 p.velocities.resize(DOF);
289 p.accelerations.resize(DOF);
290 for(
int i = 0; i<
DOF; i++)
292 p.positions.at(i) = traj_start.at(i);
293 p.velocities.at(i) = 0.0;
294 p.accelerations.at(i) = 0.0;
296 std::vector<trajectory_msgs::JointTrajectoryPoint>::iterator it;
297 it = temp_traj.points.begin();
298 temp_traj.points.insert(it,p);
328 ROS_INFO(
"Received new goal trajectory with %lu points",goal->trajectory.points.size());
349 watchdog_counter = 0;
350 if (!
ros::ok() || current_operation_mode_ !=
"velocity")
364 ROS_INFO(
"Preempted trajectory action");
367 std::vector<double> des_vel;
368 if(traj_generator_->
step(q_current, des_vel))
375 std_msgs::Float64MultiArray target_joint_vel;
376 for(
int i=0; i<
DOF; i++)
378 target_joint_vel.data.push_back(des_vel.at(i));
382 joint_vel_pub_.
publish(target_joint_vel);
386 ROS_INFO(
"An controller error occured!");
393 if(watchdog_counter < 10)
395 std_msgs::Float64MultiArray target_joint_vel;
396 for(
int i=0; i<
DOF; i++)
398 target_joint_vel.data.push_back(0.0);
400 joint_vel_pub_.
publish(target_joint_vel);
410 int main(
int argc,
char ** argv)
412 ros::init(argc, argv,
"cob_trajectory_controller");
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ros::ServiceClient srvClient_SetOperationMode
void SetPTPvel(double vel)
void publish(const boost::shared_ptr< M > &message) const
ros::Subscriber operation_mode_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool srvCallback_setVel(cob_srvs::SetFloat::Request &req, cob_srvs::SetFloat::Response &res)
std::string current_operation_mode_
void executeFollowTrajectory(const control_msgs::FollowJointTrajectoryGoalConstPtr &goal)
std::vector< double > last_q
bool step(std::vector< double > current_pos, std::vector< double > &desired_vel)
std::vector< double > q_current
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
bool moveThetas(std::vector< double > conf_goal, std::vector< double > conf_current)
Will move the component to a goal configuration in Joint Space.
ros::NodeHandle n_private_
trajectory_msgs::JointTrajectory traj_2_
ros::Publisher joint_vel_pub_
ros::ServiceServer srvServer_SetAcc_
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
std::vector< std::string > JointNames_
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
ros::Subscriber controller_state_
bool moveTrajectory(trajectory_msgs::JointTrajectory pfad, std::vector< double > conf_current)
trajectory_msgs::JointTrajectory traj_
int main(int argc, char **argv)
void spawnTrajector(trajectory_msgs::JointTrajectory trajectory)
genericArmCtrl * traj_generator_
cob_trajectory_controller_node()
const std::string & getNamespace() const
std::vector< double > last_q2
bool isPreemptRequested()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void SetPTPacc(double acc)
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
void state_callback(const control_msgs::JointTrajectoryControllerStatePtr &message)
bool srvCallback_Stop(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
bool getParam(const std::string &key, std::string &s) const
actionlib::SimpleActionServer< control_msgs::FollowJointTrajectoryAction > as_
std::vector< double > last_q3
ros::ServiceServer srvServer_Stop_
std::vector< double > joint_distance_
bool hasParam(const std::string &key) const
void operationmode_callback(const std_msgs::StringPtr &message)
bool srvCallback_setAcc(cob_srvs::SetFloat::Request &req, cob_srvs::SetFloat::Response &res)
ROSCPP_DECL void spinOnce()
std::vector< double > last_q1
ros::ServiceServer srvServer_SetVel_
std::vector< double > startposition_