diff options
| author | Donald Delmar Davis <don@suspectdevices.com> | 2017-10-20 21:45:59 -0700 |
|---|---|---|
| committer | Donald Delmar Davis <don@suspectdevices.com> | 2017-10-20 21:45:59 -0700 |
| commit | a934709f661745e7747d7223626d41dfb205e201 (patch) | |
| tree | 336b79458fb68baa6b3f9eb371bab92fd04dc789 /Arduino/ems-templates | |
| parent | 261ac02efc8361ac1e55d9be72cfab1c8ea55237 (diff) | |
Move unused modules to ems-templates
Diffstat (limited to 'Arduino/ems-templates')
| -rwxr-xr-x | Arduino/ems-templates/Heater.h | 263 | ||||
| -rwxr-xr-x | Arduino/ems-templates/Pump.h | 200 | ||||
| -rwxr-xr-x | Arduino/ems-templates/Thermocouple.cpp | 9 | ||||
| -rwxr-xr-x | Arduino/ems-templates/Thermocouple.h | 178 |
4 files changed, 650 insertions, 0 deletions
diff --git a/Arduino/ems-templates/Heater.h b/Arduino/ems-templates/Heater.h new file mode 100755 index 0000000..a498e27 --- /dev/null +++ b/Arduino/ems-templates/Heater.h @@ -0,0 +1,263 @@ +/************************************************** + * Heater.h - Describe the heater interface + * + * + */ + #ifndef HEATER_H + #define HEATER_H + +#include "Common.h" +#include "TaskScheduler.h" +#include "Monitor.h" +#include "Machine.h" + +#include "Thermocouple.h" + +// move to common.h + +#define HEATER_OFF 0 +#define HEATER_OUT_MAX 255 +#define HEATER_OUT_MIN 0 +#define HEATER_MAX_TEMP 280.00 +#define HEATER_PID_SAMPLE_TIME 100L + +static void run(); +static void CheckHeaters(); + +static void TMP (uint8_t kwIndex, uint8_t verb,char *args); +static void HSW (uint8_t kwIndex, uint8_t verb,char *args); + +static void H1P (uint8_t kwIndex, uint8_t verb,char *args); +static void H1I (uint8_t kwIndex, uint8_t verb,char *args); +static void H1D (uint8_t kwIndex, uint8_t verb,char *args); + +static void H2P (uint8_t kwIndex, uint8_t verb,char *args); +static void H2I (uint8_t kwIndex, uint8_t verb,char *args); +static void H2D (uint8_t kwIndex, uint8_t verb,char *args); + + +const double SampleTimeInSec = ((double)HEATER_PID_SAMPLE_TIME)/1000; + + + +class Heater { + private: + + Task runHeaters; + + public: + double setPoint; + bool isOn; + int H1Output, H2Output; + double H1SetPoint, H1LastInput, H1ITerm; + double H2SetPoint, H2LastInput, H2ITerm; + + //TODO: P,I,D 'accessors' + double P1, I1, D1, P2, I2, D2; + + + void runThermostat(){ + double input, output, error, i, d; + + i = I1 * SampleTimeInSec; + d = D1 / SampleTimeInSec; + input = thermocouple.getTemp1(); + error = H1SetPoint - input; + + H1ITerm += (i * error); + if(H1ITerm > HEATER_OUT_MAX) { + H1ITerm = HEATER_OUT_MAX; + } + else if(H1ITerm < HEATER_OUT_MIN){ + H1ITerm= HEATER_OUT_MIN; + } + + output = P1 * error + H1ITerm - d * (input - H1LastInput); + + if(output > HEATER_OUT_MAX){ + output = HEATER_OUT_MAX; + } + else if(output < HEATER_OUT_MIN) { + output = HEATER_OUT_MIN; + } + H1Output = (int) output; + + if(isOn) { + analogWrite(HTR1_PIN,H1Output); + } + + H1LastInput = input; + + input = thermocouple.getTemp2(); + error = H2SetPoint - input; + i= I2 * SampleTimeInSec; + d= D2 / SampleTimeInSec; + + H2ITerm += (i * error); + if(H2ITerm > HEATER_OUT_MAX) { + H2ITerm = HEATER_OUT_MAX; + } + else if(H2ITerm < HEATER_OUT_MIN){ + H2ITerm= HEATER_OUT_MIN; + } + + output = P2 * error + H2ITerm - d * (input - H2LastInput); + if(output > HEATER_OUT_MAX){ + output = HEATER_OUT_MAX; + } + else if(output < HEATER_OUT_MIN) { + output = HEATER_OUT_MIN; + } + + H2Output = (int) output; + + if(isOn) { + analogWrite(HTR2_PIN,H2Output); + } + + H2LastInput = input; + + }; + + void init() { + pinMode(HTR1_PIN, OUTPUT); + pinMode(HTR2_PIN, OUTPUT); + analogWrite(HTR1_PIN, HEATER_OFF); // number foo + analogWrite(HTR2_PIN, HEATER_OFF); + + + P1=1; + I1=0.05; + D1=0.25; + + P2=1; + I2=0.05; + D2=0.25; + + isOn=0; + setPoint=0.0; + + // register monitor actions + monitor.registerAction(_TMP_, &TMP); + monitor.registerAction(_HSW_, &HSW); + + monitor.registerAction(_H1P_, &H1P); + monitor.registerAction(_H1I_, &H1I); + monitor.registerAction(_H1D_, &H1D); + + monitor.registerAction(_H2P_, &H2P); + monitor.registerAction(_H2I_, &H2I); + monitor.registerAction(_H2D_, &H2D); + + // TODO: check to see if turning on the heater will change the temp. + + // set tasks for sceduled reads + runHeaters.set(HEATER_PID_SAMPLE_TIME, TASK_FOREVER, &run); + machine.todolist.addTask(runHeaters); + runHeaters.enable(); + } + + void StopHeaters() { + isOn=0; + CheckHeaters(); + } + + + +}; + +Heater heater; + +static void run() { + CheckHeaters(); + +} + + +// check heaters and change state if necessary +// Check the temperature and turn the heater off if the target temp reached. +// Turn the heater on of the temp is below he target. +// TODO: add PID functionality here. +static void CheckHeaters() { + double t; + t=thermocouple.getTemp1(); + if (t>HEATER_MAX_TEMP) { + heater.isOn=false; + monitor.warn("overtmp d.%.02d", FAKE_A_FLOAT(t)); + } + t=thermocouple.getTemp2(); + if (t>HEATER_MAX_TEMP) { + heater.isOn=false; + monitor.warn("overtmp d.%.02d", FAKE_A_FLOAT(t)); + } + if (heater.isOn){ + heater.runThermostat(); + } else { + analogWrite(HTR1_PIN, HEATER_OFF); + analogWrite(HTR2_PIN, HEATER_OFF); + } +} + + + + +// define or query P, I, and D for heater 1 + +static void TMP(uint8_t kwIndex, uint8_t verb, char* args) { + if (verb == '!') { + heater.H1SetPoint = heater.H2SetPoint = heater.setPoint = atof(args); + } + monitor.update("TMP", "%d.%.02d", FAKE_A_FLOAT(heater.setPoint)); +} + +static void HSW(uint8_t kwIndex, uint8_t verb, char* args) { + //monitor.debug("atoi('ON')=%d",atoi("ON")); + if (verb == '!') { + heater.isOn = atoi(args); + } + monitor.update("HSW", "%d", heater.isOn); +} + +static void H1P(uint8_t kwIndex, uint8_t verb, char* args) { + if (verb == '!') { + heater.P1 = atof(args); + } + monitor.update("H1P", "%d.%.02d", FAKE_A_FLOAT(heater.P1)); +} + +static void H1I(uint8_t kwIndex, uint8_t verb, char* args) { + if (verb == '!') { + heater.I1 = atof(args); + } + monitor.update("H1I", "%d.%.02d", FAKE_A_FLOAT(heater.I1)); +} +static void H1D(uint8_t kwIndex, uint8_t verb, char* args) { + if (verb == '!') { + heater.D1 = atof(args); + } + monitor.update("H1D", "%d.%.02d", FAKE_A_FLOAT(heater.D1)); +} + +static void H2P(uint8_t kwIndex, uint8_t verb, char* args) { + if (verb == '!') { + heater.P2 = atof(args); + } + monitor.update("H2P", "%d.%.02d", FAKE_A_FLOAT(heater.P2)); +} + +static void H2I(uint8_t kwIndex, uint8_t verb, char* args) { + if (verb == '!') { + heater.I2 = atof(args); + } + monitor.update("H2I", "%d.%.02d", FAKE_A_FLOAT(heater.I2)); +} + +static void H2D(uint8_t kwIndex, uint8_t verb, char* args) { + if (verb == '!') { + heater.D1 = atof(args); + } + + monitor.update("H2D", "%d.%.02d", FAKE_A_FLOAT(heater.D2)); +} + + #endif // HEATER_H diff --git a/Arduino/ems-templates/Pump.h b/Arduino/ems-templates/Pump.h new file mode 100755 index 0000000..c8293dd --- /dev/null +++ b/Arduino/ems-templates/Pump.h @@ -0,0 +1,200 @@ +/************************************* + * Pump.h - Manage the hydraulic pump. + */ +#ifndef PUMP_H +#define PUMP_H + +#include "Common.h" +#include "TaskScheduler.h" +#include "Monitor.h" +#include "Machine.h" +#include "Clock.h" + +// move to common +#define PUMP_ON HIGH +#define PUMP_OFF LOW +#define MAX_PUMP_RUN_SECONDS 5 +#define GOING_UP LOW +#define GOING_DOWN HIGH +#define SOLENOID_OFF LOW + +static void runThePump(); +static void readPumpPressure(); + +static void PMS (uint8_t kwIndex, uint8_t verb,char *args); +static void MUP (uint8_t kwIndex, uint8_t verb,char *args); +static void MDN (uint8_t kwIndex, uint8_t verb,char *args); + +static void PS1 (uint8_t kwIndex, uint8_t verb,char *args); +static void SOL (uint8_t kwIndex, uint8_t verb,char *args); + +class Pump { +private: + Task checkPressure; + Task runPump; + bool wereWeGoingDown; + bool wasTheMotorOn; + long int startedAt; + bool motorOn; + bool goingDown; +public: + int pressure; + bool pressureChanged; + + void motor(bool state) { + + motorOn=state; + + if((motorOn==PUMP_ON) && (wasTheMotorOn==PUMP_OFF)) + { + startedAt=clock.second(); + + } + digitalWrite(PUMP_PIN, motorOn?PUMP_ON:PUMP_OFF); + if (motorOn != wasTheMotorOn) { + monitor.update("PMS", "%d", motorOn); + } + wasTheMotorOn=motorOn; + } + + bool isMotorOn() {return motorOn;} + + bool areWeGoingDown() {return goingDown;} + + + void solenoid(bool state) { + goingDown=state; + digitalWrite(SOLENOID_PIN, goingDown?GOING_DOWN:GOING_UP); + if (goingDown != wereWeGoingDown) { + monitor.update("SOL", "%d", goingDown); + } + goingDown = wereWeGoingDown; + + + } + + void init() { + + + pinMode(PUMP_PIN, OUTPUT); + digitalWrite(PUMP_PIN, PUMP_OFF); + pinMode(SOLENOID_PIN, OUTPUT); + digitalWrite(SOLENOID_PIN, GOING_UP); + StopPump(); + // register monitor commands + monitor.registerAction(_PMS_, &PMS); + monitor.registerAction(_PS1_, &PS1); + monitor.registerAction(_SOL_, &SOL); + monitor.registerAction(_MUP_, &MUP); + monitor.registerAction(_MDN_, &MDN); + + checkPressure.set(100L, TASK_FOREVER, &readPumpPressure); + machine.todolist.addTask(checkPressure); + checkPressure.enable(); + + runPump.set(100L, TASK_FOREVER, &runThePump); + machine.todolist.addTask(runPump); + runPump.enable(); + + } + + void Press() { + solenoid(GOING_UP); + motor(PUMP_ON); + CheckPump(); // ?? + } + + void Release() { + solenoid(GOING_DOWN); + motor(PUMP_ON); + CheckPump(); + + } + + void CheckPump() { + if((motorOn) && ((clock.second()-startedAt) > MAX_PUMP_RUN_SECONDS)){ + monitor.debug("Motor run timed out"); + motor(PUMP_OFF); + } + + // check here for the home switch. + + } + + void StopPump() { + motor(PUMP_OFF); + solenoid(SOLENOID_OFF); + } + +// 'accessors' + bool updatePressure() { + int tol = 50; // number foo bad move to macro + + int thisReading = analogRead(PS1_APIN); + if (thisReading < 224) { + pressure=0; + } else { + pressure=(thisReading-200)*10; + } + pressureChanged = abs(pressure - pressure) >= tol; + if (pressureChanged) + pressure = pressure; + return pressureChanged; + } + + void setPressureChanged(bool v) { + pressureChanged = v; + } +}; + + +Pump pump; + +static void runThePump() { + pump.CheckPump(); +} + +static void readPumpPressure() { + if (pump.updatePressure()) { + monitor.update("PS1", "%d", pump.pressure); + pump.setPressureChanged(false); + } +} + +// should also take on as a value + +static void PMS (uint8_t kwIndex, uint8_t verb,char *args) { + if (verb == '!') { + pump.motor((bool) atoi(args)); + } else { + monitor.update("PMS", "%d", pump.isMotorOn()); + } +} + +// should also take on as a value +static void SOL (uint8_t kwIndex, uint8_t verb,char *args) { + if (verb == '!') { + pump.solenoid((bool)atoi(args)); + } else { + monitor.update("SOL", "%d", pump.areWeGoingDown()); + } +} + +static void MUP (uint8_t kwIndex, uint8_t verb,char *args) { + monitor.update("ACK","Going up"); + pump.Press(); +} + +static void MDN (uint8_t kwIndex, uint8_t verb,char *args) { + monitor.update("ACK", "Going down"); + pump.Release(); +} + + +static void PS1 (uint8_t kwIndex, uint8_t verb,char *args) { + monitor.update("PS1", "%d", pump.pressure); +} + + +#endif // PUMP_H + diff --git a/Arduino/ems-templates/Thermocouple.cpp b/Arduino/ems-templates/Thermocouple.cpp new file mode 100755 index 0000000..c06528c --- /dev/null +++ b/Arduino/ems-templates/Thermocouple.cpp @@ -0,0 +1,9 @@ +/****************************************************************** + * Thermocouple.cpp - type description for temperature measurement. + */ + +#include "Thermocouple.h" + +Thermocouples thermocouple; + + diff --git a/Arduino/ems-templates/Thermocouple.h b/Arduino/ems-templates/Thermocouple.h new file mode 100755 index 0000000..13e72a3 --- /dev/null +++ b/Arduino/ems-templates/Thermocouple.h @@ -0,0 +1,178 @@ +/****************************************************************** + * Thermocouple.h - type description for temperature measurement. + * + * this should be generalized into a temperature sensor. + * + */ + + #ifndef THERMOCOUPLE_H + #define THERMOCOUPLE_H + + +#include <SPI.h> +#include "Adafruit_MAX31855.h" + +#include "Common.h" +#include "TaskScheduler.h" +#include "Monitor.h" +#include "Machine.h" + +static double CtoF(double C) { + return (9.0*C)/5.0 + 32.0; +} + +// output temperature values when they have changed +static void readPlate1Temp(); +static void readPlate2Temp(); +static void readAmbientTemp(); +static void TS1 (uint8_t kwIndex, uint8_t verb,char *args); +static void TS2 (uint8_t kwIndex, uint8_t verb,char *args); +static void AMT (uint8_t kwIndex, uint8_t verb,char *args); + +class Thermocouples { + private: + Adafruit_MAX31855 _therm1; + double _temp1; + bool _temp1changed; + + Adafruit_MAX31855 _therm2; + double _temp2; + bool _temp2changed; + + // ambient temp is made up of the avg ambient temp from each thermocouple + double _ambientTemp; + bool _ambChanged; + + Task _temp1Read; + Task _temp2Read; + Task _ambientTempRead; + + public: + Thermocouples() : _therm1(TS1_CS_PIN), _therm2(TS2_CS_PIN) {} + Adafruit_MAX31855* therm1() {return &_therm1;} + Adafruit_MAX31855* therm2() {return &_therm2;} + + // temperature 1 changes + bool updateTemp1() { + double tol = 1; + double temp = therm1()->readFarenheit(); + _temp1changed = fabs(_temp1 - temp) >= tol; + if (_temp1changed) + _temp1 = temp; + return _temp1changed; + } + + void setTemp1Changed(bool v) { + _temp1changed = v; + } + + // temperature 2 changes + bool updateTemp2() { + double tol = 1; + double temp = therm2()->readFarenheit(); + _temp2changed = fabs(_temp2 - temp) >= tol; + if (_temp2changed) + _temp2 = temp; + return _temp2changed; + } + + void setTemp2Changed(bool v) { + _temp2changed = v; + } + + // Check for Ambient temperature changes + bool updateAmbientTemp() { + double tol = 1; + // Farenheit temperature + double avgTemp = CtoF((therm1()->readInternal() + therm2()->readInternal())/2.0); + _ambChanged = fabs(_ambientTemp - avgTemp) >= tol; + if (_ambChanged) { + _ambientTemp = avgTemp; + } + return _ambChanged; + } + + void setAmbChanged(bool v) { + _ambChanged = v; + } + + double getTemp1() { return _temp1; } + double getTemp2() { return _temp2; } + double getAmb() { return _ambientTemp; } + + bool updateTemp2(double temp); + + void init() { + monitor.registerAction(_TS1_, &TS1); + monitor.registerAction(_TS2_, &TS2); + monitor.registerAction(_AMT_, &AMT); + + // set tasks for sceduled reads + _temp1Read.set(100L, TASK_FOREVER, &readPlate1Temp); + machine.todolist.addTask(_temp1Read); + _temp1Read.enable(); + + _temp2Read.set(100L, TASK_FOREVER, &readPlate2Temp); + machine.todolist.addTask(_temp2Read); + _temp2Read.enable(); + + _ambientTempRead.set(100L, TASK_FOREVER, &readAmbientTemp); + machine.todolist.addTask(_ambientTempRead); + _ambientTempRead.enable(); + } + + +}; + +extern Thermocouples thermocouple; + +/*---------------------------------------------------------------------- + * Schedule Callbacks + *--------------------------------------------------------------------*/ +// return the temp only if the plate temp has changed +static void readPlate1Temp() { + if (thermocouple.updateTemp1()) { + double _f = thermocouple.getTemp1(); + monitor.update("TS1", "%d.%.02d", int(_f),int(abs(_f - int(_f))*100)); + thermocouple.setTemp1Changed(false); + } +} + +static void readPlate2Temp() { + if (thermocouple.updateTemp2()) { + double _f = thermocouple.getTemp2(); + monitor.update("TS2", "%d.%.02d", int(_f),int(abs(_f - int(_f))*100)); + thermocouple.setTemp2Changed(false); + } +} + +static void readAmbientTemp() { + if (thermocouple.updateAmbientTemp()) { + double _f = thermocouple.getAmb(); + monitor.update("AMT", "%d.%.02d", int(_f),int(abs(_f - int(_f))*100)); + thermocouple.setAmbChanged(false); + } +} + +/*---------------------------------------------------------------------- + * Monitor Callbacks + *--------------------------------------------------------------------*/ + +// unconditionally return temp +static void TS1 (uint8_t kwIndex, uint8_t verb,char *args) { + double _f = thermocouple.getTemp1(); + monitor.update("TS1", "%d.%.02d", int(_f),int(abs(_f - int(_f))*100)); +} + +static void TS2 (uint8_t kwIndex, uint8_t verb,char *args) { + double _f = thermocouple.getTemp2(); + monitor.update("TS2", "%d.%.02d", int(_f),int(abs(_f - int(_f))*100)); +} + +static void AMT (uint8_t kwIndex, uint8_t verb,char *args) { + double _f = thermocouple.getAmb(); + monitor.update("AMT", "%d.%.02d", int(_f),int(abs(_f - int(_f))*100)); +} + + + #endif // THERMOCOUPLE_H |
