Charging circuit code

Dependencies:   mbed

Fork of Serial_HelloWorld_Mbed by mbed official

main.cpp

Committer:
samuelstradling
Date:
2016-04-28
Revision:
1:291cc733b64a
Parent:
0:879aa9d0247b
Child:
2:091e7846d11a

File content as of revision 1:291cc733b64a:

#include "mbed.h"

#define mBED2

#define VCC 3.3
#define B 3984
#define K 273
#define T0 (K+25)
#define R0 10000
#define TEMP_LIMIT 25

#ifdef mBED1
    #define R1A 10037
    #define R1B 10056
    #define R2A 10047
    #define R2B 10012
    #define R3A 10041
    #define R3B 10048
#else
    #define R1A 10033
    #define R1B 10023
    #define R2A 9988
    #define R2B 10036
    #define R3A 10070
    #define R3B 10067
#endif
 
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut buzzer(p22);
LocalFileSystem local("local");

float res_to_temp(float res);
float volt_to_temp(int batteryA, float volt);
float volt_to_res(int batteryA, float volt);
 
int main() {
    
    char myname[64];
    float voltageA = .0, voltageB = .0;
    float tempA = .0, tempB = .0;
    float resA = .0, resB = .0;
    int i;
    FILE *fp;
    #ifdef mBED1
        int mBed = 1;
    #else
        int mBed = 2;
    #endif
    
    buzzer = 0;
    
    pc.printf("\n\n\n");
    for (i = 0; i < 1000; i++) {
        sprintf(myname, "/local/log%04d.csv", i);
        pc.printf("Try file: <%s>\n", myname);    
        if ((fp = fopen(myname, "r")) == 0) {
            pc.printf("File not found: <%s>\n", myname);
            break;
        } else {
            pc.printf("File exists: <%s>\n", myname);
            fclose(fp);
        }
    }
    
    pc.printf("Creating File: <%s>\n", myname);
    fp = fopen(myname, "w");   
    fprintf(fp, "VA, resA, tempA, VB, resB, tempB\n");
    
    AnalogIn va1(p17);
    AnalogIn va2(p18);
    AnalogIn vb1(p19);
    AnalogIn vb2(p20);
    
    DigitalIn run(p21);
    
    while(run) {
        wait_ms(5000);
        voltageA = (va1.read() - va2.read())*3.3;
        voltageB = (vb1.read() - vb2.read())*3.3;
        resA = volt_to_res(1, voltageA);
        resB = volt_to_res(0, voltageB);
        tempA = volt_to_temp(1, voltageA);
        tempB = volt_to_temp(0, voltageB);
        pc.printf("mBed%i: VA: %f, resA: %f, tempA: %f, VB: %f, resB: %f, tempB: %f\n", mBed, voltageA, resA, tempA-K, voltageB, resB, tempB-K);
        fprintf(fp, "%f, %f, %f, %f, %f, %f\n", mBed, voltageA, resA, tempA-K, voltageB, resB, tempB-K);
        
        if(((tempA-K) > TEMP_LIMIT) || ((tempB-K) > TEMP_LIMIT)){
            buzzer = 1;
        }        
    }
    
    fclose(fp); 
    
}

float volt_to_temp(int batteryA, float volt){
    float res = volt_to_res(batteryA, volt);
    return res_to_temp(res);
}

float volt_to_res(int batteryA, float volt){
    if(batteryA){
        return R3A * ((VCC * R2A - volt * (R1A + R2A)) / (VCC * R1A + volt * (R1A + R2A)));
    }else{
        return R3B * ((VCC * R2B - volt * (R1B + R2B)) / (VCC * R1B + volt * (R1B + R2B)));
    }
}

float res_to_temp(float Rt){
    
    return (B * T0) / (B + (T0 * log(Rt / R0)));
    
}