Skip to content

Commit

Permalink
Fix bug vedderb#7, detection now works while apps are active
Browse files Browse the repository at this point in the history
  • Loading branch information
vedderb committed Jun 9, 2015
1 parent 43b362f commit a89ad0f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion applications/app_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ static msg_t adc_thread(void *arg) {
ms_without_power = 0;
}
pulses_without_power_before = ms_without_power;
mcpwm_set_current(0.0);
mcpwm_set_brake_current(timeout_get_brake_current());
continue;
}

Expand Down
1 change: 1 addition & 0 deletions applications/app_ppm.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ static msg_t ppm_thread(void *arg) {
pulses_without_power = 0;
}
pulses_without_power_before = pulses_without_power;
mcpwm_set_brake_current(timeout_get_brake_current());
continue;
}

Expand Down
13 changes: 12 additions & 1 deletion conf_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ void conf_general_read_app_configuration(app_configuration *conf) {
* A pointer to the configuration that should be stored.
*/
bool conf_general_store_app_configuration(app_configuration *conf) {
mcpwm_unlock();
mcpwm_release_motor();

utils_sys_lock_cnt();
Expand Down Expand Up @@ -326,6 +327,7 @@ void conf_general_read_mc_configuration(mc_configuration *conf) {
* A pointer to the configuration that should be stored.
*/
bool conf_general_store_mc_configuration(mc_configuration *conf) {
mcpwm_unlock();
mcpwm_release_motor();

utils_sys_lock_cnt();
Expand Down Expand Up @@ -367,6 +369,9 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut
mcconf.sl_min_erpm = min_rpm;
mcpwm_set_configuration(&mcconf);

mcpwm_lock();

mcpwm_lock_override_once();
mcpwm_set_current(current);

// Spin up the motor
Expand All @@ -380,6 +385,7 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut
}

// Release the motor and wait a few commutations
mcpwm_lock_override_once();
mcpwm_set_current(0.0);
int tacho = mcpwm_get_tachometer_value(0);
for (int i = 0;i < 2000;i++) {
Expand Down Expand Up @@ -414,6 +420,8 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut
break;
}
}

mcpwm_lock_override_once();
mcpwm_set_duty(low_duty);

// Average the cycle integrator for 100 commutations
Expand All @@ -435,7 +443,8 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut
float avg_cycle_integrator_running = mcpwm_read_reset_avg_cycle_integrator();
float rpm = rpm_sum / rpm_iterations;

mcpwm_set_current(0.0);
mcpwm_lock_override_once();
mcpwm_release_motor();

// Try to figure out the coupling factor
avg_cycle_integrator_running -= *int_limit;
Expand All @@ -446,5 +455,7 @@ bool conf_general_detect_motor_param(float current, float min_rpm, float low_dut
// Restore settings
mcpwm_set_configuration(&mcconf_old);

mcpwm_unlock();

return ok_steps == 5 ? true : false;
}
3 changes: 3 additions & 0 deletions flash_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ uint16_t flash_helper_erase_new_app(uint32_t new_app_size) {

new_app_size += flash_addr[NEW_APP_BASE];

mcpwm_unlock();
mcpwm_release_motor();
utils_sys_lock_cnt();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, DISABLE);
Expand All @@ -116,6 +117,7 @@ uint16_t flash_helper_write_new_app_data(uint32_t offset, uint8_t *data, uint32_
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

mcpwm_unlock();
mcpwm_release_motor();
utils_sys_lock_cnt();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, DISABLE);
Expand All @@ -139,6 +141,7 @@ uint16_t flash_helper_write_new_app_data(uint32_t offset, uint8_t *data, uint32_
void flash_helper_jump_to_bootloader(void) {
typedef void (*pFunction)(void);

mcpwm_unlock();
mcpwm_release_motor();
usbDisconnectBus(&USBD1);
usbStop(&USBD1);
Expand Down
38 changes: 37 additions & 1 deletion mcpwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ static volatile float amp_seconds_charged;
static volatile float watt_seconds;
static volatile float watt_seconds_charged;
static volatile bool dccal_done;
static volatile bool lock_enabled;
static volatile bool lock_override_once;

// KV FIR filter
#define KV_FIR_TAPS_BITS 7
Expand Down Expand Up @@ -212,6 +214,8 @@ void mcpwm_init(mc_configuration *configuration) {
watt_seconds = 0.0;
watt_seconds_charged = 0.0;
dccal_done = false;
lock_enabled = false;
lock_override_once = false;

mcpwm_init_hall_table(conf.hall_dir, conf.hall_fwd_add, conf.hall_rev_add);

Expand Down Expand Up @@ -499,6 +503,7 @@ void mcpwm_set_configuration(mc_configuration *configuration) {
void mcpwm_init_hall_table(int dir, int fwd_add, int rev_add) {
const int comms1[8] = {-1,1,3,2,5,6,4,-1};
const int comms2[8] = {-1,1,5,6,3,2,4,-1};
// const int fwd_to_rev[7] = {-1,4,3,2,1,6,5};

memcpy(hall_to_phase_table, dir ? comms1 : comms2, sizeof(int[8]));
memcpy(hall_to_phase_table + 8, dir ? comms2 : comms1, sizeof(int[8]));
Expand Down Expand Up @@ -697,6 +702,27 @@ void mcpwm_release_motor(void) {
mcpwm_set_current(0.0);
}

/**
* Lock the control by disabling all control commands.
*/
void mcpwm_lock(void) {
lock_enabled = true;
}

/**
* Unlock all control commands.
*/
void mcpwm_unlock(void) {
lock_enabled = false;
}

/**
* Allow just one motor control command in the locked state.
*/
void mcpwm_lock_override_once(void) {
lock_override_once = true;
}

/**
* Get the electrical position (or commutation step) of the motor.
*
Expand Down Expand Up @@ -1064,7 +1090,17 @@ static int try_input(void) {
ignore_iterations = MCPWM_DETECT_STOP_TIME;
}

return ignore_iterations;
int retval = ignore_iterations;

if (!ignore_iterations && lock_enabled) {
if (!lock_override_once) {
retval = 1;
} else {
lock_override_once = false;
}
}

return retval;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions mcpwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ void mcpwm_set_current(float current);
void mcpwm_set_brake_current(float current);
void mcpwm_brake_now(void);
void mcpwm_release_motor(void);
void mcpwm_lock(void);
void mcpwm_unlock(void);
void mcpwm_lock_override_once(void);
int mcpwm_get_comm_step(void);
float mcpwm_get_duty_cycle_set(void);
float mcpwm_get_duty_cycle_now(void);
Expand Down
5 changes: 5 additions & 0 deletions timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ systime_t timeout_get_timeout_msec(void) {
return timeout_msec;
}

float timeout_get_brake_current(void) {
return timeout_brake_current;
}

static msg_t timeout_thread(void *arg) {
(void)arg;

chRegSetThreadName("Timeout");

for(;;) {
if (timeout_msec != 0 && chTimeElapsedSince(last_update_time) > MS2ST(timeout_msec)) {
mcpwm_unlock();
mcpwm_set_brake_current(timeout_brake_current);
has_timeout = true;
} else {
Expand Down
1 change: 1 addition & 0 deletions timeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ void timeout_configure(systime_t timeout, float brake_current);
void timeout_reset(void);
bool timeout_has_timeout(void);
systime_t timeout_get_timeout_msec(void);
float timeout_get_brake_current(void);

#endif /* TIMEOUT_H_ */

0 comments on commit a89ad0f

Please sign in to comment.