One Motor 0.6.0
RoboMaster全平台一站式电机驱动库
载入中...
搜索中...
未找到
DjiMotorFrames.hpp
1#ifndef ONEMOTOR_DJIMOTORFRAME_HPP
2#define ONEMOTOR_DJIMOTORFRAME_HPP
3
4#include <cstdint>
5#ifdef ONE_MOTOR_LINUX
6#include <format>
7#else
8#include <sstream>
9#include <iomanip>
10#endif
11#include <string>
12
14
15namespace OneMotor::Motor::DJI
16{
25 {
29 explicit operator Can::CanFrame() const
30 {
31 Can::CanFrame frame{};
32 frame.id = canId;
33 frame.dlc = 8;
34 frame.data[0] = static_cast<uint8_t>(ecd >> 8);
35 frame.data[1] = static_cast<uint8_t>(ecd & 0xFF);
36 frame.data[2] = static_cast<uint8_t>(rpm >> 8);
37 frame.data[3] = static_cast<uint8_t>(rpm & 0xFF);
38 frame.data[4] = static_cast<uint8_t>(current >> 8);
39 frame.data[5] = static_cast<uint8_t>(current & 0xFF);
40 frame.data[6] = temperature;
41 return frame;
42 };
43
49 {
50 canId = frame.id;
51 ecd = static_cast<uint16_t>(frame.data[0] << 8) | frame.data[1];
52 rpm = static_cast<int16_t>(static_cast<uint16_t>(frame.data[2] << 8) | frame.data[3]);
53 current = static_cast<int16_t>(static_cast<uint16_t>(frame.data[4] << 8) | frame.data[5]);
54 temperature = frame.data[6];
55 };
56
61 [[nodiscard]] std::string format() const
62 {
63#ifdef ONE_MOTOR_LINUX
64 return std::format("ID: {:X}, Ang: {} RPM, ecd: {} / 8191, CRT: {} mA, temperature: {} deg",
66#else
67 std::ostringstream oss;
68 oss << "ID: " << std::hex << std::uppercase << canId << std::dec
69 << ", Ang: " << rpm << " RPM, ecd: " << ecd << " / 8191, CRT: "
70 << current << " mA, temperature: " << temperature << " deg";
71 return oss.str();
72#endif
73 };
74
75 uint16_t canId;
76 uint16_t ecd;
77 int16_t rpm;
78 int16_t current;
79 uint8_t temperature;
80 };
81
83 {
84 uint16_t last_ecd;
85 uint16_t ecd;
87 float angular;
88 int16_t real_current;
89 uint8_t temperature;
91 int32_t total_round;
97 [[nodiscard]] std::string format() const
98 {
99#ifdef ONE_MOTOR_LINUX
100 return std::format(
101 "M3508 Status:\n"
102 "\t- ECD: {} (last: {})\n"
103 "\t- Angle (single round): {:.2f} deg\n"
104 "\t- Angular Velocity: {:.2f} deg/s\n"
105 "\t- Total Angle: {:.2f} deg\n"
106 "\t- Total Rounds: {}\n"
107 "\t- Real Current: {} mA\n"
108 "\t- Output Current: {}\n"
109 "\t- Temperature: {} °C",
110 ecd, last_ecd,
112 angular,
118#else
119 std::ostringstream oss;
120 oss << "M3508 Status:\n"
121 << "\t- ECD: " << ecd << " (last: " << last_ecd << ")\n"
122 << "\t- Angle (single round): " << std::fixed << std::setprecision(2) << angle_single_round << " deg\n"
123 << "\t- Angular Velocity: " << std::fixed << std::setprecision(2) << angular << " deg/s\n"
124 << "\t- Total Angle: " << std::fixed << std::setprecision(2) << total_angle << " deg\n"
125 << "\t- Total Rounds: " << total_round << "\n"
126 << "\t- Real Current: " << real_current << " mA\n"
127 << "\t- Output Current: " << output_current << "\n"
128 << "\t- Temperature: " << temperature << " °C";
129 return oss.str();
130#endif
131 };
132 };
133}
134
135#endif //ONEMOTOR_DJIMOTORFRAME_HPP
定义了与平台无关的CAN帧结构体。
一个与平台无关的CAN帧结构体。
定义 CanFrame.hpp:36
uint32_t id
CAN ID (标准帧或扩展帧)
定义 CanFrame.hpp:38
uint8_t data[ONE_MOTOR_CAN_MAX_DLEN]
CAN数据负载
定义 CanFrame.hpp:46
定义 DjiMotorFrames.hpp:83
int32_t total_round
累计总圈数,带方向
定义 DjiMotorFrames.hpp:91
int16_t output_current
PID计算后输出给电机的电流值
定义 DjiMotorFrames.hpp:92
float total_angle
累计总角度 (°),带方向
定义 DjiMotorFrames.hpp:90
float angular
角速度 (度/秒)
定义 DjiMotorFrames.hpp:87
uint16_t ecd
当前编码器值
定义 DjiMotorFrames.hpp:85
std::string format() const
将电机状态格式化为可读字符串。
定义 DjiMotorFrames.hpp:97
uint8_t temperature
温度 (°C)
定义 DjiMotorFrames.hpp:89
float angle_single_round
单圈角度 (0-360°)
定义 DjiMotorFrames.hpp:86
int16_t real_current
实际电流 (mA)
定义 DjiMotorFrames.hpp:88
uint16_t last_ecd
上一次的编码器值
定义 DjiMotorFrames.hpp:84
uint16_t ecd
编码器原始值 (0~8191)
定义 DjiMotorFrames.hpp:76
int16_t current
电机实际电流 (mA)
定义 DjiMotorFrames.hpp:78
uint8_t temperature
电机温度 (°C)
定义 DjiMotorFrames.hpp:79
int16_t rpm
转速 (RPM)
定义 DjiMotorFrames.hpp:77
std::string format() const
将帧内容格式化为可读字符串。
定义 DjiMotorFrames.hpp:61
uint16_t canId
CAN ID
定义 DjiMotorFrames.hpp:75
RawStatusFrame(Can::CanFrame frame)
从通用的 Can::CanFrame 构造此结构体。
定义 DjiMotorFrames.hpp:48