鱼香ros第二章节点学习
在Setup.Py 增加节点
Python节点:
import rclpy from rclpy.node import Node def main(): rclpy.init()#初始化 node = Node("python_node")#创建节点 node.get_logger().info('你好 Python 节点!');#获取日志打印内容 rclpy.spin(node)#执行节点 rclpy.shutdown()#清理节点C节点:
if __name__=="__main__": main() #include "rclcpp/rclcpp.hpp" int main(int argc, char **argv) { rclcpp::init(argc, argv); auto node = std::make_shared<rclcpp::Node>("cpp_node"); RCLCPP_INFO(node->get_logger(), "你好 C++ 节点!"); rclcpp::spin(node); rclcpp::shutdown(); return 0; }面向对象编程python
import rclpy from rclpy.node import Node class PersonNode(Node):#类 def __init__(self, node_name: str, name: str, age: int) -> None:#节点名称 节点变量 super().__init__(node_name) self.age = age #变量赋值 self.name = name #变量赋值 def eat(self, food_name: str):# 节点名称 节点变量 self.get_logger().info(f'我叫{self.name},今年{self.age}岁,我现在正在吃{food_name}') def main(): rclpy.init() node = PersonNode('person_node', '法外狂徒张三', '18')#类的对象 node.eat('鱼香肉丝') rclpy.spin(node) rclpy.shutdown()python多线程
import threading#多线程库 import requests#下载库 class Download: def download(self, url, callback):#函数 (函数名 网址 统计的字数) print(f'线程:{threading.get_ident()} 开始下载:{url}')#打印线程编号 response = requests.get(url) response.encoding = 'utf-8' callback(url, response.text)#调用回调函数 def start_download(self, url, callback):#开始下载 (函数名 网址 统计的字数) thread = threading.Thread(target=self.download, args=(url, callback)) #多线程下载 (线程目标 线程参数) thread.start() #启动线程 def download_finish_callback(url, result): #普通函数 用于回调 print(f'{url}下载完成,共:{len(result)}字,内容为:{result[:5]}...') def main(): d = Download() d.start_download('http://localhost:8000/novel1.txt', download_finish_callback) d.start_download('http://localhost:8000/novel2.txt', download_finish_callback) d.start_download('http://localhost:8000/novel3.txt', download_finish_callback)