From c9a870031cca3764caa7b7b3487ff1b0eb052f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sun, 11 Mar 2018 21:14:26 +0100 Subject: [PATCH] Adjustments for ADC calibration and bugfix in MPPT algorithm --- README.md | 7 ++++--- src/adc_dma.cpp | 14 +++++++++++--- src/adc_dma.h | 4 ++++ src/config.h | 2 +- src/data_objects.h | 1 - src/dcdc.cpp | 2 +- src/main.cpp | 13 +++++++------ src/output_can.cpp | 8 +++++++- 8 files changed, 35 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ef18c33..0e2c02c 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ # MPPT Charger Software + Software based on ARM mbed library for the LibreSolar 20 A MPPT solar charge controller ## Important remarks - Do **not** use mbed Ticker class, as it disturbs the timer settings and might cause the charge controller to crash. In addition to that, it contains a lot of overhead and is not very efficient. Timers should be programmed bare metal. Currently, TIM1 is used for PWM generation and TIM15 to trigger ADC readings. -## State machine in class ChargeController +- The PWM signal generation for the DC/DC controller inside the charge controller is done in the file dcdc.cpp. Most of the dangerous functions which could turn your MOSFETs into magic smoke are implemented here, so don't touch it unless you know what you're doing. + +## State machine of the charge controller The charge management is implemented using a state machine with the following states: @@ -26,6 +29,4 @@ This mode is only used for lead-acid batteries after several deep-discharge cycl ![Text](img/MPPT_flow_chart.png) -## DC/DC controller using class HalfBridge -The PWM signal generation for the DC/DC controller inside the charge controller is done by a separate class HalfBridge. Most of the dangerous functions which could blow up your MOSFETs are implemented here. diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 14fdd4c..1517594 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -32,6 +32,7 @@ volatile uint32_t adc_filtered[NUM_ADC_CH] = {0,0,0,0,0,0,0}; #define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST) volatile int num_adc_conversions; +bool new_reading_available = false; extern float solar_voltage; extern float battery_voltage; @@ -133,6 +134,9 @@ extern "C" void DMA1_Channel1_IRQHandler(void) adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST); } num_adc_conversions++; + if (num_adc_conversions % 100 == 0) { + new_reading_available = true; + } //DMA1->IFCR |= DMA_IFCR_CTCIF1; } DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers @@ -152,6 +156,7 @@ void setup_adc() uint32_t function = (uint32_t)NC; analogin_t _adc; analogin_t* obj = &_adc; + static bool adc_calibrated = false; for (unsigned int i = 0; i < sizeof(adc_channels)/sizeof(AnalogValue_t); i++) { @@ -191,6 +196,12 @@ void setup_adc() error("Cannot initialize ADC"); } + // ADC calibration is done only once + if (!adc_calibrated) { + adc_calibrated = true; + HAL_ADCEx_Calibration_Start(&obj->handle); + } + ADC_ChannelConfTypeDef sConfig = {0}; // Configure ADC channel @@ -232,9 +243,6 @@ void setup_adc() error("Cannot initialize ADC"); } - // calibrate ADC (doesn't matte that it's after first reading of ADC value) - HAL_ADCEx_Calibration_Start(&obj->handle); - // ADC sampling time register // 000: 1.5 ADC clock cycles // 001: 7.5 ADC clock cycles diff --git a/src/adc_dma.h b/src/adc_dma.h index 0f7f6a9..1ddd040 100644 --- a/src/adc_dma.h +++ b/src/adc_dma.h @@ -18,6 +18,10 @@ #ifndef ADC_DMA_H #define ADC_DMA_H +#include "mbed.h" + +extern bool new_reading_available; + void update_measurements(); void setup_adc_timer(); diff --git a/src/config.h b/src/config.h index dc56b03..6b2e2c8 100644 --- a/src/config.h +++ b/src/config.h @@ -85,7 +85,7 @@ static const AnalogValue_t adc_channels[] = { { 1, PIN_TEMP_INT, &temp_mosfets, 0.0 }, { 5, PIN_V_REF, &ref_voltage, 0.0 }, { 6, PIN_V_BAT, &battery_voltage, 110.0 / 10 }, // battery voltage divider 100k + 10k - { 7, PIN_V_SOLAR, &solar_voltage, 1056.0 / 56.0 }, // solar voltage divider: 100k + 5.6k + { 7, PIN_V_SOLAR, &solar_voltage, 105.6 / 5.6 }, // solar voltage divider: 100k + 5.6k { 8, PIN_I_LOAD, &load_current, 1000 / 2 / (1500.0 / 22.0) }, // op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm { 9, PIN_I_DCDC, &dcdc_current, 1000 / 2 / (1500.0 / 22.0) } }; diff --git a/src/data_objects.h b/src/data_objects.h index 4e56a35..5ab079a 100644 --- a/src/data_objects.h +++ b/src/data_objects.h @@ -79,7 +79,6 @@ typedef struct { #define TS_T_ARRAY 16 // 0x10 #define TS_T_MAP 20 // 0x14 - // ThingSet CAN special types and simple values #define TS_T_TIMESTAMP 32 // 0x20 #define TS_T_DECIMAL_FRAC 36 // 0x24 diff --git a/src/dcdc.cpp b/src/dcdc.cpp index d058eec..3af5377 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -137,7 +137,7 @@ void dcdc_start(float pwm_duty) { // Break and Dead-Time Register // MOE = 1: Main output enable - //TIM1->BDTR |= TIM_BDTR_MOE; + TIM1->BDTR |= TIM_BDTR_MOE; _enabled = true; } diff --git a/src/main.cpp b/src/main.cpp index 0e6624e..ec66b65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,15 +17,13 @@ #ifndef UNIT_TEST #include "mbed.h" +#include // log for thermistor calculation + #include "config.h" #include "data_objects.h" #include "dcdc.h" #include "charger.h" #include "adc_dma.h" - -//#include "24LCxx_I2C.h" -#include // log for thermistor calculation - #include "output.h" #include "output_can.h" @@ -92,8 +90,11 @@ int main() while(1) { - update_measurements(); - update_mppt(); + if (new_reading_available) { + update_measurements(); + update_mppt(); + new_reading_available = false; + } can_process_outbox(); can_process_inbox(); diff --git a/src/output_can.cpp b/src/output_can.cpp index db0b227..f689cb1 100644 --- a/src/output_can.cpp +++ b/src/output_can.cpp @@ -35,6 +35,10 @@ CANMsgQueue can_rx_queue; // preliminary simple CAN functions to send data to the bus for logging // Data format based on CBOR specification (except for first byte, which uses // only 6 bit to specify type and transport protocol) +// +// Protocol details: +// https://github.com/LibreSolar/ThingSet/blob/master/can.md + void can_pub_msg(DataObject_t data_obj) { union float2bytes { float f; char b[4]; } f2b; // for conversion of float to single bytes @@ -136,7 +140,9 @@ void can_pub_msg(DataObject_t data_obj) void can_send_data() { for (unsigned int i = 0; i < sizeof(dataObjects)/sizeof(DataObject_t); ++i) { - can_pub_msg(dataObjects[i]); + if (dataObjects[i].category == TS_C_OUTPUT) { + can_pub_msg(dataObjects[i]); + } } }