Dependencies:   KellerDruck_pressure PID PWM-Coil-driver Sensirion_SF04 VL6180

Fork of PUMP_SCRIPT1 by Ian Wolf

main.cpp

Committer:
iwolf32
Date:
2017-07-26
Revision:
4:79b23d1fbcd1
Parent:
3:9ff79ea3a294
Child:
5:f2cb21046678
Child:
6:e3eff8a04b25

File content as of revision 4:79b23d1fbcd1:

#include "main.h"
//---------------------------
void dutycycleincrease()
{
    dutycycle=dutycycle+0.01;
}

void dutycycledecrease()
{
    dutycycle=dutycycle-0.01;
}

void frequencyincrease()
{
    frequency=frequency+1;
}
void frequencydecrease()
{
    frequency=frequency-1;
}
void injectorvalvecontrol(){

while(true){
float openvalue=(((1/frequency)*dutycycle));
float offvalue=(1/frequency)-openvalue;
    
injector.on();
wait(openvalue);
injector.off();
wait(offvalue);
}
}

//-----------------------------
void pumpTachTrigger()
{
    pumpTachCounts++;
}

void pump_tach_update()
{
    float i = pumpTachCounts; // In case it triggers mid-calculation
    pumpTachCounts = 0;
    pumpRpm = (i/pumpTachPoles)*60;
}

void pump_init()
{
    pump.period(.001); // 1kHz PWM
    pump = 0;
    //ledGrn.period(.001);

    InterruptIn pumpTach(pumpTachPin);
    pumpTach.rise(&pumpTachTrigger);

    pump_control_PID.setInputLimits(pumpMinPSI, pumpMaxPSI);
    pump_control_PID.setOutputLimits(0.0, 1.0); // Output is a PWM signal ranging from 0-1
    pump_control_PID.setMode(AUTO_MODE);
    pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI); // pump setpoint based on pot 2*/
}

void pump_pid_update(char error)
{
    if (pumpPressure.status != 0x40) {
        pump = 0;
        pump_control_PID.reset();
    } else {
        pump_control_PID.setSetPoint(((double)pot2)*pumpMaxPSI);

        //Update the process variable.
        pump_control_PID.setProcessValue(pumpPressure.pressurePSI);
        //PID calculation and set the new output value.
        pump = pump_control_PID.compute();
        //pump = 0.1;
        //ledGrn = ((float)1.0-pump.read());
    }
}

void update_pressures()
{
    Timer timer;
    timer.start();
    char error;
    while (true) {
        i2c1_m.lock();
        timer.reset();
        error = pumpPressure.readPT();
        //error |= mixerPressure.readPT();
        int wait = (200 - timer.read_ms());
        i2c1_m.unlock();
        Thread::wait(wait);
        pump_pid_update(error);
    }
}

void update_flow1()
{
    Timer timer;
    timer.start();
    char error;
    while (true) {
        i2c2_m.lock();
        timer.reset();
        error = mainflow.Measure(FLOW);
        int wait = (200 - timer.read_ms());
        i2c2_m.unlock();
        Thread::wait(wait);
    }
}

//------------------------------------------------
void update_flow2()
{
    Timer timer;
    timer.start();
    char error;
    while (true) {
        i2c3_m.lock();
        timer.reset();
        error = loopflow.Measure(FLOW);
        int wait = (200 - timer.read_ms());
        i2c3_m.unlock();
        Thread::wait(wait);
    }
}
//------------------------------------------------   

void print_process_values()
{
    //Thread::wait(100); // Wait initially to allow sensors to update, prevents a zero reading from going to serial
    Timer timer;
    timer.start();
    while (true) {
        stdio_m.lock();
        timer.reset();

        pc.printf("%.02fkPa %.02fpsi %.02fC %02X %.2f%% %.0fRPM %u %.0f %s %u %.0f %s %.3f %.02fHz %.02f\r\n",
                  pumpPressure.pressureKPA, pumpPressure.pressurePSI, pumpPressure.temperatureC, pumpPressure.status,
                  pump.read()*100, pumpRpm,
                  mainflow.flow.u16, (((float)mainflow.flow.i16) / mainflow.scaleFactor.u16), mainflow.flowUnitStr, 
                  loopflow.flow.u16, (((float)loopflow.flow.i16) / loopflow.scaleFactor.u16), loopflow.flowUnitStr,
                  ((double)pot2)*pumpMaxPSI, frequency, dutycycle);//, agentlevel;
        int wait = (1000 - timer.read_ms());

        stdio_m.unlock();
        Thread::wait(wait);
    }
}

// main() runs in its own thread in the OS
int main()
{
    pump_init();
    //ledBlu = 1;
    pc.printf("Serenity Starting up...\n\r");
//--------------------------------------------
dutycycleup.rise(&dutycycleincrease);
dutycycledown.rise(&dutycycledecrease);
frequencyup.rise(&frequencyincrease);
frequencydown.rise(&frequencydecrease);    

    //Thread to execute injector valve control
    Injector_Valve_Control.set_priority(osPriorityHigh);
    Injector_Valve_Control.start(injectorvalvecontrol);
//--------------------------------------------

    // Thread to poll pressure sensors
    update_pressures_t.set_priority(osPriorityNormal);
    update_pressures_t.start(update_pressures); 

    // Thread to poll flow sensor
    update_flow1_t.set_priority(osPriorityNormal);
    update_flow1_t.start(update_flow1);
    
    //Thread to poll flow sensor
    update_flow2_t.set_priority(osPriorityNormal);
    update_flow2_t.start(update_flow2);

    // Thread to send process values to serial port
    print_process_values_t.set_priority(osPriorityLow);
    print_process_values_t.start(&print_process_values);

    while (true) {
         pump_tach_update();
        
    }
}