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

Committer:
mbedalvaro
Date:
Mon Dec 08 07:03:38 2014 +0000
Revision:
1:4284f27d638d
Parent:
0:4b5874bff9bb
this is the latest code for the active markers project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:4b5874bff9bb 1 #include "mbed.h"
mbedalvaro 0:4b5874bff9bb 2 #include "Flipper.h"
mbedalvaro 0:4b5874bff9bb 3
mbedalvaro 0:4b5874bff9bb 4 // The following parameters can be changed by serial commands:
mbedalvaro 0:4b5874bff9bb 5 // 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
mbedalvaro 0:4b5874bff9bb 6 // for 28.09, then the of cycles per frame is 150/28.09=5.339
mbedalvaro 1:4284f27d638d 7 float CAMERA_FPS=58.75;//28.0915;//28.09;
mbedalvaro 1:4284f27d638d 8 unsigned long CAMERA_TRIGGER_PERIOD=1000000.0/CAMERA_FPS; // it is recommendable that this division gives an integer!
mbedalvaro 1:4284f27d638d 9 float EXPOSURE_TIME_FACTOR=1.0/3;
mbedalvaro 0:4b5874bff9bb 10 unsigned long EXPOSURE_TIME=EXPOSURE_TIME_FACTOR*CAMERA_TRIGGER_PERIOD;
mbedalvaro 1:4284f27d638d 11 unsigned long WAITING_TIME=CAMERA_TRIGGER_PERIOD-EXPOSURE_TIME;
mbedalvaro 0:4b5874bff9bb 12
mbedalvaro 1:4284f27d638d 13 // NOTE: since the delays (waiting time and exposure time) are in microseconds, we NEED to have an integer number of cycles per CAMERA frame...
mbedalvaro 1:4284f27d638d 14 // (no pb if there is not an integer INSIDE the exposure time, but better to have it). Hence the "approximate" value:
mbedalvaro 1:4284f27d638d 15 float approximate_NbCylclesExposureTime=6;
mbedalvaro 1:4284f27d638d 16 unsigned long NB_CYCLES_PER_CAMERA_FRAME=1.0*approximate_NbCylclesExposureTime/EXPOSURE_TIME_FACTOR;
mbedalvaro 0:4b5874bff9bb 17
mbedalvaro 0:4b5874bff9bb 18 unsigned long LED_PERIOD=1000000.0/(NB_CYCLES_PER_CAMERA_FRAME*CAMERA_FPS); //in us
mbedalvaro 1:4284f27d638d 19 // NOTE: there is no warangy this division will be integer! PLUS it must be a multiple of 2!! mmm...
mbedalvaro 0:4b5874bff9bb 20
mbedalvaro 0:4b5874bff9bb 21 unsigned int NB_FRAMES_TOGGLE_QP=1; // this is the nb of camera frames before toggling the modulation (note: there is no synchronization!)
mbedalvaro 0:4b5874bff9bb 22
mbedalvaro 0:4b5874bff9bb 23 enum SignalMode {PHASE=0, QUADRATURE, TOGGLE_PHASE_QUADRATURE};
mbedalvaro 0:4b5874bff9bb 24 SignalMode currentSignalMode=TOGGLE_PHASE_QUADRATURE; //NOTE: all the leds mode affected by this at the same time
mbedalvaro 0:4b5874bff9bb 25
mbedalvaro 1:4284f27d638d 26 //Trigger cameraTrigger(cameraTriggerPin,1.0*CAMERA_FPS,(unsigned int)EXPOSURE_TIME);
mbedalvaro 1:4284f27d638d 27 Trigger cameraTrigger(cameraTriggerPin,(unsigned int)WAITING_TIME-27,(unsigned int)EXPOSURE_TIME);
mbedalvaro 0:4b5874bff9bb 28 Flipper ledSource(ledPin, LED_PERIOD);
mbedalvaro 0:4b5874bff9bb 29 Shutter lcdShutter(shutterPin, LED_PERIOD);
mbedalvaro 0:4b5874bff9bb 30
mbedalvaro 0:4b5874bff9bb 31 Serial pc(USBTX, USBRX); // tx, rx
mbedalvaro 0:4b5874bff9bb 32 void processSerial();
mbedalvaro 0:4b5874bff9bb 33
mbedalvaro 0:4b5874bff9bb 34 int main()
mbedalvaro 0:4b5874bff9bb 35 {
mbedalvaro 0:4b5874bff9bb 36 pc.baud(9600);
mbedalvaro 0:4b5874bff9bb 37
mbedalvaro 0:4b5874bff9bb 38
mbedalvaro 0:4b5874bff9bb 39 cameraTrigger.setQPToggleFrames(NB_FRAMES_TOGGLE_QP);
mbedalvaro 0:4b5874bff9bb 40 cameraTrigger.start();
mbedalvaro 0:4b5874bff9bb 41
mbedalvaro 0:4b5874bff9bb 42 ledSource.start();
mbedalvaro 0:4b5874bff9bb 43
mbedalvaro 1:4284f27d638d 44 // add some delay?
mbedalvaro 1:4284f27d638d 45 // wait_us(100);
mbedalvaro 1:4284f27d638d 46 lcdShutter.mixSignal(true);
mbedalvaro 0:4b5874bff9bb 47 lcdShutter.start();
mbedalvaro 0:4b5874bff9bb 48
mbedalvaro 0:4b5874bff9bb 49 // spin in a main loop. flipper will interrupt it to call flip
mbedalvaro 0:4b5874bff9bb 50 while(1) {
mbedalvaro 0:4b5874bff9bb 51 processSerial();
mbedalvaro 0:4b5874bff9bb 52 }
mbedalvaro 0:4b5874bff9bb 53 }
mbedalvaro 0:4b5874bff9bb 54
mbedalvaro 0:4b5874bff9bb 55 // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly:
mbedalvaro 0:4b5874bff9bb 56 char incomingValue[5];
mbedalvaro 0:4b5874bff9bb 57 char incomingByte;
mbedalvaro 0:4b5874bff9bb 58 char indexString=0;
mbedalvaro 0:4b5874bff9bb 59
mbedalvaro 0:4b5874bff9bb 60 void processSerial()
mbedalvaro 0:4b5874bff9bb 61 {
mbedalvaro 0:4b5874bff9bb 62
mbedalvaro 0:4b5874bff9bb 63 while(pc.readable()>0) {
mbedalvaro 0:4b5874bff9bb 64 char incomingByte =pc.getc();
mbedalvaro 0:4b5874bff9bb 65
mbedalvaro 0:4b5874bff9bb 66 if (incomingByte >='0' && incomingByte <='9') {
mbedalvaro 0:4b5874bff9bb 67 incomingValue[indexString]=incomingByte;
mbedalvaro 0:4b5874bff9bb 68 indexString++;
mbedalvaro 0:4b5874bff9bb 69 } else { // check for terminator (i.e. "CONTROL CODES"):
mbedalvaro 0:4b5874bff9bb 70 if (incomingByte=='P') {
mbedalvaro 0:4b5874bff9bb 71 currentSignalMode=PHASE;
mbedalvaro 0:4b5874bff9bb 72 cameraTrigger.toggleQP(false);
mbedalvaro 0:4b5874bff9bb 73 ledSource.flip();
mbedalvaro 0:4b5874bff9bb 74 } else if (incomingByte=='Q') {
mbedalvaro 0:4b5874bff9bb 75 currentSignalMode=QUADRATURE;
mbedalvaro 0:4b5874bff9bb 76 cameraTrigger.toggleQP(false);
mbedalvaro 0:4b5874bff9bb 77 ledSource.flip();
mbedalvaro 0:4b5874bff9bb 78 } else if (incomingByte=='T') {
mbedalvaro 0:4b5874bff9bb 79 currentSignalMode=TOGGLE_PHASE_QUADRATURE; // periodic toogling (every a TOGGLE_QP_PERIOD)
mbedalvaro 0:4b5874bff9bb 80 cameraTrigger.toggleQP(true);
mbedalvaro 0:4b5874bff9bb 81 } else if (incomingByte=='s') { // simulated shutter OFF
mbedalvaro 0:4b5874bff9bb 82 lcdShutter.mixSignal(false);
mbedalvaro 0:4b5874bff9bb 83 } else if (incomingByte=='S') { // simulated shutter ON
mbedalvaro 0:4b5874bff9bb 84 lcdShutter.mixSignal(true);
mbedalvaro 0:4b5874bff9bb 85
mbedalvaro 0:4b5874bff9bb 86 // CAMERA PARAMETERS:
mbedalvaro 0:4b5874bff9bb 87 } else if (incomingByte == 'F') { // camera frame rate
mbedalvaro 0:4b5874bff9bb 88 incomingValue[indexString]=0; // termination for the string
mbedalvaro 0:4b5874bff9bb 89 indexString=0;
mbedalvaro 0:4b5874bff9bb 90 cameraTrigger.setFrameRate(atoi(incomingValue));
mbedalvaro 0:4b5874bff9bb 91 } else if (incomingByte == 'E') { // control Exposure time:
mbedalvaro 0:4b5874bff9bb 92 incomingValue[indexString]=0; // termination for the string
mbedalvaro 0:4b5874bff9bb 93 indexString=0;
mbedalvaro 0:4b5874bff9bb 94 cameraTrigger.setExposure(atoi(incomingValue));
mbedalvaro 1:4284f27d638d 95 } else if (incomingByte == 'L') { // get the LED PERIOD (to adjust the real LCD shutter)
mbedalvaro 1:4284f27d638d 96 // incomingValue[indexString]=0; // termination for the string
mbedalvaro 1:4284f27d638d 97 // indexString=0;
mbedalvaro 1:4284f27d638d 98 pc.printf("\n\nCAMERA FPS: %f fps\n", CAMERA_FPS);
mbedalvaro 1:4284f27d638d 99 pc.printf("CAMERA TRIGGER PERIOD: %i us\n", CAMERA_TRIGGER_PERIOD);
mbedalvaro 1:4284f27d638d 100 pc.printf("EXPOSURE TIME: %i us\n", EXPOSURE_TIME);
mbedalvaro 1:4284f27d638d 101 pc.printf("\nLED PERIOD: %i us\n", LED_PERIOD);
mbedalvaro 1:4284f27d638d 102 pc.printf("LED FREQ: %f Hz\n", 1.0/LED_PERIOD*1000000);
mbedalvaro 1:4284f27d638d 103 pc.printf("TOGGLE QP every: %i frames\n", NB_FRAMES_TOGGLE_QP);
mbedalvaro 0:4b5874bff9bb 104 }
mbedalvaro 0:4b5874bff9bb 105
mbedalvaro 0:4b5874bff9bb 106 }
mbedalvaro 0:4b5874bff9bb 107 }
mbedalvaro 0:4b5874bff9bb 108 }