GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
can.h
浏览该文件的文档.
1
6#pragma once
7
8#include <cstdint>
9#include <string>
10#include <asio.hpp>
11
12#include "base.hpp"
13#include "core/async.hpp"
14#include "core/logger.h"
15#include "write_queue.hpp"
16
17namespace roboctrl::io{
18
19using can_id_type = uint32_t;
20
21inline constexpr uint64_t can_write_key(can_id_type id, size_t payload_size) {
22 return uint64_t{id} | (uint64_t{payload_size} << 32);
23}
24
28class can:public keyed_io_base<std::uint32_t>,public logable<can>{
29public:
33 struct info_type{
35 std::string name;
37 std::string interface_name;
38
39 using key_type = std::string;
40 using owner_type = can;
41
42 const std::string& key() const{return name;}
43 };
44
45 using key_type = std::uint32_t;
46
50 can(const info_type& info);
51
52 ~can() = default;
53
55 void start();
56
61 awaitable<void> send(can_id_type id,byte_span data);
62
67
69 std::string desc()const{
70 return std::format("bare can({} on {})",info_.name, info_.interface_name);
71 }
72
73private:
74 asio::posix::stream_descriptor stream_;
75 write_queue write_queue_;
76 info_type info_;
77 std::array<std::byte,20> buffer_;
78 std::string interface_name_;
79 bool started_ {false};
80};
81
82static_assert(keyed_io<can>);
83}
异步任务上下文组件。
CAN 设备对象,支持根据 ID 分发回调。
Definition can.h:28
std::string desc() const
返回包含逻辑名称和 Linux 接口名的可读描述。
Definition can.h:69
awaitable< void > task()
接收循环任务。
Definition can.cpp:87
void start()
启动接收协程;重复调用不会重复启动。
Definition can.cpp:79
awaitable< void > send(can_id_type id, byte_span data)
发送带 CAN ID 的帧。
Definition can.cpp:110
带 key 的 IO 基类,根据键值派发数据。
Definition base.hpp:157
将同一异步流上的写操作串行化。
用于提供日志功能的辅助基类 通过CRTP模式实现,要求派生类实现desc()方法以提供描述信息,在继承时传入派生类自身类型作为模板参数。 继承这个类后,派生类可以方便地使用log_debug、log_i...
Definition logger.h:178
IO的基础组件。
std::span< const std::byte > byte_span
只读 byte span。
Definition base.hpp:45
用于日志输出的组件。
asio::awaitable< T > awaitable
协程任务类型。
Definition async.hpp:46
CAN 初始化参数。
Definition can.h:33
std::string interface_name
实际 SocketCAN 接口名,例如 can0 或 CAN_CHASSIS。
Definition can.h:37
std::string name
项目内稳定名称,供电机等其他组件引用。
Definition can.h:35