日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ros(2) 发布者publisher的编程实现

發布時間:2024/8/23 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ros(2) 发布者publisher的编程实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.創建功能包

cd ~/testROS_ws/src

catkin_create_pkg topic_publisher?std_msgs rospy roscpp geometry_msgs turtlesim

cd?~/testROS_ws/src/topic_publisher/src

2.編輯代碼

tourch test.cpp

/*** 該例程將發布turtle1/cmd_vel話題,消息類型geometry_msgs::Twist*/#include <ros/ros.h> #include <geometry_msgs/Twist.h>int main(int argc, char **argv) {// ROS節點初始化ros::init(argc, argv, "velocity_publisher");// 創建節點句柄ros::NodeHandle n;// 創建一個Publisher,發布名為/turtle1/cmd_vel的topic,消息類型為geometry_msgs::Twist,隊列長度10ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);// 設置循環的頻率ros::Rate loop_rate(10);int count = 0;while (ros::ok()){// 初始化geometry_msgs::Twist類型的消息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("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", vel_msg.linear.x, vel_msg.angular.z);// 按照循環頻率延時loop_rate.sleep();}return 0; }

3.編譯代碼

3.1修改cmakelists.txt

添加這兩句:

add_executable(test src/test.cpp) target_link_libraries(test ${catkin_LIBRARIES})

3.2 編譯

catkin_make

3.3設置環境變量

source devel/setup.bash

為了避免每次設置在/home目錄下

ctrl+h

編輯.bashrc

source /home/<user_name>/<worksapce_name>/devel/setup.bash

4運行節點

重新打開終端

roscore

運行海龜仿真器

rosrun turtlesim turtlesim_node

運行節點

rosrun <功能包名> <節點名>

rosrun?topic_publisher?test

?

5.python腳本編寫和運行

python代碼

#!/usr/bin/env python # -*- coding: utf-8 -*- # 該例程將發布turtle1/cmd_vel話題,消息類型geometry_msgs::Twistimport rospy from geometry_msgs.msg import Twistdef velocity_publisher():# ROS節點初始化rospy.init_node('velocity_publisher', anonymous=True)# 創建一個Publisher,發布名為/turtle1/cmd_vel的topic,消息類型為geometry_msgs::Twist,隊列長度10turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)#設置循環的頻率rate = rospy.Rate(10) while not rospy.is_shutdown():# 初始化geometry_msgs::Twist類型的消息vel_msg = Twist()vel_msg.linear.x = 0.5vel_msg.angular.z = 0.2# 發布消息turtle_vel_pub.publish(vel_msg)rospy.loginfo("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", vel_msg.linear.x, vel_msg.angular.z)# 按照循環頻率延時rate.sleep()if __name__ == '__main__':try:velocity_publisher()except rospy.ROSInterruptException:pass

改變權限

sudo chmod 777 test.py

運行

python test.py?

?

?

總結

以上是生活随笔為你收集整理的ros(2) 发布者publisher的编程实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。