Skip to content

Commit 0625612

Browse files
authored
Merge pull request #196 from Firmament-Autopilot/staging/fmu-v6c
Optimize dshot
2 parents c827da8 + bc4033d commit 0625612

File tree

6 files changed

+186
-195
lines changed

6 files changed

+186
-195
lines changed

src/hal/actuator/actuator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ struct actuator_device {
8181
typedef struct actuator_device* actuator_dev_t;
8282
struct dshot_command {
8383
rt_uint16_t chan_mask; /* chan_mask: bitmask of channels to target (1<<chan) */
84-
rt_uint16_t value; /* value: DShot command value (0..47) */
84+
rt_uint16_t value[16]; /* value: DShot command value (0..47) */
85+
rt_size_t size;
8586
rt_uint8_t repeat; /* repeat: number of times to send (e.g. 6) */
8687
rt_uint16_t wait_ms; /* wait_ms: milliseconds to wait after the full sequence */
8788
};

src/module/syscmd/cmd_act.c

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
*****************************************************************************/
1616
#include <firmament.h>
1717

18+
#include "FMS.h"
19+
#include "hal/actuator/actuator.h"
1820
#include "module/syscmd/optparse.h"
1921
#include "module/syscmd/syscmd.h"
2022

23+
MCN_DECLARE(control_output);
24+
MCN_DECLARE(fms_output);
25+
2126
static void show_usage(void)
2227
{
2328
COMMAND_USAGE("act", "<command> [options]");
@@ -48,6 +53,47 @@ static void show_get_usage(void)
4853
SHELL_OPTION("-c, --channel", "Selected channel in hex, e.g, f means channel 1 to channel 4.");
4954
}
5055

56+
static rt_size_t act_device_write(rt_device_t dev, rt_uint16_t chan_sel, const rt_uint16_t* chan_val, rt_size_t size)
57+
{
58+
actuator_dev_t act_dev = (actuator_dev_t)dev;
59+
rt_size_t ret = size;
60+
61+
/* Suspend controller output */
62+
mcn_suspend(MCN_HUB(control_output));
63+
64+
if (act_dev->config.protocol == ACT_PROTOCOL_PWM) {
65+
ret = rt_device_write(dev, chan_sel, chan_val, size);
66+
} else if (act_dev->config.protocol == ACT_PROTOCOL_DSHOT) {
67+
68+
printf("Press any key to stop...\n");
69+
70+
while (1) {
71+
/* type any key to exit */
72+
if (syscmd_has_input()) {
73+
syscmd_flush();
74+
break;
75+
}
76+
77+
if (rt_device_write(dev, chan_sel, chan_val, size) != size) {
78+
printf("dshot write failed!\n");
79+
ret = 0;
80+
break;
81+
;
82+
}
83+
84+
systime_msleep(10);
85+
}
86+
} else {
87+
/* Unknown protocol */
88+
ret = 0;
89+
}
90+
91+
/* Resume controller output */
92+
mcn_resume(MCN_HUB(control_output));
93+
94+
return ret;
95+
}
96+
5197
static int set(int argc, struct optparse options)
5298
{
5399
char* arg;
@@ -65,6 +111,17 @@ static int set(int argc, struct optparse options)
65111
uint16_t chan_val[16] = { 0 };
66112
rt_device_t dev = NULL;
67113

114+
FMS_Out_Bus fms_out;
115+
if (mcn_copy_from_hub(MCN_HUB(fms_output), &fms_out) == FMT_EOK) {
116+
if (fms_out.status != VehicleStatus_Disarm) {
117+
printf("act set command only allowed when disarmed\n");
118+
return EXIT_FAILURE;
119+
}
120+
} else {
121+
printf("failed to get vehicle status\n");
122+
return EXIT_FAILURE;
123+
}
124+
68125
while ((option = optparse_long(&options, longopts, NULL)) != -1) {
69126
switch (option) {
70127
case 'h':
@@ -122,7 +179,7 @@ static int set(int argc, struct optparse options)
122179
chan_val[i] = val;
123180
}
124181

125-
rt_device_write(dev, chan_sel, chan_val, 16);
182+
act_device_write(dev, chan_sel, chan_val, 16);
126183
} else {
127184
uint8_t index = 0;
128185
for (int i = 0; i < 16; i++) {
@@ -135,7 +192,7 @@ static int set(int argc, struct optparse options)
135192
}
136193
}
137194

138-
rt_device_write(dev, chan_sel, chan_val, index);
195+
act_device_write(dev, chan_sel, chan_val, index);
139196
}
140197

141198
return EXIT_SUCCESS;

src/module/syscmd/cmd_dshot.c

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "module/syscmd/syscmd.h"
1212

1313
MCN_DECLARE(fms_output);
14+
MCN_DECLARE(control_output);
1415

1516
static void show_usage(void)
1617
{
@@ -103,59 +104,87 @@ int cmd_dshot(int argc, char** argv)
103104
}
104105

105106
actuator_dev_t act = (actuator_dev_t)dev;
107+
if (act->config.protocol != ACT_PROTOCOL_DSHOT) {
108+
console_printf("device %s protocol is not dshot\n", dev_name);
109+
return EXIT_FAILURE;
110+
}
111+
106112
chan_sel &= act->chan_mask;
107113
if (chan_sel == 0) {
108114
console_printf("no valid channels selected on %s\n", dev_name);
109115
return EXIT_FAILURE;
110116
}
111117

112118
struct dshot_command c = { 0 };
119+
uint16_t cmd_val = 0;
113120
c.chan_mask = chan_sel;
121+
c.size = 0;
114122

115123
if (STRING_COMPARE(cmd, "reverse")) {
116-
c.value = DSHOT_CMD_SPIN_DIRECTION_2;
124+
cmd_val = DSHOT_CMD_SPIN_DIRECTION_2;
117125
c.repeat = 10;
118126
c.wait_ms = 0;
119127
} else if (STRING_COMPARE(cmd, "normal")) {
120-
c.value = DSHOT_CMD_SPIN_DIRECTION_1;
128+
cmd_val = DSHOT_CMD_SPIN_DIRECTION_1;
121129
c.repeat = 10;
122130
c.wait_ms = 0;
123131
} else if (STRING_COMPARE(cmd, "save")) {
124-
c.value = DSHOT_CMD_SAVE_SETTINGS;
132+
cmd_val = DSHOT_CMD_SAVE_SETTINGS;
125133
c.repeat = 10;
126134
c.wait_ms = 35;
127135
} else if (STRING_COMPARE(cmd, "beep1")) {
128-
c.value = DSHOT_CMD_BEEP1;
136+
cmd_val = DSHOT_CMD_BEEP1;
129137
c.repeat = 1;
130138
c.wait_ms = 400;
131139
} else if (STRING_COMPARE(cmd, "beep2")) {
132-
c.value = DSHOT_CMD_BEEP2;
140+
cmd_val = DSHOT_CMD_BEEP2;
133141
c.repeat = 1;
134142
c.wait_ms = 400;
135143
} else if (STRING_COMPARE(cmd, "beep3")) {
136-
c.value = DSHOT_CMD_BEEP3;
144+
cmd_val = DSHOT_CMD_BEEP3;
137145
c.repeat = 1;
138146
c.wait_ms = 400;
139147
} else if (STRING_COMPARE(cmd, "beep4")) {
140-
c.value = DSHOT_CMD_BEEP4;
148+
cmd_val = DSHOT_CMD_BEEP4;
141149
c.repeat = 1;
142150
c.wait_ms = 400;
143151
} else if (STRING_COMPARE(cmd, "beep5")) {
144-
c.value = DSHOT_CMD_BEEP5;
152+
cmd_val = DSHOT_CMD_BEEP5;
145153
c.repeat = 1;
146154
c.wait_ms = 400;
147155
} else {
148156
console_printf("unknown command: %s\n", cmd);
149157
return EXIT_FAILURE;
150158
}
151159

152-
if (rt_device_control(dev, ACT_CMD_DSHOT_SEND, &c) != RT_EOK) {
153-
console_printf("failed to send dshot command\n");
154-
return EXIT_FAILURE;
155-
} else {
156-
console_printf("dshot %s sent to %s channel mask 0x%X\n", cmd, dev_name, c.chan_mask);
160+
for (uint16_t i = 0; i < 16; i++) {
161+
if (c.chan_mask & (1 << i)) {
162+
c.value[i] = cmd_val;
163+
c.size++;
164+
} else {
165+
c.value[i] = 0;
166+
}
157167
}
158168

169+
/* Suspend controller output */
170+
mcn_suspend(MCN_HUB(control_output));
171+
172+
for (uint16_t i = 0; i < c.repeat; i++) {
173+
if (rt_device_control(dev, ACT_CMD_DSHOT_SEND, &c) != RT_EOK) {
174+
console_printf("failed to send dshot command\n");
175+
/* Resume controller output */
176+
mcn_resume(MCN_HUB(control_output));
177+
return EXIT_FAILURE;
178+
}
179+
systime_msleep(5);
180+
}
181+
systime_msleep(c.wait_ms);
182+
183+
/* Resume controller output */
184+
mcn_resume(MCN_HUB(control_output));
185+
186+
console_printf("dshot %s sent to %s channel mask 0x%X\n", cmd, dev_name, c.chan_mask);
187+
159188
return EXIT_SUCCESS;
160189
}
161190

target/sieon/s1/board/board.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "driver/mag/qmc5883l.h"
3333
#include "driver/mtd/gd25qxx.h"
3434
#include "driver/range_finder/tofsense.h"
35-
#include "drv_actuator.h"
35+
#include "drv_act.h"
3636
#include "drv_adc.h"
3737
#include "drv_eth.h"
3838
#include "drv_fdcan.h"
@@ -399,7 +399,7 @@ void bsp_early_initialize(void)
399399
RT_CHECK(drv_spi_init());
400400

401401
/* pwm driver init */
402-
RT_CHECK(drv_actuator_init());
402+
RT_CHECK(drv_act_init());
403403

404404
/* can driver init */
405405
RT_CHECK(drv_fdcan_init());

0 commit comments

Comments
 (0)