[Ros] ros entry record

ROS entry

Half-day introductory ROS, the overall feeling is better understood, python write without compilation super cool, completion of ros to learn electronically controlled to go.

installation

Add Source

> sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

Add key

> sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

Installation ros

> sudo apt update
> sudo apt install ros-melodic-desktop-full

Initialization rosdep

> sudo rosdep init
> rosdep update

problem:

No module named 'rosdep2'

solve:

sudo update-alternatives --config python

Manual switching python2

Set the environment

> echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
> source ~/.bashrc

Installation rosinstall

> sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential

Knowledge Point

Topic, service.

Topics have publishers and subscribers, the publisher just released, subscribers simply open the door to let the data come in. Data released called topic.

Service is commonly used

instruction

  1. Rosrun later access package name + node name, node operating
  2. roscore start master
  3. rosnode list displays all nodes rosnode info node name
  4. Node display diagram rqt_graph
  5. Similarly rostopic list
  6. rostopic pub topic name content plus -r can specify how many times per second hair cycle

Workspace

Create a workspace

mkdir -p catkin_ws/src
cd catkin_ws/src
catkin_init_workspace

Compile space

cd catkin_ws
catkin_make

Set Environment Variables

source devel/setup.sh

Check the Environment Variables

echo $ROS_PACKAGE_PATH

Creating Feature Pack (must)

cd src
catkin_create test_pkg roscpp rospy std_msgs

Compile Feature Pack

cd catkin_ws
catkin_make
source devel/setup.bash

python under question

  1. It seems only python2
  2. It must be added in the file header #! / Usr / bin / env python2
  3. I would add that #! / Usr / bin / env python results being given no module named rospkg, the feeling is python version, then replaced python2 resolved.
  4. After the python compiler does not need to finish, you can directly rosrun.
  5. Remember to give executable permission.

coding

publisher

#!/usr/bin/env python2
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10) # 发布话题名为chatter的topic,类型为String,队列长10
    rospy.init_node('talker', anonymous=True) # 初始化节点,节点名为talker
    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()# 每隔100ms 一次

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

listener

#!/usr/bin/env python2
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) # 订阅chatter话题,这里只需要指定话题,因为多个不同的节点可能发布同一话题

    rospy.spin()# 循环调callback直到结束

if __name__ == '__main__':
    listener()

To be continued ...

Guess you like

Origin www.cnblogs.com/aoru45/p/11372122.html