diff --git a/applications/app_rccar.c b/applications/app_rccar.c index 03c979e8..572ad433 100644 --- a/applications/app_rccar.c +++ b/applications/app_rccar.c @@ -77,9 +77,9 @@ static msg_t rccar_thread(void *arg) { for(;;) { chEvtWaitAny((eventmask_t) 1); -#define HYST 0.1 -#define USE_PID 1 -#define PID_MAX_RPM 10000 +#define HYST 0.15 +#define USE_PID 0 +#define PID_MAX_RPM 15000 if (servodec_get_time_since_update() < 500) { float servo_val = servodec_get_servo_as_float(0); diff --git a/conf_general.h b/conf_general.h index ad1e9fcb..98765e6b 100644 --- a/conf_general.h +++ b/conf_general.h @@ -29,6 +29,7 @@ * Settings */ #define AUTO_PRINT_FAULTS 0 +#define SYSTEM_CORE_CLOCK 168000000 /* * Select only one hardware version diff --git a/hwconf/hw_40.h b/hwconf/hw_40.h index 314b5409..e30aed67 100644 --- a/hwconf/hw_40.h +++ b/hwconf/hw_40.h @@ -80,9 +80,11 @@ // ADC macros and settings // Component parameters -#define V_REG 3.3 -#define VIN_R1 33000.0 -#define VIN_R2 2200.0 +#define V_REG 3.3 +#define VIN_R1 33000.0 +#define VIN_R2 2200.0 +#define CURRENT_AMP_GAIN 10.0 +#define CURRENT_SHUNT_RES 0.001 // Input voltage #define GET_INPUT_VOLTAGE() ((V_REG / 4095.0) * (float)ADC_Value[ADC_IND_VIN_SENS] * ((VIN_R1 + VIN_R2) / VIN_R2)) diff --git a/hwconf/hw_r2.h b/hwconf/hw_r2.h index f9df99a8..4df0b256 100644 --- a/hwconf/hw_r2.h +++ b/hwconf/hw_r2.h @@ -90,9 +90,11 @@ // ADC macros and settings // Component parameters -#define V_REG 3.3 -#define VIN_R1 33000.0 -#define VIN_R2 2200.0 +#define V_REG 3.3 +#define VIN_R1 33000.0 +#define VIN_R2 2200.0 +#define CURRENT_AMP_GAIN 10.0 +#define CURRENT_SHUNT_RES 0.001 // Input voltage #define GET_INPUT_VOLTAGE() ((V_REG / 4095.0) * (float)ADC_Value[ADC_IND_VIN_SENS] * ((VIN_R1 + VIN_R2) / VIN_R2)) diff --git a/mcpwm.c b/mcpwm.c index b9e4fc06..da130cdb 100644 --- a/mcpwm.c +++ b/mcpwm.c @@ -270,7 +270,7 @@ void mcpwm_init(void) { // Time Base configuration TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseStructure.TIM_Period = 168000000 / switching_frequency_now; + TIM_TimeBaseStructure.TIM_Period = SYSTEM_CORE_CLOCK / switching_frequency_now; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; @@ -407,7 +407,7 @@ void mcpwm_init(void) { TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseStructure.TIM_Period = 168000000 / switching_frequency_now; + TIM_TimeBaseStructure.TIM_Period = SYSTEM_CORE_CLOCK / switching_frequency_now; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); @@ -454,7 +454,7 @@ void mcpwm_init(void) { // 32-bit timer for RPM measurement RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); - uint16_t PrescalerValue = (uint16_t) ((168000000 / 2) / 1000000) - 1; + uint16_t PrescalerValue = (uint16_t) ((SYSTEM_CORE_CLOCK / 2) / 1000000) - 1; // Time base configuration TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; @@ -489,7 +489,7 @@ void mcpwm_init(void) { // Various time measurements RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); - PrescalerValue = (uint16_t) ((168000000 / 2) / 10000000) - 1; + PrescalerValue = (uint16_t) ((SYSTEM_CORE_CLOCK / 2) / 10000000) - 1; // Time base configuration TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; @@ -662,7 +662,7 @@ float mcpwm_get_kv_filtered(void) { * The motor current. */ float mcpwm_get_tot_current(void) { - return last_current_sample * (3.3 / 4095.0) / (0.001 * 10.0); + return last_current_sample * (3.3 / 4095.0) / (CURRENT_SHUNT_RES * CURRENT_AMP_GAIN); } /** @@ -672,7 +672,7 @@ float mcpwm_get_tot_current(void) { * The filtered motor current. */ float mcpwm_get_tot_current_filtered(void) { - return last_current_sample_filtered * (3.3 / 4095.0) / (0.001 * 10.0); + return last_current_sample_filtered * (3.3 / 4095.0) / (CURRENT_SHUNT_RES * CURRENT_AMP_GAIN); } /** @@ -915,7 +915,7 @@ static void set_duty_cycle_hw(float dutyCycle) { MCPWM_SWITCH_FREQUENCY_MAX * fabsf(dutyCycle); } - timer_tmp.top = 168000000 / switching_frequency_now; + timer_tmp.top = SYSTEM_CORE_CLOCK / switching_frequency_now; update_adc_sample_pos(&timer_tmp); set_next_timer_settings(&timer_tmp); } @@ -1905,7 +1905,7 @@ static void set_switching_frequency(int frequency) { switching_frequency_now = frequency; mc_timer_struct timer_tmp; memcpy(&timer_tmp, (void*)&timer_struct, sizeof(mc_timer_struct)); - timer_tmp.top = 168000000 / switching_frequency_now; + timer_tmp.top = SYSTEM_CORE_CLOCK / switching_frequency_now; update_adc_sample_pos(&timer_tmp); set_next_timer_settings(&timer_tmp); } diff --git a/servo.c b/servo.c index d42a0a77..b8694812 100644 --- a/servo.c +++ b/servo.c @@ -83,7 +83,7 @@ static void servo_init_timer(void) { // TIM7 clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); - PrescalerValue = (uint16_t) ((168e6L / 2) / SERVO_CNT_SPEED) - 1; + PrescalerValue = (uint16_t) ((SYSTEM_CORE_CLOCK / 2) / SERVO_CNT_SPEED) - 1; // Time base configuration TIM_TimeBaseStructure.TIM_Period = 0xFFFF; diff --git a/servo_dec.c b/servo_dec.c index d5360548..706428cc 100644 --- a/servo_dec.c +++ b/servo_dec.c @@ -126,7 +126,7 @@ void servodec_init(void (*d_func)(void)) { /* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); - PrescalerValue = (uint16_t) ((168000000 / 2) / TIMER_FREQ) - 1; + PrescalerValue = (uint16_t) ((SYSTEM_CORE_CLOCK / 2) / TIMER_FREQ) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; diff --git a/servo_dec.h b/servo_dec.h index 3556d6d1..58828512 100644 --- a/servo_dec.h +++ b/servo_dec.h @@ -26,6 +26,7 @@ #define SERVO_DEC_H_ #include +#include // Servo function indexes #define SERVODEC_IND_STEERING 0