Working Rx IRQ

Dependencies:   Control_Code_LookUp MCP23017 MODSERIAL mbed

Fork of Control_Code_LookUp by Rivian Irvine Team

Committer:
htran8
Date:
Wed Mar 07 04:39:44 2018 +0000
Revision:
2:68f92f17d445
Parent:
1:b8231536af32
Rx Working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jrodenburg 0:11aaf2483a3c 1 // MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE
jrodenburg 0:11aaf2483a3c 2 // AUTHOR: JUSTIN RODENBURG
jrodenburg 0:11aaf2483a3c 3 // DATE: SEPTEMBER 2017
jrodenburg 0:11aaf2483a3c 4
jrodenburg 0:11aaf2483a3c 5 //PLAN TO HAVE 18 CHANNELS PER MBED
htran8 2:68f92f17d445 6 //HARDWARE:
jrodenburg 0:11aaf2483a3c 7 //2 X VALVE DRIVER BOARD
jrodenburg 0:11aaf2483a3c 8 //1 X MBED
jrodenburg 0:11aaf2483a3c 9 //1 X ADC MUX (16 CHN)
jrodenburg 0:11aaf2483a3c 10
jrodenburg 1:b8231536af32 11 //FORMAT FOR CELL DATA: CHANNEL.TEMPERATURE.STATUS.E 1.23.1.E, 1.23.1.E
jrodenburg 0:11aaf2483a3c 12
jrodenburg 0:11aaf2483a3c 13 #include "mbed.h"
jrodenburg 0:11aaf2483a3c 14 #include "MCP23017.h"
jrodenburg 0:11aaf2483a3c 15 #include "MODSERIAL.h"
jrodenburg 0:11aaf2483a3c 16 #include <string>
jrodenburg 0:11aaf2483a3c 17
jrodenburg 1:b8231536af32 18 #ifdef DEBUG
jrodenburg 1:b8231536af32 19 #define printf(fmt, ...) (0)
jrodenburg 1:b8231536af32 20 #endif
jrodenburg 1:b8231536af32 21
jrodenburg 0:11aaf2483a3c 22
jrodenburg 0:11aaf2483a3c 23 DigitalOut rLed(LED1);
jrodenburg 0:11aaf2483a3c 24 DigitalOut gLed(LED2);
jrodenburg 0:11aaf2483a3c 25
jrodenburg 0:11aaf2483a3c 26 //DIGITAL CONTROL PINS
jrodenburg 0:11aaf2483a3c 27 DigitalOut valve(PTB8);
jrodenburg 0:11aaf2483a3c 28 DigitalOut heater(PTB9);
jrodenburg 0:11aaf2483a3c 29
jrodenburg 0:11aaf2483a3c 30 //Init. I2C for CE Board
jrodenburg 0:11aaf2483a3c 31 MCP23017 mcp = MCP23017(PTC9, PTC8, 0x40);
jrodenburg 0:11aaf2483a3c 32
jrodenburg 0:11aaf2483a3c 33 //GLOBAL
jrodenburg 0:11aaf2483a3c 34 // pullUp resistance
htran8 2:68f92f17d445 35 int rUp = 10000;
htran8 2:68f92f17d445 36 //LUT size
jrodenburg 0:11aaf2483a3c 37 int sizeLUT = 34;
jrodenburg 0:11aaf2483a3c 38
jrodenburg 0:11aaf2483a3c 39 //LOOK UP TABLE
jrodenburg 0:11aaf2483a3c 40 const struct LUT {
jrodenburg 0:11aaf2483a3c 41 int adc;
jrodenburg 0:11aaf2483a3c 42 int temp;
jrodenburg 0:11aaf2483a3c 43 };
jrodenburg 0:11aaf2483a3c 44
jrodenburg 0:11aaf2483a3c 45 LUT thermLUT[] = {
jrodenburg 0:11aaf2483a3c 46 {62349,-40},
jrodenburg 0:11aaf2483a3c 47 {61393,-35},
jrodenburg 0:11aaf2483a3c 48 {60223,-30},
jrodenburg 0:11aaf2483a3c 49 {58818,-25},
jrodenburg 0:11aaf2483a3c 50 {57159,-20},
jrodenburg 0:11aaf2483a3c 51 {55240,-15},
jrodenburg 0:11aaf2483a3c 52 {53054,-10},
jrodenburg 0:11aaf2483a3c 53 {50605,-5},
jrodenburg 0:11aaf2483a3c 54 {47928,0},
jrodenburg 0:11aaf2483a3c 55 {45069,5},
jrodenburg 0:11aaf2483a3c 56 {42068,10},
jrodenburg 0:11aaf2483a3c 57 {38975,15},
jrodenburg 0:11aaf2483a3c 58 {35856,20},
jrodenburg 0:11aaf2483a3c 59 {32768,25},
jrodenburg 0:11aaf2483a3c 60 {29753,30},
jrodenburg 0:11aaf2483a3c 61 {26867,35},
jrodenburg 0:11aaf2483a3c 62 {24147,40},
jrodenburg 0:11aaf2483a3c 63 {21602,45},
jrodenburg 0:11aaf2483a3c 64 {19257,50},
jrodenburg 0:11aaf2483a3c 65 {17116,55},
jrodenburg 0:11aaf2483a3c 66 {15178,60},
jrodenburg 0:11aaf2483a3c 67 {13465,65},
jrodenburg 0:11aaf2483a3c 68 {11941,70},
jrodenburg 0:11aaf2483a3c 69 {10579,75},
jrodenburg 0:11aaf2483a3c 70 {9374,80},
jrodenburg 0:11aaf2483a3c 71 {8309,85},
jrodenburg 0:11aaf2483a3c 72 {7375,90},
jrodenburg 0:11aaf2483a3c 73 {6548,95},
jrodenburg 0:11aaf2483a3c 74 {5817,100},
jrodenburg 0:11aaf2483a3c 75 {5179,105},
jrodenburg 0:11aaf2483a3c 76 {4618,110},
jrodenburg 0:11aaf2483a3c 77 {4127,115},
jrodenburg 0:11aaf2483a3c 78 {3686,120},
jrodenburg 0:11aaf2483a3c 79 {3304,125}
jrodenburg 0:11aaf2483a3c 80 };
jrodenburg 0:11aaf2483a3c 81
jrodenburg 0:11aaf2483a3c 82 //ANALOG PINS
jrodenburg 0:11aaf2483a3c 83 AnalogIn ain0(A0);
jrodenburg 0:11aaf2483a3c 84 AnalogIn ain1(A1);
jrodenburg 0:11aaf2483a3c 85 AnalogIn ain2(A2);
jrodenburg 0:11aaf2483a3c 86 AnalogIn ain3(A3);
jrodenburg 0:11aaf2483a3c 87 AnalogIn ain4(A4);
jrodenburg 0:11aaf2483a3c 88 AnalogIn ain5(A5);
jrodenburg 0:11aaf2483a3c 89
jrodenburg 0:11aaf2483a3c 90 MODSERIAL pc(USBTX, USBRX);
jrodenburg 0:11aaf2483a3c 91
jrodenburg 0:11aaf2483a3c 92 volatile int bufA_index;
jrodenburg 0:11aaf2483a3c 93 const int numChnls = 15;
jrodenburg 0:11aaf2483a3c 94 float chTemps[numChnls];
htran8 2:68f92f17d445 95 float chGoalTemps[numChnls];
jrodenburg 1:b8231536af32 96 int chStatus[numChnls];
jrodenburg 0:11aaf2483a3c 97 int numSamples = 5;
jrodenburg 0:11aaf2483a3c 98 volatile bool dataRecieved = false;
jrodenburg 1:b8231536af32 99 int chnlSel;
jrodenburg 0:11aaf2483a3c 100 char rxBuf[50];
htran8 2:68f92f17d445 101 int tempup=1;
htran8 2:68f92f17d445 102
htran8 2:68f92f17d445 103 #define RX_SOF 0x7B
htran8 2:68f92f17d445 104 #define RX_EOF 0x7D
jrodenburg 0:11aaf2483a3c 105
jrodenburg 0:11aaf2483a3c 106 void rxInterrupt(MODSERIAL_IRQ_INFO *info){
jrodenburg 0:11aaf2483a3c 107 gLed = 0;
jrodenburg 0:11aaf2483a3c 108 wait(2);
jrodenburg 0:11aaf2483a3c 109 gLed = 1;
jrodenburg 0:11aaf2483a3c 110 dataRecieved = true;
jrodenburg 0:11aaf2483a3c 111 }
jrodenburg 0:11aaf2483a3c 112
htran8 2:68f92f17d445 113 typedef struct {
htran8 2:68f92f17d445 114 unsigned char SOF_flag;
htran8 2:68f92f17d445 115 unsigned char data_len;
htran8 2:68f92f17d445 116 unsigned char chanDelim;
htran8 2:68f92f17d445 117 unsigned char chanID;
htran8 2:68f92f17d445 118 unsigned char tempDelim;
htran8 2:68f92f17d445 119 unsigned char setTemp;
htran8 2:68f92f17d445 120 unsigned char chanStatDelim;
htran8 2:68f92f17d445 121 unsigned char chanStat;
htran8 2:68f92f17d445 122 } HOST_PACKET;
htran8 2:68f92f17d445 123
jrodenburg 0:11aaf2483a3c 124 void parseRXData(){
jrodenburg 0:11aaf2483a3c 125 int pCount = 0; //count data collected
jrodenburg 0:11aaf2483a3c 126 int i = 0;
jrodenburg 1:b8231536af32 127 int chnl;
jrodenburg 0:11aaf2483a3c 128
htran8 2:68f92f17d445 129 HOST_PACKET *RxPkt;
jrodenburg 1:b8231536af32 130
htran8 2:68f92f17d445 131 chTemps[0] = 1;
htran8 2:68f92f17d445 132
htran8 2:68f92f17d445 133 string data = "";
htran8 2:68f92f17d445 134
htran8 2:68f92f17d445 135 RxPkt = (HOST_PACKET *)(rxBuf);
htran8 2:68f92f17d445 136
jrodenburg 0:11aaf2483a3c 137
htran8 2:68f92f17d445 138 pc.printf("fl = %02x\n", RxPkt->SOF_flag);
htran8 2:68f92f17d445 139 pc.printf("len = %02x\n", RxPkt->data_len);
htran8 2:68f92f17d445 140 pc.printf("ch = %02x\n", RxPkt->chanID);
htran8 2:68f92f17d445 141 pc.printf("temp = %02x\n", RxPkt->setTemp);
htran8 2:68f92f17d445 142 pc.printf("chanStat = %02x\n", RxPkt->chanStat);
htran8 2:68f92f17d445 143
htran8 2:68f92f17d445 144 tempup = 1;
htran8 2:68f92f17d445 145
htran8 2:68f92f17d445 146 // Exit if the packet does not contain correct header
htran8 2:68f92f17d445 147 if (RxPkt->SOF_flag != RX_SOF)
htran8 2:68f92f17d445 148 return;
htran8 2:68f92f17d445 149
jrodenburg 0:11aaf2483a3c 150 while(pCount < 3){
jrodenburg 0:11aaf2483a3c 151 if(rxBuf[i] != '.'){
jrodenburg 0:11aaf2483a3c 152 data += rxBuf[i];
jrodenburg 0:11aaf2483a3c 153 }
jrodenburg 0:11aaf2483a3c 154 else{
htran8 2:68f92f17d445 155 pCount++;
jrodenburg 0:11aaf2483a3c 156 if(pCount == 1){ //get channel
jrodenburg 1:b8231536af32 157 if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){
jrodenburg 0:11aaf2483a3c 158 //check if channel is out of array index
htran8 2:68f92f17d445 159 rLed = 0;
jrodenburg 0:11aaf2483a3c 160 }
jrodenburg 0:11aaf2483a3c 161 else{
jrodenburg 0:11aaf2483a3c 162 chnl = atoi(data.c_str());
jrodenburg 1:b8231536af32 163 chnl = chnl-1; //fix for array indexing
jrodenburg 1:b8231536af32 164 chnlSel = chnl;
htran8 2:68f92f17d445 165 //pc.printf("CHANNEL: %i \r\n", chnl);
jrodenburg 0:11aaf2483a3c 166 }
jrodenburg 0:11aaf2483a3c 167 }
jrodenburg 0:11aaf2483a3c 168 else if(pCount == 2){ //get channel temperature
jrodenburg 0:11aaf2483a3c 169 chGoalTemps[chnl] = atoi(data.c_str());
jrodenburg 1:b8231536af32 170 //pc.printf("TEMPERATURE: %i \r\n", atoi(data.c_str()));
jrodenburg 0:11aaf2483a3c 171 }
jrodenburg 0:11aaf2483a3c 172 else if(pCount == 3){ //get channel status
jrodenburg 1:b8231536af32 173 chStatus[chnl] = atoi(data.c_str());
htran8 2:68f92f17d445 174 //pc.printf("STATUS: %i \r\n", atoi(data.c_str()));
htran8 2:68f92f17d445 175 }
htran8 2:68f92f17d445 176 data = "";
jrodenburg 0:11aaf2483a3c 177 }
jrodenburg 0:11aaf2483a3c 178 i++;
jrodenburg 0:11aaf2483a3c 179 }
jrodenburg 1:b8231536af32 180 //wait(30);
jrodenburg 0:11aaf2483a3c 181 }
jrodenburg 0:11aaf2483a3c 182
htran8 2:68f92f17d445 183 float getTemp(float ADC_val){
htran8 2:68f92f17d445 184 int i = 0;
jrodenburg 0:11aaf2483a3c 185 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){
htran8 2:68f92f17d445 186 i++;
htran8 2:68f92f17d445 187 }
htran8 2:68f92f17d445 188
jrodenburg 0:11aaf2483a3c 189 float a = float(thermLUT[i-1].temp - thermLUT[i].temp);
jrodenburg 0:11aaf2483a3c 190 float b = float(thermLUT[i-1].adc - thermLUT[i].adc);
htran8 2:68f92f17d445 191
jrodenburg 0:11aaf2483a3c 192 float m = a/b;
jrodenburg 0:11aaf2483a3c 193 float y = (m*(ADC_val - thermLUT[i-1].adc))+thermLUT[i-1].temp;
htran8 2:68f92f17d445 194
jrodenburg 0:11aaf2483a3c 195 return y;
jrodenburg 0:11aaf2483a3c 196 }
jrodenburg 0:11aaf2483a3c 197
jrodenburg 0:11aaf2483a3c 198 //16-bit ADC --> 2^16 = 65536
jrodenburg 0:11aaf2483a3c 199 void readTemps(){
jrodenburg 0:11aaf2483a3c 200 float a_0 = 0.0;
htran8 2:68f92f17d445 201
htran8 2:68f92f17d445 202 #if 0
jrodenburg 0:11aaf2483a3c 203 for(int i=0; i<numSamples; i++){
jrodenburg 0:11aaf2483a3c 204 a_0 += ain0.read_u16();
jrodenburg 0:11aaf2483a3c 205 }
htran8 2:68f92f17d445 206
jrodenburg 0:11aaf2483a3c 207 a_0 = a_0/numSamples;
htran8 2:68f92f17d445 208
jrodenburg 1:b8231536af32 209 //printf("ADC VAL: %f \r\n", a_0);
jrodenburg 0:11aaf2483a3c 210
htran8 2:68f92f17d445 211 chTemps[0] = getTemp(a_0);
htran8 2:68f92f17d445 212 #else
htran8 2:68f92f17d445 213 a_0 = .2;
htran8 2:68f92f17d445 214 if (chTemps[0] >= 6)
htran8 2:68f92f17d445 215 tempup = 0;
htran8 2:68f92f17d445 216 if (chTemps[0] <= 0)
htran8 2:68f92f17d445 217 tempup = 1;
htran8 2:68f92f17d445 218
htran8 2:68f92f17d445 219 if (tempup)
htran8 2:68f92f17d445 220 chTemps[0] += a_0;
htran8 2:68f92f17d445 221 else
htran8 2:68f92f17d445 222 chTemps[0] -= a_0;
htran8 2:68f92f17d445 223 #endif
htran8 2:68f92f17d445 224
jrodenburg 1:b8231536af32 225 //send current temperature of selected channel to GUI for display
jrodenburg 1:b8231536af32 226 pc.printf("%f \r\n", chTemps[chnlSel]);
htran8 2:68f92f17d445 227
jrodenburg 1:b8231536af32 228 wait(1.5);
htran8 2:68f92f17d445 229
jrodenburg 1:b8231536af32 230 //printf("TEMPERATURE (C): %f \r\n", chTemps[0]);
htran8 2:68f92f17d445 231
jrodenburg 1:b8231536af32 232 //printf("TEMPERATURE (C): 15 \r\n");
htran8 2:68f92f17d445 233
jrodenburg 1:b8231536af32 234 //printf("TEMPERATURE (C): 15 \r\n");
jrodenburg 0:11aaf2483a3c 235 }
jrodenburg 0:11aaf2483a3c 236
jrodenburg 0:11aaf2483a3c 237
htran8 2:68f92f17d445 238
jrodenburg 0:11aaf2483a3c 239
jrodenburg 0:11aaf2483a3c 240 int main() {
jrodenburg 1:b8231536af32 241 //printf("Main Entered.\n");
jrodenburg 0:11aaf2483a3c 242 //turn off LEDs
jrodenburg 0:11aaf2483a3c 243 rLed = 1;
jrodenburg 0:11aaf2483a3c 244 gLed = 1;
htran8 2:68f92f17d445 245
jrodenburg 0:11aaf2483a3c 246 //configure MCP23017
jrodenburg 0:11aaf2483a3c 247 mcp.config(0,0,0);
htran8 2:68f92f17d445 248
jrodenburg 0:11aaf2483a3c 249 //
jrodenburg 0:11aaf2483a3c 250 pc.baud(9600);
htran8 2:68f92f17d445 251 //pc.autoDetectChar(RX_EOF);
htran8 2:68f92f17d445 252 pc.autoDetectChar('\n');
jrodenburg 0:11aaf2483a3c 253 pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect);
htran8 2:68f92f17d445 254
jrodenburg 0:11aaf2483a3c 255 while(1) {
jrodenburg 0:11aaf2483a3c 256 //read temperatures
jrodenburg 0:11aaf2483a3c 257 readTemps();
htran8 2:68f92f17d445 258
jrodenburg 0:11aaf2483a3c 259 //buffer for noise
jrodenburg 1:b8231536af32 260 double hyst = 0.2;
htran8 2:68f92f17d445 261
jrodenburg 0:11aaf2483a3c 262 //check if we recieved data/need to update TCTF data
jrodenburg 0:11aaf2483a3c 263 if(dataRecieved){
jrodenburg 0:11aaf2483a3c 264 dataRecieved = false;
htran8 2:68f92f17d445 265 pc.printf("Count = %d\n", pc.rxBufferGetCount());
jrodenburg 1:b8231536af32 266 pc.move(rxBuf, 50);
jrodenburg 0:11aaf2483a3c 267 parseRXData();
htran8 2:68f92f17d445 268 pc.rxBufferFlush();
htran8 2:68f92f17d445 269
jrodenburg 0:11aaf2483a3c 270 }
htran8 2:68f92f17d445 271
jrodenburg 0:11aaf2483a3c 272 //Control loop:
jrodenburg 0:11aaf2483a3c 273 //Loop through all channels and control
jrodenburg 0:11aaf2483a3c 274 //chiller = 18C
jrodenburg 0:11aaf2483a3c 275 for(int i=0; i<=numChnls; i++){
jrodenburg 0:11aaf2483a3c 276 //control chiller
jrodenburg 1:b8231536af32 277 if(chStatus[i] == 1){
jrodenburg 1:b8231536af32 278 if(chTemps[i] > ((chGoalTemps[i])+hyst)){
jrodenburg 1:b8231536af32 279 //Turn on chiller
jrodenburg 1:b8231536af32 280 //printf("CHILLER ON \r\n");
jrodenburg 1:b8231536af32 281 mcp.config(0,0,0);
htran8 2:68f92f17d445 282 mcp.write_bit(1, 8);
jrodenburg 1:b8231536af32 283 //printf("HEATER OFF \r\n");
jrodenburg 1:b8231536af32 284 mcp.write_bit(0, 9);
jrodenburg 1:b8231536af32 285 }
jrodenburg 1:b8231536af32 286 else if (chTemps[i] < ((chGoalTemps[i])-hyst)){
jrodenburg 1:b8231536af32 287 //turn off chiller
jrodenburg 1:b8231536af32 288 //printf("CHILLER OFF \r\n");
jrodenburg 1:b8231536af32 289 mcp.config(0,0,0);
htran8 2:68f92f17d445 290 mcp.write_bit(0, 8);
jrodenburg 1:b8231536af32 291 //printf("HEATER ON \r\n");
jrodenburg 1:b8231536af32 292 mcp.write_bit(1, 9);
jrodenburg 1:b8231536af32 293 }
jrodenburg 1:b8231536af32 294 else{
jrodenburg 1:b8231536af32 295 //turn off chiller
jrodenburg 1:b8231536af32 296 //printf("CHILLER OFF \r\n");
jrodenburg 1:b8231536af32 297 mcp.config(0,0,0);
htran8 2:68f92f17d445 298 mcp.write_bit(0, 8);
jrodenburg 1:b8231536af32 299 //printf("HEATER OFF \r\n");
jrodenburg 1:b8231536af32 300 mcp.write_bit(0, 9);
jrodenburg 1:b8231536af32 301 }
htran8 2:68f92f17d445 302
htran8 2:68f92f17d445 303
jrodenburg 0:11aaf2483a3c 304 }
jrodenburg 1:b8231536af32 305 wait(0);
jrodenburg 0:11aaf2483a3c 306 }
jrodenburg 0:11aaf2483a3c 307 }
jrodenburg 0:11aaf2483a3c 308 }