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$ 
相关推荐
马可家的菠萝11 小时前
你收藏的资料,有多少真的被你第二次打开过?
笔记·知识管理·效率提升·个人知识库
小刘在重生~12 小时前
Java Lock 显式锁案例|ReentrantLock 解决多线程安全问题(超详细实战)
java·笔记·面试
职豚求职小程序12 小时前
中国人寿27秋招笔试[特殊字符]北森机考|备考攻略
笔记
江湖人称菠萝包12 小时前
【Qt】《Qt 5.9 C++开发指南》笔记-Chapter9-Qt Charts
笔记·qt·qt5
乐橙开放平台12 小时前
从「多套客户端」到一套开放能力:乐橙视频监控能力复盘
笔记·物联网·自动化·音视频·智能家居
AI职业加油站12 小时前
数据要素价值释放年:大数据治理工程师,站上职业新风口
人工智能·学习·职场和发展·数据分析·职场发展
啄缘之间13 小时前
1.【SVA】什么是SVA?
经验分享·学习·测试用例·verilog·sva
山岚的运维笔记13 小时前
mysql 专业笔记 -- 第 36 章:MySQL 管理
运维·数据库·笔记·后端·mysql·oracle·dba
sunoo-22914 小时前
【嵌入式裸机开发Day04】i.MX6ULL 蜂鸣器驱动详解:从引脚地址查找到代码实现全流程
arm开发·单片机·嵌入式硬件·学习·串口·裸机
Purple Coder14 小时前
stm32
学习