Skip to content

Commit fcb7e12

Browse files
committed
Made servo output a config option
1 parent 69d3a1b commit fcb7e12

16 files changed

+68
-38
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* Added TEMP_SENSOR_KTY84_130.
1313
* Major UAVCAN update. See: https://github.com/vedderb/bldc/pull/269
1414
* Avoid numerical instability when mapping is done over a narrow range. See: https://github.com/vedderb/bldc/issues/262
15+
* App Balance updates.
16+
* Added servo_out_enable appconf option, so that the PPM port can be used to control servos with the default firmware.
1517

1618
=== FW 5.02 ===
1719
* IMU calibration improvement.

appconf/appconf_default.h

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#ifndef APPCONF_UAVCAN_RAW_MODE
4343
#define APPCONF_UAVCAN_RAW_MODE UAVCAN_RAW_MODE_CURRENT
4444
#endif
45+
#ifndef APPCONF_SERVO_OUT_ENABLE
46+
#define APPCONF_SERVO_OUT_ENABLE false
47+
#endif
4548
#ifndef APPCONF_SEND_CAN_STATUS_RATE_HZ
4649
#define APPCONF_SEND_CAN_STATUS_RATE_HZ 50
4750
#endif

applications/app.c

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "comm_can.h"
2727
#include "imu.h"
2828
#include "crc.h"
29+
#include "servo_simple.h"
2930

3031
// Private variables
3132
static app_configuration appconf;
@@ -70,6 +71,14 @@ void app_set_configuration(app_configuration *conf) {
7071

7172
imu_init(&conf->imu_conf);
7273

74+
if (appconf.app_to_use != APP_PPM &&
75+
appconf.app_to_use != APP_PPM_UART &&
76+
appconf.servo_out_enable) {
77+
servo_simple_init();
78+
} else {
79+
servo_simple_stop();
80+
}
81+
7382
// Configure balance app before starting it.
7483
app_balance_configure(&appconf.app_balance_conf, &appconf.imu_conf);
7584

applications/app_ppm.c

-18
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
#include "comm_can.h"
3030
#include <math.h>
3131

32-
// Only available if servo output is not active
33-
#if !SERVO_OUT_ENABLE
34-
3532
// Settings
3633
#define MAX_CAN_AGE 0.1
3734
#define MIN_PULSES_WITHOUT_POWER 50
@@ -54,10 +51,8 @@ static float input_val = 0.0;
5451
static volatile float direction_hyst = 0;
5552

5653
// Private functions
57-
#endif
5854

5955
void app_ppm_configure(ppm_config *conf) {
60-
#if !SERVO_OUT_ENABLE
6156
config = *conf;
6257
pulses_without_power = 0;
6358

@@ -66,20 +61,14 @@ void app_ppm_configure(ppm_config *conf) {
6661
}
6762

6863
direction_hyst = config.max_erpm_for_dir * 0.20;
69-
#else
70-
(void)conf;
71-
#endif
7264
}
7365

7466
void app_ppm_start(void) {
75-
#if !SERVO_OUT_ENABLE
7667
stop_now = false;
7768
chThdCreateStatic(ppm_thread_wa, sizeof(ppm_thread_wa), NORMALPRIO, ppm_thread, NULL);
78-
#endif
7969
}
8070

8171
void app_ppm_stop(void) {
82-
#if !SERVO_OUT_ENABLE
8372
stop_now = true;
8473

8574
if (is_running) {
@@ -90,18 +79,12 @@ void app_ppm_stop(void) {
9079
while(is_running) {
9180
chThdSleepMilliseconds(1);
9281
}
93-
#endif
9482
}
9583

9684
float app_ppm_get_decoded_level(void) {
97-
#if !SERVO_OUT_ENABLE
9885
return input_val;
99-
#else
100-
return 0.0;
101-
#endif
10286
}
10387

104-
#if !SERVO_OUT_ENABLE
10588
static void servodec_func(void) {
10689
ppm_rx = true;
10790
chSysLockFromISR();
@@ -532,4 +515,3 @@ static THD_FUNCTION(ppm_thread, arg) {
532515

533516
}
534517
}
535-
#endif

commands.c

-2
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,8 @@ void commands_process_packet(unsigned char *data, unsigned int len,
451451
} break;
452452

453453
case COMM_SET_SERVO_POS: {
454-
#if SERVO_OUT_ENABLE
455454
int32_t ind = 0;
456455
servo_simple_set_output(buffer_get_float16(data, 1000.0, &ind));
457-
#endif
458456
} break;
459457

460458
case COMM_SET_MCCONF: {

conf_general.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define FW_VERSION_MAJOR 5
2525
#define FW_VERSION_MINOR 03
2626
// Set to 0 for building a release and iterate during beta test builds
27-
#define FW_TEST_VERSION_NUMBER 16
27+
#define FW_TEST_VERSION_NUMBER 17
2828

2929
#include "datatypes.h"
3030

@@ -74,8 +74,8 @@
7474
//#define HW60_IS_MK4
7575
#define HW60_IS_MK5
7676

77-
//#define HW_SOURCE "hw_60.c"
78-
//#define HW_HEADER "hw_60.h"
77+
#define HW_SOURCE "hw_60.c"
78+
#define HW_HEADER "hw_60.h"
7979

8080
//#define HW_SOURCE "hw_r2.c"
8181
//#define HW_HEADER "hw_r2.h"
@@ -123,8 +123,8 @@
123123
//#define HW_SOURCE "hw_binar_v1.c"
124124
//#define HW_HEADER "hw_binar_v1.h"
125125

126-
#define HW_SOURCE "hw_hd60.c"
127-
#define HW_HEADER "hw_hd60.h"
126+
//#define HW_SOURCE "hw_hd60.c"
127+
//#define HW_HEADER "hw_hd60.h"
128128

129129
//#define HW_SOURCE "hw_hd75.c"
130130
//#define HW_HEADER "hw_hd75.h"
@@ -258,9 +258,6 @@
258258
/*
259259
* Servo output driver
260260
*/
261-
#ifndef SERVO_OUT_ENABLE
262-
#define SERVO_OUT_ENABLE 0 // Enable servo output
263-
#endif
264261
#define SERVO_OUT_PULSE_MIN_US 1000 // Minimum pulse length in microseconds
265262
#define SERVO_OUT_PULSE_MAX_US 2000 // Maximum pulse length in microseconds
266263
#define SERVO_OUT_RATE_HZ 50 // Update rate in Hz

confgenerator.c

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ int32_t confgenerator_serialize_appconf(uint8_t *buffer, const app_configuration
194194
buffer[ind++] = conf->can_mode;
195195
buffer[ind++] = (uint8_t)conf->uavcan_esc_index;
196196
buffer[ind++] = conf->uavcan_raw_mode;
197+
buffer[ind++] = conf->servo_out_enable;
197198
buffer[ind++] = conf->app_to_use;
198199
buffer[ind++] = conf->app_ppm_conf.ctrl_type;
199200
buffer_append_float32_auto(buffer, conf->app_ppm_conf.pid_max_erpm, &ind);
@@ -535,6 +536,7 @@ bool confgenerator_deserialize_appconf(const uint8_t *buffer, app_configuration
535536
conf->can_mode = buffer[ind++];
536537
conf->uavcan_esc_index = buffer[ind++];
537538
conf->uavcan_raw_mode = buffer[ind++];
539+
conf->servo_out_enable = buffer[ind++];
538540
conf->app_to_use = buffer[ind++];
539541
conf->app_ppm_conf.ctrl_type = buffer[ind++];
540542
conf->app_ppm_conf.pid_max_erpm = buffer_get_float32_auto(buffer, &ind);
@@ -860,6 +862,7 @@ void confgenerator_set_defaults_appconf(app_configuration *conf) {
860862
conf->can_mode = APPCONF_CAN_MODE;
861863
conf->uavcan_esc_index = APPCONF_UAVCAN_ESC_INDEX;
862864
conf->uavcan_raw_mode = APPCONF_UAVCAN_RAW_MODE;
865+
conf->servo_out_enable = APPCONF_SERVO_OUT_ENABLE;
863866
conf->app_to_use = APPCONF_APP_TO_USE;
864867
conf->app_ppm_conf.ctrl_type = APPCONF_PPM_CTRL_TYPE;
865868
conf->app_ppm_conf.pid_max_erpm = APPCONF_PPM_PID_MAX_ERPM;

confgenerator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// Constants
1111
#define MCCONF_SIGNATURE 2835115562
12-
#define APPCONF_SIGNATURE 3708020089
12+
#define APPCONF_SIGNATURE 1729624904
1313

1414
// Functions
1515
int32_t confgenerator_serialize_mcconf(uint8_t *buffer, const mc_configuration *conf);

datatypes.h

+1
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ typedef struct {
786786
bool pairing_done;
787787
bool permanent_uart_enabled;
788788
SHUTDOWN_MODE shutdown_mode;
789+
bool servo_out_enable;
789790

790791
// CAN modes
791792
CAN_MODE can_mode;

hwconf/hw.h

+10
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,16 @@
491491
#define HW_PAS2_PIN HW_UART_TX_PIN
492492
#endif
493493

494+
#ifndef HW_ICU_TIMER
495+
#ifdef HW_USE_SERVO_TIM4
496+
#define HW_ICU_TIMER TIM4
497+
#define HW_ICU_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE)
498+
#else
499+
#define HW_ICU_TIMER TIM3
500+
#define HW_ICU_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE)
501+
#endif
502+
#endif
503+
494504
// Functions
495505
void hw_init_gpio(void);
496506
void hw_setup_adc_channels(void);

hwconf/hw_stormcore_100d.h

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273

274274
// ICU Peripheral for servo decoding
275275
#define HW_ICU_TIMER TIM9
276+
#define HW_ICU_TIM_CLK_EN() RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE)
276277
#define HW_ICU_DEV ICUD9
277278
#define HW_ICU_CHANNEL ICU_CHANNEL_1
278279
#define HW_ICU_GPIO_AF GPIO_AF_TIM9

hwconf/hw_stormcore_60d.h

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@
301301

302302
// ICU Peripheral for servo decoding
303303
#define HW_ICU_TIMER TIM9
304+
#define HW_ICU_TIM_CLK_EN() RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE)
304305
#define HW_ICU_DEV ICUD9
305306
#define HW_ICU_CHANNEL ICU_CHANNEL_1
306307
#define HW_ICU_GPIO_AF GPIO_AF_TIM9

hwconf/hw_unity.h

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199

200200
// ICU Peripheral for servo decoding
201201
#define HW_ICU_TIMER TIM9
202+
#define HW_ICU_TIM_CLK_EN() RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE)
202203
#define HW_ICU_DEV ICUD9
203204
#define HW_ICU_CHANNEL ICU_CHANNEL_1
204205
#define HW_ICU_GPIO_AF GPIO_AF_TIM9

main.c

-4
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ int main(void) {
268268
#endif
269269
#endif
270270

271-
#if SERVO_OUT_ENABLE
272-
servo_simple_init();
273-
#endif
274-
275271
// Threads
276272
chThdCreateStatic(periodic_thread_wa, sizeof(periodic_thread_wa), NORMALPRIO, periodic_thread, NULL);
277273
chThdCreateStatic(flash_integrity_check_thread_wa, sizeof(flash_integrity_check_thread_wa), LOWPRIO, flash_integrity_check_thread, NULL);

servo_simple.c

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 Benjamin Vedder [email protected]
2+
Copyright 2016 - 2021 Benjamin Vedder [email protected]
33
44
This file is part of the VESC firmware.
55
@@ -24,10 +24,15 @@
2424
#include "stm32f4xx_conf.h"
2525
#include "utils.h"
2626

27+
/**
28+
* TODO: Use the chibios driver instead of the ST-driver.
29+
*/
30+
2731
// Settings
2832
#define TIM_CLOCK 1000000 // Hz
2933

30-
#if SERVO_OUT_ENABLE
34+
// Private variables
35+
static volatile bool m_is_running = false;
3136

3237
void servo_simple_init(void) {
3338
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
@@ -63,9 +68,28 @@ void servo_simple_init(void) {
6368
servo_simple_set_output(0.5);
6469

6570
TIM_Cmd(HW_ICU_TIMER, ENABLE);
71+
72+
m_is_running = true;
73+
}
74+
75+
void servo_simple_stop(void) {
76+
if (m_is_running) {
77+
palSetPadMode(HW_ICU_GPIO, HW_ICU_PIN, PAL_MODE_INPUT);
78+
TIM_Cmd(HW_ICU_TIMER, DISABLE);
79+
TIM_DeInit(HW_ICU_TIMER);
80+
m_is_running = false;
81+
}
82+
}
83+
84+
bool servo_simple_is_running(void) {
85+
return m_is_running;
6686
}
6787

6888
void servo_simple_set_output(float out) {
89+
if (!m_is_running) {
90+
return;
91+
}
92+
6993
utils_truncate_number(&out, 0.0, 1.0);
7094

7195
float us = (float)SERVO_OUT_PULSE_MIN_US + out *
@@ -78,5 +102,3 @@ void servo_simple_set_output(float out) {
78102
HW_ICU_TIMER->CCR2 = (uint32_t)us;
79103
}
80104
}
81-
82-
#endif

servo_simple.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 Benjamin Vedder [email protected]
2+
Copyright 2016 - 2021 Benjamin Vedder [email protected]
33
44
This file is part of the VESC firmware.
55
@@ -20,8 +20,12 @@
2020
#ifndef SERVO_SIMPLE_H_
2121
#define SERVO_SIMPLE_H_
2222

23+
#include <stdbool.h>
24+
2325
// Functions
2426
void servo_simple_init(void);
27+
void servo_simple_stop(void);
28+
bool servo_simple_is_running(void);
2529
void servo_simple_set_output(float out);
2630

2731
#endif /* SERVO_SIMPLE_H_ */

0 commit comments

Comments
 (0)