Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Sat Mar 09 22:32:40 2019 +0000
Revision:
44:741ee27c8a34
Parent:
43:291bbdba48f3
Child:
45:bd8da8d90dbb
Added Master Slave controls.; Added I2C subroutines in boards.cpp.

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