View Single Post
Old 01-03-16, 02:17 PM   #14
Acuario
Apprentice EcoRenovator
 
Join Date: May 2011
Location: Tortosa, Spain
Posts: 221
Thanks: 2
Thanked 81 Times in 46 Posts
Default Code for heatpump controller

Ok, here we go.. This is my code for my heat pump controller. First an explanation of the structure.

There are 9 individual modules (.ino files) that make up the controller. Each one attempts to encapsulate functionality of a particular part of the controller.

As the complexity of the controller and the necessity to run various peripheral parts increased it became evident that a real time operating system would be needed, many of the modules run functions specifically related to the peripherals leaving the 'main' program (thread) to control the state of the machine.

Enough waffle..
The code for the RTOS was found here:
https://github.com/greiman/NilRTOS-Arduino

Other code libraries are either part of the Arduino code or are easily found searching on Google. Some code is modified from samples and extended to meet my needs. The code is commented so hopefully makes sense. I'll split it into individual posts as it's a bit much all in one...
Note: Some defines/code may not be used as it was there in development and I haven't removed it yet..


1. Main header file HeatpumpController.h
#pragma once
#ifndef HPCONTROLLER_H
#define HPCONTROLLER_H

int defTemp; //Temperature to run a defrost cycle
int defTime; //Time to run defrost cycle
int defUpperTemp; //If evap reaches this temperature abandon the defrost cycle
int maxWaterTemp; //Maximum circuit water temperature
int desiredRoomTemp; //Desired room temperature
int hysterisis; //Hysterisis setting
int defStep; //Defrost step
int deltaOn; //Temp to turn on pump
int deltaOff; //Temp to turn off pump
bool operatingState; //System operating state - true=on, false=off
bool machineRunningState = FALSE; //State of machine - true=on, false=off
int operatingMode; //System operating mode
char systemState[20]; //System state string
bool readAnalog = FALSE; //Flag to indicate an analogue conversion is taking place. Needed for rtos so 2 analogue reads don't clash
bool rtosStarted = FALSE; //Flag the RTOS has started (needed for change from delay to nilDelay...)

unsigned long lastDefrost = 0;
unsigned long timeBetweenDefrost; //Time between defrost cycles
unsigned long defrostRunTime;
unsigned long defrostStartDelay;
unsigned long defrostStartTime;
bool defrostFlag = FALSE; //Flag to say if we are in a defrost cycle (and to start a defrost cycle)

float lastTempEvaporator;
float lastTempCompressor;
float tempCompressor;
float tempEvaporator;
float humidity;
float dht11Temperature;
float dewPointTemp;

#define ON 0
#define OFF 1
#define COOL 0
#define HEAT 1

//Analogue sensors
int ntcSensor[4];
#define flowTemp ntcSensor[0]
#define returnTemp ntcSensor[1]
#define ambientTemp ntcSensor[2]
#define s4 ntcSensor[3]

//Heat pump control pins (Digital outputs)
#define FANLOW 2
#define FANHIGH 3
#define COMPRESSOR 4
#define DHT11PIN 5
#define DS18B20 7
#define PUMP 8
#define VALVE 9

//Status of individual relays. Set all on so startup sets the pin outputs correctly
bool compStat = ON;
bool fanLow = ON;
bool fanHigh = ON;
bool valve = ON;
bool pump = ON;

//Non volatile memory storage
//#define STARTDELAY 0 //Startup delay time
//#define MAX_TEMP 1 //
//#define MIN_TEMP 2
//#define HEATCOOL 3
#define DEFROSTTIME 4 //Defrost time
#define DEFROSTTEMP 5 //Defrost start temperature
#define DEFROSTMAXTEMP 6 //Temperature to stop defrost (overrides time for defrost cycle)
#define MAXCOMPTEMP 7 //Maximum compressor temperature
#define LASTSYSTEMSTATE 8 //Last state - on or off
#define LASTSYSTEMMODE 9 //Last operating mode (heat/cool)
#define DESIREDROOMTEMP 10 //Temperature for room
#define MAXWATERTEMP 11 //Maximum water temperature
#define HYSTERISIS 12 //Hysterisis setting
#define TIMEBETWEENDEFROST 13 //Time between defrost cycles
#define LASTCOMPRUN 14 //Time last compressor run - note 4 bytes!
#define NEXTFREE 18

/* System constants */
#define DEFROSTTEMP 2
#define DEFROST_OFFSET 10
#define COMPSTARTDELAY 180000 //Minimum delay time between compressor starts (for power fail/reset etc)

/* LCD Pages */
#define LCD_PAGE0 0 //General status
#define LCD_PAGE1 1 //Defrost settings
#define LCD_PAGE2 2
#define LCD_PAGE3 3
#define LCD_PAGE4 4
#define LCD_PAGES 4

int lcd_page = LCD_PAGE0; //Starting LCD page

#include <Time.h>
tmElements_t tm;

void writeTime(tmElements_t tmw);
void readTime();
void saveSystemState(char *state);

//Stuff for debug output via serial port
void SerialPrint2(char *format, ...);
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINTST(x,...) SerialPrint2 (x,__VA_ARGS__)
#define DEBUG_PRINTLN(x) Serial.println (x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTST(x,...)
#define DEBUG_PRINTLN(x)
#endif

//Thread definitions for NilRTOS
NIL_THREAD(threadEthernet, arg);
NIL_THREAD(threadReadNTC, arg);
NIL_THREAD(threadReadDHT11, arg);
NIL_THREAD(threadReadSensors, arg);
NIL_THREAD(threadReadBAC1000, arg);
NIL_THREAD(threadDisplayOLED, arg);
NIL_THREAD(threadSetBACTime, arg);

//System strings
const char sOn[] = "On";
const char sOff[] = "Off";
const char sTrue[] = "True";
const char sFalse[] = "False";
#endif
Acuario is offline   Reply With Quote