CAMCTL - One shot capture. - Calibration of camera to be able to work under different light conditions.

Dependents:   frdm_MasterVehicle

Files at this revision

API Documentation at this revision

Comitter:
JalilChavez
Date:
Tue Oct 14 03:52:23 2014 +0000
Commit message:
controller of linescan camera

Changed in this revision

camctl.cpp Show annotated file Show diff for this revision Revisions of this file
camctl.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6cad676fdc0c camctl.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/camctl.cpp	Tue Oct 14 03:52:23 2014 +0000
@@ -0,0 +1,174 @@
+#include "camctl.h"
+
+/**********************************************************************************
+* Object declaration
+***********************************************************************************/
+
+DigitalOut CAMCTL__xPinSI(PTD1);
+DigitalOut CAMCTL__xPinCLK(PTD3);
+AnalogIn CAMCTL__xPinAN(PTB2);
+DigitalOut debug(PTB3);
+DigitalOut debugGetData(PTB10);
+
+Timeout CAMCTL__xGlobalTimer;
+Timeout CAMCTL__xClkTimer;
+Timeout CAMCTL__xSiTimer;
+
+/**********************************************************************************
+* Local function prototypes
+***********************************************************************************/
+static void CAMCTL__vGlobalTimer(void);
+static void CAMCTL__vSignalHdlr_SI(void);
+static void CAMCTL__vSignalHdlr_CLK(void);
+
+/**********************************************************************************
+* Local variables
+***********************************************************************************/
+static CAMCTL__tstCameraOptions CAMCTL__stCameraOptions;
+static uint16_t CAMCTL__au16LineBuffer[BUFFER_LENGTH] = {0};
+
+/**********************************************************************************
+* CAMCTL-Module
+***********************************************************************************/
+static void CAMCTL__vGlobalTimer()
+{
+    static bool boStartDone = false;
+    if ( CAMCTL__stCameraOptions.enCaptureMode == nOneShot &&
+         CAMCTL__stCameraOptions.u8ActiveCapture == true    &&
+         CAMCTL__stCameraOptions.u8ClkEnd == false &&
+         boStartDone == false )
+    {
+        CAMCTL__xSiTimer.attach_us(&CAMCTL__vSignalHdlr_SI, START_DELAY);
+        CAMCTL__xClkTimer.attach_us(&CAMCTL__vSignalHdlr_CLK, START_DELAY + CLK_PULSE_WIDTH_HALF);
+        boStartDone = true;
+    }
+    else if( CAMCTL__stCameraOptions.enCaptureMode == nOneShot &&
+             CAMCTL__stCameraOptions.u8ActiveCapture == true    &&
+             CAMCTL__stCameraOptions.u8ClkEnd == true &&
+             boStartDone == true )
+    {        
+        boStartDone = false;
+        CAMCTL__xGlobalTimer.detach();
+        CAMCTL__xClkTimer.detach();
+        CAMCTL__xSiTimer.detach();
+    }
+    else
+    {
+    }
+    CAMCTL__xGlobalTimer.attach_us(&CAMCTL__vGlobalTimer,BASE_PERIOD);
+}
+void CAMCTL__vSignalHdlr_SI()
+{
+    static bool boSIDone = false;
+    if( boSIDone == false )
+    {
+        boSIDone = true;
+        CAMCTL__xPinSI = true;
+        CAMCTL__xSiTimer.attach_us(&CAMCTL__vSignalHdlr_SI, SI_PULSE_WIDTH);
+    }
+    else
+    {
+        CAMCTL__xPinSI = false;
+        boSIDone = false;
+    }        
+}
+void CAMCTL__vSignalHdlr_CLK()
+{
+    static uint8_t u8PulseCtr = 0;
+    
+    if( u8PulseCtr < NO_OF_CLK_PULSES )
+    {
+        if ( u8PulseCtr == 0 )
+        {
+            CAMCTL__xPinCLK = true;
+            u8PulseCtr++;
+        }
+        else
+        {
+            CAMCTL__xPinCLK = !CAMCTL__xPinCLK;
+            if( CAMCTL__xPinCLK == false )
+            {
+                if( ( u8PulseCtr >= 1 ) && (u8PulseCtr < BUFFER_LENGTH )  )
+                {
+                    CAMCTL__au16LineBuffer[u8PulseCtr-1] = CAMCTL__xPinAN.read_u16();
+                }
+                u8PulseCtr++;
+            }
+        }
+        CAMCTL__xClkTimer.attach_us(&CAMCTL__vSignalHdlr_CLK, CLK_PULSE_WIDTH);
+    }
+    else
+    {
+        u8PulseCtr = 0;
+        CAMCTL__stCameraOptions.u8ClkEnd = true;
+    }
+        
+}
+
+void CAMCTL_vTriggerOneShotCapture()
+{
+    CAMCTL__stCameraOptions.u8ActiveCapture = true;
+    CAMCTL__stCameraOptions.u8ClkEnd = false;
+    CAMCTL__stCameraOptions.enCaptureMode = nOneShot;
+    CAMCTL__xGlobalTimer.attach_us(&CAMCTL__vGlobalTimer,BASE_PERIOD);
+}
+
+void CAMCTL_vTriggerContinuousCapture()
+{
+    CAMCTL__stCameraOptions.u8ActiveCapture = true;
+    CAMCTL__stCameraOptions.u8ClkEnd = false;
+    CAMCTL__stCameraOptions.enCaptureMode = nContinuous;
+    CAMCTL__xGlobalTimer.attach_us(&CAMCTL__vGlobalTimer,BASE_PERIOD);
+}
+
+void CAMCTL_vStopContinuousCapture( void )
+{
+    CAMCTL__stCameraOptions.u8ActiveCapture = false;
+    CAMCTL__stCameraOptions.enCaptureMode = nNone;
+    CAMCTL__xGlobalTimer.detach();
+    CAMCTL__xClkTimer.detach();
+    CAMCTL__xSiTimer.detach();
+}
+
+uint16_t * CAMCTL_pu16GetData( void )
+{
+    debugGetData = !debugGetData;
+    debug = CAMCTL__stCameraOptions.u8ClkEnd;
+    if( CAMCTL__stCameraOptions.u8ClkEnd == true )
+    {
+        return CAMCTL__au16LineBuffer;
+    }
+    else
+    {
+        return NULL;    
+    }
+}
+
+void CAMCTL_vInit(void)
+{
+    CAMCTL__stCameraOptions.u8ActiveCapture = false;
+    CAMCTL__stCameraOptions.u8ClkEnd = false;
+    CAMCTL__stCameraOptions.enCaptureMode = nNone;
+}
+
+bool CAMCTL_boCalibrateCamera(uint16_t* pu16ThreshCalibration, uint8_t u8Frames2Wait)
+{
+    static uint8_t u8FrameCtr = 0;
+    uint16_t* pu16DataBuffer;
+    bool boConvDone = false;
+    
+    CAMCTL_vTriggerOneShotCapture();
+    pu16DataBuffer = NULL;
+    while( pu16DataBuffer == NULL )
+    {
+        pu16DataBuffer = CAMCTL_pu16GetData();
+    }
+    u8FrameCtr++;        
+    if( u8FrameCtr > u8Frames2Wait )
+    {
+        u8FrameCtr = 0;
+        boConvDone = true;
+        memcpy( pu16ThreshCalibration, pu16DataBuffer, (BUFFER_LENGTH)*sizeof(uint16_t));
+    }
+    return boConvDone;
+}
\ No newline at end of file
diff -r 000000000000 -r 6cad676fdc0c camctl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/camctl.h	Tue Oct 14 03:52:23 2014 +0000
@@ -0,0 +1,79 @@
+#ifndef _CAMCTL_H_
+#define _CAMCTL_H_
+
+#include "mbed.h"
+
+/**********************************************************************************
+* Camera parameters
+***********************************************************************************/
+
+#define BASE_PERIOD             20/* us
+* Description: All times are based on this time
+*/
+
+#define START_DELAY             10/* x1000 = 1ms
+* Description: All times are based on this time
+*/
+
+#define SI_PULSE_WIDTH          15/* x BASE_PERIOD = 10ms
+* Description: Pulse width of SI signal
+*/
+
+
+#define SI_PULSE_WIDTH_HALF     (SI_PULSE_WIDTH/2)/* x BASE_PERIOD = 10ms
+* Description: Pulse width of SI signal
+*/
+
+#define CLK_PULSE_WIDTH_HALF    (SI_PULSE_WIDTH/2)/* x BASE_PERIOD = 10ms
+* Description: Pulse width of SI signal
+*/
+
+/*#define EXPOSURE_TIME       (uint8)(15)*//* x BASE_PERIOD = 10ms
+* Description: Time between firts SI pulse and second one.
+*/
+
+#define CLK_PULSE_WIDTH         SI_PULSE_WIDTH/* x BASE_PERIOD = 10ms
+* Description: Must be the same value used for SI_PULSE_WIDTH
+*/
+
+#define NO_OF_CLK_PULSES        129/*
+* Description: Number of pulses to trigger between 2 SI pulses.
+*/
+
+#define IDLE_TIME               15/*us
+* Description: Time to wait before the next SI pulse.
+*/
+
+#define BUFFER_LENGTH           128/*
+* Description: Number of to get from linescan camera.
+*/
+
+/**********************************************************************************
+* Variable types definition
+***********************************************************************************/
+typedef enum
+{
+    nContinuous = 0,
+    nOneShot,
+    nNone,
+    nNumberOfModes
+}CAMCTL__tenCaptureMode;
+
+typedef struct
+{
+    uint8_t u8ActiveCapture : 1;
+    uint8_t u8ClkEnd : 1;
+    CAMCTL__tenCaptureMode enCaptureMode;
+}CAMCTL__tstCameraOptions;
+
+/**********************************************************************************
+* Global functions
+***********************************************************************************/
+void CAMCTL_vInit(void);
+void CAMCTL_vTriggerOneShotCapture(void);
+void CAMCTL_vTriggerContinuousCapture(void);
+void CAMCTL_vStopContinuousCapture(void);
+uint16_t * CAMCTL_pu16GetData( void );
+bool CAMCTL_boCalibrateCamera(uint16_t* pu16ThreshCalibration, uint8_t u8Frames2Wait);
+
+#endif /* END OF _CAMCTL_H_ */
\ No newline at end of file