Skip to content

Commit

Permalink
Merge pull request #51 from ZeniteSolar/joaomario/new_ids
Browse files Browse the repository at this point in the history
New ids and fix can frequency
  • Loading branch information
AlejoPm03 authored Jun 28, 2024
2 parents 0376f40 + 8ef5d9c commit e52ebd6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
2 changes: 1 addition & 1 deletion firmware/src/can_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/can_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions firmware/src/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -59,10 +59,8 @@
#endif

#ifdef MACHINE_ON
#define MACHINE_TIMER_FREQUENCY 300 //<! machine timer frequency in Hz
#define MACHINE_TIMER_PRESCALER 1024 //<! machine timer prescaler
#define MACHINE_FREQUENCY 100 //<! machine frequency in Hz
#define MACHINE_CLK_DIVIDER_VALUE (MACHINE_TIMER_FREQUENCY)/(MACHINE_FREQUENCY) //<! machine_run clock divider
#define MACHINE_TIMER_FREQUENCY 200U //<! machine timer frequency in Hz
#define MACHINE_TIMER_PRESCALER 1024U //<! machine timer prescaler
#endif // MACHINE_ON

#ifdef LED_ON
Expand All @@ -83,8 +81,6 @@

#ifdef CAN_ON
#define SPI_ON
#define CAN_APP_SEND_STATE_FREQ 40//36000 //<! state msg frequency in Hz
#define CAN_APP_SEND_MEASUREMENTS_FREQ 4//6000 //<! adc msg frequency in Hz

// CANBUS DEFINITONS
// ----------------------------------------------------------------------------
Expand Down
71 changes: 31 additions & 40 deletions firmware/src/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,40 @@ volatile uint8_t led_clk_div;
*/
void machine_init(void)
{
//clr_bit(PRR0, PRTIM2); // Activates clock

// MODE 2 -> 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();
}

/**
Expand Down Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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;
}

2 changes: 1 addition & 1 deletion firmware/src/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e52ebd6

Please sign in to comment.