ros学习笔记15~40

本学习内容来自机器人工匠阿杰,本人仅作学习笔记记录,方便后续查看学习。详细内容可自行搜索去看机器人工匠阿杰up主,讲的很好。

launch文件启动多个节点

单独调试某个节点

发发送cmd_vel话题让机器人原地沿z轴正向旋转

同时订阅发布进行避障模拟

cpp 复制代码
#include<ros/ros.h>
#include<sensor_msgs/LaserScan.h>
#include<geometry_msgs/Twist.h>

ros::Publisher vel_pub;
int nCount = 0;
void LidarCallback(const sensor_msgs::LaserScan msg){
        float fMidDist = msg.ranges[180];
        ROS_INFO("前方测距 range[180] = %.2f 米", fMidDist);

        geometry_msgs::Twist vel_cmd;
        if(nCount>0){
                nCount--;
                return;
        }
        if (fMidDist<1.5)
        {
               vel_cmd.angular.z=0.3;
               nCount = 50;
        }else{
               vel_cmd.linear.x=0.05; 
        }
        vel_pub.publish(vel_cmd);
        
}

int main(int argc, char  *argv[])
{
   ros::init(argc, argv, "lidar_node");
   ros::NodeHandle n;
  
   ros::Subscriber lidar_sub = n.subscribe("/scan", 10, &LidarCallback);
   vel_pub = n.advertise<geometry_msgs::Twist>("cmd_vel",10);
   ros::spin();

   return 0;
}

imu航向锁定

cpp 复制代码
#include<ros/ros.h>
#include<sensor_msgs/Imu.h>
#include<tf/tf.h>
#include<geometry_msgs/Twist.h>

ros::Publisher vel_pub;

void IMUCallback(const sensor_msgs::Imu msg){
    // 检测消息包中四元数数据是否存在
    if(msg.orientation_covariance[0]<0)
        return;    
    
    // 四元数转换 转换后的四元术对象quaternion
    tf::Quaternion quaternion(
        msg.orientation.x,
        msg.orientation.y,
        msg.orientation.z,
        msg.orientation.w
    );
    // 欧拉角
    double roll,pitch,yaw;

    tf::Matrix3x3(quaternion).getRPY(roll,pitch,yaw);
    
    // 弧度转角度
    roll = roll*180/M_PI;
    pitch = pitch*180/M_PI;
    yaw = yaw*180/M_PI;
    ROS_INFO("滚转= %.0f 仰俯= %0.f 朝向= %0.f",roll,pitch,yaw);
    // 速度消息包
    geometry_msgs::Twist vel_cmd;
    // 目标朝向角
    double target_yaw = 90;
    // 当前机器人与目标朝向角差
    double diff_angle = target_yaw-yaw;

    // 计算速度

    vel_cmd.angular.z = diff_angle*0.01;
    vel_cmd.linear.x = 0.1;
    vel_pub.publish(vel_cmd);


}


int main(int argc, char  *argv[])
{
    setlocale(LC_ALL,"");
    ros::init(argc,argv,"imu_node");
    ros::NodeHandle n;
    ros::Subscriber imu_sub = n.subscribe("/imu/data",10,IMUCallback);
    vel_pub = n.advertise<geometry_msgs::Twist>("/cmd_vel",10);
    ros::spin();
   
}
bash 复制代码
catkin_create_pkg qq_msgs roscpp rospy std_msgs message_generation message_runtime

修改完毕直接catkin_ws 进行 catkin_make 编译

bash 复制代码
sxd@sxd:~/catkin_ws$ rosmsg show qq_msgs/Carry
string grade
int64 star
string data

sxd@sxd:~/catkin_ws$ 
相关推荐
共享家952712 小时前
OpenClaw的通道配置
人工智能·学习·openclaw
Cloud_Shy61813 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第三章 Item 21 - 24)
开发语言·人工智能·笔记·python·迭代器模式
nnsix13 小时前
Unity HybirdCLR 简单了解 笔记
笔记
MartinYeung514 小时前
[论文学习]基于梯度迭代上下文优化的 LLM 隐私越狱攻击框架
学习·区块链
handler0115 小时前
【算法】并查集(普通/扩展/带权)模板与例题
数据结构·c++·笔记·算法·c·图论·查并集
MartinYeung515 小时前
[论文学习]大型语言模型中 PII 洩漏的系统性调查
学习
中屹指纹浏览器15 小时前
指纹浏览器环境克隆、批量派生的风控隐患剖析与标准化新建环境实操指南
经验分享·笔记
.千余15 小时前
【C++】C++手写Vector容器:从底层源码模拟实现
开发语言·c++·经验分享·笔记·学习
元直数字电路验证16 小时前
云计算实验笔记(四):容器编排(Container Orchestration)
运维·笔记·docker·云计算
nashane16 小时前
HarmonyOS 6学习:句柄泄漏(Fd Leak)从“崩溃现场”到“代码行”的精准狙击指南
学习·华为·音视频·harmonyos