diff --git a/firmware/src/can_app.c b/firmware/src/can_app.c index 1dda581..a321fe0 100644 --- a/firmware/src/can_app.c +++ b/firmware/src/can_app.c @@ -78,7 +78,7 @@ inline void can_app_send_measurements(void) msg.length = CAN_MSG_MCB19_1_MEASUREMENTS_LENGTH; msg.flags.rtr = 0; - measurements.io_avg = 100 * measurements.vo_avg_sum / measurements.vo_avg_sum_count; + measurements.vo_avg = 100 * measurements.vo_avg_sum / measurements.vo_avg_sum_count; measurements.io_avg = 100 * measurements.io_avg_sum / measurements.io_avg_sum_count; measurements.vi_avg = 100 * measurements.vi_avg_sum / measurements.vi_avg_sum_count; measurements.dt_avg = 255 * measurements.dt_avg_sum / measurements.dt_avg_sum_count; diff --git a/firmware/src/can_app.h b/firmware/src/can_app.h index 70dc2f0..42f88a3 100644 --- a/firmware/src/can_app.h +++ b/firmware/src/can_app.h @@ -32,8 +32,8 @@ void can_app_extractor_mic17_msc(can_t *msg); void check_can(void); #ifdef CAN_ON -#define CAN_APP_SEND_STATE_CLK_DIV CAN_APP_SEND_STATE_FREQ -#define CAN_APP_SEND_MEASUREMENTS_CLK_DIV CAN_APP_SEND_MEASUREMENTS_FREQ +#define CAN_APP_SEND_STATE_CLK_DIV (MACHINE_TIMER_FREQUENCY / CAN_MSG_MCB19_2_STATE_FREQUENCY) +#define CAN_APP_SEND_MEASUREMENTS_CLK_DIV (MACHINE_TIMER_FREQUENCY / CAN_MSG_MCB19_2_MEASUREMENTS_FREQUENCY) #else #define CAN_APP_SEND_STATE_CLK_DIV 1 #define CAN_APP_SEND_MEASUREMENTS_CLK_DIV 1 diff --git a/firmware/src/conf.h b/firmware/src/conf.h index 6044289..df65482 100644 --- a/firmware/src/conf.h +++ b/firmware/src/conf.h @@ -26,7 +26,7 @@ #define VERBOSE_ON_ERROR //#define PRINT_INFOS -#define CAN_SIGNATURE_SELF CAN_SIGNATURE_MCB19_1 +#define CAN_SIGNATURE_SELF CAN_SIGNATURE_MCB19_2 // MODULES ACTIVATION #define USART_ON @@ -42,8 +42,8 @@ #ifdef CONTROL_ON #define D_MIN 0 #define D_MAX 0.9 -#define VO_SETPOINT 14 -#define IO_MAX 8 +#define VO_SETPOINT 14.6 +#define IO_MAX 10 #define VO_MAX 16 #define VI_MIN 21 #define PERIOD 0.0009960853844391542f;// 0.0001995171684523453f; // 0.0002 // 5kHz @@ -59,10 +59,8 @@ #endif #ifdef MACHINE_ON -#define MACHINE_TIMER_FREQUENCY 300 // CTC with TOP on OCR2 - TCCR2A = (1 << WGM21) | (0 << WGM20) // mode 2 - | (0 << COM2B1) | (0 << COM2B0) // do nothing - | (0 << COM2A1) | (0 << COM2A0); // do nothing - - TCCR2B = -#if MACHINE_TIMER_PRESCALER == 1 - (0 << CS22) | (0 << CS21) | (1 << CS20) // Prescaler N=1 -#elif MACHINE_TIMER_PRESCALER == 8 - (0 << CS22) | (1 << CS21) | (0 << CS20) // Prescaler N=8 -#elif MACHINE_TIMER_PRESCALER == 32 - (0 << CS22) | (1 << CS21) | (1 << CS20) // Prescaler N=32 -#elif MACHINE_TIMER_PRESCALER == 64 - (1 << CS22) | (0 << CS21) | (0 << CS20) // Prescaler N=64 -#elif MACHINE_TIMER_PRESCALER == 128 - (1 << CS22) | (0 << CS21) | (1 << CS20) // Prescaler N=128 -#elif MACHINE_TIMER_PRESCALER == 256 - (1 << CS22) | (1 << CS21) | (0 << CS20) // Prescaler N=256 -#elif MACHINE_TIMER_PRESCALER == 1024 - (1 << CS22) | (1 << CS21) | (1 << CS20) // Prescaler N=1024 + // clr_bit(PRR0, PRTIM2); // Activates clock + + // MODE 2 -> CTC with TOP on OCR1 + TCCR2A = (1 << WGM21) | (0 << WGM20) // mode 2 + | (0 << COM2B1) | (0 << COM2B0) // do nothing + | (0 << COM2A1) | (0 << COM2A0); // do nothing + + TCCR2B = +#if MACHINE_TIMER_PRESCALER == 1 + (0 << CS22) | (0 << CS21) | (1 << CS20) // Prescaler N=1 +#elif MACHINE_TIMER_PRESCALER == 8 + (0 << CS22) | (1 << CS21) | (0 << CS20) // Prescaler N=8 +#elif MACHINE_TIMER_PRESCALER == 32 + (0 << CS22) | (1 << CS21) | (1 << CS20) // Prescaler N=32 +#elif MACHINE_TIMER_PRESCALER == 64 + (1 << CS22) | (0 << CS21) | (0 << CS20) // Prescaler N=64 +#elif MACHINE_TIMER_PRESCALER == 128 + (1 << CS22) | (0 << CS21) | (1 << CS20) // Prescaler N=128 +#elif MACHINE_TIMER_PRESCALER == 256 + (1 << CS22) | (1 << CS21) | (0 << CS20) // Prescaler N=256 +#elif MACHINE_TIMER_PRESCALER == 1024 + (1 << CS22) | (1 << CS21) | (1 << CS20) // Prescaler N=1024 #else - 0 + 0 #endif - | (0 << WGM22); // mode 2 + | (0 << WGM22); // mode 2 - OCR2A = MACHINE_TIMER_TOP; // OCR2A = TOP = fcpu/(N*2*f) -1 + OCR2A = MACHINE_TIMER_TOP; // OCR2A = TOP = fcpu/(N*2*f) -1 - TIMSK2 |= (1 << OCIE2A); // Activates interruption + TIMSK2 |= (1 << OCIE2A); // Activates interruption + + set_machine_initial_state(); + set_state_initializing(); - set_machine_initial_state(); - set_state_initializing(); } /** @@ -117,7 +118,7 @@ inline void print_configurations(void) VERBOSE_MSG_MACHINE(usart_send_string("\nadc_f: ")); VERBOSE_MSG_MACHINE(usart_send_uint16( ADC_FREQUENCY )); VERBOSE_MSG_MACHINE(usart_send_string("\nmachine_f: ")); - VERBOSE_MSG_MACHINE(usart_send_uint16( MACHINE_FREQUENCY )); + VERBOSE_MSG_MACHINE(usart_send_uint16( MACHINE_TIMER_FREQUENCY )); VERBOSE_MSG_MACHINE(usart_send_char('\n')); } @@ -373,16 +374,6 @@ inline void machine_run(void) */ ISR(TIMER2_COMPA_vect) { - if(++machine_clk_divider == MACHINE_CLK_DIVIDER_VALUE){ - machine_clk_divider = 0; - machine_clk = 1; - - /*if(machine_clk){ - for(;;){ - pwm_reset(); - VERBOSE_MSG_ERROR(if(machine_clk) usart_send_string("\nERROR: CLOCK CONFLICT!!!\n")); - } - }*/ - } + machine_clk = 1; } diff --git a/firmware/src/machine.h b/firmware/src/machine.h index 9f1b7cd..bbc19aa 100644 --- a/firmware/src/machine.h +++ b/firmware/src/machine.h @@ -19,7 +19,7 @@ // Equations for mode 2 (CTC with TOP OCR2A) // Note the resolution. For example.. at 150hz, ICR1 = PWM_TOP = 159, so it -#define MACHINE_TIMER_TOP ((F_CPU/(2*MACHINE_TIMER_PRESCALER))/(MACHINE_TIMER_FREQUENCY) -1) +#define MACHINE_TIMER_TOP (uint8_t)(((F_CPU) / ((MACHINE_TIMER_PRESCALER) * (MACHINE_TIMER_FREQUENCY))) - 1) #include "dbg_vrb.h" #ifdef ADC_ON