first
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* DShot 数字电调协议 —— 公共 API。
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_DRIVERS_DSHOT_H_
|
||||
#define ZEPHYR_INCLUDE_DRIVERS_DSHOT_H_
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** 单个 DShot 设备(一个定时器)可驱动的最大通道数。 */
|
||||
#define DSHOT_MAX_CHANNELS 4
|
||||
|
||||
/** 油门范围(DShot 值 48..2047 为油门)。 */
|
||||
#define DSHOT_THROTTLE_MIN 48U
|
||||
#define DSHOT_THROTTLE_MAX 2047U
|
||||
|
||||
/** 表示"电机停转"的原始 DShot 值。 */
|
||||
#define DSHOT_VALUE_STOP 0U
|
||||
|
||||
/*
|
||||
* 双向 / 3D 电调把油门区间一分为二:
|
||||
* 0 停转
|
||||
* 48 .. 1047 反转,48 最慢,1047 最快
|
||||
* 1048 .. 2047 正转,1048 最慢,2047 最快
|
||||
*/
|
||||
#define DSHOT_3D_SPLIT 1047U
|
||||
#define DSHOT_3D_FORWARD_MIN 1048U
|
||||
|
||||
/**
|
||||
* DShot 特殊命令(帧值 0..47)。
|
||||
* 命令需连续重复若干帧(通常 >= 10)电调才会接受;调用 dshot_send_command()
|
||||
* 会自动处理重复。
|
||||
*/
|
||||
enum dshot_command {
|
||||
DSHOT_CMD_MOTOR_STOP = 0,
|
||||
DSHOT_CMD_BEEP1 = 1,
|
||||
DSHOT_CMD_BEEP2 = 2,
|
||||
DSHOT_CMD_BEEP3 = 3,
|
||||
DSHOT_CMD_BEEP4 = 4,
|
||||
DSHOT_CMD_BEEP5 = 5,
|
||||
DSHOT_CMD_ESC_INFO = 6,
|
||||
DSHOT_CMD_SPIN_DIRECTION_1 = 7,
|
||||
DSHOT_CMD_SPIN_DIRECTION_2 = 8,
|
||||
DSHOT_CMD_3D_MODE_OFF = 9,
|
||||
DSHOT_CMD_3D_MODE_ON = 10,
|
||||
DSHOT_CMD_SETTINGS_REQUEST = 11,
|
||||
DSHOT_CMD_SAVE_SETTINGS = 12,
|
||||
DSHOT_CMD_EXTENDED_TELEMETRY_ENABLE = 13,
|
||||
DSHOT_CMD_EXTENDED_TELEMETRY_DISABLE = 14,
|
||||
DSHOT_CMD_SPIN_DIRECTION_NORMAL = 20,
|
||||
DSHOT_CMD_SPIN_DIRECTION_REVERSED = 21,
|
||||
DSHOT_CMD_LED0_ON = 22,
|
||||
DSHOT_CMD_LED1_ON = 23,
|
||||
DSHOT_CMD_LED2_ON = 24,
|
||||
DSHOT_CMD_LED3_ON = 25,
|
||||
DSHOT_CMD_LED0_OFF = 26,
|
||||
DSHOT_CMD_LED1_OFF = 27,
|
||||
DSHOT_CMD_LED2_OFF = 28,
|
||||
DSHOT_CMD_LED3_OFF = 29,
|
||||
DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30,
|
||||
DSHOT_CMD_SILENT_MODE_ON_OFF = 31,
|
||||
DSHOT_CMD_MAX = 47,
|
||||
};
|
||||
|
||||
/**
|
||||
* 输出映射配置(PX4/BF 风格)。
|
||||
*/
|
||||
struct dshot_output_config {
|
||||
/** true=双向/3D 电调:dshot_set_output() 取 -1.0(全反转)..0.0(停)..+1.0(全正转);
|
||||
* false:0.0(停)..1.0(全正转)。 */
|
||||
bool output_3d;
|
||||
/** 非零输出的最小比例 [0..1]。 */
|
||||
float idle;
|
||||
/** 中位死区半宽 [0..1](仅 3D)。 */
|
||||
float deadband;
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置输出映射配置。
|
||||
* @param dev DShot 设备。
|
||||
* @param cfg 输出映射配置。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int dshot_set_output_config(const struct device *dev,
|
||||
const struct dshot_output_config *cfg);
|
||||
|
||||
/**
|
||||
* 设置某通道的归一化输出。
|
||||
* 按配置(含 3D 死区与最小 idle)映射为原始 DShot 油门并立即生效。
|
||||
* @param dev DShot 设备。
|
||||
* @param channel 通道号 0..3。
|
||||
* @param value 归一化输出(见 dshot_output_config)。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int dshot_set_output(const struct device *dev, uint8_t channel, float value);
|
||||
|
||||
/**
|
||||
* 设置某通道的油门。
|
||||
* @param dev DShot 设备。
|
||||
* @param channel 通道号 0..3(对应 TIM CH1..CH4)。
|
||||
* @param throttle 油门值,0(停/未解锁)..2047(满);1..47 保留给命令,请用
|
||||
* dshot_send_command()。
|
||||
* @param telemetry_request 是否置位帧内的遥测请求位。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int dshot_set_throttle(const struct device *dev, uint8_t channel,
|
||||
uint16_t throttle, bool telemetry_request);
|
||||
|
||||
/**
|
||||
* 在某通道发送 DShot 特殊命令。
|
||||
* 命令帧重复 repeats 次(协议要求至少 ~10 次)后,通道恢复原油门值。
|
||||
* @param dev DShot 设备。
|
||||
* @param channel 通道号 0..3。
|
||||
* @param cmd 命令(enum dshot_command)。
|
||||
* @param repeats 连续发送帧数(建议 >= 10)。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int dshot_send_command(const struct device *dev, uint8_t channel,
|
||||
enum dshot_command cmd, uint16_t repeats);
|
||||
|
||||
/**
|
||||
* 启动周期性 DShot 帧流。
|
||||
* 启动定时器与周期 DMA burst 发送;所有通道初始输出 0(电机停转)。
|
||||
* @param dev DShot 设备。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int dshot_start(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 停止帧流并把所有输出拉低。
|
||||
* @param dev DShot 设备。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int dshot_stop(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 读取某通道最近的双向 DShot 遥测(eRPM)。
|
||||
* 仅在设备配置了 dshot-bidirectional 时有意义;每次捕获回复后后台刷新。
|
||||
* @param dev DShot 设备。
|
||||
* @param channel 通道号 0..3。
|
||||
* @param erpm 输出:电转速;电机停转时为 0。
|
||||
* @return 0 有有效遥测值,-EAGAIN 暂无。
|
||||
*/
|
||||
int dshot_get_erpm(const struct device *dev, uint8_t channel, int32_t *erpm);
|
||||
|
||||
/**
|
||||
* 诊断用:以千分比覆盖 '0' 位的高电平时间。
|
||||
* '1' 位高电平严格保持为 '0' 位的两倍。DShot 规范为 T0H=375(3/8)、
|
||||
* T1H=750(6/8);此接口仅用于校准解码阈值不标准的电调。下一帧生效。
|
||||
* @param dev DShot 设备。
|
||||
* @param t0h_permille '0' 位高电平占位周期的千分比。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int dshot_set_duty_permille(const struct device *dev, uint32_t t0h_permille);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ZEPHYR_INCLUDE_DRIVERS_DSHOT_H_ */
|
||||
Reference in New Issue
Block a user