Skip to content

Instantly share code, notes, and snippets.

@donghee
Last active February 14, 2023 17:49
Show Gist options
  • Save donghee/ba5c1022e619be6c37ed4c88744f5265 to your computer and use it in GitHub Desktop.
Save donghee/ba5c1022e619be6c37ed4c88744f5265 to your computer and use it in GitHub Desktop.
hybrid power control in mbed 2
#include "mbed.h"
DigitalOut power_out_led(D11);
DigitalOut mppt_led(D12);
DigitalOut fuel_led(D13);
DigitalOut fuel_off(D7);
DigitalOut batt_off(D9);
AnalogIn power_out_volt(A0);
AnalogIn power_out_curr(A1);
AnalogIn mppt_volt(PB_1);
AnalogIn mppt_curr(A3);
AnalogIn fuel_volt(A4);
AnalogIn fuel_curr(A5);
Serial pc(PA_9, PA_10); //9600bps
double mppt_voltage, fuel_voltage, power_out_voltage;
double mppt_current, fuel_current, power_out_current;
void need_power_input_output() {
//power_out_led = 1;
mppt_led = 1;
//fuel_led = 1;
wait(1);
power_out_led = 0;
mppt_led = 0;
fuel_led = 0;
wait(1);
}
void read_voltages() {
double new_mppt_voltage, new_fuel_voltage, new_power_out_voltage;
double alpha = 0.001;
// Ref: voltage_current_sensor.sch
// 1) mppt_volt.read() value is percentage value.
// 2) Multiply by 3.3 is due to convert 'mppt_volt.read()' to voltage unit.
// 3) Division by 1.1 is due to the voltage divider of negative input of TLV9041(U4) opamp. https://i.imgur.com/qRKcHOF.png
// 4) Last of Multiply by 11.0 is due to the voltage divider of positive input of TLV9041(U4) opamp.
new_mppt_voltage = mppt_volt.read() * 3.3 / 1.1 * 11.0;
// Ref: dcdc.sch
// 1) To convert fuel_volt.read() to voltage unit, multiply by 3.3
// 2) Multiply by 11.0 from voltage divider (R6, R7)
new_fuel_voltage = fuel_volt.read() * 3.3 * 11.0;
// Ref: voltage_current_sensor.sch
// Same as above 'mppt_volt.read()'
new_power_out_voltage = power_out_volt.read() * 3.3 / 1.1 * 11.0;
// low pass filter
mppt_voltage = alpha * new_mppt_voltage + (1.0-alpha) * mppt_voltage;
fuel_voltage = alpha * new_fuel_voltage + (1.0-alpha) * fuel_voltage;
power_out_voltage = alpha * new_power_out_voltage + (1.0-alpha) * power_out_voltage;
}
void read_currents() {
double new_mppt_current, new_fuel_current, new_power_out_current;
double alpha = 0.001;
double fuel_current_offset = (0.055 * 0.0);
double mppt_current_offset = (0.01313 * ??); // find the ?? value to calibrate the mppt current sensor.
double power_out_current_offset = (0.01313 * ??); // // find the ?? value to calibrate the power current sensor.
//pc.printf("mppt: %.2f FUEL: %.2f OUT: %.2f \r\n", mppt_curr.read(), fuel_curr.read(), power_out_curr.read());
// Ref: voltage_current_sensor.sch
// 1) To convert mppt_curr.read() to voltage unit, multiply by 3.3
// 2) +0.5 : this is shifting voltage from opamp circuits(https://i.imgur.com/I4dknZ8.png) *alway 0.5v shift down*
// 3) -2.5 : ACS780xLRTR-150B current is sensor is Bidirectional, So minus half of 5V that voltage reference of current sensor.
// 4) power_out_current_offset: it depends on circuit of current sensor!, so you should calibarate this offset using electronic lab equipment.
// 5) 1000/13.33 is voltage to current ratio of ACS780xLRTR-150B. ACS780xLRTR-150B Bidirectional Sensitivity is 13.33 mV/A
// 6) I think, correct calculation is:
new_mppt_current = ((mppt_curr.read() * 3.3) + 0.5 - 2.5 + mppt_current_offset) * (1000 / 13.33);
// Ref: dcdc.sh
// 1) To convert fuel_curr.read() to voltage unit, multiply by 3.3
// 2) -0.23 is offset voltage that is 230mV from datasheet of i7c4W008A120v
// 3) fuel_current_offset value is do not used.
// 4) 1000.0/55.0 is voltage to current ratio of i7c4W008A120v.
new_fuel_current = (fuel_curr.read() * 3.3 - 0.23 + fuel_current_offset) * (1000.0/55.0); // i7c4W008A120v voltage to current
// I think, correct calculation is:
new_power_out_current = ((power_out_curr.read() * 3.3) + 0.5 - 2.5 + power_out_current_offset) * (1000 / 13.33);
// low pass filter
mppt_current = alpha * new_mppt_current + (1.0-alpha) * mppt_current;
fuel_current = alpha * new_fuel_current + (1.0-alpha) * fuel_current;
power_out_current = alpha * new_power_out_current + (1.0-alpha) * power_out_current;
}
int main() {
batt_off = 0;
fuel_off = 0;
pc.printf("Hybrid Power Controller\r\n");
Ticker voltage_reader, current_reader;
voltage_reader.attach(&read_voltages, 0.001);
current_reader.attach(&read_currents, 0.001);
while(1) {
need_power_input_output();
//pc.printf("%f, %f, %f, %f, %f, %f\r\n", mppt_volt.read(), mppt_curr.read(), fuel_volt.read(), fuel_curr.read(), power_out_volt.read(), power_out_curr.read());
// pc.printf("mppt: %.2fv, %.1fa FUEL: %.1fv, %.1fa OUT: %.2fv, %.1fa\r\n", mppt_voltage, mppt_current,
// fuel_voltage, fuel_current,
// power_out_voltage, power_out_current);
pc.printf("mppt: %.2fv, %.1fa FUEL: %.1fv, %.1fa OUT: %.1fa\r\n", mppt_voltage, mppt_current,
fuel_voltage, fuel_current,
power_out_current);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment