level= (IntermediateCategory)


(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.


Tutorial Level:

Writing a new controller

In this example, we're writing a new controller for the PR2 Robot, that tracks a commanded position. Since the PR2 Robot provides the EffortJointInterface, we'll make our controller use that interface. This means that our controller will be usable for all robots that provide the EffortJointInterface, not just for the PR2 Robot.

#include <controller_interface/controller.h>
#include <hardware_interface/joint_command_interface.h>
#include <pluginlib/class_list_macros.h>

namespace controller_ns{

class PositionController : public controller_interface::Controller<hardware_interface::EffortJointInterface>
  bool init(hardware_interface::EffortJointInterface* hw, ros::NodeHandle &n)
    // get joint name from the parameter server
    std::string my_joint;
    if (!n.getParam("joint", my_joint)){
      ROS_ERROR("Could not find joint name");
      return false;

    // get the joint object to use in the realtime loop
    joint_ = hw->getHandle(my_joint);  // throws on failure
    return true;

  void update(const ros::Time& time, const ros::Duration& period)
    double error = setpoint_ - joint_.getPosition();

  void starting(const ros::Time& time) { }
  void stopping(const ros::Time& time) { }

  hardware_interface::JointHandle joint_;
  static const double gain_ = 1.25;
  static const double setpoint_ = 3.00;
PLUGINLIB_DECLARE_CLASS(package_name, PositionController, controller_ns::PositionController, controller_interface::ControllerBase);

Wiki: ros_control/Tutorials/Creating a controller (last edited 2017-11-03 16:21:24 by BenceMagyar)