ROS control-----hardware_interface简介

   hardware_interface在ros_control中占有重要地位,controller通过hardware_interface与机器人硬件获得连接。


1 hardware_interface主要数据结构

1.1 resource handle

    这些handle主要功能包括存入resource,并提供相应获取resource接口。




1.2 hardware interface

    这些硬件接口主要功能有register handle/get handle/和claim resource/clearclaim/getclaims等。


1.3 RobotHW

    定义了一些框架接口如下,用户可以填充实现。

a)hardware interface切换:

virtual bool init (ros::NodeHandle &root_nh,ros::NodeHandle &robot_hw_nh)

virtual bool prepareSwitch (const std::list< ControllerInfo > &start_list, const std::list< ControllerInfo > &stop_list)

virtual void doSwitch (const std::list< ControllerInfo > &, const std::list< ControllerInfo > &)

virtual void read (const ros::Time &time, constros::Duration &period)

virtual void write (const ros::Time &time, constros::Duration &period)

b)resource管理

virtual bool checkForConflict (const std::list< ControllerInfo > &info) const

另外由于该接口继承了hardware_interface::InterfaceManager所以还支持:

T * get ()

std::vector< std::string > getInterfaceResources (std::string iface_type) const

std::vector< std::string > getNames () const

void registerInterface (T *iface)

void registerInterfaceManager (InterfaceManager *iface_man)




2 应用示例

  这里参考了ROS wiki给出如何定义自己robot并使用controller manager控制它。

  目前ros_control已经支持了一些常用标准hardware interface和controller,如果能满足需求可以直接使用,如果不能满足需求可以自定义扩展自己的接口和controller,如下示意图所示:


      上图中白色框中接口是ros_control已经支持的标准hardware接口,对应是已经支持的controller,下面Robot Specific controller是用户扩展定义的controller。


#include <hardware_interface/joint_command_interface.h>
#include <hardware_interface/joint_state_interface.h>
#include <hardware_interface/robot_hw.h>

class MyRobot : public hardware_interface::RobotHW
{
public:
  MyRobot() 
 { 

   //register and use existing robot standard hardware interfaces
   // connect and register the joint state interface
   hardware_interface::JointStateHandle state_handle_a("A", &pos[0], &vel[0], &eff[0]);
   jnt_state_interface.registerHandle(state_handle_a);

   hardware_interface::JointStateHandle state_handle_b("B", &pos[1], &vel[1], &eff[1]);
   jnt_state_interface.registerHandle(state_handle_b);

   registerInterface(&jnt_state_interface);

   // connect and register the joint position interface
   hardware_interface::JointHandle pos_handle_a(jnt_state_interface.getHandle("A"), &cmd[0]);
   jnt_pos_interface.registerHandle(pos_handle_a);

   hardware_interface::JointHandle pos_handle_b(jnt_state_interface.getHandle("B"), &cmd[1]);
   jnt_pos_interface.registerHandle(pos_handle_b);

   registerInterface(&jnt_pos_interface);

   
   //register some robot specific interfaces
registerInterface(&cool_interface);
//Implement robot-specific resouce management
bool checkForConflict(const std::list<ControllerInfo>& info) const
{ // this list of controllers cannot be running at the same time
 ... return true;
// this list of controller can be running at the same time
...
return false;
}

}

private: hardware_interface::JointStateInterface jnt_state_interface;
//standard hardware interface
hardware_interface::PositionJointInterface jnt_pos_interface;//standard hardware interface
MyCustomInterface cool_interface; //custom specific hardware interface
double cmd[2]; double pos[2]; double vel[2]; double eff[2];

}



 

以上添加了custom hardware interface来支持用特定controller做特定控制;另一种方式是在MyRobot仅仅添加一些特定函数接口实现特定控制(没有定义特定硬件接口class),通过MyRobot类自注册实现这些接口的可访问如下,

#include <hardware_interface/joint_command_interface.h>
#include <hardware_interface/joint_state_interface.h>
#include <hardware_interface/robot_hw.h>

class MyRobot : public hardware_interface::RobotHW, public hardware_interface::HardwareInterface
{
public:
  MyRobot()
 {
   //register and use existing robot standard hardware interfaces
   // connect and register the joint state interface

   hardware_interface::JointStateHandle state_handle_a("A", &pos[0], &vel[0], &eff[0]);
   jnt_state_interface.registerHandle(state_handle_a);

   hardware_interface::JointStateHandle state_handle_b("B", &pos[1], &vel[1], &eff[1]);
   jnt_state_interface.registerHandle(state_handle_b);

   registerInterface(&jnt_state_interface);

   // connect and register the joint position interface
   hardware_interface::JointHandle pos_handle_a(jnt_state_interface.getHandle("A"), &cmd[0]);
   jnt_pos_interface.registerHandle(pos_handle_a);

   hardware_interface::JointHandle pos_handle_b(jnt_state_interface.getHandle("B"), &cmd[1]);
   jnt_pos_interface.registerHandle(pos_handle_b);

   registerInterface(&jnt_pos_interface);

 

    // register the MyRobot class itself to make the 'someCoolFunction' available
    // the MyRobot class inherits from HardwareInterface to make this possible


     registerInterface(this);

     void someCoolFunction();


   // Implement robot-specific resouce management
    bool checkForConflict(const std::list<ControllerInfo>& info) const
    {
       // this list of controllers cannot be running at the same time
       ...
       return true;

       // this list of controller can be running at the same time
       ...
       return false;
    }


  }

  private:
  hardware_interface::JointStateInterface jnt_state_interface; //standard hardware interface
  hardware_interface::PositionJointInterface jnt_pos_interface;//standard hardware interface
  double cmd[2]; double pos[2]; double vel[2]; double eff[2];

};



以上的MyRbot class通过如下代码,就可以被controller manager和controller使用了:


main()
{
   MyRobot robot;
  controller_manager::ControllerManager cm(&robot);

  while (true)
  {
     robot.read(); //to access hardware driver, generally need do some wrap and customization in MyRobot class
     cm.update(robot.get_time(), robot.get_period());
     robot.write(); //to access hardware driver, generally need do some wrap and customization in MyRobot class
     sleep();
  }
}




3 参考文献

  参考ROS WiKi改编。





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值