Skip to content

Commit

Permalink
moved dc calibration to separate function.
Browse files Browse the repository at this point in the history
  • Loading branch information
rewolff committed Jun 28, 2014
1 parent 7be5693 commit 679739b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 16 additions & 10 deletions mcpwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,21 @@ static int try_input(void);
static WORKING_AREA(timer_thread_wa, 1024);
static msg_t timer_thread(void *arg);


void do_dc_cal (void)
{
DCCAL_ON();
chThdSleepMilliseconds(1000);
curr0_sum = 0;
curr1_sum = 0;
curr_start_samples = 0;
while(curr_start_samples < 2000) {};
curr0_offset = curr0_sum / curr_start_samples;
curr1_offset = curr1_sum / curr_start_samples;
DCCAL_OFF();
}


void mcpwm_init(void) {
chSysLock();
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
Expand Down Expand Up @@ -477,15 +492,7 @@ void mcpwm_init(void) {

// Calibrate current offset
ENABLE_GATE();
DCCAL_ON();
chThdSleepMilliseconds(500);
curr0_sum = 0;
curr1_sum = 0;
curr_start_samples = 0;
while(curr_start_samples < 2000) {};
curr0_offset = curr0_sum / curr_start_samples;
curr1_offset = curr1_sum / curr_start_samples;
DCCAL_OFF();
do_dc_cal ();

// Various time measurements
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
Expand Down Expand Up @@ -569,7 +576,6 @@ void mcpwm_set_current(float current) {

if (state != MC_STATE_RUNNING) {
set_duty_cycle_hl(SIGN(current)*(MCPWM_MIN_DUTY_CYCLE + 0.001));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void utils_norm_angle(float *angle);
int utils_truncate_number(float *number, float min, float max);
float utils_map(float x, float in_min, float in_max, float out_min, float out_max);

// return the sign of the argument. -1 if negative, 1 if 0 or positive.
// return the sign of the argument. -1 if negative, 1 if zero or positive.
#define SIGN(x) ((x<0)?-1:1)

#endif /* UTILS_H_ */

0 comments on commit 679739b

Please sign in to comment.