完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
C++
创建功能包 cd ~/catkin_workspace/src catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim 编写程序 nano ~/catkin_workspace/src/learning_topic/src/velocity_publisher.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist */ #include #include int main(int argc, char **argv) { // ROS节点初始化 ros::init(argc, argv, "velocity_publisher"); // 创建节点句柄 ros::NodeHandle n; // 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10 ros::Publisher turtle_vel_pub = n.advertise // 设置循环的频率 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("Publish turtle velocity command[%0.2f m/s, %0.2f rad/s]", vel_msg.linear.x, vel_msg.angular.z); // 按照循环频率延时 loop_rate.sleep(); } return 0; } 配置CMakeLists.txt nano ~/catkin_workspace/src/learning_topic/CMakeLists.txt 在Build内,Install前加入 add_executable(velocity_publisher src/velocity_publisher.cpp) target_link_libraries(velocity_publisher ${catkin_LIBRARIES}) 设置需要编译的代码和生成的可执行文件; 设置链接库; 编译并运行发布者 cd ~/catkin_workspace catkin_make # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtlesim_node # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_topic velocity_publisher # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rqt_graph python 创建并编写脚本 cd ~/catkin_workspace/src/learning_topic mkdir scripts cd ~/catkin_workspace/src/learning_topic/scripts nano velocity_publisher.py #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist import rospy from geometry_msgs.msg import Twist def velocity_publisher(): # ROS节点初始化 rospy.init_node('velocity_publisher', anonymous=True) # 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10 turtle_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.5 vel_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 +x ~/catkin_workspace/src/learning_topic/scripts/velocity_publisher.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtlesim_node # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_topic velocity_publisher.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rqt_graph |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1523 浏览 1 评论
synopsys 的design ware:DW_fpv_div,浮点数除法器,默认32位下,想提升覆盖率(TMAX),如果用功能case去提升覆盖率呢?
1726 浏览 1 评论
RK3588 GStreamer调试四路鱼眼摄像头四宫格显示报错
4153 浏览 1 评论
【飞凌嵌入式OK3576-C开发板体验】RKNN神经网络-YOLO图像识别
254 浏览 0 评论
【飞凌嵌入式OK3576-C开发板体验】SSH远程登录网络配置及CAN通讯
1336 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-9 09:33 , Processed in 0.704057 second(s), Total 73, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号