Halo
发布于 2024-04-23 / 136 阅读 / 0 评论 / 0 点赞

ros 编程简介

install ros

apt install -y curl gnupg2 lsb-core
sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | apt-key add -

apt update
apt install -y ros-noetic-ros-base

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

apt install -y python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential

rosdep init && rosdep update

create workspace

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

install tf2_ros

apt install -y ros-$ROS_DISTRO-turtle-tf2 ros-$ROS_DISTRO-tf2-tools ros-$ROS_DISTRO-tf ros-$ROS_DISTRO-roslint

vrpn_client_ros(c++)

apt install -y git ros-$ROS_DISTRO-vrpn
cd ~/catkin_ws/src
git clone https://github.com/ros-drivers/vrpn_client_ros
cd ~/catkin_ws
catkin_make
source devel/setup.bash
roslaunch vrpn_client_ros assil_vicon.launch

pub/sub(py)

cd ~/catkin_ws/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
roscd beginner_tutorials
mkdir scripts && cd scripts
nano talker.py
#!/usr/bin/env python

## Simple talker demo that published std_msgs/Strings messages
## to the 'chatter' topic

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
nano listener.py
#!/usr/bin/env python

## Simple talker demo that listens to std_msgs/Strings published 
## to the 'chatter' topic

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
cd ~/catkin_ws/
python src/beginner_tutorials/scripts/talker.py
python src/beginner_tutorials/scripts/listener.py

评论