본문 바로가기

Enginius/Robotics

ROS tutorial (publisher / subscriber)

Writing the Publisher Node in C++

 

source ~/catkin_ws/devel/setup.bash

- To let the OS to know where ROS files are. 

roscd beginner_tutorials


- Make src/talker.cpp


Building the nodes- Modify CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)

project(beginner_tutorials)


## Find catkin and any catkin packages

find_package(catkin REQUIRED COMPONENTS

  roscpp

  rospy

  std_msgs

)


## Declare a catkin package

catkin_package()


## Specify additional locations of header files

## Your package locations should be listed before other locations

include_directories(

# include

  ${catkin_INCLUDE_DIRS}

)


add_executable(talker src/talker.cpp)

target_link_libraries(talker ${catkin_LIBRARIES})

add_dependencies(talker beginner_tutorials_generate_messages_cpp)


add_executable(listener src/listener.cpp)

target_link_libraries(listener ${catkin_LIBRARIES})

add_dependencies(listener beginner_tutorials_generate_messages_cpp)


add_executable(talker src/talker.cpp)

target_link_libraries(talker ${catkin_LIBRARIES})

add_dependencies(talker beginner_tutorials_generate_messages_cpp)


add_executable(listener src/listener.cpp)

target_link_libraries(listener ${catkin_LIBRARIES})

add_dependencies(listener beginner_tutorials_generate_messages_cpp)

- This will create two executables, talker and listner, which by default will go to into package directory of your devel space, located by default at ~/catkin_ws/devel/lib/<package name>



source ~/catkin_ws/devel/setup.bash

- Let the terminal knows ROS.  


rosrun beginner_tutorials talker

[ INFO] [1533680834.204181475]: hello world 0

[ INFO] [1533680834.304349319]: hello world 1

[ INFO] [1533680834.404339320]: hello world 2

[ INFO] [1533680834.504308288]: hello world 3

[ INFO] [1533680834.604425992]: hello world 4

[ INFO] [1533680834.704335552]: hello world 5


rosrun
 beginner_tutorials listner

[ INFO] [1533680866.009809858]: I heard: [hello world 3]

[ INFO] [1533680866.109670809]: I heard: [hello world 4]

[ INFO] [1533680866.209616448]: I heard: [hello world 5]

[ INFO] [1533680866.309694638]: I heard: [hello world 6]

[ INFO] [1533680866.409597115]: I heard: [hello world 7]


Writing the Publisher Node in Python

 

mkdir scripts

- This is where the Python scripts are saved. 

cd scripts

wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/talker.py

#!/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 = "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

chmod +x talker.py

- This is required for the Python script to be executable. 


python talker.py

- Run talker. 


wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py

#!/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():


    # 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()


chmod +x listener.py

python listener.py



Running the publishers and subscribers

cd ~/catkin_ws

source ./devel/setup.bash

rosrun beginner_tutorials talker

rosrun beginner_tutorials talker.py

rosrun beginner_tutorials listener

rosrun beginner_tutorials listener.py






Creating a ROS service


----- I am too tired for this. 



















































#

'Enginius > Robotics' 카테고리의 다른 글

Homogeneous transformation  (0) 2019.03.27
ROS cheat sheet  (0) 2018.08.08
ROS tutorial (ROS package / node / topic / message / service / parameter/ launch)  (0) 2018.08.08
MuJoCo Walker2d  (0) 2018.07.27
Baxter in Gazebo  (0) 2018.07.27