Treehouse Mbed Team / Mbed 2 deprecated 1U5_proto_X

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //-------------------------------------------------------------------------------
00002 // 
00003 //  Treehouse Designs Inc.
00004 //  Colorado Springs, Colorado
00005 // 
00006 //  Copyright (c) 2018 by Treehouse Designs Inc. 
00007 //  Copyright (c) 2018 by Agility Power Systems Inc. 
00008 // 
00009 //  This code is the property of Treehouse Designs, Inc. (Treehouse) and
00010 //  Agility Power Systems Inc. (Agility) and may not be redistributed
00011 //  in any form without prior written permission from 
00012 //  both copyright holders, Treehouse and Agility.
00013 //
00014 //  The above copyright notice and this permission notice shall be included in
00015 //  all copies or substantial portions of the Software.
00016 // 
00017 //   
00018 //-------------------------------------------------------------------------------
00019 // 
00020 //  REVISION HISTORY:
00021 //  
00022 //   $Author: $
00023 //   $Rev: $
00024 //   $Date: $
00025 //   $URL: $
00026 // 
00027 //-------------------------------------------------------------------------------
00028 
00029 #include "mbed.h"
00030 #include "math.h"
00031 #include "globals.h"
00032 #include "parameters.h"
00033 #include "all_io.h"
00034 #include "stdio.h"
00035 #include "stdlib.h"
00036 #include "serial.h"
00037 #include "adc.h"
00038 #include "adc_defs.h"
00039 #include "menu.h"
00040 #include "boards.h"
00041 #include "command.h"
00042 #include "clocks.h"
00043 //#include "th_rtz.h"
00044 #include "temp.h"
00045 
00046 #define IS_MASTER
00047 //#define IS_SLAVE
00048 
00049 //DigitalOut myled(LED1);
00050 //Ticker slowClk;
00051 //Ticker fastClk;
00052 Ticker stepup;
00053 
00054 unsigned short my12=0;
00055 volatile bool updateReady = FALSE;
00056 volatile bool pulseReady = FALSE;
00057 unsigned int loopTime = 0;
00058 bool raw = FALSE;
00059 bool running = FALSE;
00060 bool testing = FALSE;
00061 bool buck = TRUE;
00062 bool pcConnected = FALSE;   //New for 1U5
00063 bool master = FALSE;        //New for 1U5
00064 bool slave1 = FALSE;        //New for 1U5
00065 bool slave2 = FALSE;        //New for 1U5
00066 
00067 unsigned int max_rows = 2048;
00068 unsigned int bCodeRow[13];
00069 unsigned int max_boards = 3;
00070 unsigned short row = 0;
00071 int row_test = 0;
00072 unsigned short slave_code = 0;
00073 unsigned short new_slave_code = 0;
00074 
00075 double CURRENT_48_OFFSET = 33940;
00076 double CURRENT_24_OFFSET = 33580;
00077 double CURRENT_12_OFFSET = 57700;
00078 
00079 float temperatureVal;
00080 
00081 //void fastFlip(unsigned short numFast){
00082 //    if(numFast >= 1){ec0 = ~ec0;}
00083 //    if(numFast >= 2){ec1 = ~ec1;}
00084 //    ...
00085 //    if(numFast >= 36){ec35 = ~ec35;}
00086 //}
00087 
00088 //void slowFlip(unsigned short numFast){
00089 //    numSlow = MAX_CHIPS - numFast;
00090 //    if( numSlow  >= 36){ec35 = ~ec35;}else{return;}
00091 //    if( numSlow  >= 35){ec34 = ~ec34;}else{return;}
00092 //    ...
00093 //    if( numSlow  >= 1){ec0 = ~ec0;}
00094 //}
00095 
00096 
00097 
00098 int main() {    
00099     
00100     /*if(MS){ //New for 1U5
00101         #ifdef IS_MASTER
00102           master = TRUE;
00103         #endif
00104         #ifdef IS_SLAVE
00105           slave1 = TRUE;
00106           slave2 = FALSE;
00107         #endif
00108     }else{
00109         #ifdef IS_SLAVE
00110           slave1 = FALSE;
00111           slave2 = TRUE;
00112         #endif
00113     }*/
00114     
00115     //Slave_Init();
00116     
00117     initOut();
00118     //Serial dcm2(PC_10, PC_11, 9600);
00119     //char storage;
00120     
00121     fan_pwm.period(1.0f);
00122     //slowClk.attach_us(&slowFlip, CLK_SLOW);
00123     //fastClk.attach_us(&fastFlip, CLK_FAST);
00124     //stepup.attach_us(&progress, CLK_PROG);
00125     ILR_enable.rise(&ILR);
00126     
00127     vdac = vdSet;
00128 
00129     running = FALSE;
00130     
00131     
00132     temperatureVal = GetTemperature();
00133     runFan(temperatureVal);
00134     wait(1);
00135    
00136     initSerial();
00137     
00138     //initI2C();
00139     
00140     initDRT(); // Display Refresh Timer
00141    
00142     initADC();
00143 
00144     struct adcValues adcVals = getADCresults();
00145     struct displayValues dispVals = calcDisplayValues(adcVals);
00146     struct statusValues statVals = checkLevels(adcVals);
00147    
00148     row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
00149     unsigned int old_row = row;
00150    
00151     menu_banner();
00152     sprintf(strbuf, "\r\nPress Enter to continue");
00153     sendSerial(strbuf);
00154     //while(waitCommand()){ // Wait for user to press Enter
00155     //}
00156    
00157     menuRedraw(WITH_PROMPT);
00158    
00159     initBoards(adcVals);
00160     
00161     en_out_code = 2;
00162     setClocks(en_out_code);
00163 
00164     while (1)
00165     {
00166       #ifdef IS_MASTER
00167       
00168       adcVals = getADCresults();
00169         
00170       //temperatureVal = getTEMPERATUREresults();
00171       //setFan(temperatureVal);
00172       temperatureVal = GetTemperature();
00173       runFan(temperatureVal);
00174       
00175       dispVals = calcDisplayValues(adcVals); 
00176       
00177       statVals = checkLevels(adcVals); 
00178       
00179       processCommand();
00180       
00181       //dcm2.printf("Hello?");
00182       /*if(dcm2.readable()) {
00183           storage = dcm2.getc();
00184           storage += 1;
00185           dcm2.putc(storage);
00186       }*/
00187       
00188       /*char men = 'm';
00189       char bor = 'b';
00190       if (dcm2.readable()) {
00191           storage = dcm2.getc();
00192           if (storage == men) {
00193               dcm2.printf("This is the demo menu.\n");
00194           } else if (storage == bor) {
00195               dcm2.printf(" %d\n", boardsActive);
00196           }
00197       }*/
00198       
00199       // Select RUN from menu to activate running mode.
00200       // BRDS, MULT activate testing mode. MY12 emulates running mode wthout being in running mode.
00201       // CAL and UNCAL do not change state of running or testing.
00202       if(adcVals.i12 < CURRENT_12_OFFSET){    
00203             row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
00204             buck = TRUE;
00205       }else{
00206             row = (unsigned short)(dispVals.i12f*dispVals.v12f*(-ROW_CORRECTION_FACTOR));
00207             buck = FALSE;
00208       }
00209       row_test = abs((int)(row-old_row));
00210       //updateTerminal(adcVals, statVals);
00211       if(updateReady){
00212          updateReady = FALSE;
00213          updateTerminal(adcVals, statVals);  // May want to gate this call when we run a headless system.
00214 
00215       }
00216       #endif
00217       
00218       /*checkFlip();
00219       if(running && master){ //New for 1U5
00220           //The current sensors results are single-ended. Results below the midpoint are for buck mode, above is for boost mode.
00221             if(row_test>ROW_HYSTERESIS){
00222                 old_row = row;
00223                 if(row<=1023){
00224                     slave_code = updateMasterControls(row);    //New for 1U5
00225                     sendSlaveCommands(slave_code);  //New for 1U5
00226                 }
00227             }
00228         }
00229       else if(running && !master){  //New for 1U5
00230             new_slave_code = getMasterCommands();    //New for 1U5
00231             if(new_slave_code != slave_code){
00232                 slave_code = new_slave_code;
00233                 updateSlaveControls(slave_code);     //New for 1U5
00234             //}
00235         }
00236       }else if(!testing){
00237           row_test = abs((int)(my12-old_row));
00238           if(row_test>ROW_HYSTERESIS){
00239              old_row = my12;
00240              if(row<=1023){
00241                  updateMasterControls(my12);
00242              }
00243           }
00244           numFast = 36;
00245       }*/
00246     }//end while(1)
00247 }//end void main(void)