0:安装
wget http://fishros.com/install -O fishros && . fishros
1:运行第一个机器人
ros2 run turtlesim turtlesim_node
使用方向健进行控制(在另一个终端)
ros2 run turtlesim turtle_teleop_key
2原理解析
打开另一个终端-->输入rqt-->Plugins-->Introspection-->Node graph
2:第一个节点
import rclpy
from rclpy.node import Node
def main():
rclpy.init()
node = Node("python_node")
node.get_logger().info("hello!python Node!")
node.get_logger().warn("Look at me!")
rclpy.spin(node)
rclpy.shutdown()
if __name__== '__main__':
main()
3:功能包
生成功能包
ros2 pkg create --build-type ament_python --license Apache-2.0 demo_python_pkg
添加声明
添加依赖
构建功能包
colcon build
修改环境变量
source install/setup.bash
执行程序
ros2 run demo_python_pkg python_node
工作空间
构建工作空间
mkdir -p chapt2_ws/src
mv demo_python_pkg/ chapt2_ws/src/
rm -rf install/ build/ log/
cd chapt2_ws/
colcon build
构建指定的包
colcon build --packages-select demo_python_pkg
Multithreading
开启服务器
echo "Stupid duck" >novel1.txt
echo "Stupid pig" >novel2.txt
echo "Stupid dog" >novel3.txt
python -m http.server
进行项目重启
colcon build
重置项目环境
source install/setup.bash
进行运行
ros2 run demo_python_pkg learn_thread
相关代码
import threading
import requests
class Download:
def download(self,url,callback_world_count):
print(f"Thread:{threading.get_ident()}Start:{url}")
response=requests.get(url)
callback_world_count(url,response.text)
def start_download(self,url,callback_world_count):
thread=threading.Thread(target=self.download,args=(url,callback_world_count))
thread.start()
def world_count(url,result):
print(f"url:{url}--result:{result[:5]}")
def main():
download=Download()
download.start_download('http://0.0.0.0:8000/novel1.txt',world_count)
download.start_download('http://0.0.0.0:8000/novel2.txt',world_count)
download.start_download('http://0.0.0.0:8000/novel3.txt',world_count)