synch the camera trigger with the signal in phase and quadrature (every frame or every N frames). We can also simulate the product by the in-phase signal (with a small arbitrary phase difference)

Dependencies:   mbed

main.cpp

Committer:
mbedalvaro
Date:
2014-07-14
Revision:
0:4b5874bff9bb
Child:
1:4284f27d638d

File content as of revision 0:4b5874bff9bb:

#include "mbed.h"
#include "Flipper.h"

// The following parameters can be changed by serial commands:
// NOTE: program 9 on the LCC-230 gives a freq of 150Hz. If frame rate for the camera is 30fps, then the nb of cycles per frame is 150/30=5
// for 28.09, then the of cycles per frame is 150/28.09=5.339
float CAMERA_FPS=15;//28.0915;//28.09;
unsigned long CAMERA_TRIGGER_PERIOD=1000000.0/CAMERA_FPS;
float EXPOSURE_TIME_FACTOR=1.0/4;
unsigned long EXPOSURE_TIME=EXPOSURE_TIME_FACTOR*CAMERA_TRIGGER_PERIOD;

float NB_CYCLES_PER_EXPOSURE_TIME=4;//5.339;//1;//1.0*150/28; // this is the nb of cycles of the led during one camera frame
float NB_CYCLES_PER_CAMERA_FRAME=1.0/EXPOSURE_TIME_FACTOR*NB_CYCLES_PER_EXPOSURE_TIME;

unsigned long LED_PERIOD=1000000.0/(NB_CYCLES_PER_CAMERA_FRAME*CAMERA_FPS); //in us

unsigned int NB_FRAMES_TOGGLE_QP=1; // this is the nb of camera frames before toggling the modulation (note: there is no synchronization!)

enum SignalMode {PHASE=0, QUADRATURE, TOGGLE_PHASE_QUADRATURE};
SignalMode currentSignalMode=TOGGLE_PHASE_QUADRATURE; //NOTE: all the leds mode affected by this at the same time

Trigger cameraTrigger(cameraTriggerPin,1.0*CAMERA_FPS,(unsigned int)EXPOSURE_TIME);
Flipper ledSource(ledPin, LED_PERIOD);
Shutter lcdShutter(shutterPin, LED_PERIOD);

Serial pc(USBTX, USBRX); // tx, rx
void processSerial();

int main()
{
    pc.baud(9600);


    cameraTrigger.setQPToggleFrames(NB_FRAMES_TOGGLE_QP);
    cameraTrigger.start();
    
    ledSource.start();
    
    // add some delay:
    wait_us(5000);
    lcdShutter.mixSignal(false);
    lcdShutter.start();

    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        processSerial();
    }
}

// String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
char incomingValue[5];
char incomingByte;
char indexString=0;

void processSerial()
{

    while(pc.readable()>0) {
        char incomingByte =pc.getc();

        if (incomingByte >='0' && incomingByte <='9') {
            incomingValue[indexString]=incomingByte;
            indexString++;
        } else { // check for terminator (i.e. "CONTROL CODES"):
            if (incomingByte=='P') {
                currentSignalMode=PHASE;
                cameraTrigger.toggleQP(false);
                ledSource.flip();
            } else if (incomingByte=='Q') {
                currentSignalMode=QUADRATURE;
                cameraTrigger.toggleQP(false);
                ledSource.flip();
            } else if (incomingByte=='T') {
                currentSignalMode=TOGGLE_PHASE_QUADRATURE; // periodic toogling (every a TOGGLE_QP_PERIOD)
                cameraTrigger.toggleQP(true);
            } else if (incomingByte=='s') { // simulated shutter OFF
                lcdShutter.mixSignal(false);
            } else if (incomingByte=='S') { // simulated shutter ON
                lcdShutter.mixSignal(true);
            
            // CAMERA PARAMETERS:
            } else if (incomingByte == 'F') { // camera frame rate
                incomingValue[indexString]=0; // termination for the string
                indexString=0;
                cameraTrigger.setFrameRate(atoi(incomingValue));   
            } else if (incomingByte == 'E') { // control Exposure time:
                incomingValue[indexString]=0; // termination for the string
                indexString=0;
                cameraTrigger.setExposure(atoi(incomingValue));
            }
            
        }
    }
}