ROS入門記錄 3

2021-07-30 11:06:17 字數 4418 閱讀 2611

int64 a

int64 b

int64 c

int64 a

int64 b

---int64 sum

$ roscd beginner_tutorials

$ mkdir msg

$ echo "int64 num" > msg/num.msg

# 也能在num.msg中新增別的:

string first_name

string last_name

uint8 age

uint32 score

#在package.xml中新增依賴項

message_generation

message_runtime

#在cmakelists.txt中修改

find_package(catkin required components

roscpp

rospy

std_msgs

message_generation

)catkin_package(

...catkin_depends message_runtime ...

...)

add_message_files(

files

num.msg

)generate_messages(

dependencies

std_msgs

)

可以檢視msg

$ rosmsg show num

or$ rosmsg show beginner_tutorials/num

int64 num

$ roscd beginner_tutorials

$ mkdir srv # 建立srv資料夾

#  roscp [package_name] [file_to_copy_path] [copy_path]  # 從別的package拷貝service過來

$ roscp rospy_tutorials addtwoints.srv srv/addtwoints.srv

同樣需要修改package.xml檔案,這步之前做過就不用做了

message_generationbuild_depend>

message_runtimerun_depend>

然後,需要修改cmakelists.txt檔案

add_service_files

( files

addtwoints.srv

)

可以檢視srv

$ rossrv show addtwoints

or$ rossrv show beginner_tutorials/addtwoints

int64 a

int64 b

---int64 sum

$ roscd beginner_tutorials

$ cd ../..

$ catkin_make install

$ cd -

$ roscd beginner_tutorials

$ mkdir scripts

$ cd scripts

$ wget

/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/talker.py

$ chmod +x talker.py

關於talker.py的解釋,鏈結

# talker.py

import rospy

from std_msgs.msg import string

deftalker

(): pub = rospy.publisher('chatter', string, queue_size=10)

rospy.init_node('talker', anonymous=true)

rate = rospy.rate(10) # 10hz

while

not rospy.is_shutdown():

hello_str = "hello world %s" % rospy.get_time()

rospy.loginfo(hello_str)

pub.publish(hello_str)

rate.sleep()

if __name__ == '__main__':

try:

talker()

except rospy.rosinterruptexception:

pass

以上創造了乙個node,不斷的傳送 「hello world time()」

還需要創造乙個接收的node:

$ roscd beginner_tutorials/scripts/

$ wget

/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py

$ chmod +x listener.py

關於listener.py的解釋,鏈結

# listener.py

import rospy

from std_msgs.msg import string

defcallback

(data):

rospy.loginfo(rospy.get_caller_id() + 'i heard %s', data.data)

deflistener

():# in ros, nodes are uniquely named. if two nodes with the same

# name are launched, the previous one is kicked off. the

# anonymous=true flag means that rospy will choose a unique

# name for our 'listener' node so that multiple listeners can

# run simultaneously.

rospy.init_node('listener', anonymous=true)

rospy.subscriber('chatter', string, callback)

# spin() simply keeps python from exiting until this node is stopped

rospy.spin()

if __name__ == '__main__':

listener()

$ cd ~/catkin_ws

$ catkin_make

$ 準備工作

$ cd ~/catkin_ws

$ source devel/setup.bash # 環境變數

$ roscore # 開啟master,如果之前已經有,會報error

$ 啟動talker

$ rosrun beginner_tutorials talker #如果是c++

$ rosrun beginner_tutorials talker.py #如果是python

$ 在另乙個terminal啟動listener

$ rosrun beginner_tutorials listener #如果是c++

$ rosrun beginner_tutorials listener.py #如果是python

可以看到兩個視窗的log,talker發出的訊息,listener在延遲一點點時間後收到。

# talker

[info]

[walltime: 1492089752.305381]

hello

world 1492089752.31

# listener

[info] [walltime: 1492089752.306185] /listener_26673_1492089744027i heard hello world 1492089752.31

ROS入門3 建立ROS程式包

乙個catkin程式包必須要符合一下要求 程式包必須包含乙個catkin版本的cmakelists.txt檔案,而catkin metapackages中必須包含一對cmakelist.txt檔案的引用.每個目錄下只能存在乙個程式包 用catkin create pkg命令建立乙個catkin工作空...

Python入門記錄3

python 模組 python 檔案,包含 python 物件定義和 python 語句 import 關鍵字引入模組 當前搜尋路徑 shell 變數pythonpath 預設路徑 fromtimeimporttimezone 只引入模組中的乙個屬性或方法 fromtimeimport 引入全部模...

ROS學習(一) ROS入門

注 主要是古月部落格學習過程中的記錄,其專欄為 ros robot operating system 是用於機械人的一種次級作業系統,可以提供硬體抽象描述 底層驅動程式管理 共用功能執行 程式間的訊息傳遞 程式發行包管理等功能。ros的設計目標是在機械人研發中提高 復用率,是一種分布式處理框架,可執...