Files
Misaki af5987b37b first
2026-10-27 00:34:27 +08:00

47 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* DShot 使用示例 —— 上电解锁电调,然后缓慢推油门。
*
* 电调上电时必须收到一段时间的 0(DSHOT_CMD_MOTOR_STOP)才会 arm,
* 之后油门值 48..2047 生效。
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/dshot.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
static const struct device *const dshot = DEVICE_DT_GET(DT_ALIAS(dshot0));
int main(void)
{
if (!device_is_ready(dshot)) {
LOG_ERR("dshot device not ready");
return -1;
}
/* 1. 启动帧流,所有通道输出 0,持续 2 秒让电调完成初始化 */
dshot_start(dshot);
k_sleep(K_SECONDS(2));
/* 2. 可选:蜂鸣一声确认连接(BEEP1,重复 50 帧) */
dshot_send_command(dshot, 0, DSHOT_CMD_BEEP1, 50);
k_sleep(K_MSEC(500));
/* 3. 通道 0 推到 10% 油门 */
dshot_set_throttle(dshot, 0, 250, false);
LOG_INF("motor 0 at ~12%% throttle");
/* 4. 5 秒后请求一次遥测(遥测位只影响下一帧) */
k_sleep(K_SECONDS(5));
dshot_set_throttle(dshot, 0, 250, true);
/* 5. 停车 */
k_sleep(K_SECONDS(5));
dshot_set_throttle(dshot, 0, 0, false);
LOG_INF("motor stopped");
return 0;
}