Writing a simple publisher and subscriber (Python)

ROS မွာ nodes ေတြက သီးျခား executables program ေတြအျဖစ္ အလုပ္လုပ္ေနျပီး သူတို႕ရဲ႕ addresses ေတြကိုေတာ့ rosmaster မွာ သြားျပီး register လုပ္ထားပါတယ္။ node ေတြက သူတို႕ပို႕ခ်င္တဲ့ ေဒတာေတြ (သို႕) လိုခ်င္တဲ့ေဒတာေတြကိုေတာ့ topic ေတြနဲ႕ အဆက္အသြယ္လုပ္ၾကပါတယ္။ ဆိုပါေတာ့ ကြ်န္ေတာ္သည္ A ဆိုတဲ့ node program တစ္ခုနဲ႕ အလုပ္လုပ္ေနျပီး ၊ ကြ်န္ေတာ့္ရဲ႕ အခန္းေဖာ္ကေတာ့ B ဆိုတဲ့ node program တစ္ခုနဲ႕ အလုပ္လုပ္ေနပါတယ္။ ကြ်န္ေတာ္တို႕ ၂ ဦးကေတာ့ အခန္းနံပါတ္ ၅၀၀ ဆိုတဲ့ rosmaster ပဲထားပါေတာ့ အဲ့ေအာက္မွာ အလုပ္လုပ္ၾကပါတယ္။ ကြ်န္ေတာ္တို႕ သူငယ္ခ်င္း ၂ ခုက ဘယ္လို အဆက္အသြယ္လုပ္မလဲဆိုေတာ့ topic ေတြကို Publish လုပ္ျခင္း၊ Subscribe လုပ္ျခင္းနဲ႕ ဆက္သြယ္ေနပါတယ္။ ဒါဟာ ros ရဲ႕ အေျခခံအက်ဆံုး philosophy တစ္ခုျဖစ္ပါတယ္။ ဒီေတာ့ nodes ေတြကို ဘယ္လို တည္ေဆာက္မလဲ၊ topic ေတြနဲ႕ ဘယ္လို အလုပ္လုပ္မလဲၾကည့္ရေအာင္။

ROS နဲ႕ အလုပ္လုပ္ဖို႕ရာအတြက္ working directory တစ္ခုကို ဖန္တီးေပးဖို႕ လိုအပ္ပါတယ္။ ဘယ္လိုဖန္တီးမလဲဆိုတာကိုေတာ့ ဒီလင့္မွာ သြားဖတ္နိုင္ပါတယ္။ ကၽြန္ေတာ္ကေတာ့ လြယ္လြယ္ကူကူနဲ႕ လူျပိန္းနည္းေရးထားပါတယ္။

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make

အထက္ပါ ကုဒ္ေတြအတိုင္း terminal မွာ run လိုက္ရင္ေတာ့ catkin_ws ဆိုတဲ့ working directory တစ္ခုကို ရလာပါလိမ့္မယ္။ working directory ရလာရင္ေတာ့ ကြ်န္ေတာ္တို႕ node ေတြကုိ ေရးသားဖို႕အတြက္ ros package ကို ဆက္ျပီး ဖန္တီးေပးဖို႕လိုအပ္ပါတယ္။ ဘယ္လိုဖန္တီးမလဲဆိုတာ အေသးစိတ္သိခ်င္ရင္ေတာ့ ဒီလင့္မွာ သြားဖတ္နိုင္ပါတယ္။ ေအာက္ပါ ကုဒ္ေတြအတိုင္း terminal ကေနတဆင့္ packages ေတြ ဖန္တီးရေအာင္…

# You should have created this in the Creating a Workspace Tutorial
cd ~/catkin_ws/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

ဒီမွာ beginner_tutorials လို႕ေခၚတဲ့ package တစ္ခုကို ဖန္တီးလိုက္ပါတယ္။ သူရဲ႕ dependencies ေတြကေတာ့ std_msgs၊ rospy၊ နဲ႕ roscpp ပဲျဖစ္ပါတယ္။ beginner_tutorials ဆိုတဲ့ packages ထဲမွာ ဘာေတြပါလဲဆိုတာကိုေတာ့ ေအာက္ပါပံုမွာ ျမင္ရပါလိမ့္မယ္။

package.xml ထဲမွာေတာ့ description tags ေတြ၊ maintainer tags ေတြ၊ lincense tags ေတြနဲ႕ dependencies tags ေတြ ပါဝင္ပါလိမ့္မယ္။ CMakeLists.txt ကေတာ့ software packages ေတြကို build လုပ္ဖုိ႕အတြက္ Cmake build system ရဲ႕ input file ပဲ ျဖစ္ပါတယ္။ include မွာေတာ့ software packages အတြက္ လိုအပ္တဲ့ library ဖိုင္ေတြ သြားထားပါမယ္။ src ဖိုင္မွာေတာ့ ကြ်န္ေတာ္တို႕ ေရးခ်င္တဲ့ nodes ေတြကို ေရးမွာျဖစ္ပါတယ္။ ဒီမွာေတာ့ ကၽြန္ေတာ္ python နဲ႕ nodes ေတြေရးမွာ ျဖစ္တဲ့အတြက္ scripts ဆိုတဲ့ folder ေလးတည္ေဆာက္လိုက္မယ္။ src ဖိုင္ကိုေတာ့ C++ နဲ႕ ေရးထားတဲ့ nodes ေတြအတြက္ သီးသန္႕ထားခ်င္လို႕ပဲ ျဖစ္ပါတယ္။ scripts ဖိုင္ထဲမွာ talker.py နဲ႕ listener.py ဆိုတဲ့ ဖိုင္ ၂ ခုကို သြားတည္ေဆာက္လိုက္ပါမယ္။

talker.py မွာေတာ့ ေအာက္ပါ program ကို သြားျပီး ကူးထည့္ထားရေအာင္…

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def talker():
     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 = String()
     hello_str.data = "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

listener.py မွာေတာ့ ေအာက္ပါ program code ေတြကို သြားျပီး ကူးထည့္ရေအာင္….

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
     rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)

def listener():
     rospy.init_node('listener', anonymous=True)
     rospy.Subscriber('chatter', String, callback)
     rospy.spin()

if __name__ == '__main__':
     listener()

အားလံုးကူထည့္ျပီးရင္ေတာ့ ဒီဖိုင္ ၂ခုရွိတဲ့ ေနရာကိုသြားျပီး executables ဖိုင္ေတြ ျဖစ္ေအာင္ လုပ္ထားလိုက္ပါမယ္။ ေအာက္ပါ command ေတြကို termial မွာ run လိုက္ပါ။

roscd beginner_tutorials
cd scripts
chmod +x talker.py listener.py

video tutorials ကိုေတာ့ ဒီ youtube link မွာ ၾကည့္နိုင္ပါတယ္။

Publisher

Subscriber

results matching ""

    No results matching ""