Skip to content

Commit

Permalink
Fix the blocked code if no measurement is read from the scale
Browse files Browse the repository at this point in the history
  • Loading branch information
eamars committed Oct 11, 2023
1 parent 8bcb221 commit 3c8f9e2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
27 changes: 18 additions & 9 deletions src/charge_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ ChargeModeState_t charge_mode_wait_for_zero(ChargeModeState_t prev_state) {
scale_config.scale_handle->force_zero();
}

// Perform measurement
float current_measurement = scale_block_wait_for_next_measurement();

data_buffer.enqueue(current_measurement);
// Perform measurement (max delay 300 seconds )
float current_measurement;
if (scale_block_wait_for_next_measurement(300, &current_measurement)){
data_buffer.enqueue(current_measurement);
}

// Generate stop condition
if (data_buffer.getCounter() >= 10){
Expand All @@ -152,7 +153,7 @@ ChargeModeState_t charge_mode_wait_for_zero(ChargeModeState_t prev_state) {
}
}

// Wait for 200 for next measurement
// Wait for minimum 300 ms (but can skip if previously wait already)
vTaskDelayUntil(&last_measurement_tick, pdMS_TO_TICKS(300));
}

Expand Down Expand Up @@ -193,7 +194,8 @@ ChargeModeState_t charge_mode_wait_for_complete(ChargeModeState_t prev_state) {

// Run the PID controlled loop to start charging
// Perform the measurement
float current_weight = scale_block_wait_for_next_measurement();
float current_weight;
scale_block_wait_for_next_measurement(0, &current_weight);
current_sample_tick = xTaskGetTickCount();

float error = charge_mode_config.target_charge_weight - current_weight;
Expand Down Expand Up @@ -257,7 +259,12 @@ ChargeModeState_t charge_mode_wait_for_cup_removal(ChargeModeState_t prev_state)

// Post charge analysis (while waiting for removal of the cup)
vTaskDelay(pdMS_TO_TICKS(1000)); // Wait for other tasks to complete
float error = charge_mode_config.target_charge_weight - scale_block_wait_for_next_measurement();

// Take current measurement
float current_measurement;
scale_block_wait_for_next_measurement(0, &current_measurement);

float error = charge_mode_config.target_charge_weight - current_measurement;

// Update LED colour before moving to the next stage
// Over charged
Expand Down Expand Up @@ -299,7 +306,8 @@ ChargeModeState_t charge_mode_wait_for_cup_removal(ChargeModeState_t prev_state)
}

// Perform measurement
float current_weight = scale_block_wait_for_next_measurement();
float current_weight;
scale_block_wait_for_next_measurement(0, &current_weight);
data_buffer.enqueue(current_weight);

// Generate stop condition
Expand Down Expand Up @@ -346,7 +354,8 @@ ChargeModeState_t charge_mode_wait_for_cup_return(ChargeModeState_t prev_state)
}

// Perform measurement
float current_weight = scale_block_wait_for_next_measurement();
float current_weight;
scale_block_wait_for_next_measurement(0, &current_weight);
if (current_weight >= 0) {
break;
}
Expand Down
26 changes: 23 additions & 3 deletions src/scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,29 @@ float scale_get_current_measurement() {
}


float scale_block_wait_for_next_measurement() {
/*
Block wait for the next available measurement.
block_time_ms set to 0 to wait indefinitely.
*/
bool scale_block_wait_for_next_measurement(uint32_t block_time_ms, float * current_measurement) {
TickType_t delay_ticks;

if (block_time_ms == 0) {
delay_ticks = portMAX_DELAY;
}
else {
delay_ticks = pdMS_TO_TICKS(block_time_ms);
}

// You can only call this once the scheduler starts
xSemaphoreTake(scale_config.scale_measurement_ready, portMAX_DELAY);
return scale_get_current_measurement();
if (xSemaphoreTake(scale_config.scale_measurement_ready, delay_ticks) == pdTRUE){
*current_measurement = scale_get_current_measurement();

return true;
}

// No valid measurement
return false;
}

2 changes: 1 addition & 1 deletion src/scale.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extern "C" {
// Scale related calls
bool scale_init();
float scale_get_current_measurement();
float scale_block_wait_for_next_measurement();
bool scale_block_wait_for_next_measurement(uint32_t block_time_ms, float * current_measurement);
void set_scale_unit(scale_unit_t scale_unit);
void set_scale_driver(scale_driver_t scale_driver);
const char * get_scale_unit_string(bool);
Expand Down

0 comments on commit 3c8f9e2

Please sign in to comment.