Initial version of PWM charge controller part added
This commit is contained in:
@@ -76,9 +76,13 @@ void system_control() // called by control timer (see hardware.cpp)
|
||||
// convert ADC readings to meaningful measurement values
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_port, &ls_port);
|
||||
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
pwm_chg_control(&pwm_chg, &hs_port, bat_port);
|
||||
#else
|
||||
// control PWM of the DC/DC according to hs and ls port settings
|
||||
// (this function includes MPPT algorithm)
|
||||
dcdc_control(&dcdc, &hs_port, &ls_port);
|
||||
#endif
|
||||
|
||||
load_check_overcurrent(&load);
|
||||
update_dcdc_led(half_bridge_enabled());
|
||||
@@ -215,8 +219,12 @@ void setup()
|
||||
|
||||
load_init(&load);
|
||||
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
pwm_chg_init(&pwm_chg);
|
||||
#else // MPPT
|
||||
dcdc_init(&dcdc);
|
||||
half_bridge_init(PWM_FREQUENCY, 300, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage
|
||||
#endif
|
||||
|
||||
data_objects_read_eeprom();
|
||||
ts.set_conf_callback(data_objects_update_conf); // after each configuration change, data should be written back to EEPROM
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
/* LibreSolar MPPT charge controller firmware
|
||||
* Copyright (c) 2016-2018 Martin Jäger (www.libre.solar)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __PCB_PWM_01_H_
|
||||
#define __PCB_PWM_01_H_
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
#define CHARGER_TYPE_PWM 1 // PWM charge controller instead of MPPT
|
||||
|
||||
#define PWM_TIM 3 // use TIM3 timer
|
||||
|
||||
#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current
|
||||
#define LOAD_CURRENT_MAX 20 // PCB maximum load switch current
|
||||
|
||||
#define PIN_UEXT_TX PA_2
|
||||
#define PIN_UEXT_RX PA_3
|
||||
#define PIN_UEXT_SCL PB_6
|
||||
#define PIN_UEXT_SDA PB_7
|
||||
#define PIN_UEXT_MISO PA_11
|
||||
#define PIN_UEXT_MOSI PA_12
|
||||
#define PIN_UEXT_SCK PB_3
|
||||
#define PIN_UEXT_SSEL PA_15
|
||||
|
||||
#define PIN_SWD_TX PA_9
|
||||
#define PIN_SWD_RX PA_10
|
||||
|
||||
#define PIN_LOAD_DIS PB_2
|
||||
#define PIN_USB_PWR_DIS PB_5
|
||||
|
||||
#define PIN_REF_I_DCDC PA_4
|
||||
|
||||
enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
|
||||
|
||||
// assignment LED numbers on PCB to their meaning
|
||||
#define NUM_LEDS 5
|
||||
|
||||
#define LED_RXTX 0 // LED1, used to indicate when sending data
|
||||
#define LED_SOC_1 1 // LED2
|
||||
#define LED_SOC_2 2 // LED3
|
||||
#define LED_SOC_3 3 // LED4
|
||||
#define LED_LOAD 4 // LED5
|
||||
|
||||
// LED pins and pin state configuration to switch above LEDs on
|
||||
#define NUM_LED_PINS 3
|
||||
static const PinName led_pins[NUM_LED_PINS] = {
|
||||
// A B C
|
||||
PB_13, PB_15, PB_14
|
||||
};
|
||||
static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
{ PIN_FLOAT, PIN_HIGH, PIN_LOW }, // LED1
|
||||
{ PIN_FLOAT, PIN_LOW, PIN_HIGH }, // LED2
|
||||
{ PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED3
|
||||
{ PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED4
|
||||
{ PIN_HIGH, PIN_FLOAT, PIN_LOW } // LED5
|
||||
};
|
||||
|
||||
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
|
||||
#define PIN_ADC_TEMP_BAT
|
||||
|
||||
// typical value for Semitec 103AT-5 thermistor: 3435
|
||||
#define NTC_BETA_VALUE 3435
|
||||
|
||||
#define ADC_GAIN_V_BAT (132 / 12)
|
||||
#define ADC_GAIN_V_SOLAR (1 + 120/12 + 120/8.2)
|
||||
#define ADC_GAIN_I_LOAD (1000 / 2 / (150/2.2)) // amp gain: 150/2.2, resistor: 2 mOhm
|
||||
#define ADC_GAIN_I_DCDC (1000 / 2 / (150/2.2))
|
||||
|
||||
#define ADC_OFFSET_V_SOLAR (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset
|
||||
|
||||
|
||||
// position in the array written by the DMA controller
|
||||
enum {
|
||||
ADC_POS_V_BAT, // ADC 0 (PA_0)
|
||||
ADC_POS_V_SOLAR, // ADC 1 (PA_1)
|
||||
ADC_POS_I_LOAD, // ADC 5 (PA_5)
|
||||
ADC_POS_I_DCDC, // ADC 6 (PA_6)
|
||||
ADC_POS_TEMP_BAT, // ADC 7 (PA_7)
|
||||
ADC_POS_VREF_MCU, // ADC 17
|
||||
ADC_POS_TEMP_MCU, // ADC 18
|
||||
NUM_ADC_CH // trick to get the number of elements
|
||||
};
|
||||
|
||||
// selected ADC channels (has to match with above enum)
|
||||
#define ADC_CHSEL ( \
|
||||
ADC_CHSELR_CHSEL0 | \
|
||||
ADC_CHSELR_CHSEL1 | \
|
||||
ADC_CHSELR_CHSEL5 | \
|
||||
ADC_CHSELR_CHSEL6 | \
|
||||
ADC_CHSELR_CHSEL7 | \
|
||||
ADC_CHSELR_CHSEL17 | \
|
||||
ADC_CHSELR_CHSEL18 \
|
||||
)
|
||||
|
||||
#endif
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
/* LibreSolar MPPT charge controller firmware
|
||||
* Copyright (c) 2016-2018 Martin Jäger (www.libre.solar)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "pwm_chg.h"
|
||||
#include "config.h"
|
||||
#include "pcb.h"
|
||||
|
||||
#include <time.h> // for time(NULL) function
|
||||
#include <math.h> // for fabs function
|
||||
|
||||
|
||||
// timer used for PWM generation has to be globally selected for the used PCB
|
||||
#if (PWM_TIM == 3)
|
||||
|
||||
static int _pwm_resolution;
|
||||
|
||||
static bool _enabled;
|
||||
|
||||
void pwm_chg_init_registers(int freq_Hz)
|
||||
{
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
|
||||
// Enable TIM3 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
|
||||
// Select alternate function mode on PB1 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1;
|
||||
|
||||
// Select AF2 on PB1
|
||||
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL1_Pos;
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
TIM3->PSC = SystemCoreClock / 10000;
|
||||
|
||||
// Capture/Compare Mode Register 1
|
||||
// OCxM = 110: Select PWM mode 1 on OCx
|
||||
// OCxPE = 1: Enable preload register on OCx (reset value)
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4PE;
|
||||
|
||||
// Capture/Compare Enable Register
|
||||
// CCxP: Active high polarity on OCx (default = 0)
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4P); // PB1 / TIM3_CH4: high-side
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CEN; // edge-aligned mode
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
|
||||
// set PWM frequency and resolution
|
||||
_pwm_resolution = 10000 / freq_Hz;
|
||||
|
||||
// Auto Reload Register
|
||||
TIM3->ARR = _pwm_resolution;
|
||||
}
|
||||
|
||||
void pwm_chg_set_duty_cycle(float duty)
|
||||
{
|
||||
TIM3->CCR4 = _pwm_resolution * duty;
|
||||
}
|
||||
|
||||
void pwm_chg_duty_cycle_step(int delta)
|
||||
{
|
||||
if ((int)TIM3->CCR4 + delta <= (_pwm_resolution + 1) && (int)TIM3->CCR4 + delta >= 0) {
|
||||
TIM3->CCR4 += delta;
|
||||
}
|
||||
}
|
||||
|
||||
float pwm_chg_get_duty_cycle() {
|
||||
return (float)(TIM3->CCR4) / (_pwm_resolution);;
|
||||
}
|
||||
|
||||
void pwm_chg_start(float pwm_duty)
|
||||
{
|
||||
pwm_chg_set_duty_cycle(pwm_duty);
|
||||
|
||||
// Capture/Compare Enable Register
|
||||
// CCxE = 1: Enable the output on OCx
|
||||
// CCxP = 0: Active high polarity on OCx (default)
|
||||
// CCxNE = 1: Enable the output on OC1N
|
||||
// CCxNP = 0: Active high polarity on OC1N (default)
|
||||
TIM3->CCER |= TIM_CCER_CC4E;
|
||||
|
||||
_enabled = true;
|
||||
}
|
||||
|
||||
void pwm_chg_stop()
|
||||
{
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4E);
|
||||
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
void pwm_chg_init(pwm_chg_t *pwm_chg)
|
||||
{
|
||||
pwm_chg->solar_current_max = DCDC_CURRENT_MAX;
|
||||
pwm_chg->solar_current_min = 0.05;
|
||||
|
||||
//pwm_chg->pwm_delta = 1;
|
||||
pwm_chg->off_timestamp = -10000; // start immediately
|
||||
|
||||
// calibration parameters
|
||||
//pwm_chg->offset_voltage_start = 3.0; // V charging switched on if Vsolar > Vbat + offset
|
||||
//pwm_chg->offset_voltage_stop = 1.0; // V charging switched off if Vsolar < Vbat + offset
|
||||
pwm_chg->restart_interval = 60; // s When should we retry to start charging after low solar power cut-off?
|
||||
|
||||
pwm_chg_init_registers(20);
|
||||
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
void pwm_chg_control(pwm_chg_t *pwm_chg, power_port_t *solar_port, power_port_t *bat_port)
|
||||
{
|
||||
// for testing
|
||||
//solar_port->voltage_input_start = 14.0;
|
||||
//solar_port->voltage_input_stop = 13.0;
|
||||
|
||||
//printf("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, i_max: %.2f, PWM: %.1f, chg_en: %d",
|
||||
// dcdc_power_new, dcdc->power, solar_port->voltage, bat_port->voltage, solar_port->current, bat_port->current,
|
||||
// bat_port->current_output_max, half_bridge_get_duty_cycle() * 100.0, bat_port->output_allowed);
|
||||
|
||||
if (_enabled) {
|
||||
if (bat_port->output_allowed == false || solar_port->input_allowed == false
|
||||
|| (solar_port->voltage < solar_port->voltage_input_stop && bat_port->current < 0.1))
|
||||
{
|
||||
pwm_chg_stop();
|
||||
}
|
||||
else if (bat_port->voltage > (bat_port->voltage_output_target - bat_port->droop_resistance * bat_port->current) // output voltage above target
|
||||
|| bat_port->current > bat_port->current_output_max // output current limit exceeded
|
||||
//|| (solar_port->voltage < (solar_port->voltage_input_start - solar_port->droop_resistance * solar_port->current) && bat_port->current > 0.1) // input voltage below limit
|
||||
|| solar_port->current < solar_port->current_input_max // input current (negative signs) above limit
|
||||
/*|| fabs(dcdc->ls_current) > dcdc->ls_current_max // current above hardware maximum
|
||||
|| dcdc->temp_mosfets > 80*/) // temperature limits exceeded
|
||||
{
|
||||
pwm_chg_duty_cycle_step(-1);
|
||||
}
|
||||
else {
|
||||
pwm_chg_duty_cycle_step(1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (bat_port->output_allowed == true
|
||||
&& bat_port->voltage < bat_port->voltage_output_target
|
||||
&& bat_port->voltage > bat_port->voltage_output_min
|
||||
&& solar_port->input_allowed == true
|
||||
&& solar_port->voltage > solar_port->voltage_input_start
|
||||
&& time(NULL) > (pwm_chg->off_timestamp + pwm_chg->restart_interval))
|
||||
{
|
||||
pwm_chg_start(1);
|
||||
printf("PWM charger start.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// dummy functions for non-PWM charge controllers
|
||||
void pwm_chg_init(pwm_chg_t *pwm_chg) {}
|
||||
void pwm_chg_control(pwm_chg_t *pwm_chg, power_port_t *solar_port, power_port_t *bat_port) {}
|
||||
void pwm_chg_duty_cycle_step(int delta) {}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
/* LibreSolar charge controller firmware
|
||||
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef PWM_CHG_H
|
||||
#define PWM_CHG_H
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @brief PWM charger MOSFET switch control functions
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "power_port.h"
|
||||
|
||||
/** PWM charger type
|
||||
*
|
||||
* Contains all data belonging to the DC/DC sub-component of the PCB, incl.
|
||||
* actual measurements and calibration parameters.
|
||||
*/
|
||||
typedef struct {
|
||||
// actual measurements
|
||||
float solar_current;
|
||||
float temp_mosfets;
|
||||
float solar_power;
|
||||
|
||||
float solar_current_max; // MOSFET maximum (continuous)
|
||||
float solar_current_min; // --> if lower, charger is switched off
|
||||
|
||||
int pwm_delta; // direction of PWM change
|
||||
int off_timestamp; // time when charger was switched off last time
|
||||
|
||||
// calibration parameters
|
||||
float offset_voltage_start; // V charging switched on if Vsolar > Vbat + offset
|
||||
float offset_voltage_stop; // V charging switched off if Vsolar < Vbat + offset
|
||||
int restart_interval; // s When should we retry to start charging after low solar power cut-off?
|
||||
} pwm_chg_t;
|
||||
|
||||
|
||||
void pwm_chg_init(pwm_chg_t *pwm_chg);
|
||||
|
||||
void pwm_chg_control(pwm_chg_t *pwm_chg, power_port_t *solar_port, power_port_t *bat_port);
|
||||
|
||||
void pwm_chg_duty_cycle_step(int delta);
|
||||
|
||||
#endif /* PWM_CHG_H */
|
||||
Reference in New Issue
Block a user