如何编写ros的python程序

如何编写ros的python程序,第1张

1先建立工作空间~/catkin_ws参阅/note/516843231/3在工程目录下建一个src子目录和.py文件源代码文件:topic_publisher.py~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#!/usr/bin/envpythonimportroslibroslib.load_manifest('basic')importrospyfromstd_msgs.msgimportInt32rospy.init_node('topic_publisher')pub=rospy.Publisher('counter',Int32)rate=rospy.Rate(2)count=0whilenotrospy.is_shutdown():pub.publish(count)count+=1rate.sleep()~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *** 作步骤如下liao@liao-eagle:~/catkin_ws$lsbuilddevelinstallsrcliao@liao-eagle:~/catkin_ws$cdsrcliao@liao-eagle:~/catkin_ws/src$lsbasicbeginner_tutorialsCMakeLists.txtfirstsecondliao@liao-eagle:~/catkin_ws/src$cdbasic/liao@liao-eagle:~/catkin_ws/闷春野src/basic$lsCMakeLists.txtpackage.xmlsrcliao@liao-eagle:~/catkin_ws/src/basic$cdsrcliao@liao-eagle:~/catkin_ws/src/basic/src$vitopic_publisher.pyliao@liao-eagle:~/catkin_ws/src/basic/src$chmod+xtopic_publisher.py4回到~/catkin_ws目录下,编译程序$cd~/catkin_wsliao@liao-eagle:~/catkin_ws$catkin_make5将森游当前工程加入蚂喊到shell环境里去liao@liao-eagle:~/catkin_ws$echo"source~/catkin_ws/devel/setup.bash">>~/.bashrcliao@liao-eagle:~/catkin_ws$source~/.bashrc6运行程序(需开两个终端)liao@liao-eagle:~/catkin_ws$roscoreliao@liao-eagle:~/catkin_ws$rosrunbasictopic_publisher.py7通过rostopic查看相关信息liao@liao-eagle:~/catkin_ws$rostopiclist/counter/rosout/rosout_agg

1 先建立工作空间~/catkin_ws

2 在工作空间~/catkin_ws下创建包pkg目录second

好处是通过创建包自动生成package.xml和CMakeLists.txt

3 在second目录下创建hello.cpp源代码文件

~~~~伍启~~~~~~~~~~~~~~~~~~~~~~~

eagle:~/catkin_ws$ cd src

eagle:~/catkin_ws/src$ tree second/

second/

├── CMakeLists.txt

├── package.xml

└── src

└── hello.cpp

1 directory, 3 files

4 编写hello.cpp文件

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <ros/ros.h>

int main(int argc, char **argv) {

ros::init(argc, argv, "helloros1234444")

ros::NodeHandle n

ROS_INFO("Hello, ROS!")

ros::spinOnce()

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5 修改编译册唤makefile文件 CMakeLists.txt

~~~~~~~~~~~~~~~~~~

cmake_minimum_required(VERSION 2.8.3)

project(simple_examples)

find_package(catkin REQUIRED COMPONENTS roscpp)

catkin_package()

include_directories(include $catkin_INCLUDE_DIRS)

add_executable(hello hello.cpp)

target_link_libraries(hello ${catkin_LIBRARIES})

~~~~~~~~~~~~~~~~~~

6 回到~/catkin_ws编译程序

liao@liao-eagle:~/catkin_ws$ catkin_make

Base path: /home/liao/catkin_ws

Source space: /home/liao/catkin_ws/src

Build space: /home/liao/catkin_ws/build

Devel space: /home/liao/catkin_ws/devel

Install space: /home/liao/catkin_ws/install

####

#### Running command: "make cmake_check_build_system" in "/home/liao/catkin_ws/build"

####

####

#### Running command: "make -j2 -l2" in "/home/liao/catkin_ws/build"

####

Scanning dependencies of target hello

[100%] Building CXX object second/CMakeFiles/hello.dir/src/hello.cpp.o

Linking CXX executable /home/liao/catkin_ws/devel/lib/second/hello

[100%] Built target hello

liao@liao-eagle:~/catkin_ws$

7 测腔姿如试c++程序

7.1一个终端里运行roscore

7.2 另外一个终端里运行 rosrun second hello

liao@liao-eagle:~/catkin_ws$ rosrun second hello

[ INFO] [1442208641.400826666]: Hello, ROS!

liao@liao-eagle:~/catkin_ws$ rospack find second

/home/liao/catkin_ws/src/second

liao@liao-eagle:~/catkin_ws$

望采纳


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12405622.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存