ros_control控制真实电机的方法
声明:参考 此项目对电机驱动方法进行总结。
需要的硬件
- 用于下载程序的单片机,此处使用arduino uno
- 电机驱动板,此处使用常规的L298N
- 直流电机带编码器/霍尔元件
软件部分
- 和硬件交互的robot_hardware_interface.h,作用:声明接口类,构造器添加publisher用于发布关节角度,client用于接收返回角度。需要继承hardware_interface::RobotHW。
代码如下:
#include <hardware_interface/joint_state_interface.h>
#include <hardware_interface/joint_command_interface.h>
#include <hardware_interface/robot_hw.h>
#include <joint_limits_interface/joint_limits.h>
#include <joint_limits_interface/joint_limits_interface.h>
#include <joint_limits_interface/joint_limits_rosparam.h>
#include <joint_limits_interface/joint_limits_urdf.h>
#include <controller_manager/controller_manager.h>
#include <boost/scoped_ptr.hpp>
#include <ros/ros.h>
#include <rospy_tutorials/Floats.h>
#include <three_dof_planar_manipulator/Floats_array.h>
#include <angles/angles.h>
class ROBOTHardwareInterface : public hardware_interface::RobotHW
{
public:
ROBOTHardwareInterface(ros::NodeHandle& nh);
~ROBOTHardwareInterface();
void init();
void update(const ros::TimerEvent& e);
void read();
void write(ros::Duration elapsed_time);
ros::Publisher pub;
ros::ServiceClient client;
rospy_tutorials::Floats joints_pub;
three_dof_planar_manipulator::Floats_array joint_read;
protected:
hardware_interface::JointStateInterface joint_state_interface_;
hardware_interface::PositionJointInterface position_joint_interface_;
hardware_interface::EffortJointInterface effort_joint_interface_;
joint_limits_interface::EffortJointSaturationInterface effortJointSaturationInterface;
int num_joints_;
std::string joint_name_;
double joint_position_;
double joint_velocity_;
double joint_effort_;
double joint_position_command_;
double joint_effort_command_;
double joint_velocity_command_;
ros::NodeHandle nh_;
ros::Timer non_realtime_loop_;
ros::Duration elapsed_time_;
double loop_hz_;
boost::shared_ptr<controller_manager::ControllerManager> controller_manager_;
};
- 类的实现:robot_hardware_interface_node.cpp,在这里向ros注册控制器名称,通过服务的方式实现read方法:在cpp文件中声明客户端向服务器发起请求获得角度返回值。write方法:发布joint_effort_command_(目标值)到/joints_to_arduino话题。此处是定义功能,实际角度发布是在terminal使用rostopic pub发布的,当然也可以直接用脚本发布。注意joint_effort_command_表示目标的effor:
#include <ros_control_example/robot_hardware_interface.h>
ROBOTHardwareInterface::ROBOTHardwareInterface(ros::NodeHandle& nh) : nh_(nh) {
init();
controller_manager_.reset(new controller_manager::ControllerManager(this, nh_));
loop_hz_=5;
ros::Duration update_freq = ros::Duration(1.0/loop_hz_);
pub = nh_.advertise<rospy_tutorials::Floats>("/joints_to_aurdino",10);
client = nh_.serviceClient<three_dof_planar_manipulator::Floats_array>("/read_joint_state");
non_realtime_loop_ = nh_.createTimer(update_freq, &ROBOTHardwareInterface::update, this);
}
ROBOTHardwareInterface::~ROBOTHardwareInterface() {
}
void ROBOTHardwareInterface::init()

本文详细介绍了使用ROS控制系统控制真实电机的过程,从硬件选型到软件实现,包括使用Arduino作为中间件,通过L298N电机驱动板控制直流电机,并实现关节状态的读写。同时,讲解了如何配置控制器,利用PID参数调整电机性能。
最低0.47元/天 解锁文章
4125





