A copy of very incomplete program for forum

Dependencies:   mbed SDFileSystem

Committer:
roselea
Date:
Sat Mar 17 14:15:20 2012 +0000
Revision:
0:bfcb5b67b1d6

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roselea 0:bfcb5b67b1d6 1 /*
roselea 0:bfcb5b67b1d6 2 *
roselea 0:bfcb5b67b1d6 3 */
roselea 0:bfcb5b67b1d6 4
roselea 0:bfcb5b67b1d6 5 ////////////////////////////////////////////////////////////////////
roselea 0:bfcb5b67b1d6 6 // Test code to read temperature from a Maxim DS18B20 or DS18S20
roselea 0:bfcb5b67b1d6 7 // 1-wire device
roselea 0:bfcb5b67b1d6 8 ////////////////////////////////////////////////////////////////////
roselea 0:bfcb5b67b1d6 9
roselea 0:bfcb5b67b1d6 10 #include <mbed.h>
roselea 0:bfcb5b67b1d6 11 #include "DebugTrace.h"
roselea 0:bfcb5b67b1d6 12 #include "DS18B20.h"
roselea 0:bfcb5b67b1d6 13 #include "OneWireDefs.h"
roselea 0:bfcb5b67b1d6 14 #include "SDFileSystem.h"
roselea 0:bfcb5b67b1d6 15
roselea 0:bfcb5b67b1d6 16 DebugTrace trace(ON, TO_SERIAL);
roselea 0:bfcb5b67b1d6 17 SDFileSystem *poSD;
roselea 0:bfcb5b67b1d6 18 bool bSDfs_active=false;
roselea 0:bfcb5b67b1d6 19 DigitalIn oSDCardPresent(p15);
roselea 0:bfcb5b67b1d6 20
roselea 0:bfcb5b67b1d6 21 const int NUM_THERMS=5; // set of themometers
roselea 0:bfcb5b67b1d6 22 const int SAMPLE_SIZE=5; // how many readings to average over
roselea 0:bfcb5b67b1d6 23 #define ReturnAir 0
roselea 0:bfcb5b67b1d6 24 #define WasteAir 1
roselea 0:bfcb5b67b1d6 25 #define FreshAir 2
roselea 0:bfcb5b67b1d6 26 #define MixedAir 3
roselea 0:bfcb5b67b1d6 27 #define HeatedAir 4
roselea 0:bfcb5b67b1d6 28 DS18B20 oThermos[NUM_THERMS]={DS18B20(true, false, false, p16),DS18B20(true, false, false, p17),DS18B20(true, false, false, p18),DS18B20(true, false, false, p19),DS18B20(true, false, false, p20)};
roselea 0:bfcb5b67b1d6 29 float fTemps[NUM_THERMS][SAMPLE_SIZE+1]; //average and the last SAMPLE_SIZE readings
roselea 0:bfcb5b67b1d6 30 int iCurrentSample;
roselea 0:bfcb5b67b1d6 31
roselea 0:bfcb5b67b1d6 32 DigitalOut oPump(p23);
roselea 0:bfcb5b67b1d6 33 typedef enum {Cooling,Neutral,Heating} tState;
roselea 0:bfcb5b67b1d6 34 tState eState=Neutral; //main system state
roselea 0:bfcb5b67b1d6 35 int iSubState=0;
roselea 0:bfcb5b67b1d6 36 int iHeatingStates,iCoolingStates,iCoolingStep,iCoolingValue;
roselea 0:bfcb5b67b1d6 37 float fHeatingStep,fHeatingValue;
roselea 0:bfcb5b67b1d6 38
roselea 0:bfcb5b67b1d6 39 DigitalOut oLedPump(LED1);
roselea 0:bfcb5b67b1d6 40 DigitalOut oLedRecirc(LED2);
roselea 0:bfcb5b67b1d6 41 DigitalOut oLedByp(LED3);
roselea 0:bfcb5b67b1d6 42 DigitalOut oLedFileOpen(LED4);
roselea 0:bfcb5b67b1d6 43
roselea 0:bfcb5b67b1d6 44 const int NUM_SERVOS=2;
roselea 0:bfcb5b67b1d6 45 #define HeatExBypass 0
roselea 0:bfcb5b67b1d6 46 #define FreshAirBypass 1
roselea 0:bfcb5b67b1d6 47 typedef struct {
roselea 0:bfcb5b67b1d6 48 int iBase,iRange,iCurrent;
roselea 0:bfcb5b67b1d6 49 PwmOut oOut;
roselea 0:bfcb5b67b1d6 50 } tServo ;
roselea 0:bfcb5b67b1d6 51 tServo oServos[NUM_SERVOS]={{0,0,0,PwmOut(p21)},{0,0,0,PwmOut(p22)}};
roselea 0:bfcb5b67b1d6 52 int BypassState;
roselea 0:bfcb5b67b1d6 53
roselea 0:bfcb5b67b1d6 54 float MaxHeat,SetPoint;
roselea 0:bfcb5b67b1d6 55 int TempFreq,LogFreq,ControlFreq;
roselea 0:bfcb5b67b1d6 56
roselea 0:bfcb5b67b1d6 57 void read_temps();
roselea 0:bfcb5b67b1d6 58 void do_controls();
roselea 0:bfcb5b67b1d6 59 void adjust_pump(float current,float required);
roselea 0:bfcb5b67b1d6 60 void log_values(time_t now);
roselea 0:bfcb5b67b1d6 61 void set_servo(tServo *poServo,float fTarget);
roselea 0:bfcb5b67b1d6 62
roselea 0:bfcb5b67b1d6 63 int main() {
roselea 0:bfcb5b67b1d6 64 int x;
roselea 0:bfcb5b67b1d6 65 time_t start,nexttemp,nextcontrol,nextlog,now;
roselea 0:bfcb5b67b1d6 66 printf("Roselea Air conditioning control system\n");
roselea 0:bfcb5b67b1d6 67 FILE *fp;
roselea 0:bfcb5b67b1d6 68
roselea 0:bfcb5b67b1d6 69 do { // wait for SD to be inserted hten attempt to open setting file
roselea 0:bfcb5b67b1d6 70
roselea 0:bfcb5b67b1d6 71 if (oSDCardPresent==1) {
roselea 0:bfcb5b67b1d6 72 printf("Waiting for SD Card\n");
roselea 0:bfcb5b67b1d6 73 while (oSDCardPresent) { //flash led while waitinf for the card
roselea 0:bfcb5b67b1d6 74 oLedFileOpen=!oLedFileOpen;
roselea 0:bfcb5b67b1d6 75 wait (0.5);
roselea 0:bfcb5b67b1d6 76 }
roselea 0:bfcb5b67b1d6 77 oLedFileOpen=0;
roselea 0:bfcb5b67b1d6 78 wait (0.5); //allow poSD time to stabilse
roselea 0:bfcb5b67b1d6 79 }
roselea 0:bfcb5b67b1d6 80 poSD=new SDFileSystem(p11, p12, p13, p14, "poSD");
roselea 0:bfcb5b67b1d6 81 bSDfs_active=true;
roselea 0:bfcb5b67b1d6 82 fp = fopen("/poSD/Settings.txt", "r");
roselea 0:bfcb5b67b1d6 83 if (fp == NULL) {
roselea 0:bfcb5b67b1d6 84 printf("Could not open file for read\nPlease insert card with Settings.txt on it\n");
roselea 0:bfcb5b67b1d6 85 while (oSDCardPresent==0) { // wait for card to be removed then try again
roselea 0:bfcb5b67b1d6 86 oLedFileOpen=!oLedFileOpen;
roselea 0:bfcb5b67b1d6 87 wait (0.1);
roselea 0:bfcb5b67b1d6 88 }
roselea 0:bfcb5b67b1d6 89 delete poSD;
roselea 0:bfcb5b67b1d6 90 bSDfs_active=false;
roselea 0:bfcb5b67b1d6 91 }
roselea 0:bfcb5b67b1d6 92 } while (fp==NULL);
roselea 0:bfcb5b67b1d6 93 oLedFileOpen=1;
roselea 0:bfcb5b67b1d6 94
roselea 0:bfcb5b67b1d6 95 struct tm t;
roselea 0:bfcb5b67b1d6 96 fscanf(fp, "%d %d %d %d:%d:%d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec); //read the real time
roselea 0:bfcb5b67b1d6 97 // convert to timestamp
roselea 0:bfcb5b67b1d6 98 t.tm_mon--;
roselea 0:bfcb5b67b1d6 99 t.tm_year=t.tm_year-1900;
roselea 0:bfcb5b67b1d6 100 set_time(mktime(&t));
roselea 0:bfcb5b67b1d6 101 printf("Time has been set to %s\n",asctime(&t));
roselea 0:bfcb5b67b1d6 102
roselea 0:bfcb5b67b1d6 103 for (x=0; x<NUM_SERVOS; x++) { //initialise servos from file values
roselea 0:bfcb5b67b1d6 104 int l,h;
roselea 0:bfcb5b67b1d6 105 fscanf(fp,"%d %d",&l,&h); //read low and high values
roselea 0:bfcb5b67b1d6 106 oServos[x].iBase=20000-l;
roselea 0:bfcb5b67b1d6 107 oServos[x].iRange=(20000-h)-oServos[x].iBase; // calculate operation range
roselea 0:bfcb5b67b1d6 108 oServos[x].oOut.period_us(20000);
roselea 0:bfcb5b67b1d6 109 oServos[x].oOut.pulsewidth_us(oServos[x].iBase);
roselea 0:bfcb5b67b1d6 110 printf("Servo %d has base of %d and range of %d\n",x+1,oServos[x].iBase,oServos[x].iRange);
roselea 0:bfcb5b67b1d6 111 }
roselea 0:bfcb5b67b1d6 112 fscanf(fp,"%d %d %d",&TempFreq,&LogFreq,&ControlFreq);
roselea 0:bfcb5b67b1d6 113 printf("temp read time %d, log time %d and control time %d\n",TempFreq,LogFreq,ControlFreq);
roselea 0:bfcb5b67b1d6 114 fscanf(fp,"%f",&SetPoint);
roselea 0:bfcb5b67b1d6 115 printf("Temperature setpoint is %.1f \n",SetPoint);
roselea 0:bfcb5b67b1d6 116 fscanf(fp,"%d %f",&iHeatingStates,&fHeatingStep);
roselea 0:bfcb5b67b1d6 117 printf("There are %d heating states @ rate of %.1f\n",iHeatingStates,fHeatingStep);
roselea 0:bfcb5b67b1d6 118 fscanf(fp,"%d %d",&iCoolingStates,&iCoolingStep);
roselea 0:bfcb5b67b1d6 119 printf("There are %d cooling states @ rate of %d\n",iCoolingStates,iCoolingStep);
roselea 0:bfcb5b67b1d6 120
roselea 0:bfcb5b67b1d6 121 fclose(fp);
roselea 0:bfcb5b67b1d6 122 oLedFileOpen=0;
roselea 0:bfcb5b67b1d6 123
roselea 0:bfcb5b67b1d6 124 // Initilize hardware
roselea 0:bfcb5b67b1d6 125 for (x=0; x<NUM_THERMS; x++) //set up thermometers
roselea 0:bfcb5b67b1d6 126 if (!oThermos[x].initialize())
roselea 0:bfcb5b67b1d6 127 printf("Thermometer %d failed to initialse\n",x);
roselea 0:bfcb5b67b1d6 128 else {
roselea 0:bfcb5b67b1d6 129 fTemps[x][0]=oThermos[x].readTemperature();
roselea 0:bfcb5b67b1d6 130 for (int y=1; y<=SAMPLE_SIZE; y++)
roselea 0:bfcb5b67b1d6 131 fTemps[x][y]=fTemps[x][0]; //load into all entries
roselea 0:bfcb5b67b1d6 132 }
roselea 0:bfcb5b67b1d6 133 iCurrentSample=1;
roselea 0:bfcb5b67b1d6 134
roselea 0:bfcb5b67b1d6 135 oPump=0; // Turn off the pump
roselea 0:bfcb5b67b1d6 136 oLedPump=0;
roselea 0:bfcb5b67b1d6 137
roselea 0:bfcb5b67b1d6 138 //Set_servo(FreshAirBypass,0);
roselea 0:bfcb5b67b1d6 139 //Set_servo(HeatExBypass,0);
roselea 0:bfcb5b67b1d6 140
roselea 0:bfcb5b67b1d6 141 start = time(NULL);
roselea 0:bfcb5b67b1d6 142 nexttemp=start-10; //cause all actions at the start
roselea 0:bfcb5b67b1d6 143 nextcontrol=start-10;
roselea 0:bfcb5b67b1d6 144 nextlog=start-10;
roselea 0:bfcb5b67b1d6 145
roselea 0:bfcb5b67b1d6 146 // infinite control loop ...
roselea 0:bfcb5b67b1d6 147 while (1) {
roselea 0:bfcb5b67b1d6 148 now=time(NULL);
roselea 0:bfcb5b67b1d6 149 printf("%d\n",now);
roselea 0:bfcb5b67b1d6 150 if (now>=nexttemp) {
roselea 0:bfcb5b67b1d6 151 nexttemp+=TempFreq;
roselea 0:bfcb5b67b1d6 152 read_temps();
roselea 0:bfcb5b67b1d6 153 }
roselea 0:bfcb5b67b1d6 154 if (now>=nextcontrol) {
roselea 0:bfcb5b67b1d6 155 nextcontrol+=ControlFreq;
roselea 0:bfcb5b67b1d6 156 do_controls();
roselea 0:bfcb5b67b1d6 157 }
roselea 0:bfcb5b67b1d6 158 if (eState>0 && fTemps[HeatedAir][0]!=fHeatingValue) adjust_pump(fTemps[HeatedAir][0],fHeatingValue);
roselea 0:bfcb5b67b1d6 159
roselea 0:bfcb5b67b1d6 160 if (oSDCardPresent==1 && bSDfs_active) { //SD Card removed and SD file system is instansiated
roselea 0:bfcb5b67b1d6 161 delete poSD; //deconstruct the SD File System
roselea 0:bfcb5b67b1d6 162 bSDfs_active=false;
roselea 0:bfcb5b67b1d6 163 printf("SD Card removed\n");
roselea 0:bfcb5b67b1d6 164 }
roselea 0:bfcb5b67b1d6 165 if (oSDCardPresent==0 && !bSDfs_active) { //SD Card inserted and SD file system is void
roselea 0:bfcb5b67b1d6 166 poSD=new SDFileSystem(p11, p12, p13, p14, "poSD"); //construct the SD File System
roselea 0:bfcb5b67b1d6 167 bSDfs_active=true;
roselea 0:bfcb5b67b1d6 168 printf("SD Card inserted\n");
roselea 0:bfcb5b67b1d6 169 }
roselea 0:bfcb5b67b1d6 170 if (now>=nextlog) {
roselea 0:bfcb5b67b1d6 171 nextlog+=LogFreq;
roselea 0:bfcb5b67b1d6 172 log_values(now);
roselea 0:bfcb5b67b1d6 173 }
roselea 0:bfcb5b67b1d6 174 wait (1); //waste some time
roselea 0:bfcb5b67b1d6 175 }
roselea 0:bfcb5b67b1d6 176 }
roselea 0:bfcb5b67b1d6 177
roselea 0:bfcb5b67b1d6 178 void read_temps() {
roselea 0:bfcb5b67b1d6 179 int x;
roselea 0:bfcb5b67b1d6 180 printf("conversion start\n");
roselea 0:bfcb5b67b1d6 181 for (x=0; x<NUM_THERMS; x++)
roselea 0:bfcb5b67b1d6 182 fTemps[x][0]=oThermos[x].readTemperature();
roselea 0:bfcb5b67b1d6 183 printf("conversion finished\n");
roselea 0:bfcb5b67b1d6 184 }
roselea 0:bfcb5b67b1d6 185
roselea 0:bfcb5b67b1d6 186 void do_controls() {
roselea 0:bfcb5b67b1d6 187 }
roselea 0:bfcb5b67b1d6 188
roselea 0:bfcb5b67b1d6 189 void adjust_pump(float current,float required) {
roselea 0:bfcb5b67b1d6 190 if (current>required)
roselea 0:bfcb5b67b1d6 191 oPump=0;
roselea 0:bfcb5b67b1d6 192 else
roselea 0:bfcb5b67b1d6 193 oPump=1;
roselea 0:bfcb5b67b1d6 194 }
roselea 0:bfcb5b67b1d6 195
roselea 0:bfcb5b67b1d6 196 void set_servo(tServo *poServo,float fTarget)
roselea 0:bfcb5b67b1d6 197 {
roselea 0:bfcb5b67b1d6 198 }
roselea 0:bfcb5b67b1d6 199 void log_values(time_t now) {
roselea 0:bfcb5b67b1d6 200 FILE *fp;
roselea 0:bfcb5b67b1d6 201 int x;
roselea 0:bfcb5b67b1d6 202 char sTime[25];
roselea 0:bfcb5b67b1d6 203 strftime (sTime,25,"%x %X",localtime(&now));
roselea 0:bfcb5b67b1d6 204 if (bSDfs_active) {
roselea 0:bfcb5b67b1d6 205 fp=fopen("/poSD/log.csv","a");
roselea 0:bfcb5b67b1d6 206 if (fp) {
roselea 0:bfcb5b67b1d6 207 oLedFileOpen=1;
roselea 0:bfcb5b67b1d6 208 fprintf(fp,"%s,",sTime); //time
roselea 0:bfcb5b67b1d6 209 for (x=0; x<NUM_THERMS; x++) fprintf(fp,"%.1f,",fTemps[x]); //fTemps
roselea 0:bfcb5b67b1d6 210 //fprintf(fp,"%d,%.1f,",HeatingOn,HeatingSetPoint); // heating data
roselea 0:bfcb5b67b1d6 211 for (x=0; x<NUM_SERVOS; x++) fprintf(fp,"%d,",oServos[x].iCurrent); //servo positions
roselea 0:bfcb5b67b1d6 212 fprintf(fp,",0\r\n");
roselea 0:bfcb5b67b1d6 213 fclose(fp);
roselea 0:bfcb5b67b1d6 214 oLedFileOpen=0;
roselea 0:bfcb5b67b1d6 215 printf("Written - ");
roselea 0:bfcb5b67b1d6 216 } else printf("Failed - ");
roselea 0:bfcb5b67b1d6 217 } else printf("SD removed - ");
roselea 0:bfcb5b67b1d6 218 fp=stdout;
roselea 0:bfcb5b67b1d6 219 fprintf(fp,"%s,",sTime); //time
roselea 0:bfcb5b67b1d6 220 for (x=0; x<NUM_THERMS; x++) fprintf(fp,"%.1f,",fTemps[x]); //fTemps
roselea 0:bfcb5b67b1d6 221 //fprintf(fp,"%d,%.1f,",HeatingOn,HeatingSetPoint); // heating data
roselea 0:bfcb5b67b1d6 222 for (x=0; x<NUM_SERVOS; x++) fprintf(fp,"%d,",oServos[x].iCurrent); //servo positions
roselea 0:bfcb5b67b1d6 223 fprintf(fp,"0\n");
roselea 0:bfcb5b67b1d6 224
roselea 0:bfcb5b67b1d6 225
roselea 0:bfcb5b67b1d6 226 }