Skip to content

Commit eb57436

Browse files
committed
Optimize dshot
1 parent 0625612 commit eb57436

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

src/hal/actuator/actuator.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,7 @@ uint16_t dshot_pack_frame(uint16_t value, bool telemetry)
133133

134134
uint16_t dshot_throttle_to_value(float norm_throttle)
135135
{
136-
if (norm_throttle < 0.0f) {
137-
norm_throttle = 0.0f;
138-
} else if (norm_throttle > 1.0f) {
139-
norm_throttle = 1.0f;
140-
}
141-
142-
return (uint16_t)(DSHOT_MIN_THROTTLE + (uint16_t)(norm_throttle * DSHOT_RANGE));
136+
return (uint16_t)(DSHOT_MIN_THROTTLE + (uint16_t)(constrain_float(norm_throttle, 0.0f, 1.0f) * DSHOT_RANGE));
143137
}
144138

145139
/**

src/module/syscmd/cmd_act.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,25 @@ static void show_get_usage(void)
5353
SHELL_OPTION("-c, --channel", "Selected channel in hex, e.g, f means channel 1 to channel 4.");
5454
}
5555

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)
56+
static rt_size_t act_device_write(actuator_dev_t act_dev, rt_uint16_t chan_sel, const rt_uint16_t* chan_val, rt_size_t size)
5757
{
58-
actuator_dev_t act_dev = (actuator_dev_t)dev;
5958
rt_size_t ret = size;
6059

6160
/* Suspend controller output */
6261
mcn_suspend(MCN_HUB(control_output));
6362

6463
if (act_dev->config.protocol == ACT_PROTOCOL_PWM) {
65-
ret = rt_device_write(dev, chan_sel, chan_val, size);
64+
ret = rt_device_write(&act_dev->parent, chan_sel, chan_val, size);
6665
} else if (act_dev->config.protocol == ACT_PROTOCOL_DSHOT) {
67-
6866
printf("Press any key to stop...\n");
69-
7067
while (1) {
7168
/* type any key to exit */
7269
if (syscmd_has_input()) {
7370
syscmd_flush();
7471
break;
7572
}
7673

77-
if (rt_device_write(dev, chan_sel, chan_val, size) != size) {
74+
if (rt_device_write(&act_dev->parent, chan_sel, chan_val, size) != size) {
7875
printf("dshot write failed!\n");
7976
ret = 0;
8077
break;
@@ -109,7 +106,7 @@ static int set(int argc, struct optparse options)
109106
uint8_t all = 0;
110107
uint16_t chan_sel = 0;
111108
uint16_t chan_val[16] = { 0 };
112-
rt_device_t dev = NULL;
109+
actuator_dev_t dev = NULL;
113110

114111
FMS_Out_Bus fms_out;
115112
if (mcn_copy_from_hub(MCN_HUB(fms_output), &fms_out) == FMT_EOK) {
@@ -132,7 +129,7 @@ static int set(int argc, struct optparse options)
132129
chan_sel = 0xFFFF;
133130
break;
134131
case 'd':
135-
dev = rt_device_find(options.optarg);
132+
dev = (actuator_dev_t)rt_device_find(options.optarg);
136133
if (dev == NULL) {
137134
printf("Can not find device:%s\n", options.optarg);
138135
return EXIT_FAILURE;
@@ -154,6 +151,7 @@ static int set(int argc, struct optparse options)
154151

155152
int arg_c = argc - options.optind;
156153
int chan_num = 0;
154+
uint16_t val;
157155

158156
for (int i = 0; i < 16; i++) {
159157
if (chan_sel & (1 << i)) {
@@ -167,7 +165,6 @@ static int set(int argc, struct optparse options)
167165
}
168166

169167
if (all) {
170-
uint16_t val;
171168
arg = optparse_arg(&options);
172169
if (arg == NULL) {
173170
show_set_usage();
@@ -176,6 +173,10 @@ static int set(int argc, struct optparse options)
176173
val = atoi(arg);
177174

178175
for (int i = 0; i < 16; i++) {
176+
if (val < dev->range[0] || val > dev->range[1]) {
177+
printf("Invalid value! The allowed range is [%d, %d]\n", dev->range[0], dev->range[1]);
178+
return EXIT_FAILURE;
179+
}
179180
chan_val[i] = val;
180181
}
181182

@@ -188,7 +189,13 @@ static int set(int argc, struct optparse options)
188189
show_set_usage();
189190
return EXIT_FAILURE;
190191
}
191-
chan_val[index++] = atoi(arg);
192+
193+
val = atoi(arg);
194+
if (val < dev->range[0] || val > dev->range[1]) {
195+
printf("the allowed range is [%d, %d]\n", dev->range[0], dev->range[1]);
196+
return EXIT_FAILURE;
197+
}
198+
chan_val[index++] = val;
192199
}
193200
}
194201

target/sieon/s1/drivers/drv_act.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ rt_size_t main_act_write(actuator_dev_t dev, rt_uint16_t chan_sel, const rt_uint
853853
/* Handle dshot command */
854854
dshot_val = val;
855855
} else {
856-
float norm_throttle = (val > 1000) ? ((float)(val - 1000) / 1000.0f) : 0.0f;
856+
float norm_throttle = (float)(val - 1000) / 1000.0f;
857857
dshot_val = dshot_throttle_to_value(norm_throttle);
858858
}
859859

0 commit comments

Comments
 (0)