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)
相关推荐
放下华子我只抽RuiKe54 分钟前
FastAPI 全栈后端(一):为什么选择 FastAPI
前端·javascript·深度学习·react.js·机器学习·前端框架·fastapi
Shadow(⊙o⊙)11 分钟前
Linux基础IO-1.0——open、close、read及write-深入手搓分析!
linux·运维·服务器·开发语言·c++·学习
我是一颗柠檬12 分钟前
【JDK8新特性】Stream流API上Day4
java·开发语言·后端
A南方故人14 分钟前
将容器内的元素变为可拖拽
开发语言·javascript·ecmascript
小小de风呀17 分钟前
de风——【从零开始学C++】(九)—vector的基本使用
开发语言·c++
MepSUxjvy18 分钟前
002:RAG 入门-LangChain 读取文本
开发语言·python·langchain
我是一颗柠檬18 分钟前
【JDK8新特性】方法引用与构造器引用Day3
java·开发语言·后端·intellij-idea
子榆.19 分钟前
CANN自定义GEMM算子(Ascend C手写高性能矩阵乘法)
c语言·开发语言·矩阵
迁旭27 分钟前
Claude Code /status 功能技术文档
前端·javascript·人工智能·react.js·机器学习·gpt-3·文心一言
星星~笑笑30 分钟前
react Next.js oss上传 上传阿里云
javascript·react.js·阿里云·js