环境安装
- ubuntu20.04server官方版本
- 安装Ubuntu-mate-desktop和vnc
sudo apt install ubuntun-mate-core ubuntu-mate-desktop
sudo apt install x11vnc
x11vnc -storepasswd
x11vnc -usepw - 安装ros
ubuntu20.04安装ros - rosdep安装方法1
- rosdep安装方法2参考方法1视频,把网址全部换为镜像
创立工作空间
mkdir -p ~/__name__/src
cd ~/__name__/src
catkin_init__workspace
cd ..
catkin_make
source devel/setup.bash
ench $ROS_PACKAGE_PATH #check path
创建、编译功能包
创建工作包
cd ~/__name__/src
catkin_create_pkg
depends
- roscpp#c++
- rospy#python
- std_msgs
- turtlesim
- geometry_msgs
编译功能包
cd ..
catkin_make
source ~/__name__/devel/setup.bash
测试发布者代码c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <ros/ros.h> #include <geometry_msgs/Twist.h> int main(int argc, char **argv) { ros::init(argc, argv, "__name__"); //ROS节点初始化 ros::NodeHandle n; //创建节点句柄 ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10); //创建一个Publisher,发布名为/turtle/cmd_vel的topic,消息类型为geometry_msgs::twist,队列长度10 ros::Rate loop_rate(10); int count = 0; while(ros::ok()) { //初始化geometry_msgs::Twis类型的消息 geometry_msgs::Twist vel_msg; vel_msg.linear.x=0.5; vel_msg.angular.z=0.2; //发布消息 turtle_vel_pub.publish(vel_msg); ROS_INFO("xxx"); loop_rate.sleep(); } return 0; } |
测试发布者代码python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import rospy from geometry_msgs.msg import Twist def velocity_pubilisher(): rospy.init_node('__name__',anonymus=True) # ROS节点初始化 turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel',Twist,queue_size=10) # 创建一个Publisher,发布为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度为10 rate = rospy.Rate(10) while not rospy.is_shutdown(): vel_msg = Twist() vel_msg.linear.x = 0.5 vel_msg.angular.z = 0.2 turtle.vel_pub.publish(vel_msg) rospy.loginfo("xxx") rate.sleep() if __name__=="__main__": try: velocity_pubilisher() except rospy.ROSInterruptException pass |
测试订阅者代码c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <ros/ros.h> #include <turtlesim/Pose.h> void poseCallback(const turtle::Pose::ConstPtr& msg){ ROS_INFO("%0.6f,%0.6f",msg->x,msg->y); } int main(int argc, char **argv) { ros::init(argc, argv, "__name__"); //ROS节点初始化 ros::NodeHandle n; //创建节点句柄 ros::Subscriber pose_sub = n.Subscribe("/turtle1/pose",10,poseCallback); //创建一个Publisher,订阅名为/turtle/pose的topic,注册回调函数poseCallback ros::spin(); return 0; } |
测试订阅者代码python
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import rospy from turtlesim.msg import Pose def poseCallback(msg): rospy.loginfo("%0.9f,%0.6f",msg.x,msg.y) def pose_subscribe(): rospy.init_node('__name__',anonymus=True) # ROS节点初始化 rospy.Subscriber('/turtle1/pose',Pose,poseCallback) # 创建一个Publisher,发布为/turtle1/pose的topic,注册回调函数possCallback rospy.spin() if __name__=="__main__": pose_subscribe() |
配置Cmakelist.txt
将以下代码放置于Cmakelist.txt build中
add_executable(__name__ src/__name__.cpp)
target_link_libraries(__name__ ${catkin_LIBRARIES})
编译并运行
cd ~/__name__
catkin_make
source devel/setup.bash
roscore
rosrun xxx xxx
自定义话题消息
- 定义msg文件
- 在package.xml添加功能包依赖
- 在Cmakelist.txt添加编译选项
- 编译生成语言相关文件