Adjustments for ADC calibration and bugfix in MPPT algorithm
This commit is contained in:
@@ -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
|
||||
|
||||

|
||||
|
||||
## 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.
|
||||
|
||||
+11
-3
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
+1
-1
@@ -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) }
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
+7
-6
@@ -17,15 +17,13 @@
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include "mbed.h"
|
||||
#include <math.h> // 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 <math.h> // 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();
|
||||
|
||||
+7
-1
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user