Drain batteries through an LM324 controlled CEB6030L from a defunct graphics card

Dependencies:   mbed FastAnalogIn PID mbed-rtos

main.cpp

Committer:
eisd
Date:
2016-07-05
Revision:
1:712b9b5470d4
Parent:
0:e7492a4f4b2f
Child:
2:e7ce63ada1d2

File content as of revision 1:712b9b5470d4:

#include "mbed.h"
#include "rtos.h"
#include "FastAnalogIn.h"

AnalogOut Iset(PTE30);
FastAnalogIn Iget(PTE20);
AnalogIn Vbat(PTB0);
int flop = 0;
void adjust() {
    //flop = (flop + 1) % 4;
    Iset.write(flop == 0? 0.2/4.61 : 0);
}
float k = 0.01, iget = Iget.read(), vbat = Vbat.read(), vbat_rest = Vbat.read();
void filter() {
    iget = k * Iget.read() + (1 - k) * iget;
    vbat = k * Vbat.read() + (1 - k) * vbat;
    if (Iset.read() == 0)
        vbat_rest = k * Vbat.read() + (1 - k) * vbat;
}
void rest(const void *) {
    while (true) {
        Thread::wait(5000);
        float x = Iset;
        Iset.write_u16(16);
        Thread::wait(400);
        Iset.write_u16(0);
        Thread::wait(100);
        Iset = x;
    }
}
int main()
{
    Ticker t;
    t.attach(&adjust, 3.0);
    Ticker t2;
    t2.attach_us(&filter, 1000);
    Thread t3(&rest);
    Iset.write(0.0);
    Serial pc(USBTX, USBRX);
    pc.baud(115200);

    while (true) {
        //pc.printf("Iset %f    Iget %6d %f    Vbat %6d %f    \r", Iset.read(), Iget.read_u16(), Iget.read(), Vbat.read_u16(), Vbat.read());
        pc.printf("Iset %f      Iget %8d %f %f", Iset.read(), Iget.read_u16(), Iget.read() * 36.140224, iget * 36.140224);
        pc.printf("      Vbat %8d %f %f %f\r", Vbat.read_u16(), Vbat.read() * 36.7538, vbat * 36.7538, vbat_rest * 36.7538);
        Thread::wait(500);
    }
}