Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Sun Mar 10 17:56:52 2019 +0000
Revision:
45:bd8da8d90dbb
Parent:
44:741ee27c8a34
Child:
47:d10bf65cb7dc
Added numFast, changed LUT_1.

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 volatile bool dosFlip = FALSE;
Slord2142 41:bd54c88e3334 71 volatile bool dofFlip = FALSE;
Slord2142 41:bd54c88e3334 72
Slord2142 41:bd54c88e3334 73 void slowFlip() {
Slord2142 41:bd54c88e3334 74 dosFlip = TRUE;
Slord2142 41:bd54c88e3334 75 }
Slord2142 41:bd54c88e3334 76
Slord2142 41:bd54c88e3334 77 void fastFlip() {
Slord2142 41:bd54c88e3334 78 dofFlip = TRUE;
Slord2142 41:bd54c88e3334 79 }
mfwic 25:8bcc8bea0e31 80
mfwic 45:bd8da8d90dbb 81 void fastFlip(unsigned short numFast){
mfwic 45:bd8da8d90dbb 82 if(numFast >= 1){ec0 = ~ec0;}
mfwic 45:bd8da8d90dbb 83 if(numFast >= 2){ec1 = ~ec1;}
mfwic 45:bd8da8d90dbb 84 ...
mfwic 45:bd8da8d90dbb 85 if(numFast >= 36){ec35 = ~ec35;}
mfwic 45:bd8da8d90dbb 86 }
mfwic 45:bd8da8d90dbb 87
mfwic 45:bd8da8d90dbb 88 void slowFlip(unsigned short numFast){
mfwic 45:bd8da8d90dbb 89 numSlow = MAX_CHIPS - numFast;
mfwic 45:bd8da8d90dbb 90 if( numSlow >= 36){ec35 = ~ec35;}else{return;}
mfwic 45:bd8da8d90dbb 91 if( numSlow >= 35){ec34 = ~ec34;}else{return;}
mfwic 45:bd8da8d90dbb 92 ...
mfwic 45:bd8da8d90dbb 93 if( numSlow >= 1){ec0 = ~ec0;}
mfwic 45:bd8da8d90dbb 94 }
mfwic 45:bd8da8d90dbb 95
mfwic 44:741ee27c8a34 96
mfwic 44:741ee27c8a34 97
mfwic 44:741ee27c8a34 98 int main() {
mfwic 44:741ee27c8a34 99
mfwic 44:741ee27c8a34 100 if(MS){ //New for 1U5
mfwic 44:741ee27c8a34 101 master = TRUE;
mfwic 44:741ee27c8a34 102 }else{
mfwic 44:741ee27c8a34 103 master = FALSE;
mfwic 44:741ee27c8a34 104 }
Slord2142 41:bd54c88e3334 105
Slord2142 41:bd54c88e3334 106 initOut();
Slord2142 41:bd54c88e3334 107
Slord2142 41:bd54c88e3334 108 slowClk.attach(&slowFlip, 0.0003);
Slord2142 41:bd54c88e3334 109 fastClk.attach(&fastFlip, 0.00001);
mfwic 43:291bbdba48f3 110
mfwic 43:291bbdba48f3 111 running = FALSE;
mfwic 43:291bbdba48f3 112
mfwic 43:291bbdba48f3 113 initSerial();
Slord2142 41:bd54c88e3334 114
mfwic 44:741ee27c8a34 115 initI2C();
mfwic 44:741ee27c8a34 116
mfwic 43:291bbdba48f3 117 initDRT(); // Display Refresh Timer
mfwic 43:291bbdba48f3 118
mfwic 43:291bbdba48f3 119 initADC();
mfwic 43:291bbdba48f3 120
mfwic 43:291bbdba48f3 121 struct adcValues adcVals = getADCresults();
mfwic 43:291bbdba48f3 122 struct displayValues dispVals = calcDisplayValues(adcVals);
mfwic 43:291bbdba48f3 123 struct statusValues statVals = checkLevels(adcVals);
mfwic 43:291bbdba48f3 124
mfwic 43:291bbdba48f3 125 row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
mfwic 43:291bbdba48f3 126 unsigned int old_row = row;
mfwic 43:291bbdba48f3 127
mfwic 43:291bbdba48f3 128 menu_banner();
mfwic 43:291bbdba48f3 129 sprintf(strbuf, "\r\nPress Enter to continue");
mfwic 43:291bbdba48f3 130 sendSerial(strbuf);
mfwic 43:291bbdba48f3 131 //while(waitCommand()){ // Wait for user to press Enter
mfwic 43:291bbdba48f3 132 //}
mfwic 43:291bbdba48f3 133
mfwic 43:291bbdba48f3 134 menuRedraw(WITH_PROMPT);
mfwic 43:291bbdba48f3 135
mfwic 43:291bbdba48f3 136 initBoards(adcVals);
mfwic 43:291bbdba48f3 137
mfwic 43:291bbdba48f3 138 while (1)
mfwic 43:291bbdba48f3 139 {
mfwic 43:291bbdba48f3 140 adcVals = getADCresults();
mfwic 43:291bbdba48f3 141
mfwic 43:291bbdba48f3 142 dispVals = calcDisplayValues(adcVals);
mfwic 43:291bbdba48f3 143
mfwic 43:291bbdba48f3 144 statVals = checkLevels(adcVals);
mfwic 43:291bbdba48f3 145
mfwic 43:291bbdba48f3 146 processCommand();
mfwic 43:291bbdba48f3 147
mfwic 43:291bbdba48f3 148 checkFlip();
mfwic 43:291bbdba48f3 149
mfwic 43:291bbdba48f3 150 // Select RUN from menu to activate running mode.
mfwic 43:291bbdba48f3 151 // BRDS, MULT activate testing mode. MY12 emulates running mode wthout being in running mode.
mfwic 43:291bbdba48f3 152 // CAL and UNCAL do not change state of running or testing.
mfwic 43:291bbdba48f3 153 if(adcVals.i12 < CURRENT_12_OFFSET){
mfwic 43:291bbdba48f3 154 row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
mfwic 43:291bbdba48f3 155 buck = TRUE;
mfwic 43:291bbdba48f3 156 }else{
mfwic 43:291bbdba48f3 157 row = (unsigned short)(dispVals.i12f*dispVals.v12f*(-ROW_CORRECTION_FACTOR));
mfwic 43:291bbdba48f3 158 buck = FALSE;
mfwic 43:291bbdba48f3 159 }
mfwic 43:291bbdba48f3 160 row_test = abs((int)(row-old_row));
mfwic 44:741ee27c8a34 161 if(running && master){ //New for 1U5
mfwic 43:291bbdba48f3 162 //The current sensors results are single-ended. Results below the midpoint are for buck mode, above is for boost mode.
mfwic 44:741ee27c8a34 163 if(row_test>ROW_HYSTERESIS){
mfwic 44:741ee27c8a34 164 old_row = row;
mfwic 44:741ee27c8a34 165 if(row<=1023){
mfwic 44:741ee27c8a34 166 slave_code = updateMasterControls(row); //New for 1U5
mfwic 44:741ee27c8a34 167 sendSlaveCommands(slave_code); //New for 1U5
mfwic 44:741ee27c8a34 168 }
mfwic 43:291bbdba48f3 169 }
mfwic 44:741ee27c8a34 170 else if(running && !master){ //New for 1U5
mfwic 44:741ee27c8a34 171 slave_code = getMasterCommands(); //New for 1U5
mfwic 44:741ee27c8a34 172 updateSlaveControls(slave_code); //New for 1U5
mfwic 44:741ee27c8a34 173 }
mfwic 43:291bbdba48f3 174 }else if(!testing){
mfwic 43:291bbdba48f3 175 row_test = abs((int)(my12-old_row));
mfwic 43:291bbdba48f3 176 if(row_test>ROW_HYSTERESIS){
mfwic 43:291bbdba48f3 177 old_row = my12;
mfwic 43:291bbdba48f3 178 if(row<=1023){
mfwic 44:741ee27c8a34 179 updateMasterControls(my12);
mfwic 43:291bbdba48f3 180 }
mfwic 43:291bbdba48f3 181 }
mfwic 43:291bbdba48f3 182 }
mfwic 43:291bbdba48f3 183
mfwic 43:291bbdba48f3 184 if(updateReady && pcConnected){
mfwic 43:291bbdba48f3 185 updateReady = FALSE;
mfwic 43:291bbdba48f3 186 updateTerminal(adcVals, statVals); // May want to gate this call when we run a headless system.
mfwic 43:291bbdba48f3 187
mfwic 43:291bbdba48f3 188 }
mfwic 43:291bbdba48f3 189 }//end while(1)
mfwic 43:291bbdba48f3 190 }//end void main(void)