ros、c++基于类的编程基础

本文介绍了在ROS中如何进行基于C++的类编程,包括类定义与实现的分离,以及在ROS环境下使用ros::spinOnce()和ros::spin()的区别。同时,解释了全局句柄与私有节点句柄的差异,特别是参数服务器中句柄的选择。最后提到了在Python中应用相同概念的计划,并提供了cmakeLists和package.xml文件的相关内容。
摘要由CSDN通过智能技术生成

在这里插入图片描述
一般而言,为实现更好的模块化开发和代码复用,一般会将类的定义和实现分开。
总体来说,就是.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数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值