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
- Rosrun later access package name + node name, node operating
- roscore start master
- rosnode list displays all nodes rosnode info node name
- Node display diagram rqt_graph
- Similarly rostopic list
- 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
- It seems only python2
- It must be added in the file header #! / Usr / bin / env python2
- I would add that #! / Usr / bin / env python results being given no module named rospkg, the feeling is python version, then replaced python2 resolved.
- After the python compiler does not need to finish, you can directly rosrun.
- 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 ...