From 6f3369862370d744055eb9a60876b8dc29d51611 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Wed, 30 Oct 2013 12:39:58 +0800 Subject: [PATCH] Fix dilatometer, other misc stuff Camera_GetImage needs to modify IplImage* not CvMat* Other bug fixes. --- server/Makefile | 3 ++ server/actuators/pregulator.c | 1 + server/image.c | 76 ++++++++++++++++++++--------------- server/image.h | 2 +- server/parameters | 5 ++- server/sensor.c | 10 ++--- server/sensors/dilatometer.c | 38 ++++++++++++++++-- server/sensors/strain.h | 1 + 8 files changed, 93 insertions(+), 43 deletions(-) diff --git a/server/Makefile b/server/Makefile index c314b20..00ed880 100644 --- a/server/Makefile +++ b/server/Makefile @@ -18,6 +18,9 @@ all : $(BIN) : $(OBJ) $(CXX) $(FLAGS) -o $(BIN) $(OBJ) $(LIB) +microscope : microscope.o + $(CXX) $(FLAGS) -o microscope microscope.o $(LIB) + %.o : %.c $(CXX) $(FLAGS) -c $< diff --git a/server/actuators/pregulator.c b/server/actuators/pregulator.c index 1cd62d3..8b9d06e 100644 --- a/server/actuators/pregulator.c +++ b/server/actuators/pregulator.c @@ -31,6 +31,7 @@ bool Pregulator_Cleanup(int id) bool Pregulator_Set(int id, double value) { double anti_calibrated = Data_Calibrate(value, preg_cal, pwm_raw, sizeof(pwm_raw)/sizeof(double)); + Log(LOGDEBUG, "Pregulator value %f -> PWM duty cycle %f", value, anti_calibrated); if (anti_calibrated < 0) anti_calibrated = 0; if (anti_calibrated > 1) diff --git a/server/image.c b/server/image.c index e1a586c..d19ee7a 100644 --- a/server/image.c +++ b/server/image.c @@ -5,9 +5,8 @@ #include #include -CvCapture *capture; -IplImage *frame; -int captureID = -1; +static CvCapture * g_capture = NULL; +static int g_captureID = -1; void Image_Handler(FCGIContext * context, char * params) { @@ -29,61 +28,74 @@ void Image_Handler(FCGIContext * context, char * params) return; } - CvMat * g_src = NULL; // Source Image - CvMat * g_encoded; // Encoded Image + IplImage * src = NULL; // Source Image + CvMat * encoded = NULL; // Encoded Image - Camera_GetImage( num, width, height ,g_src); - g_encoded = cvEncodeImage("test_encode.jpg",g_src,0); + Camera_GetImage( num, width, height ,&src); + + Log(LOGDEBUG, "About to encode"); + encoded = cvEncodeImage(".jpg",src,0); + Log(LOGDEBUG, "Encoded"); Log(LOGNOTE, "Sending image!"); FCGI_PrintRaw("Content-type: image/jpg\r\n"); FCGI_PrintRaw("Cache-Control: no-cache, no-store, must-revalidate\r\n\r\n"); //FCGI_PrintRaw("Content-Length: %d", g_encoded->rows*g_encoded->cols); - FCGI_WriteBinary(g_encoded->data.ptr,1,g_encoded->rows*g_encoded->cols); + FCGI_WriteBinary(encoded->data.ptr,1,encoded->rows*encoded->cols); - cvReleaseMat(&g_encoded); - cvReleaseMat(&g_src); + cvReleaseMat(&encoded); + cvReleaseImageHeader(&src); } - bool Camera_GetImage(int num, int width, int height, CvMat * image) +/** + * Attempts to get an image from a camera + * @param num - Camera id + * @param width - Width to force + * @param height - Height to force + * @param image - Pointer to CvMat* to set with result + * @returns true on success, false on error + */ + bool Camera_GetImage(int num, int width, int height, IplImage ** frame) { + Log(LOGDEBUG, "Called with arguments num=%d width=%d height=%d frame=%p", num,width,height, frame); static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // Need to use a mutex to ensure 2 captures are not open at once pthread_mutex_lock(&mutex); bool result = false; - if( capture == NULL) + if( g_capture == NULL) { - capture = cvCreateCameraCapture(num); - captureID = num; + g_capture = cvCreateCameraCapture(num); + g_captureID = num; } - else if( num != captureID) + else if( num != g_captureID) { - cvReleaseCapture(&capture); - capture = cvCreateCameraCapture(num); - captureID = num; + cvReleaseCapture(&g_capture); + g_capture = cvCreateCameraCapture(num); + g_captureID = num; } - cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width); - cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height); - - frame = cvQueryFrame(capture); - if( frame == NULL) - return result; + //cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_WIDTH, width); + //cvSetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_HEIGHT, height); - // Convert the IplImage pointer to CvMat - CvMat stub; - image = cvGetMat(frame, &stub, 0, 0); - if( image == NULL) - return result; + *frame = cvQueryFrame(g_capture); + result = (*frame != NULL); + //cvShowImage("display", *image); + //cvWaitKey(0); + //cvSaveImage("test.jpg",*image,0); + + Log(LOGDEBUG, "At end of mutex"); + pthread_mutex_unlock(&mutex); //Close the mutex - return true; + + //NOTE: Never have a "return" statement before the mutex is unlocked; it causes deadlocks! + return result; } void Image_Cleanup() { // Release the capture and IplImage pointers - cvReleaseImageHeader(&frame); - cvReleaseCapture(&capture); + //cvReleaseImageHeader(&g_frame); + cvReleaseCapture(&g_capture); } diff --git a/server/image.h b/server/image.h index 7f4271d..8e2cd68 100644 --- a/server/image.h +++ b/server/image.h @@ -12,7 +12,7 @@ extern void Image_Init(); extern void Image_Handler(FCGIContext * context, char * params); extern void Image_Cleanup(); -extern bool Camera_GetImage(int num, int width, int height, CvMat * image); +extern bool Camera_GetImage(int num, int width, int height, IplImage ** image); #endif //_IMAGE_H diff --git a/server/parameters b/server/parameters index 5fdc595..a8dc0a0 100644 --- a/server/parameters +++ b/server/parameters @@ -25,8 +25,9 @@ pin_test="0" #auth_uri="ldaps://ldap.pheme.uwa.edu.au#ou=Users,ou=UWA,dc=uwads,dc=uwa,dc=edu,dc=au" #UWA #auth_uri="/etc/shadow" #auth_uri="shadow" -auth_uri="mysql://localhost#root,$(cat mysql_password)" +#auth_uri="mysql://localhost#root,$(cat mysql_password)" ## OPTIONS TO BE PASSED TO SERVER; DO NOT EDIT -parameters="-v $verbosity -p $pin_test -A $auth_uri" +parameters="-v $verbosity -p $pin_test" +# -A $auth_uri" diff --git a/server/sensor.c b/server/sensor.c index bc424c3..b934e8f 100644 --- a/server/sensor.c +++ b/server/sensor.c @@ -84,16 +84,16 @@ void Sensor_Init() Sensor_Add("pressure_high1", PRES_HIGH1, Pressure_Read, Pressure_Init, Pressure_Cleanup, NULL); Sensor_Add("pressure_low0", PRES_LOW0, Pressure_Read, Pressure_Init, Pressure_Cleanup, NULL); //Sensor_Add("../testing/count.py", 0, Piped_Read, Piped_Init, Piped_Cleanup, 1e50,-1e50,1e50,-1e50); - Sensor_Add("strain0", STRAIN0, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); - Sensor_Add("strain1", STRAIN1, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); - Sensor_Add("strain2", STRAIN2, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); - Sensor_Add("strain3", STRAIN3, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); + Sensor_Add("strain0_endhoop", STRAIN0, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); + Sensor_Add("strain1_endlong", STRAIN1, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); + Sensor_Add("strain2_midhoop", STRAIN2, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); + Sensor_Add("strain3_midlong", STRAIN3, Strain_Read, Strain_Init, Strain_Cleanup, Strain_Sanity); //Sensor_Add("pressure0", PRESSURE0, Pressure_Read, Pressure_Init, 5000,0,5000,0); //Sensor_Add("pressure1", PRESSURE1, Pressure_Read, Pressure_Init, 5000,0,5000,0); //Sensor_Add("pressure_feedback", PRESSURE_FEEDBACK, Pressure_Read, Pressure_Init, 5000,0,5000,0); //Sensor_Add("enclosure", ENCLOSURE, Enclosure_Read, Enclosure_Init, 1,1,1,1); Sensor_Add("dilatometer_pos", DIL_POS, Dilatometer_Read, Dilatometer_Init, Dilatometer_Cleanup, NULL); - Sensor_Add("dilatometer_diff",DIL_DIFF, Dilatometer_Read, Dilatometer_Init, Dilatometer_Cleanup, NULL); + //Sensor_Add("dilatometer_diff",DIL_DIFF, Dilatometer_Read, Dilatometer_Init, Dilatometer_Cleanup, NULL); } /** diff --git a/server/sensors/dilatometer.c b/server/sensors/dilatometer.c index f197941..8d04fb5 100644 --- a/server/sensors/dilatometer.c +++ b/server/sensors/dilatometer.c @@ -6,6 +6,7 @@ #include "cv.h" #include "highgui_c.h" #include "dilatometer.h" +#include "../image.h" #include // test positions @@ -121,9 +122,20 @@ bool Dilatometer_Cleanup(int id) void CannyThreshold() { + + // Create greyscale array + if (g_srcGray == NULL) + { + Log(LOGDEBUG, "%d %d %d", g_srcRGB->rows, g_srcRGB->cols, CV_8UC1); + g_srcGray = cvCreateMat(g_srcRGB->rows,g_srcRGB->cols,CV_8UC1); + } + // Convert the RGB source file to grayscale + Log(LOGDEBUG, "About to cvCvtColor(%p, %p, %d)", g_srcRGB, g_srcGray, CV_RGB2GRAY); cvCvtColor(g_srcRGB,g_srcGray,CV_RGB2GRAY); + + Log(LOGDEBUG, "About to cvCreateMat"); if ( g_edges == NULL) { g_edges = cvCreateMat(g_srcGray->rows,g_srcGray->cols,CV_8UC1); @@ -164,15 +176,35 @@ bool Dilatometer_GetExpansion( int id, double * value, int samples) bool result = false; double average = 0; // Get the image from the camera - result = Camera_GetImage( 0, 1600, 1200 ,&g_srcRGB); // Get a 1600x1200 image and place it into src + Log(LOGDEBUG, "GET IMAGE?"); + + IplImage * frame = NULL; + result = Camera_GetImage( 0, 1600, 1200 ,&frame); // Get a 1600x1200 image and place it into src + Log(LOGDEBUG, "Got image..."); // If an error occured when capturing image then return + + + if (result) + { + CvMat stub; + g_srcRGB = cvGetMat(frame,&stub,0,0); + result = (g_srcRGB != NULL); + Log(LOGDEBUG, "Converted image %d %p", result, g_srcRGB); + } + cvReleaseImageHeader(&frame); + if (!result) return result; + + Log(LOGDEBUG, "GOT IMAGE (without error)!"); + // Apply the Canny Edge theorem to the image CannyThreshold(); + Log(LOGDEBUG, "Got past CannyThreshold()"); + int width = g_edges->cols; int height = g_edges->rows; @@ -259,8 +291,8 @@ bool Dilatometer_Init(const char * name, int id) // Make an initial reading (will allocate memory the first time only). double val; lastPosition = 0; // Reset the last position - bool result = Dilatometer_GetExpansion(DIL_POS, &val, 1); - return result; + Dilatometer_GetExpansion(DIL_POS, &val, 1); + return true; } // Overlays a line over the given edge position diff --git a/server/sensors/strain.h b/server/sensors/strain.h index 21b926e..6112fe7 100644 --- a/server/sensors/strain.h +++ b/server/sensors/strain.h @@ -1,6 +1,7 @@ #ifndef _STRAIN_H #define _STRAIN_H +#include "../common.h" #include /** -- 2.20.1