Skip to content

Commit

Permalink
set the correct adc settings
Browse files Browse the repository at this point in the history
add the current calculation
change the name of get_uv_voltage to get_uv_current
return the Uint16 uv_current_ma in the GetHepaUVState response message
  • Loading branch information
vegano1 committed Mar 27, 2024
1 parent 4f49754 commit 01587a4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions hepa-uv/firmware/uv_control_task/uv_control_hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

using namespace uv_control_hardware;

auto UVControlHardware::get_uv_light_voltage() -> uint32_t {
return get_uv_light_voltage_reading();
auto UVControlHardware::get_uv_light_current() -> uint16_t {
return get_uv_current_reading();
}
32 changes: 20 additions & 12 deletions hepa-uv/firmware/uv_hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ static void MX_ADC_Init(ADC_TypeDef* adc, uint32_t channel) {
// Configure Regular Channel
hadc_sConfig->Channel = channel;
hadc_sConfig->Rank = ADC_REGULAR_RANK_1;
hadc_sConfig->SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
hadc_sConfig->SingleDiff = ADC_DIFFERENTIAL_ENDED;
hadc_sConfig->SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
hadc_sConfig->SingleDiff = ADC_SINGLE_ENDED;
hadc_sConfig->OffsetNumber = ADC_OFFSET_NONE;
hadc_sConfig->Offset = 0;
if (HAL_ADC_ConfigChannel(hadc, hadc_sConfig) != HAL_OK)
Expand All @@ -75,15 +75,20 @@ static void MX_ADC_Init(ADC_TypeDef* adc, uint32_t channel) {
}

uint32_t get_adc_reading(ADC_HandleTypeDef* hadc, uint32_t channel) {
// Calibrate the ADC to get more accurate reading
if (HAL_ADCEx_Calibration_Start(hadc, ADC_DIFFERENTIAL_ENDED) != HAL_OK) Error_Handler();
ADC_ChannelConfTypeDef* hadc_sConfig;
if (hadc->Instance == ADC1) {
hadc_sConfig = &hadc1_sConfig;
} else {
Error_Handler();
return 0;
}

// Calibrate the ADC to get a more accurate reading
if (HAL_ADCEx_Calibration_Start(hadc, ADC_SINGLE_ENDED) != HAL_OK) Error_Handler();

// Configure the Channel
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = channel;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;
if (HAL_ADC_ConfigChannel(hadc, &sConfig) != HAL_OK) Error_Handler();
hadc_sConfig->Channel = channel;
if (HAL_ADC_ConfigChannel(hadc, hadc_sConfig) != HAL_OK) Error_Handler();

// Start The adc
if (HAL_ADC_Start(hadc) != HAL_OK) Error_Handler();
Expand All @@ -94,10 +99,13 @@ uint32_t get_adc_reading(ADC_HandleTypeDef* hadc, uint32_t channel) {
return HAL_ADC_GetValue(hadc);
}

uint32_t get_uv_light_voltage_reading(void) {
uint16_t get_uv_current_reading(void) {
uint32_t adc_reading = get_adc_reading(&hadc1, ADC_CHANNEL_1);
// mvolts = (ADC Reading * System mV) / 12-bit adc resolution
return (uint32_t)(adc_reading*3300)/4095;;
/*
We calculate the current in mA the uv ballast is drawing as followed
current mA = (ADC Reading * System mV) / 12-bit adc resolution / 50 Op-amp gain / 0.025 Rsns
*/
return (uint16_t)((adc_reading*3300)/4095)/50/0.025;
}

void initialize_adc_hardware() {
Expand Down
4 changes: 2 additions & 2 deletions include/can/core/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,15 +1709,15 @@ struct GetHepaUVStateResponse
uint32_t timeout_s;
uint8_t uv_light_on;
uint32_t remaining_time_s;
uint32_t uv_voltage_mv;
uint16_t uv_current_ma;

template <bit_utils::ByteIterator Output, typename Limit>
auto serialize(Output body, Limit limit) const -> uint8_t {
auto iter = bit_utils::int_to_bytes(message_index, body, limit);
iter = bit_utils::int_to_bytes(timeout_s, iter, limit);
iter = bit_utils::int_to_bytes(uv_light_on, iter, limit);
iter = bit_utils::int_to_bytes(remaining_time_s, iter, limit);
iter = bit_utils::int_to_bytes(uv_voltage_mv, iter, limit);
iter = bit_utils::int_to_bytes(uv_current_ma, iter, limit);
return iter - body;
}

Expand Down
2 changes: 1 addition & 1 deletion include/hepa-uv/core/interfaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ class UVControlInterface {
auto operator=(const UVControlInterface&) -> UVControlInterface& = delete;
virtual ~UVControlInterface() = default;

virtual auto get_uv_light_voltage() -> uint32_t = 0;
virtual auto get_uv_light_current() -> uint16_t = 0;
};
} // namespace uv_control
9 changes: 5 additions & 4 deletions include/hepa-uv/core/uv_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ class UVMessageHandler {
}

void visit(const can::messages::GetHepaUVStateRequest &m) {
uv_current_ma = uv_hardware.get_uv_light_current();
auto resp = can::messages::GetHepaUVStateResponse{
.message_index = m.message_index,
.timeout_s = uv_off_timeout_s,
.uv_light_on = uv_light_on,
.remaining_time_s = (_timer.get_remaining_time() / 1000),
.uv_voltage_mv = uv_voltage_mv};
.uv_current_ma = uv_current_ma};
can_client.send_can_message(can::ids::NodeId::host, resp);
}

Expand Down Expand Up @@ -125,7 +126,7 @@ class UVMessageHandler {
}
uv_push_button = false;
uv_light_on = false;
uv_voltage_mv = 0;
uv_current_ma = 0;
return;
}

Expand Down Expand Up @@ -153,7 +154,7 @@ class UVMessageHandler {
}

// Update the voltage usage of the uv light
uv_voltage_mv = uv_hardware.get_uv_light_voltage();
uv_current_ma = uv_hardware.get_uv_light_current();

// TODO: send state change CAN message to host
}
Expand All @@ -164,7 +165,7 @@ class UVMessageHandler {
bool uv_push_button = false;
bool uv_light_on = false;
uint32_t uv_off_timeout_s = DELAY_S;
uint32_t uv_voltage_mv = 0;
uint16_t uv_current_ma = 0;

gpio_drive_hardware::GpioDrivePins &drive_pins;
uv_control_hardware::UVControlHardware &uv_hardware;
Expand Down
2 changes: 1 addition & 1 deletion include/hepa-uv/firmware/uv_control_hardware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UVControlHardware : public uv_control::UVControlInterface {
// lets migrate gpio.set/reset to it, so we can de-couple hardware
// specific functionality from logic so we can fix simulation.

auto get_uv_light_voltage() -> uint32_t final;
auto get_uv_light_current() -> uint16_t final;
};

} // namespace uv_control_hardware
2 changes: 1 addition & 1 deletion include/hepa-uv/firmware/uv_hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern "C" {
#endif // __cplusplus

uint32_t get_uv_light_voltage_reading(void);
uint16_t get_uv_current_reading(void);

#ifdef __cplusplus
} // extern "C"
Expand Down

0 comments on commit 01587a4

Please sign in to comment.