一般而言,为实现更好的模块化开发和代码复用,一般会将类的定义和实现分开。
总体来说,就是.h文件定义类:
#include <ros/ros.h>
#include <geometry\_msgs/Twist.h>
#include <nav\_msgs/Path.h>
#include <hybrid\_a\_star/PathSpeedCtrlInterface.h> //
#include <hybrid\_a\_star/GpsImuInterface.h> //
class AutonomousDrivingNode {
public:
AutonomousDrivingNode();
void pathCallback(const nav_msgs::Path::ConstPtr& msg);
void gpsCallback(const hybrid_a_star::GpsImuInterface::ConstPtr& msg);
void Run();
private:
ros::NodeHandle nh_;
ros::Publisher control_test_pub_;
ros::Publisher cmd_vel_pub_;
ros::Subscriber path_sub_;
ros::Subscriber gps_sub_;
void publishControlTest();
void publishCmdVel();
};
hybrid_a_star.cpp类的具体实现:
#include "hybrid\_a\_star/hybrid\_a\_star.h"
AutonomousDrivingNode::AutonomousDrivingNode() {
control_test_pub_ = nh_.advertise<hybrid\_a\_star::PathSpeedCtrlInterface>("control\_test", 10);
cmd_vel_pub_ = nh_.advertise<geometry\_msgs::Twist>("cmd\_vel", 10);
path_sub_ = nh_.subscribe("path", 10, &AutonomousDrivingNode::pathCallback, this);
gps_sub_ = nh_.subscribe("gps", 10, &AutonomousDrivingNode::gpsCallback, this);
}
void AutonomousDrivingNode::pathCallback(const nav_msgs::Path::ConstPtr& msg) {
for (const auto& point : msg->poses) {
// 处理每个路径点
// 示例:输出路径点的坐标
ROS\_INFO("Path Point: x=%f, y=%f", point.pose.position.x, point.pose.position.y);
}
}
void AutonomousDrivingNode::gpsCallback(const hybrid_a_star::GpsImuInterface::ConstPtr& msg)
{
// 处理gps消息
// 示例:获取GPS数据