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$ 
相关推荐
y = xⁿ10 小时前
DeepSeek Harness 学习日记:关于Agent接口,工具调用的底层实现
android·java·学习
HugoStudio_SWAN13 小时前
【擦除重绘】C++ 控制台动画:弹跳 Logo DVD 屏保效果
开发语言·c++·学习·程序人生
sakiko_14 小时前
Swift学习笔记42-SwiftUI的属性包装器(讲解+面试)
笔记·学习·ios·swiftui·swift
自小吃多15 小时前
器件移动、旋转、镜像、对齐、等间距操作笔记
笔记·嵌入式硬件
存在morning16 小时前
【Paimon 学习笔记 三】工作流程:Paimon 的批写、流写、批读与流读
笔记·学习
老当益壮梁奶奶17 小时前
Linux软件编程学习笔记(八):进程间通信详解(1)
linux·c语言·笔记·学习·算法
weixin_4660681119 小时前
《十分钟冥想》精读笔记(上):每天10分钟,给大脑做一次“系统减负”
笔记
凯尔萨厮19 小时前
Java学习笔记十(注解)
java·笔记·学习
JoannaJuanCV20 小时前
VLM学习-SFT(监督微调)
深度学习·学习·机器学习·大模型·视觉大模型·vlm·视觉编码器
程序员大雄学编程20 小时前
微积分43. 无穷积分入门:从概念到Python实战可视化
开发语言·python·学习·微积分