赞
踩
解决方法:
1. 先运行roscore 和 rosrun rviz rviz检查RViz是否正常
2.添加如下语句
- echo "export SVGA_VGPU10=0" >> ~/.bashrc
-
- 或者
-
- echo "export LIBGL_ALWAYS_SOFTWARE=1" >> ~/.bashrc
解决方法:
1.切换到对应的ppa目录
cd /etc/apt/sources.list.d
2.在该目录下ls,即可以看到对应的无法下载的fcitx-team-ubuntu-nightly-xenial.list
3. 删除.list文件,安全起见,可以进行添加后缀.bak的备份
sudo mv fcitx-team-ubuntu-nightly-xenial.list fcitx-team-ubuntu-nightly-xenial.list.bak
4.重新更新
sudo apt-get update
- //1. 添加源
- sudo vim /etc/apt/sources.list.d/ros-latest.list
- //2.添加如下内容
- deb https://mirrors.ustc.edu.cn/ros/ubuntu/ bionic main
- //3.进行更新
- sudo apt-get update
- //4. 重新安装软件包
- sudo apt-get install ros-melodic-navigation
- git config --global url."https://".insteadOf git://
- sudo apt-get update
1.创建工作空间
- mkdir catkin_ws
- cd catkin_ws
- mikdir src
- cd src // src为source缩写,用来放置源代码
- //先确认linux已下载git工具
- sudo apt install git
2.下载源代码(将htps:改为git:)
git clone git://github.com/6-robot/wpr_simulation.git
3.配置依赖包脚本文件(在sripts中)
4.编译工作区
- cd ~/catkin_ws/ //返回工作区文件夹
- catkin_make //编译文件
5.载入工作空间环境变量(否则会报错找不到软件包)
source ~/catkin_ws/devel/setup.bash
6.启动源代码
roslaunch wpr_simulaton wpb_simple.launch
7.工作空间环境变量添加进终端初始化脚本(.bashrc)
gedit ~/.bashrc
catkin_create_pkg ssr_pkg rospy roscpp std_msgs //其中rospy roscpp std_msgs为通用包
语句序号分别为133 149
发布者
- #include<ros/ros.h>
- #include<std_msgs/String.h> //引用消息标准类型
-
- int main(int argc, char *argv[])
- {
- ros::init(argc,argv,"chao_node");
- printf("Hello World!\n");
-
- ros::NodeHandle nh; //大管家
- ros::Publisher pub = nh.advertise<std_msgs::String>("kuai_shang_che", 10); //“kuai_shang_che"为主题名, 10为缓存长度
-
- ros::Rate loop_rate(10);//设置循环次数
- while(ros::ok())
- {
- printf("我要刷屏了\n");
- std_msgs::String msg; //生成消息包
- msg.data= "带飞"; //从index可看string类型为data
- pub.publish(msg); //发布消息
- loop_rate.sleep();
- }
-
- return 0;
- }
订阅者
- #include<ros/ros.h>
- #include<std_msgs/String.h>
-
- void chao_callback(std_msgs::String msg) //回调函数
- {
- ROS_INFO( msg.data.c_str( ) ); //确定打印的为string类型
- printf("\n");
- }
- int main(int argc, char *argv[])
- {
- setlocale(LC_ALL, ""); //显示中文
- ros::init(argc,argv,"ma_node");
- ros::NodeHandle nh;
- ros::Subscriber sub = nh.subscribe("kuai_shang_che",10, chao_callback); //确定订阅的话题,存储长度,回调函数
-
- while(ros::ok( ))
- {
- ros::spinOnce(); //中断
- }
- return 0;
- }
速度:m/s
旋转:rad/s
包:sensor_msgs
显示话题中的消息类型:
rostopic echo /scan --noarr
创建激光雷达包(需要引入sensor_msgs的支持包)
robot@wp:~/ros_example/src$ catkin_create_pkg lidar_pkg roscpp rospy sensor_msgs
- #include<ros/ros.h>
- #include<sensor_msgs/LaserScan.h>
-
- void LidarCallback(const sensor_msgs::LaserScan msg)
- {
- float fMidDist = msg.ranges[180]; //显示正前方的数
- ROS_INFO("前方测距 ranges[180] = %f 米", fMidDist);
- }
- int main(int argc, char *argv[])
- {
- setlocale(LC_ALL,""); //设置为中文
- ros::init(argc, argv, "lidar_node");
-
- ros::NodeHandle n;
- ros::Subscriber lidar_sub = n.subscribe("/scan", 10, &LidarCallback);
-
- ros::spin( ); //保持运行别退出
-
- return 0;
- }
- #include<ros/ros.h>
- #include<sensor_msgs/LaserScan.h> //雷达消息格式头文件
- #include <geometry_msgs/Twist.h> //速度消息格式头文件
-
- ros::Publisher vel_pub;
- void LidarCallback(const sensor_msgs::LaserScan msg)
- {
- float fMidDist = msg.ranges[180]; //显示正前方的数
- ROS_INFO("前方测距 ranges[180] = %f 米", fMidDist);
-
- geometry_msgs::Twist vel_cmd;
- if(fMidDist < 1.5)
- {
- vel_cmd.angular.z = 0.3; //左传
-
- }
- else
- {
- vel_cmd.linear.x = 0.05;
- }
- vel_pub.publish(vel_cmd);
- }
- int main(int argc, char *argv[])
- {
- setlocale(LC_ALL,""); //设置为中文
- 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;
- }
其中orientation为内部解算得到的姿态,是一个四元数
官方话题定义:
- #include<ros/ros.h>
- #include<sensor_msgs/Imu.h>
- #include<tf/tf.h>
-
- void IMUCallBack(sensor_msgs::Imu msg)
- {
- if(msg.orientation_covariance[0]< 0) //检查数据是否存在
- return;
- 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 俯仰 =%.0f 朝向= %.0f",roll, pitch, yaw);
-
-
-
-
- }
- 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);
- ros::spin( );
- return 0;
- }
- #include<ros/ros.h>
- #include<sensor_msgs/Imu.h>
- #include<tf/tf.h>
- #include <geometry_msgs/Twist.h>
-
- ros::Publisher vel_pub;
-
- void IMUCallBack(sensor_msgs::Imu msg)
- {
- if(msg.orientation_covariance[0]< 0) //检查数据是否存在
- return;
- 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 俯仰 =%.0f 朝向= %.0f",roll, pitch, yaw);
-
- double target_yaw = 90;
- double diff_angle = target_yaw - yaw;
- geometry_msgs::Twist vel_cmd;
- 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( );
- return 0;
- }
标准消息包
数组类型中数组长度可变
常用消息包
带有stamped的是数据中含有Header
自定义消息包
catkin_create_pkg qq_msgs roscpp rospy std_msgs message_generation message_runtime
更改CMake.list 文件(52行 72行 106行)
改为
- add_message_files(
- FILES
- Carry.msg //自己创建的msg文件
- )
-
-
- //72行
- ## Generate added messages and services with any dependencies listed here
- generate_messages(
- DEPENDENCIES
- std_msgs
- )
- //106行
- ## DEPENDS: system dependencies of this project that dependent projects also need
- catkin_package(
- # INCLUDE_DIRS include
- # LIBRARIES qq_msgs
- CATKIN_DEPENDS message_generation message_runtime roscpp rospy std_msgs
- # DEPENDS system_lib
- )
进入package.xml文件,补全<buildtool_depend> 与<exec_depend>,使其均含有message_runtime和message_generation
操作完成后如下图所示
- <buildtool_depend>catkin</buildtool_depend>
- <build_depend>message_generation</build_depend>
- <build_depend>message_runtime</build_depend>
- <build_depend>roscpp</build_depend>
- <build_depend>rospy</build_depend>
- <build_depend>std_msgs</build_depend>
- <build_export_depend>roscpp</build_export_depend>
- <build_export_depend>rospy</build_export_depend>
- <build_export_depend>std_msgs</build_export_depend>
- <exec_depend>message_runtime</exec_depend>
- <exec_depend>message_generation</exec_depend>
- <exec_depend>roscpp</exec_depend>
- <exec_depend>rospy</exec_depend>
- <exec_depend>std_msgs</exec_depend>
验证
robot@wp:~$ rosmsg show qq_msgs/Carry
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。