Qubit 2020 / presensfirmwareupdate

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers presens.cpp Source File

presens.cpp

00001 #include "cisme.h"
00002 #include "presens.h"
00003 #include "OxygenSensor.h"
00004 #include "debug.h"
00005 #include "adc.h"
00006 
00007 
00008 /* Due to unknown reason in old version presens was 6 symbol long. */
00009 
00010 //#define PRESENS_FLOAT_LENGTH  6
00011 
00012 Serial presenseComm(p9,p10);
00013 
00014 static float presensReadFloat(int PRESENS_FLOAT_LENGTH)
00015 {
00016     double val_double;
00017     float val_float;
00018     char buf[PRESENS_FLOAT_LENGTH];
00019     for(int i=0;i<PRESENS_FLOAT_LENGTH;i++){
00020         buf[i]='0';
00021         }
00022 
00023     char charIx = 0;
00024 
00025     while (charIx < PRESENS_FLOAT_LENGTH) {
00026         buf[charIx++] = presenseComm.getc();
00027     }
00028  
00029    buf[PRESENS_FLOAT_LENGTH]=0;
00030 
00031     DEBUG1("recieved characters=%s\r", buf);
00032 
00033     val_double=atof(buf);
00034     val_float= (float)val_double;
00035 
00036     return val_float;
00037 }
00038 
00039 
00040 void presensInit(void)
00041 {
00042     //set the baud rate for the PreSense
00043     presenseComm.baud(PRESENS_BAUD);
00044 
00045     //set the Serial parameters for the PreSense
00046     presenseComm.format(PRESENS_NO_BITS, PRESENS_PARITY, PRESENS_NO_STOP_BITS);
00047 
00048     wait(PRESENS_CONFIG_DELAY);
00049 }
00050 
00051 
00052 void presensConfig(float ledCurrent)
00053 {
00054 
00055     wait(PRESENS_CONFIG_DELAY);
00056 
00057     presenseComm.printf("");
00058 
00059     if (ledCurrent > 255 || ledCurrent < 0) {
00060         ERROR("Too big ledCurrent=%f, it should be not less than 0 and not greater than 255. Using default ledCurrent=%f", ledCurrent, PRESENS_LED_DEFAULT);
00061         ledCurrent = PRESENS_LED_DEFAULT;
00062     }
00063 
00064 
00065     presenseComm.printf("sacu%-.4d\r", (int)ledCurrent);
00066 
00067     presenseComm.printf("mode0001\r");
00068     wait(PRESENS_CONFIG_DELAY);
00069     
00070     presenseComm.printf("avrg0009\r");
00071     wait(PRESENS_CONFIG_DELAY);
00072     
00073     presenseComm.printf("view0005\r");
00074     wait(PRESENS_CONFIG_DELAY);
00075     
00076     presenseComm.printf("tmpc2000\r");
00077     wait(PRESENS_CONFIG_DELAY);
00078 
00079   //  presenseComm.printf("mode0000\r");
00080   //  wait(PRESENS_CONFIG_DELAY);
00081 
00082 }
00083 
00084 
00085 void presensGetData(float* result)
00086 {
00087     float amplitude = 0;
00088     float phase     = 0;
00089     int PRESENS_FLOAT_LENGTH = 7;
00090 
00091     presenseComm.printf("data\r");
00092     wait(0.2);
00093     adcGetData();
00094     wait(0.1);
00095 
00096     const OxygenSensor oxsense(TempCal1, TempCal2, PhaseCal1, PhaseCal2, PresCal, PRESENS_SALINITY);
00097 
00098     if (result == NULL) {
00099         ERROR("Result array is null");
00100         return;
00101     }
00102 
00103 
00104     L3 = 1;
00105 
00106     
00107     // read the each data packet
00108 
00109     while (presenseComm.readable() != 1);
00110 
00111     //waiting until letter 'A' is read 
00112     //presens amp get
00113 
00114     while (presenseComm.getc() != 65);
00115 
00116     amplitude = presensReadFloat(PRESENS_FLOAT_LENGTH);
00117 
00118     /* skip the characters semicolon ":" and letter "P" */
00119     //while (presenseComm.getc() != 80);
00120     presenseComm.getc();
00121     presenseComm.getc();
00122 
00123     //this wait seems to cure the chart stutter that was happening with the new Presens board
00124     wait(0.55);
00125 
00126     L4=1;
00127   
00128 //    wait(0.05);
00129   
00130     PRESENS_FLOAT_LENGTH = 4;
00131 
00132     phase = presensReadFloat(PRESENS_FLOAT_LENGTH) / 100;
00133 
00134     L3 = 0;
00135   
00136 
00137     // read through the rest of packet to empty out the buffer
00138 
00139     while(presenseComm.readable()) {
00140         presenseComm.getc();
00141     }
00142     wait(0.05);
00143 
00144     // end of the data extraction
00145 
00146 
00147 //    L3=1;
00148 
00149 
00150     DEBUG1("Amplitude=%5.0f Phase=%5.2f", amplitude, phase);
00151 
00152 
00153     result[0] = amplitude;
00154     result[1] = phase;
00155     result[2] = oxsense.calculate(PHTEMP, phase);
00156 
00157 
00158     if (amplitude < 600) {
00159        result[2] = 999.99;
00160     }
00161 
00162 
00163     DEBUG1("Result array:amplitude=%5.0f, phase=%2.2f, OxsenseData=%3.3f", amplitude, phase, result[2]);
00164 
00165 
00166 //    L3=0;
00167     L4=0;
00168 
00169 }
00170