Skip to content

Commit 45f75f3

Browse files
committed
output: derive motor protocols from pin capabilities
1 parent d819b3b commit 45f75f3

17 files changed

Lines changed: 92 additions & 59 deletions

File tree

src/control/multi/motor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void motor_mixer_calc(void) {
191191
}
192192
}
193193

194-
if (target.brushless) {
194+
if (profile_outputs_use_protocol(OUTPUT_PROTOCOL_DSHOT)) {
195195
motor_brushless_mixer_scale_calc(state.throttle);
196196
} else {
197197
motor_brushed_mixer_scale_calc(state.throttle);

src/control/multi/turtle_mode.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static uint8_t turtle_axis = 0;
2222
static uint8_t turtle_dir = 0;
2323

2424
void turtle_mode_start() {
25-
if (!target.brushless) {
25+
if (!profile_outputs_use_protocol(OUTPUT_PROTOCOL_DSHOT)) {
2626
return;
2727
}
2828

@@ -37,15 +37,15 @@ void turtle_mode_start() {
3737
}
3838

3939
void turtle_mode_cancel() {
40-
if (!target.brushless) {
40+
if (!profile_outputs_use_protocol(OUTPUT_PROTOCOL_DSHOT)) {
4141
return;
4242
}
4343

4444
flags.turtle_ready = 0;
4545
}
4646

4747
void turtle_mode_update() {
48-
if (!target.brushless) {
48+
if (!profile_outputs_use_protocol(OUTPUT_PROTOCOL_DSHOT)) {
4949
return;
5050
}
5151

src/control/output.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,20 @@ static bool output_is_motor_value(uint8_t index, const profile_output_t *output)
102102
}
103103

104104
const target_output_t *target_output = &target.outputs[output->target_output];
105-
if (target_output->pin == PIN_NONE || (target_output->caps & OUTPUT_CAP_MOTOR) == 0) {
105+
if (target_output->pin == PIN_NONE) {
106106
return false;
107107
}
108108

109-
return output->protocol != OUTPUT_PROTOCOL_PWM || output_has_mixer_source(index, OUTPUT_SOURCE_THROTTLE);
109+
if (output->protocol == OUTPUT_PROTOCOL_DSHOT) {
110+
return (target_output->caps & OUTPUT_CAP_DSHOT) != 0;
111+
}
112+
if (output->protocol == OUTPUT_PROTOCOL_BRUSHED) {
113+
return (target_output->caps & OUTPUT_CAP_BRUSHED) != 0;
114+
}
115+
if (output->protocol == OUTPUT_PROTOCOL_PWM) {
116+
return (target_output->caps & OUTPUT_CAP_PWM) != 0 && output_has_mixer_source(index, OUTPUT_SOURCE_THROTTLE);
117+
}
118+
return false;
110119
}
111120

112121
static float output_finalize_pwm_motor(float *value, float motor_limit) {
@@ -215,14 +224,16 @@ bool output_source_configured(output_source_t source) {
215224
if (target_output->pin == PIN_NONE) {
216225
continue;
217226
}
227+
output_caps_t cap = 0;
218228
if (output->protocol == OUTPUT_PROTOCOL_DSHOT) {
219-
return target.brushless && (target_output->caps & OUTPUT_CAP_MOTOR) != 0;
220-
}
221-
if (output->protocol == OUTPUT_PROTOCOL_BRUSHED) {
222-
return !target.brushless && (target_output->caps & OUTPUT_CAP_MOTOR) != 0;
229+
cap = OUTPUT_CAP_DSHOT;
230+
} else if (output->protocol == OUTPUT_PROTOCOL_BRUSHED) {
231+
cap = OUTPUT_CAP_BRUSHED;
232+
} else if (output->protocol == OUTPUT_PROTOCOL_PWM) {
233+
cap = OUTPUT_CAP_PWM;
223234
}
224-
if (output->protocol == OUTPUT_PROTOCOL_PWM) {
225-
return (target_output->caps & (OUTPUT_CAP_MOTOR | OUTPUT_CAP_SERVO)) != 0;
235+
if ((target_output->caps & cap) != 0) {
236+
return true;
226237
}
227238
}
228239
return false;

src/core/profile.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,18 @@ const profile_t default_profile = {
596596
// the actual profile
597597
FAST_RAM profile_t profile;
598598

599-
bool profile_output_slot_uses_motor(uint8_t target_output) {
599+
bool profile_output_slot_uses_protocol(uint8_t target_output, output_protocol_t protocol) {
600600
for (uint32_t i = 0; i < MOTOR_PIN_MAX; i++) {
601-
if (profile.outputs[i].target_output == target_output &&
602-
profile.outputs[i].protocol != OUTPUT_PROTOCOL_NONE &&
603-
profile.outputs[i].protocol != OUTPUT_PROTOCOL_PWM) {
601+
if (profile.outputs[i].target_output == target_output && profile.outputs[i].protocol == protocol) {
602+
return true;
603+
}
604+
}
605+
return false;
606+
}
607+
608+
bool profile_outputs_use_protocol(output_protocol_t protocol) {
609+
for (uint32_t i = 0; i < MOTOR_PIN_MAX; i++) {
610+
if (profile.outputs[i].protocol == protocol) {
604611
return true;
605612
}
606613
}

src/core/profile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ pid_rate_t *profile_current_pid_rates();
548548
#ifndef VEHICLE_ROVER
549549
rate_t *profile_current_rates();
550550
#endif
551-
bool profile_output_slot_uses_motor(uint8_t target_output);
551+
bool profile_output_slot_uses_protocol(uint8_t target_output, output_protocol_t protocol);
552+
bool profile_outputs_use_protocol(output_protocol_t protocol);
552553
bool profile_output_slot_uses_servo(uint8_t target_output);
553554
uint32_t profile_output_count(const profile_output_t *outputs, uint32_t size);
554555
uint32_t profile_mixer_rule_count(const profile_mixer_rule_t *mixer, uint32_t size);

src/core/target.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
FAST_RAM target_t target = {
1212
.name = "unknown",
13-
.brushless = true,
1413
};
1514

1615
#define _MACRO_STR(arg) #arg
@@ -267,19 +266,25 @@ cbor_result_t cbor_encode_output_caps_t(cbor_value_t *enc, const output_caps_t *
267266
cbor_result_t res = CBOR_OK;
268267

269268
uint32_t count = 0;
270-
if (*t & OUTPUT_CAP_MOTOR) {
269+
if (*t & OUTPUT_CAP_PWM) {
271270
count++;
272271
}
273-
if (*t & OUTPUT_CAP_SERVO) {
272+
if (*t & OUTPUT_CAP_DSHOT) {
273+
count++;
274+
}
275+
if (*t & OUTPUT_CAP_BRUSHED) {
274276
count++;
275277
}
276278

277279
CBOR_CHECK_ERROR(res = cbor_encode_array(enc, count));
278-
if (*t & OUTPUT_CAP_MOTOR) {
279-
CBOR_CHECK_ERROR(res = cbor_encode_str(enc, "motor"));
280+
if (*t & OUTPUT_CAP_PWM) {
281+
CBOR_CHECK_ERROR(res = cbor_encode_str(enc, "pwm"));
282+
}
283+
if (*t & OUTPUT_CAP_DSHOT) {
284+
CBOR_CHECK_ERROR(res = cbor_encode_str(enc, "dshot"));
280285
}
281-
if (*t & OUTPUT_CAP_SERVO) {
282-
CBOR_CHECK_ERROR(res = cbor_encode_str(enc, "servo"));
286+
if (*t & OUTPUT_CAP_BRUSHED) {
287+
CBOR_CHECK_ERROR(res = cbor_encode_str(enc, "brushed"));
283288
}
284289

285290
return res;
@@ -296,10 +301,12 @@ cbor_result_t cbor_decode_output_caps_t(cbor_value_t *dec, output_caps_t *t) {
296301
const uint8_t *name;
297302
uint32_t name_len;
298303
CBOR_CHECK_ERROR(res = cbor_decode_tstr(dec, &name, &name_len));
299-
if (buf_equal_string(name, name_len, "motor")) {
300-
caps |= OUTPUT_CAP_MOTOR;
301-
} else if (buf_equal_string(name, name_len, "servo")) {
302-
caps |= OUTPUT_CAP_SERVO;
304+
if (buf_equal_string(name, name_len, "pwm")) {
305+
caps |= OUTPUT_CAP_PWM;
306+
} else if (buf_equal_string(name, name_len, "dshot")) {
307+
caps |= OUTPUT_CAP_DSHOT;
308+
} else if (buf_equal_string(name, name_len, "brushed")) {
309+
caps |= OUTPUT_CAP_BRUSHED;
303310
}
304311
}
305312

src/core/target.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ typedef enum {
3939
typedef uint8_t vehicle_flags_t;
4040

4141
typedef enum {
42-
OUTPUT_CAP_MOTOR = (1 << 0),
43-
OUTPUT_CAP_SERVO = (1 << 1),
42+
OUTPUT_CAP_PWM = (1 << 0),
43+
OUTPUT_CAP_DSHOT = (1 << 1),
44+
OUTPUT_CAP_BRUSHED = (1 << 2),
4445
} output_cap_t;
4546

4647
typedef uint8_t output_caps_t;
@@ -226,8 +227,6 @@ typedef struct {
226227
uint8_t name[32];
227228
uint8_t manufacturer[32];
228229

229-
bool brushless;
230-
231230
target_led_t leds[LED_MAX];
232231
target_serial_port_t serial_ports[SERIAL_PORT_MAX];
233232
target_serial_port_t serial_soft_ports[SERIAL_SOFT_COUNT];
@@ -261,7 +260,6 @@ typedef struct {
261260
START_STRUCT(target_t) \
262261
TSTR_MEMBER(name, 32) \
263262
TSTR_MEMBER(manufacturer, 32) \
264-
MEMBER(brushless, bool) \
265263
ARRAY_MEMBER(leds, LED_MAX, target_led_t) \
266264
INDEX_ARRAY_MEMBER(serial_ports, SERIAL_PORT_MAX, target_serial_port_t) \
267265
INDEX_ARRAY_MEMBER(serial_soft_ports, SERIAL_SOFT_COUNT, target_serial_port_t) \

src/driver/mcu/at32/motor_dshot.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ void dshot_dma_isr(const dma_device_t dev) {
109109
if (profile.motor.dshot_telemetry && dshot_phase == dshot_gpio_port_count) {
110110
// output phase done, lets swap to input
111111
for (uint32_t i = 0; i < MOTOR_PIN_MAX; i++) {
112-
if (target.outputs[i].pin == PIN_NONE || (target.outputs[i].caps & OUTPUT_CAP_MOTOR) == 0 || !profile_output_slot_uses_motor(i)) {
112+
if (target.outputs[i].pin == PIN_NONE || (target.outputs[i].caps & OUTPUT_CAP_DSHOT) == 0 ||
113+
!profile_output_slot_uses_protocol(i, OUTPUT_PROTOCOL_DSHOT)) {
113114
continue;
114115
}
115116
dshot_gpio_init_input(target.outputs[i].pin);

src/driver/mcu/at32/motor_pwm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ void motor_pwm_init() {
2626

2727
for (uint32_t i = 0; i < MOTOR_PIN_MAX; i++) {
2828
const gpio_pins_t pin = target.outputs[i].pin;
29-
if (pin == PIN_NONE || (target.outputs[i].caps & OUTPUT_CAP_MOTOR) == 0 || !profile_output_slot_uses_motor(i)) {
29+
if (pin == PIN_NONE || (target.outputs[i].caps & OUTPUT_CAP_BRUSHED) == 0 ||
30+
!profile_output_slot_uses_protocol(i, OUTPUT_PROTOCOL_BRUSHED)) {
3031
continue;
3132
}
3233

src/driver/mcu/stm32/dma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ bool dma_can_use_dma2(dma_device_t device) {
150150
}
151151

152152
#ifdef USE_MOTOR_DSHOT
153-
if (target.brushless && dshot_phase != 0) {
153+
if (dshot_phase != 0) {
154154
return false;
155155
}
156156
#endif

0 commit comments

Comments
 (0)