ROS2快速入门0--节点

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:第一个节点

python 复制代码
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

进行项目重启

python 复制代码
colcon build

重置项目环境

python 复制代码
source install/setup.bash

进行运行

python 复制代码
ros2 run demo_python_pkg learn_thread

相关代码

python 复制代码
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)
相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
三十而立洋2 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
卡布鲁2 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
李少兄2 天前
JavaScript 隐式全局变量解析
javascript
霍霍的袁2 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超2 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int2 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
沙蒿同学2 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端