Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
Slord2142
Date:
Mon Mar 11 18:57:14 2019 +0000
Revision:
48:8e9de9ff1f22
Parent:
47:d10bf65cb7dc
Child:
49:65c714a5def2
Commented out all I2C methods and variables to allow short term operation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfwic 0:44a3005d4f20 1 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 2 //
mfwic 0:44a3005d4f20 3 // Treehouse Designs Inc.
mfwic 0:44a3005d4f20 4 // Colorado Springs, Colorado
mfwic 0:44a3005d4f20 5 //
Slord2142 41:bd54c88e3334 6 // Copyright (c) 2018 by Treehouse Designs Inc.
mfwic 0:44a3005d4f20 7 // Copyright (c) 2018 by Agility Power Systems Inc.
mfwic 0:44a3005d4f20 8 //
mfwic 0:44a3005d4f20 9 // This code is the property of Treehouse Designs, Inc. (Treehouse) and
mfwic 0:44a3005d4f20 10 // Agility Power Systems Inc. (Agility) and may not be redistributed
mfwic 0:44a3005d4f20 11 // in any form without prior written permission from
mfwic 0:44a3005d4f20 12 // both copyright holders, Treehouse and Agility.
mfwic 0:44a3005d4f20 13 //
mfwic 0:44a3005d4f20 14 // The above copyright notice and this permission notice shall be included in
mfwic 0:44a3005d4f20 15 // all copies or substantial portions of the Software.
mfwic 0:44a3005d4f20 16 //
mfwic 0:44a3005d4f20 17 //
mfwic 0:44a3005d4f20 18 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 19 //
mfwic 0:44a3005d4f20 20 // REVISION HISTORY:
mfwic 0:44a3005d4f20 21 //
mfwic 0:44a3005d4f20 22 // $Author: $
mfwic 0:44a3005d4f20 23 // $Rev: $
mfwic 0:44a3005d4f20 24 // $Date: $
mfwic 0:44a3005d4f20 25 // $URL: $
mfwic 0:44a3005d4f20 26 //
mfwic 0:44a3005d4f20 27 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 28
mfwic 0:44a3005d4f20 29 #include "mbed.h"
mfwic 42:3ae73b61f657 30 #include "math.h"
mfwic 42:3ae73b61f657 31 #include "globals.h"
mfwic 42:3ae73b61f657 32 #include "parameters.h"
mfwic 0:44a3005d4f20 33 #include "all_io.h"
mfwic 42:3ae73b61f657 34 #include "stdio.h"
mfwic 42:3ae73b61f657 35 #include "stdlib.h"
mfwic 42:3ae73b61f657 36 #include "serial.h"
mfwic 42:3ae73b61f657 37 #include "adc.h"
mfwic 42:3ae73b61f657 38 #include "adc_defs.h"
mfwic 42:3ae73b61f657 39 #include "menu.h"
mfwic 42:3ae73b61f657 40 #include "boards.h"
mfwic 42:3ae73b61f657 41 #include "command.h"
mfwic 42:3ae73b61f657 42 #include "clocks.h"
Slord2142 41:bd54c88e3334 43
Slord2142 41:bd54c88e3334 44
Slord2142 41:bd54c88e3334 45 //DigitalOut myled(LED1);
Slord2142 41:bd54c88e3334 46 Ticker slowClk;
Slord2142 41:bd54c88e3334 47 Ticker fastClk;
Slord2142 41:bd54c88e3334 48
mfwic 42:3ae73b61f657 49 unsigned short my12=0;
mfwic 42:3ae73b61f657 50 volatile bool updateReady = FALSE;
mfwic 42:3ae73b61f657 51 unsigned int loopTime = 0;
mfwic 42:3ae73b61f657 52 bool raw = FALSE;
mfwic 42:3ae73b61f657 53 bool running = FALSE;
mfwic 42:3ae73b61f657 54 bool testing = FALSE;
mfwic 42:3ae73b61f657 55 bool buck = TRUE;
mfwic 44:741ee27c8a34 56 bool pcConnected = FALSE; //New for 1U5
mfwic 44:741ee27c8a34 57 bool master = FALSE; //New for 1U5
mfwic 44:741ee27c8a34 58
mfwic 43:291bbdba48f3 59 unsigned int max_rows = 2048;
mfwic 43:291bbdba48f3 60 unsigned int bCodeRow[13];
mfwic 44:741ee27c8a34 61 unsigned short max_boards = 3;
mfwic 43:291bbdba48f3 62 unsigned short row = 0;
mfwic 44:741ee27c8a34 63 short row_test = 0;
mfwic 44:741ee27c8a34 64 unsigned short slave_code = 0;
mfwic 42:3ae73b61f657 65
mfwic 42:3ae73b61f657 66 double CURRENT_48_OFFSET = 33940;
mfwic 42:3ae73b61f657 67 double CURRENT_24_OFFSET = 33580;
mfwic 42:3ae73b61f657 68 double CURRENT_12_OFFSET = 33256;
mfwic 1:9f8583ba2431 69
Slord2142 41:bd54c88e3334 70
mfwic 25:8bcc8bea0e31 71
Slord2142 47:d10bf65cb7dc 72 //void fastFlip(unsigned short numFast){
Slord2142 47:d10bf65cb7dc 73 // if(numFast >= 1){ec0 = ~ec0;}
Slord2142 47:d10bf65cb7dc 74 // if(numFast >= 2){ec1 = ~ec1;}
Slord2142 47:d10bf65cb7dc 75 // ...
Slord2142 47:d10bf65cb7dc 76 // if(numFast >= 36){ec35 = ~ec35;}
Slord2142 47:d10bf65cb7dc 77 //}
mfwic 45:bd8da8d90dbb 78
Slord2142 47:d10bf65cb7dc 79 //void slowFlip(unsigned short numFast){
Slord2142 47:d10bf65cb7dc 80 // numSlow = MAX_CHIPS - numFast;
Slord2142 47:d10bf65cb7dc 81 // if( numSlow >= 36){ec35 = ~ec35;}else{return;}
Slord2142 47:d10bf65cb7dc 82 // if( numSlow >= 35){ec34 = ~ec34;}else{return;}
Slord2142 47:d10bf65cb7dc 83 // ...
Slord2142 47:d10bf65cb7dc 84 // if( numSlow >= 1){ec0 = ~ec0;}
Slord2142 47:d10bf65cb7dc 85 //}
mfwic 45:bd8da8d90dbb 86
mfwic 44:741ee27c8a34 87
mfwic 44:741ee27c8a34 88
mfwic 44:741ee27c8a34 89 int main() {
mfwic 44:741ee27c8a34 90
mfwic 44:741ee27c8a34 91 if(MS){ //New for 1U5
mfwic 44:741ee27c8a34 92 master = TRUE;
mfwic 44:741ee27c8a34 93 }else{
mfwic 44:741ee27c8a34 94 master = FALSE;
mfwic 44:741ee27c8a34 95 }
Slord2142 41:bd54c88e3334 96
Slord2142 41:bd54c88e3334 97 initOut();
Slord2142 41:bd54c88e3334 98
Slord2142 48:8e9de9ff1f22 99 slowClk.attach(&slowFlip, 2.5);
Slord2142 48:8e9de9ff1f22 100 fastClk.attach(&fastFlip, 1.0);
mfwic 43:291bbdba48f3 101
mfwic 43:291bbdba48f3 102 running = FALSE;
mfwic 43:291bbdba48f3 103
mfwic 43:291bbdba48f3 104 initSerial();
Slord2142 41:bd54c88e3334 105
mfwic 44:741ee27c8a34 106 initI2C();
mfwic 44:741ee27c8a34 107
mfwic 43:291bbdba48f3 108 initDRT(); // Display Refresh Timer
mfwic 43:291bbdba48f3 109
mfwic 43:291bbdba48f3 110 initADC();
mfwic 43:291bbdba48f3 111
mfwic 43:291bbdba48f3 112 struct adcValues adcVals = getADCresults();
mfwic 43:291bbdba48f3 113 struct displayValues dispVals = calcDisplayValues(adcVals);
mfwic 43:291bbdba48f3 114 struct statusValues statVals = checkLevels(adcVals);
mfwic 43:291bbdba48f3 115
mfwic 43:291bbdba48f3 116 row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
mfwic 43:291bbdba48f3 117 unsigned int old_row = row;
mfwic 43:291bbdba48f3 118
mfwic 43:291bbdba48f3 119 menu_banner();
mfwic 43:291bbdba48f3 120 sprintf(strbuf, "\r\nPress Enter to continue");
mfwic 43:291bbdba48f3 121 sendSerial(strbuf);
mfwic 43:291bbdba48f3 122 //while(waitCommand()){ // Wait for user to press Enter
mfwic 43:291bbdba48f3 123 //}
mfwic 43:291bbdba48f3 124
mfwic 43:291bbdba48f3 125 menuRedraw(WITH_PROMPT);
mfwic 43:291bbdba48f3 126
mfwic 43:291bbdba48f3 127 initBoards(adcVals);
mfwic 43:291bbdba48f3 128
mfwic 43:291bbdba48f3 129 while (1)
mfwic 43:291bbdba48f3 130 {
mfwic 43:291bbdba48f3 131 adcVals = getADCresults();
mfwic 43:291bbdba48f3 132
mfwic 43:291bbdba48f3 133 dispVals = calcDisplayValues(adcVals);
mfwic 43:291bbdba48f3 134
mfwic 43:291bbdba48f3 135 statVals = checkLevels(adcVals);
mfwic 43:291bbdba48f3 136
mfwic 43:291bbdba48f3 137 processCommand();
mfwic 43:291bbdba48f3 138
mfwic 43:291bbdba48f3 139 checkFlip();
mfwic 43:291bbdba48f3 140
mfwic 43:291bbdba48f3 141 // Select RUN from menu to activate running mode.
mfwic 43:291bbdba48f3 142 // BRDS, MULT activate testing mode. MY12 emulates running mode wthout being in running mode.
mfwic 43:291bbdba48f3 143 // CAL and UNCAL do not change state of running or testing.
mfwic 43:291bbdba48f3 144 if(adcVals.i12 < CURRENT_12_OFFSET){
mfwic 43:291bbdba48f3 145 row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
mfwic 43:291bbdba48f3 146 buck = TRUE;
mfwic 43:291bbdba48f3 147 }else{
mfwic 43:291bbdba48f3 148 row = (unsigned short)(dispVals.i12f*dispVals.v12f*(-ROW_CORRECTION_FACTOR));
mfwic 43:291bbdba48f3 149 buck = FALSE;
mfwic 43:291bbdba48f3 150 }
mfwic 43:291bbdba48f3 151 row_test = abs((int)(row-old_row));
mfwic 44:741ee27c8a34 152 if(running && master){ //New for 1U5
mfwic 43:291bbdba48f3 153 //The current sensors results are single-ended. Results below the midpoint are for buck mode, above is for boost mode.
mfwic 44:741ee27c8a34 154 if(row_test>ROW_HYSTERESIS){
mfwic 44:741ee27c8a34 155 old_row = row;
mfwic 44:741ee27c8a34 156 if(row<=1023){
mfwic 44:741ee27c8a34 157 slave_code = updateMasterControls(row); //New for 1U5
mfwic 44:741ee27c8a34 158 sendSlaveCommands(slave_code); //New for 1U5
mfwic 44:741ee27c8a34 159 }
mfwic 43:291bbdba48f3 160 }
mfwic 44:741ee27c8a34 161 else if(running && !master){ //New for 1U5
mfwic 44:741ee27c8a34 162 slave_code = getMasterCommands(); //New for 1U5
mfwic 44:741ee27c8a34 163 updateSlaveControls(slave_code); //New for 1U5
mfwic 44:741ee27c8a34 164 }
mfwic 43:291bbdba48f3 165 }else if(!testing){
mfwic 43:291bbdba48f3 166 row_test = abs((int)(my12-old_row));
mfwic 43:291bbdba48f3 167 if(row_test>ROW_HYSTERESIS){
mfwic 43:291bbdba48f3 168 old_row = my12;
mfwic 43:291bbdba48f3 169 if(row<=1023){
mfwic 44:741ee27c8a34 170 updateMasterControls(my12);
mfwic 43:291bbdba48f3 171 }
mfwic 43:291bbdba48f3 172 }
mfwic 43:291bbdba48f3 173 }
mfwic 43:291bbdba48f3 174
mfwic 43:291bbdba48f3 175 if(updateReady && pcConnected){
mfwic 43:291bbdba48f3 176 updateReady = FALSE;
mfwic 43:291bbdba48f3 177 updateTerminal(adcVals, statVals); // May want to gate this call when we run a headless system.
mfwic 43:291bbdba48f3 178
mfwic 43:291bbdba48f3 179 }
mfwic 43:291bbdba48f3 180 }//end while(1)
mfwic 43:291bbdba48f3 181 }//end void main(void)