Go to the documentation of this file.00001 #include <generic_control_toolbox/controller_action_node.hpp>
00002
00003 namespace generic_control_toolbox
00004 {
00005 ControllerActionNode::ControllerActionNode()
00006 {
00007 nh_ = ros::NodeHandle("~");
00008
00009 if (!nh_.getParam("loop_rate", loop_rate_))
00010 {
00011 ROS_WARN_STREAM("Missing loop_rate parameter for "
00012 << ros::this_node::getName() << ". Using default.");
00013 loop_rate_ = 100;
00014 }
00015
00016 got_first_ = false;
00017 joint_state_sub_ = nh_.subscribe("/joint_states", 1,
00018 &ControllerActionNode::jointStatesCb, this);
00019 state_pub_ = nh_.advertise<sensor_msgs::JointState>("/joint_command", 1);
00020 }
00021
00022 ControllerActionNode::~ControllerActionNode() {}
00023
00024 void ControllerActionNode::runController(ControllerBase &controller)
00025 {
00026 ros::Rate r(loop_rate_);
00027 ros::Time prev_time = ros::Time::now();
00028 sensor_msgs::JointState command;
00029 bool was_running = false;
00030
00031 while (ros::ok())
00032 {
00033 if (got_first_)
00034
00035 {
00036 command = controller.updateControl(state_, ros::Time::now() - prev_time);
00037 if (controller.isActive())
00038 {
00039 ROS_DEBUG_THROTTLE(10, "Controller is active, publishing");
00040 was_running = true;
00041 state_pub_.publish(command);
00042 }
00043 else
00044 {
00045 if (was_running)
00046 {
00047 state_pub_.publish(command);
00048 was_running = false;
00049 }
00050 ROS_DEBUG_THROTTLE(10, "Controller is not active, skipping");
00051 }
00052 }
00053 else
00054 {
00055 ROS_WARN_THROTTLE(10, "No joint state received");
00056 }
00057
00058 prev_time = ros::Time::now();
00059 ros::spinOnce();
00060 r.sleep();
00061 }
00062 }
00063
00064 void ControllerActionNode::jointStatesCb(
00065 const sensor_msgs::JointState::ConstPtr &msg)
00066 {
00067 ROS_INFO_ONCE("Joint state received!");
00068 state_ = *msg;
00069 got_first_ = true;
00070 }
00071 }