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)
相关推荐
老猿讲编程1 小时前
C++中的奇异递归模板模式CRTP
开发语言·c++
汤姆yu3 小时前
基于python的化妆品销售分析系统
开发语言·python·化妆品销售分析
ScilogyHunter3 小时前
C语言标准库完全指南
c语言·开发语言
sali-tec3 小时前
C# 基于halcon的视觉工作流-章52-生成标定板
开发语言·图像处理·人工智能·算法·计算机视觉
应茶茶3 小时前
C++11 核心新特性:从语法重构到工程化实践
java·开发语言·c++
学习笔记1013 小时前
第十五章认识Ajax(六)
前端·javascript·ajax
程子的小段4 小时前
C 语言实例 - 字符串复制
c语言·开发语言
-森屿安年-4 小时前
STL 容器:stack
开发语言·c++
歪歪1004 小时前
C#如何在数据可视化工具中进行数据筛选?
开发语言·前端·信息可视化·前端框架·c#·visual studio
Jyywww1215 小时前
Python基于实战练习的知识点回顾
开发语言·python