CPS-Lab* / Mbed 2 deprecated lab6

Dependencies:   ADXL362 mbed MPL3115A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ADXL362.h"
00003 #include "string.h"
00004 #include "MPL3115A2.h"
00005 #include <stdio.h>
00006 #include <time.h>
00007  
00008 DigitalOut led3(LED3);
00009 Serial pc(USBTX, USBRX);
00010 //AnalogIn gpio(D9);
00011 
00012 //map of register values and names for the adxl
00013 int aregids[34];
00014 char* aregnames[34];
00015 //populating the map
00016 void initmap()
00017 {
00018     aregids[0] = 0x00;
00019     aregids[1] = 0x01;
00020     aregids[2] = 0x02;
00021     aregids[3] = 0x03;
00022     aregids[4] = 0x08;
00023     aregids[5] = 0x09;
00024     aregids[6] = 0x0A;
00025     aregids[7] = 0x0B;
00026     aregids[8] = 0x0c;
00027     aregids[9] = 0x0D;
00028     aregids[10] = 0x0E;
00029     aregids[11] = 0x0F;
00030     aregids[12] = 0x10;
00031     aregids[13] = 0x11;
00032     aregids[14] = 0x12;
00033     aregids[15] = 0x13;
00034     aregids[16] = 0x14;
00035     aregids[17] = 0x15;
00036     aregids[18] = 0x1F;
00037     aregids[19] = 0x20;
00038     aregids[20] = 0x21;
00039     aregids[21] = 0x22;
00040     aregids[22] = 0x23;
00041     aregids[23] = 0x24;
00042     aregids[24] = 0x25;
00043     aregids[25] = 0x26;
00044     aregids[26] = 0x27;
00045     aregids[27] = 0x28;
00046     aregids[28] = 0x29;
00047     aregids[29] = 0x2A;
00048     aregids[30] = 0x2B;
00049     aregids[31] = 0x2C;
00050     aregids[32] = 0x2D;
00051     aregids[33] = 0x2E;
00052 
00053     aregnames[0] = "DEVID_AD";
00054     aregnames[1] = "DEVID_MST";
00055     aregnames[2] = "PARTID";
00056     aregnames[3] = "REVID";
00057     aregnames[4] = "XDATA";
00058     aregnames[5] = "YDATA";
00059     aregnames[6] = "ZDATA";
00060     aregnames[7] = "STATUS";
00061     aregnames[8] = "FIFO_ENTRIES_L";
00062     aregnames[9] = "FIFO_ENTRIES_H";
00063     aregnames[10] = "XDATA_L";
00064     aregnames[11] = "XDATA_H";
00065     aregnames[12] = "YDATA_L";
00066     aregnames[13] = "YDATA_H";
00067     aregnames[14] = "ZDATA_L";
00068     aregnames[15] = "ZDATA_H";
00069     aregnames[16] = "TEMP_L";
00070     aregnames[17] = "TEMP_H";
00071     aregnames[18] = "SOFT_RESET";
00072     aregnames[19] = "THRESH_ACT_L";
00073     aregnames[20] = "THRESH_ACT_H";
00074     aregnames[21] = "TIME_ACT";
00075     aregnames[22] = "THRESH_INACT_L";
00076     aregnames[23] = "THRESH_INACT_H";
00077     aregnames[24] = "TIME_INACT_L";
00078     aregnames[25] = "TIME_INACT_H";
00079     aregnames[26] = "ACT_INACT_CTL";
00080     aregnames[27] = "FIFO_CONTROL";
00081     aregnames[28] = "FIFO_SAMPLES";
00082     aregnames[29] = "INTMAP1";
00083     aregnames[30] = "INTMAP2";
00084     aregnames[31] = "FILTER_CTL";
00085     aregnames[32] = "POWER_CTL";
00086     aregnames[33] = "SELF_TEST";
00087 }
00088 
00089 //map of register values and names for the adxl
00090 char mregids[46];
00091 char* mregnames[46];
00092 //populating the map
00093 void minitmap()
00094 {
00095     mregids[0] = 0x00;
00096     mregids[1] = 0x01;
00097     mregids[2] = 0x02;
00098     mregids[3] = 0x03;
00099     mregids[4] = 0x04;
00100     mregids[5] = 0x05;
00101     mregids[6] = 0x06;
00102     mregids[7] = 0x07;
00103     mregids[8] = 0x08;
00104     mregids[9] = 0x09;
00105     mregids[10] = 0x0A;
00106     mregids[11] = 0x0B;
00107     mregids[12] = 0x0C;
00108     mregids[13] = 0x0D;
00109     mregids[14] = 0x0E;
00110     mregids[15] = 0x0F;
00111     mregids[16] = 0x10;
00112     mregids[17] = 0x11;
00113     mregids[18] = 0x12;
00114     mregids[19] = 0x13;
00115     mregids[20] = 0x14;
00116     mregids[21] = 0x15;
00117     mregids[22] = 0x16;
00118     mregids[23] = 0x17;
00119     mregids[24] = 0x18;
00120     mregids[25] = 0x19;
00121     mregids[26] = 0x1A;
00122     mregids[27] = 0x1B;
00123     mregids[28] = 0x1C;
00124     mregids[29] = 0x1D;
00125     mregids[30] = 0x1E;
00126     mregids[31] = 0x1F;
00127     mregids[32] = 0x20;
00128     mregids[33] = 0x21;
00129     mregids[34] = 0x22;
00130     mregids[35] = 0x23;
00131     mregids[36] = 0x24;
00132     mregids[37] = 0x25;
00133     mregids[38] = 0x26;
00134     mregids[39] = 0x27;
00135     mregids[40] = 0x28;
00136     mregids[41] = 0x29;
00137     mregids[42] = 0x2A;
00138     mregids[43] = 0x2B;
00139     mregids[44] = 0x2C;
00140     mregids[45] = 0x2D;
00141 
00142     mregnames[0] = "STATUS";
00143     mregnames[1] = "OUT_P_MSB";
00144     mregnames[2] = "OUT_P_CSB";
00145     mregnames[3] = "OUT_P_LSB";
00146     mregnames[4] = "OUT_T_MSB";
00147     mregnames[5] = "OUT_T_LSB";
00148     mregnames[6] = "DR_STATUS";
00149     mregnames[7] = "OUT_P_DELTA_MSB";
00150     mregnames[8] = "OUT_P_DELTA_CSB";
00151     mregnames[9] = "OUT_T_DELTA_MSB";
00152     mregnames[10] = "OUT_T_DELTA_LSB";
00153     mregnames[11] = "WHO_AM_I";
00154     mregnames[12] = "F_STATUS";
00155     mregnames[13] = "F_DATA";
00156     mregnames[14] = "F_SETUP";
00157     mregnames[15] = "TIME_DLY";
00158     mregnames[16] = "SYSMOD";
00159     mregnames[17] = "INT_SOURCE";
00160     mregnames[18] = "SOFT_RESET";
00161     mregnames[19] = "PT_DATA_CFG";
00162     mregnames[20] = "BAR_IN_MSB";
00163     mregnames[21] = "BAR_IN_LSB";
00164     mregnames[22] = "P_TGT_MSB";
00165     mregnames[23] = "P_TGT_LSB";
00166     mregnames[24] = "T_TGT";
00167     mregnames[25] = "P_WND_MSB";
00168     mregnames[26] = "P_WND_LSB";
00169     mregnames[27] = "T_WND";
00170     mregnames[28] = "P_MIN_MSB";
00171     mregnames[29] = "P_MIN_CSB";
00172     mregnames[30] = "P_MIN_LSB";
00173     mregnames[31] = "T_MIN_MSB";
00174     mregnames[32] = "T_MIN_LSB";
00175     mregnames[33] = "P_MAX_MSB";
00176     mregnames[34] = "P_MAX_CSB";
00177     mregnames[35] = "P_MAX_LSB";
00178     mregnames[36] = "T_MAX_MSB";
00179     mregnames[37] = "T_MAX_LSB";
00180     mregnames[38] = "CTRL_REG1";
00181     mregnames[39] = "CTRL_REG2";
00182     mregnames[40] = "CTRL_REG3";
00183     mregnames[41] = "CTRL_REG4";
00184     mregnames[42] = "CTRL_REG5";
00185     mregnames[43] = "OFF_P";
00186     mregnames[44] = "OFF_T";
00187     mregnames[45] = "OFF_H";
00188 }
00189 
00190 // Interface pulled from ADXL362.cpp
00191 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
00192 ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
00193 
00194 // Selects SDA as I2C1_SDA on pin PB_7
00195 // Selects SCL on I2C1_SCL on pin PB_6
00196 // The I2C address of the pressure sensor is fixed at 0x60.
00197 MPL3115A2 pressure(PB_7, PB_6, 0x60);
00198 
00199 int adxl362_reg_print(int s, int l)
00200 {
00201     //check s is between 0x00 and 0x2E
00202     if(s > 0x2E || s < 0x00) {
00203         printf("adx: requires start between 0x00 and 0x2E; given %d (0x%01x)\n", s,s);
00204         return -1;
00205     } else if(l < 0) {
00206         //check length > 0
00207         printf("adx: requires length >= 0, given %d\n", l);
00208         return -1;
00209     } else {
00210         // start sending stuff
00211         //first check DEVID_AD == 0xAD
00212         uint8_t devid = adxl362.read_reg(ADXL362::DEVID_AD);
00213         //printf("devid = %d\n", devid);
00214         if(devid != 0xAD) {
00215             printf("error: DEVID_AD is not 0xAD; exiting\n");
00216             return -1;
00217         } else {
00218             //now do stuff
00219             //if length == 0, send everything from s to end of registers
00220             //else print from s to l
00221             // formula : range = end - start - length
00222             // area = start + range
00223             int stop = 0;
00224             if(l == 0) {
00225                 stop = 34;
00226             } else {
00227                 stop = s + l;
00228             }
00229             int in = s;
00230             while(in < stop) {
00231                 //get register name
00232                 char * name = aregnames[in]; //GET NAME
00233                 ADXL362::ADXL362_register_t reg = (ADXL362::ADXL362_register_t) aregids[in];
00234                 uint8_t val = adxl362.read_reg(reg); //GET REG VAL
00235                 //print val
00236                 printf("0x%01x: %s=0x%01x\r\n", aregids[in], name, val);
00237                 in++;
00238             }
00239         }
00240     }
00241     return 0;
00242 }
00243 
00244 void knocks()
00245 {
00246     int8_t x,y,z, initx, inity, initz;
00247     //initial values of x, y, and z.
00248     //These values are the baseline for the accelerometer; knocks are detected
00249     // in relation to these initial values
00250     initx=adxl362.scanx_u8();
00251     inity=adxl362.scany_u8();
00252     initz=adxl362.scanz_u8();
00253     int count = 0;
00254     int bs = 7;
00255 
00256     while(1) {
00257         //get the values of x, y, and z to test for a knock
00258         x=adxl362.scanx_u8();
00259         y=adxl362.scany_u8();
00260         z=adxl362.scanz_u8();
00261         //if x, y, or z vary from baseline by at least bs, a knock has occured.
00262         if(x > initx+bs || y > inity+bs || z > initz+bs || x < initx-bs || y < inity-bs || z < initz-bs) {
00263             led3 = 1;
00264             count++;
00265             printf("number of knocks: %d\r\n", count);
00266             wait_ms(2000);
00267             led3 = 0;
00268         }
00269         //printf("x = %d y = %d z = %d\r\n",x,y,z);
00270         wait_ms(100);
00271     }
00272 }
00273 
00274 int mpl3115_reg_print(int s, int l)
00275 {
00276     if(s > 0x2D || s < 0x00) {
00277         printf("mpl: requires start between 0x00 and 0x20; given %d (0x%01x)\n\r", s,s);
00278         return -1;
00279     } else if(l < 0) {
00280         //check length > 0
00281         printf("mpl: requires length >= 0, given %d\n\r", l);
00282         return -1;
00283     } else {
00284         //else do stuff
00285         uint8_t id = pressure.getID();
00286         //printf("whoami = 0x%01x", id);
00287         if(id != 0xC4) {
00288             printf("error: WHO_AM_I is not 0xC4; exiting\n");
00289             return -1;
00290         } else {
00291             //now do stuff
00292             //if length == 0, send everything from s to end of registers
00293             //else print from s to l
00294             // formula : range = end - start - length
00295             // area = start + range
00296             int stop = 0;
00297             if(l == 0) {
00298                 stop = 46;
00299             } else {
00300                 stop = s + l;
00301             }
00302             int in = s;
00303             while(in < stop) {
00304                 //get register name
00305                 char * name = mregnames[in]; //GET NAME
00306                 int reg = mregids[in];
00307                 uint8_t val;
00308                 pressure.readRegs(reg,&val,sizeof(uint8_t)); //GET REG VAL
00309                 //print val
00310                 printf("0x%01x: %s=0x%01x\r\n", mregids[in], name, val);
00311                 in++;
00312             }//while
00313         }//else
00314     }//else
00315     return 0;
00316 }//print mpl
00317 
00318 int measureAlt()
00319 {
00320 
00321     printf("measuring things\r\n");
00322 
00323     uint8_t id = pressure.getID();
00324     //printf("whoami = 0x%01x\r\n", id);
00325     if(id != (uint8_t) 0xC4) {
00326         printf("error: WHO_AM_I is not 0xC4; exiting\r\n");
00327         return -1;
00328     } else {
00329         double data[900];
00330 
00331         int ind = 0;
00332 
00333         //set sample_time
00334         wait_ms(100);
00335         uint8_t st = pressure.getCTRL_REG1();
00336         uint8_t msk = 0x8f;
00337         //printf("CTL_REG1 = 0x%05x\r\n", st);
00338         pressure.setCTRL_REG1(msk);
00339         pressure.activate();
00340         //printf("CTL_REG1 = 0x%05x\r\n", pressure.getCTRL_REG1());
00341         
00342 
00343         //pressure stuff
00344         uint8_t tmp[3] ;
00345         uint32_t pres ;
00346 
00347 
00348         //GPIO pin indicates sampling should begin -- if voltage == 3.3, start
00349         while(ind < 900 && !pc.readable()) {
00350             printf("measuring...\r\n");
00351             //then we measure
00352             //pressure and temperature sampled at 10times/second (10 hz)
00353             //LED blinks during sampling at 1 hz (1 time/second)
00354             led3 = 1;
00355             for(int i = 0; i < 10; i++) {
00356                 pres = pressure.getPressure();
00357                 //pressure.readRegs(mregids[1], tmp, 3) ;
00358                 //pres = ((tmp[0]<<16)|(tmp[1]<<8)|(tmp[2])) >> 6;
00359                 data[ind++] = pres;
00360                 data[ind++] = pressure.getTemperature();
00361                 wait_ms(100);
00362             }//for
00363             led3 = 0;
00364             for(int i = 0; i < 10; i++) {
00365                 pres = pressure.getPressure();
00366                 //pressure.readRegs(mregids[1], tmp, 3) ;
00367                 //pres = ((tmp[0]<<16)|(tmp[1]<<8)|(tmp[2])) >> 6;
00368                 data[ind++] = pres;
00369                 data[ind++] = pressure.getTemperature();
00370                 wait_ms(100);
00371             }//for
00372         }//while
00373         
00374         int pind = 0;
00375         int pend = ind;
00376         char in;
00377         int ch;
00378         char p = 'p';
00379         bool ex = false;
00380         while(!ex) {
00381             printf("p%% ");
00382             while((ch = pc.getc()) != 13) {
00383                 pc.putc(ch);
00384                 in = ch;
00385             }//while
00386             if(in == p) {
00387                 while(pind < pend) {
00388                     pc.printf("\n\r%f\t%f", data[pind],data[pind+1]);
00389                     pind += 2;
00390                     ex = true;
00391                 }//while
00392             } else {
00393                 in = 0;
00394             }//if/else
00395         }//while
00396         
00397         pc.printf("\n\r");
00398     return 0;
00399     }//else
00400 }//measureAlt
00401 
00402 int main()
00403 {
00404     initmap();
00405     minitmap();
00406     adxl362.reset();
00407     pressure.activate();
00408     wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
00409     adxl362.set_mode(ADXL362::MEASUREMENT);
00410     int start, length;
00411     start = 0;
00412     length = 0;
00413     //while(1){
00414     //adxl362_reg_print(start, length);
00415        mpl3115_reg_print(start, length);
00416     //}
00417     //knocks();
00418 
00419     measureAlt();
00420     //while(1);
00421 }