Lindholm Engineers / Mbed 2 deprecated Ultrasonic_Firmware

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Main program for the ultrasonic firmware.
00002 //Created by Eric Lindholm on 7 December 2015
00003 
00004 //includes the mbed firmware and additional objects
00005 #include "mbed.h"
00006 #include "TDC1000.h"
00007 #include "TDC7200.h"
00008 
00009 /*
00010 *   The current setup has the following pin-out
00011 *   5           MOSI  to TDC1000
00012 *   6           MISO  to TDC1000
00013 *   7           SCLK  to TDC1000
00014 *   8           SPICS to TDC1000
00015 *   10          RST   to TDC1000
00016 *   11          MOSI  to TDC7200
00017 *   12          MISO  to TDC7200
00018 *   13          SCLK  to TDC7200
00019 *   14          SPICS to TDC7200
00020 *   16          EN    to TDC1000
00021 *   22          ChSel to TDC1000
00022 *   23          ERR   to TDC1000
00023 *   29          EN    to TDC7200
00024 *   30          INT   to TDC7200
00025 *       EN = Enable
00026 *       SPICS = SPI chip select
00027 *       RST = Reset
00028 *       ChSel = Channel Selection
00029 *       ERR = Error flag pin
00030 *       INT = Interrupt flag pin
00031 *       The TDC1000 is an ultrasonic driver/detector.
00032 *       The TDC7200 is a stopwatch chip for very fine time samples.
00033 */
00034 
00035 //For debugging
00036 Serial pc(USBTX, USBRX);
00037 
00038 //File-system and related function variable.
00039 LocalFileSystem local("local");
00040 char* input_data;
00041 
00042 //Instantiate TDC1000 class
00043 TDC1000 ultrasonic(p22,p23,p16,p10,p7,p8,p5,p6);
00044 
00045 //Instantiate TDC7200 class
00046 TDC7200 stopwatch(p29,p30,p12,p11,p13,p14);
00047 
00048 //Function for settings
00049 void go_to_settings(void);
00050 
00051 int externalClockFrequency = 16000000;  //in Hz
00052 double speedSoundWater = 1447;      //in m/s
00053 double distanceBetweenTransducers = 0.02;       //2cm
00054 
00055 //The start of the show.
00056 int main() {
00057     //Set the time to the current time.
00058     //Right now, though, it just sets to August 26, 2015
00059     set_time(1440603500);
00060     
00061     //Set stuff up for fluid flow measurement
00062     ultrasonic.setMODE_SELECT(2);
00063     ultrasonic.setClockFrequencyIn(externalClockFrequency);
00064     stopwatch.setMODE_SELECT(1);
00065     stopwatch.setClockFrequencyIn(externalClockFrequency);
00066     
00067     //more setup
00068     ultrasonic.setTX_FREQ_DIV(0x03);
00069     if(!ultrasonic.readToggle(CH_SWP))
00070         ultrasonic.setToggle(CH_SWP);
00071     if(!ultrasonic.readToggle(EXT_CHSEL))
00072         ultrasonic.setToggle(EXT_CHSEL);
00073     if(ultrasonic.readToggle(TEMP_MODE))
00074         ultrasonic.setToggle(TEMP_MODE);
00075     if(ultrasonic.readToggle(FORCE_SHORT_TOF))
00076         ultrasonic.setToggle(FORCE_SHORT_TOF);
00077     double tof1;
00078     
00079     //Self check: currently empty
00080     
00081     //Attempt communication with a host
00082     pc.printf("\rBegin communications...\n");
00083     
00084     //If the host is reachable, synchronize data and stuff
00085     
00086     //If the host is unreachable, store data internally and check for contact every 5 minutes.
00087     //When contact is eventually made, dump data & start synchronizing
00088     
00089     //During synchronization:
00090     while(1) {
00091         //Query user
00092         pc.printf("\n\rPress 't' to trigger a new time-of-flight measurement, or press 's' for settings.\n");
00093         char userReply;
00094         userReply = pc.getc();
00095         if(userReply == 't') {
00096             //Tell the TDC7200 to trigger a start
00097             stopwatch.startMeasurement();
00098             //wait for a new measurement to be completed.
00099             wait_us(20);
00100             tof1 = stopwatch.readTimeOfFlight(0);
00101         }
00102         else if(userReply == 's')
00103             go_to_settings();
00104         else if(userReply == 'f')
00105             stopwatch.forceMeasurementRead();
00106         
00107         //wait for the 5 or so cycles to complete and be measured
00108         
00109         //Get the time of flight from the TDC7200
00110         
00111         //Get the average time of flight from the 5 or so measurements
00112         
00113         //Put that data in internal memory for about 5 seconds
00114         //Synchronize with storage media and/or host approx. every 5 seconds.
00115         pc.printf("\n\rThe most recent time-of-flight was %X ", tof1);
00116         
00117         //Repeat several times per second
00118     }
00119         
00120     //If no host is known at all, perform first-time setup.
00121 }
00122 
00123 //settings function
00124 void go_to_settings(void) {
00125     //List all the current settings
00126     
00127     //Ask if any changes need to be made
00128 }