mirror of
https://github.com/supleed2/ELEC50003-P1-CW.git
synced 2024-11-10 01:35:50 +00:00
242 lines
7.8 KiB
Arduino
242 lines
7.8 KiB
Arduino
|
#include <Wire.h>
|
||
|
#include <INA219_WE.h>
|
||
|
#include <SPI.h>
|
||
|
#include <SD.h>
|
||
|
|
||
|
INA219_WE ina219; // this is the instantiation of the library for the current sensor
|
||
|
|
||
|
// set up variables using the SD utility library functions:
|
||
|
Sd2Card card;
|
||
|
SdVolume volume;
|
||
|
SdFile root;
|
||
|
|
||
|
const int chipSelect = 10;
|
||
|
unsigned int rest_timer;
|
||
|
unsigned int loop_trigger;
|
||
|
unsigned int int_count = 0; // a variables to count the interrupts. Used for program debugging.
|
||
|
float u0i, u1i, delta_ui, e0i, e1i, e2i; // Internal values for the current controller
|
||
|
float ui_max = 1, ui_min = 0; //anti-windup limitation
|
||
|
float kpi = 0.02512, kii = 39.4, kdi = 0; // current pid.
|
||
|
float Ts = 0.001; //1 kHz control frequency.
|
||
|
float current_measure, current_ref = 0, error_amps; // Current Control
|
||
|
float pwm_out;
|
||
|
float V_Bat;
|
||
|
boolean input_switch;
|
||
|
int state_num=0,next_state;
|
||
|
String dataString;
|
||
|
int PWM_in = 1;
|
||
|
float duty_cycle = 0;
|
||
|
int counter = 2;
|
||
|
|
||
|
int voltage_decrease = 0;
|
||
|
int voltage_increase = 0;
|
||
|
int old_state = 0;
|
||
|
float power_old = 0;
|
||
|
float power_new = 0;
|
||
|
|
||
|
void setup() {
|
||
|
//Some General Setup Stuff
|
||
|
|
||
|
Wire.begin(); // We need this for the i2c comms for the current sensor
|
||
|
Wire.setClock(700000); // set the comms speed for i2c
|
||
|
ina219.init(); // this initiates the current sensor
|
||
|
Serial.begin(9600); // USB Communications
|
||
|
|
||
|
|
||
|
//Check for the SD Card
|
||
|
Serial.println("\nInitializing SD card...");
|
||
|
if (!SD.begin(chipSelect)) {
|
||
|
Serial.println("* is a card inserted?");
|
||
|
while (true) {} //It will stick here FOREVER if no SD is in on boot
|
||
|
} else {
|
||
|
Serial.println("Wiring is correct and a card is present.");
|
||
|
}
|
||
|
|
||
|
if (SD.exists("BatCycle.csv")) { // Wipe the datalog when starting
|
||
|
SD.remove("BatCycle.csv");
|
||
|
}
|
||
|
|
||
|
|
||
|
noInterrupts(); //disable all interrupts
|
||
|
analogReference(EXTERNAL); // We are using an external analogue reference for the ADC
|
||
|
|
||
|
//SMPS Pins
|
||
|
pinMode(13, OUTPUT); // Using the LED on Pin D13 to indicate status
|
||
|
pinMode(2, INPUT_PULLUP); // Pin 2 is the input from the CL/OL switch
|
||
|
pinMode(6, OUTPUT); // This is the PWM Pin
|
||
|
|
||
|
//LEDs on pin 7 and 8
|
||
|
pinMode(7, OUTPUT);
|
||
|
pinMode(8, OUTPUT);
|
||
|
|
||
|
//Analogue input, the battery voltage (also port B voltage)
|
||
|
pinMode(A0, INPUT);
|
||
|
|
||
|
// TimerA0 initialization for 1kHz control-loop interrupt.
|
||
|
TCA0.SINGLE.PER = 999; //
|
||
|
TCA0.SINGLE.CMP1 = 999; //
|
||
|
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV16_gc | TCA_SINGLE_ENABLE_bm; //16 prescaler, 1M.
|
||
|
TCA0.SINGLE.INTCTRL = TCA_SINGLE_CMP1_bm;
|
||
|
|
||
|
// TimerB0 initialization for PWM output
|
||
|
TCB0.CTRLA = TCB_CLKSEL_CLKDIV1_gc | TCB_ENABLE_bm; //62.5kHz
|
||
|
|
||
|
interrupts(); //enable interrupts.
|
||
|
analogWrite(6, 120); //just a default state to start with
|
||
|
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
if (loop_trigger == 1){ // FAST LOOP (1kHZ)
|
||
|
state_num = next_state; //state transition
|
||
|
V_Bat = analogRead(A0)*8.192/1.03; //check the battery voltage (1.03 is a correction for measurement error, you need to check this works for you)
|
||
|
current_measure = (ina219.getCurrent_mA()); // sample the inductor current (via the sensor chip)
|
||
|
int_count++; //count how many interrupts since this was last reset to zero
|
||
|
loop_trigger = 0; //reset the trigger and move on with life
|
||
|
}
|
||
|
|
||
|
if (int_count == 100) { // SLOW LOOP (1Hz)
|
||
|
input_switch = digitalRead(2); //get the OL/CL switch status
|
||
|
switch (state_num) { // STATE MACHINE (see diagram)
|
||
|
|
||
|
case 0:{ // Start state (no current, no LEDs)
|
||
|
power_old = V_Bat * current_measure; // to get initial comparison power
|
||
|
if (input_switch == 1) { // if switch, move to charge
|
||
|
next_state = 1;
|
||
|
digitalWrite(8,true);
|
||
|
} else { // otherwise stay put
|
||
|
next_state = 0;
|
||
|
digitalWrite(8,false);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
|
||
|
case 1:{ // decrease voltage
|
||
|
|
||
|
power_new = V_Bat * current_measure; // This value is not used the first first time, the one in case 0 is used as this is overwittren in case 2 first time.
|
||
|
PWM_in = PWM_in - 1; //make a small pertubation to see how power changes
|
||
|
analogWrite(6,PWM_in); // set new PWM
|
||
|
voltage_decrease = 1;// voltage was decreased to know what state to go to
|
||
|
|
||
|
old_state = 1;
|
||
|
next_state = 2;
|
||
|
|
||
|
|
||
|
//error state
|
||
|
if(input_switch == 0){ //when turned off it does nothing
|
||
|
next_state = 0;
|
||
|
digitalWrite(8,false);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
case 2:{ // compare powers
|
||
|
|
||
|
power_new = V_Bat * current_measure; // this value is not used the first first time, the one in case 0 is used as this is overwittren in case 2 first time.
|
||
|
|
||
|
if(abs(power_new) >= abs(power_old)){
|
||
|
next_state = old_state;
|
||
|
}
|
||
|
if(abs(power_new) <= abs(power_old)){
|
||
|
if( voltage_decrease == 1){
|
||
|
next_state = 3;
|
||
|
voltage_decrease = 0;
|
||
|
}
|
||
|
if( voltage_increase == 1){
|
||
|
next_state = 1;
|
||
|
voltage_increase = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
power_old = power_new;
|
||
|
|
||
|
//error state
|
||
|
if(input_switch == 0){ //when turned off it does nothing
|
||
|
next_state = 0;
|
||
|
digitalWrite(8,false);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
|
||
|
case 3:{ // increase voltage
|
||
|
|
||
|
power_old = V_Bat * current_measure; //
|
||
|
PWM_in = PWM_in + 1; //make a small pertubation to see how power changes
|
||
|
analogWrite(6,PWM_in); // set new PWM
|
||
|
voltage_increase = 1;// voltage was decreased to know what state to go to
|
||
|
|
||
|
old_state = 3;
|
||
|
next_state = 2;
|
||
|
|
||
|
//error state
|
||
|
if(input_switch == 0){ //when turned off it does nothing
|
||
|
next_state = 0;
|
||
|
digitalWrite(8,false);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
|
||
|
default :{ // Should not end up here ....
|
||
|
Serial.println("Boop");
|
||
|
current_ref = 0;
|
||
|
next_state = 5; // So if we are here, we go to error
|
||
|
digitalWrite(7,true);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
dataString = String(state_num) + "," + String(V_Bat) + "," + String(current_measure) + "," + String(PWM_in) + "," + String(power_new) + "," + String(power_old); ; //build a datastring for the CSV file
|
||
|
Serial.println(dataString); // send it to serial as well in case a computer is connected
|
||
|
File dataFile = SD.open("BatCycle.csv", FILE_WRITE); // open our CSV file
|
||
|
if (dataFile){ //If we succeeded (usually this fails if the SD card is out)
|
||
|
dataFile.println(dataString); // print the data
|
||
|
} else {
|
||
|
Serial.println("File not open"); //otherwise print an error
|
||
|
}
|
||
|
dataFile.close(); // close the file
|
||
|
int_count = 0; // reset the interrupt count so we dont come back here for 1000ms
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Timer A CMP1 interrupt. Every 1000us the program enters this interrupt. This is the fast 1kHz loop
|
||
|
ISR(TCA0_CMP1_vect) {
|
||
|
loop_trigger = 1; //trigger the loop when we are back in normal flow
|
||
|
TCA0.SINGLE.INTFLAGS |= TCA_SINGLE_CMP1_bm; //clear interrupt flag
|
||
|
}
|
||
|
|
||
|
float saturation( float sat_input, float uplim, float lowlim) { // Saturation function
|
||
|
if (sat_input > uplim) sat_input = uplim;
|
||
|
else if (sat_input < lowlim ) sat_input = lowlim;
|
||
|
else;
|
||
|
return sat_input;
|
||
|
}
|
||
|
|
||
|
float pidi(float pid_input) { // discrete PID function
|
||
|
float e_integration;
|
||
|
e0i = pid_input;
|
||
|
e_integration = e0i;
|
||
|
|
||
|
//anti-windup
|
||
|
if (u1i >= ui_max) {
|
||
|
e_integration = 0;
|
||
|
} else if (u1i <= ui_min) {
|
||
|
e_integration = 0;
|
||
|
}
|
||
|
|
||
|
delta_ui = kpi * (e0i - e1i) + kii * Ts * e_integration + kdi / Ts * (e0i - 2 * e1i + e2i); //incremental PID programming avoids integrations.
|
||
|
u0i = u1i + delta_ui; //this time's control output
|
||
|
|
||
|
//output limitation
|
||
|
saturation(u0i, ui_max, ui_min);
|
||
|
|
||
|
u1i = u0i; //update last time's control output
|
||
|
e2i = e1i; //update last last time's error
|
||
|
e1i = e0i; // update last time's error
|
||
|
return u0i;
|
||
|
}
|