This is the device firmware for the imagingBoard on the DIY 3D Printable Raspberry Pi Raman Spectrometer. For more details please visit: http://hackaday.io/project/1279

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //  ***   BE SURE TO TEST BEFORE AND AFTER FOR 'IDLING'... CLEAR THE CCD EVERY OTHER FRAME TO TRY AND ELIMINATE NOISE BUILDUP
00004 //From http://www.ing.iac.es/~docs/ins/das/ins-das-29/integration.html
00005 //"Idling" vs. integrating
00006 //Charge from light leaks and thermal noise builds up on the detector between observations. If this charge is integrated by the detector, it may not be completely
00007 //cleared away by the clear cycle of the next observation. In that case, the observation will be contaminated by extra counts. (Often, this appears as a ramp in the
00008 //background leading up to a saturated region in the low-numbered rows.)
00009 //To avoid this problem, the detector is made to clear itself continuously between observations. This is called "idling", and is reported as such on the mimic.
00010 
00011 PwmOut masterClock(PB_4);
00012 PwmOut shiftGate(PB_8);
00013 InterruptIn shiftGate_int(PC_6);
00014 DigitalOut ICG(PB_3);
00015 AnalogIn imageIn(A0);
00016 DigitalOut LED(LED1);
00017 DigitalOut illuminator(PA_8);
00018 DigitalOut blueLED(PB_5);
00019 DigitalOut redLED(PB_10);
00020 Serial raspi(USBTX, USBRX);
00021 
00022 /*
00023 int masterFreq_period       = 20;    //microseconds
00024 int masterFreq_width        = 10;    //microseconds
00025 int shiftGate_period        = 66;    //microseconds
00026 int shiftGate_width         = 33;    //microseconds
00027 */
00028 
00029 float firmwareVersion = 0.2;
00030 
00031 
00032 int masterFreq_period       = 2;    //microseconds
00033 int masterFreq_width        = 1;    //microseconds
00034 int shiftGate_period        = 8;    //microseconds
00035 int shiftGate_width         = 4;    //microseconds
00036 
00037 int none        = 0;
00038 int veryLow     = 10;
00039 int low         = 100;
00040 int mediumLow   = 1000;
00041 int medium      = 100000;
00042 int high        = 5000000;
00043 int veryHigh    = 10000000;
00044 
00045 double imageData;
00046 int sensitivity;
00047 int pixelTotal              = 3694;
00048 int leadingDummyElements    = 16;
00049 int leadShieldedElements    = 13;
00050 int headerElements          = 3;
00051 const int signalElements    = 3648;
00052 int trailingDummyElements   = 14;
00053 int pixelCount;
00054 int readOutTrigger;
00055 int state;
00056 
00057 #define readOut_Begin               1
00058 #define readOut_ACTIVE              2
00059 #define readOut_LeadingDummy        3
00060 #define readOut_LeadingShielded     4
00061 #define readOut_headerElements      5
00062 #define readOut_signalElements      6
00063 #define readOut_trailingDummy       7
00064 #define readOut_integrationTime     8
00065 #define readOut_IDLE                9
00066 #define readOut_Finish              0
00067 
00068 #define MV(x) ((0xFFF*x)/3300)
00069 
00070 double pixelValue[signalElements] = { 0 };
00071 
00072 void error()
00073 {
00074     while(1) {
00075         LED = !LED;
00076         wait(0.5);
00077     }
00078 }
00079 
00080 void checkState()
00081 {
00082     if (readOutTrigger == 1) {
00083 //        state = readOut_LeadingDummy;
00084     }
00085     switch (state) {
00086         case readOut_Begin:
00087             redLED = 1;
00088             blueLED = 0;
00089             readOutTrigger = 1;
00090             state = readOut_ACTIVE;
00091 //            raspi.printf("+++\r\n");
00092             LED = 1;
00093             break;
00094         case readOut_ACTIVE:
00095             ICG = 1;
00096             state = readOut_LeadingDummy;
00097             break;
00098         case readOut_LeadingDummy:
00099             pixelCount++;
00100             if (pixelCount == leadingDummyElements - 1) {
00101                 pixelCount = 0;
00102                 state = readOut_LeadingShielded;
00103             }
00104             break;
00105         case readOut_LeadingShielded:
00106             pixelCount++;
00107             if (pixelCount == leadShieldedElements - 1) {
00108                 pixelCount = 0;
00109                 state = readOut_headerElements;
00110             }
00111             break;
00112         case readOut_headerElements:
00113             pixelCount++;
00114             if (pixelCount == headerElements - 1) {
00115                 pixelCount = 0;
00116                 state = readOut_signalElements;
00117             }
00118             break;
00119         case readOut_signalElements:
00120             pixelCount++;
00121             pixelValue[pixelCount] = imageIn.read_u16();
00122 //            raspi.printf("%i\t%4.12f\r\n", pixelCount,(imageIn.read_u16() * 5.0) / 4096.0);
00123             if (pixelCount == signalElements - 1) {
00124                 pixelCount = 0;
00125                 state = readOut_trailingDummy;
00126             }
00127             break;
00128         case readOut_trailingDummy:
00129             pixelCount++;
00130             if (pixelCount == trailingDummyElements - 1) {
00131                 pixelCount = 0;
00132                 state = readOut_integrationTime;
00133                 ICG = 0;
00134             }
00135             break;
00136         case readOut_integrationTime:
00137             wait_us(sensitivity);
00138             state = readOut_Finish;
00139             for (int pixelNumber=0; pixelNumber<signalElements; pixelNumber++) {
00140                 raspi.printf("%i\t%4.12f\r\n", pixelNumber, 5 - ((pixelValue[pixelNumber - 1] * 5) / 4096.0));
00141             }
00142 //            raspi.printf("---\r\n");
00143             break;
00144         case readOut_Finish:
00145             redLED = 0;
00146             state = readOut_IDLE;
00147             wait_us(sensitivity);
00148             LED = 0;
00149             illuminator = 0;
00150             ICG = 1;
00151             break;
00152         case readOut_IDLE:
00153             if (ICG == 1) {
00154 //                ICG = 0;
00155 //                state = readOut_Begin;
00156                 blueLED = 1;
00157             }
00158             break;
00159         default:
00160             break;
00161     }
00162 }
00163 
00164 void printInfo(){
00165     raspi.printf("meridianScientific\r\n");
00166     raspi.printf("ramanPi - The DIY 3D Printable RaspberryPi Raman Spectrometer\r\n");
00167     raspi.printf("Spectrometer imagingBoard\r\n");
00168     raspi.printf("-------------------------------------------------------------\r\n");
00169     raspi.printf("Firmware Version: %f\r\n",firmwareVersion);
00170     raspi.printf("Current Sensitivity: %d\r\n", sensitivity);
00171 }
00172 
00173 int main()
00174 {
00175     ICG = 1;
00176     LED = 0;
00177     blueLED = 0;
00178     redLED = 0;
00179     pixelCount = 0;
00180     readOutTrigger = 0;
00181     state = readOut_IDLE;
00182 
00183     masterClock.period_us(masterFreq_period);
00184     masterClock.pulsewidth_us(masterFreq_width);
00185 
00186     shiftGate.period_us(shiftGate_period);
00187     shiftGate.pulsewidth_us(shiftGate_width);
00188 
00189     raspi.baud(921600);
00190     wait(0.5);
00191 
00192     shiftGate_int.rise(checkState);
00193 
00194     raspi.baud(921600);
00195     blueLED = 0;
00196     sensitivity = low;
00197     while(1) {
00198         if (state != readOut_IDLE)   //reading is top priority
00199         {
00200          continue;
00201         }        
00202         char c = raspi.getc();
00203         switch (c) {
00204             case 'r':
00205                 illuminator = 1;
00206                 wait(1);
00207                 ICG = 0;
00208                 state = readOut_Begin;
00209                 break;
00210             case 'l':
00211                 sensitivity = low;
00212                 break;
00213             case 'v':
00214                 sensitivity = veryLow;
00215                 break;
00216             case 'n':
00217                 sensitivity = mediumLow;
00218                 break;
00219             case 'm':
00220                 sensitivity = medium;
00221                 break;
00222             case 'h':
00223                 sensitivity = high;
00224                 break;
00225             case 'c':
00226                 sensitivity = veryHigh;
00227                 break;
00228             case 'i':
00229                 printInfo();
00230                 break;
00231             default:
00232                 break;
00233         }
00234     }
00235 }