Keymer Lab in silico rasperry pi and ocean optics

From OpenWetWare

Jump to: navigation, search

Home        Lab Webiste        In Vitro        Research        Journal Club        Hacks       


This project uses a Ocean optics spectrometer , and a raspberry pi to measure continuously a culture of e.coli growing in a spectrometry cuvette. It is written in C and uses one of the open source Seabreeze Library APIs and a Raspberry Pi computer. It also uses the gnuplot_i C library to plot the results using a gnuplot session from a C program. An example of a 40 hour run:


Installation


Testing your installation

Before running place yourself in the /seabreeze-x.x.x/SeaBreeze/ directory and run the command:

  1. $ export LD_LIBRARY_PATH="$PWD/lib"

Once that is done go in sample-code/c and to test your device run demo-averaging.c . If it your device is connected and up and running it should ask you for integration time and number of scans to average. Once those are entered the program will run for a bit and display a table with wavelengths and counts.

Measuring continuously and plotting in realtime

To run the continuous measurement place the Image:Demo-continuous measurement.c file in sample-code/c . Then also place the gnuplot_i.c and gnuplot_i.h in sample-code/c . You the need to modify the makefile of sample-code/c as such:

  1. SEABREEZE = ../..
  2.  
  3.  
  4. APPS = seabreeze-util $(basename $(wildcard demo-*.c)) 
  5. OBJS = $(addsuffix .o,$(APPS))
  6. UTIL = util.o
  7. GNUPLOT = gnuplot_i.o
  8. all: $(APPS)
  9.  
  10. include $(SEABREEZE)/common.mk
  11.  
  12. $(APPS) : $(OBJS) $(UTIL) $(GNUPLOT)
  13. 	@echo linking $@
  14. 	@$(CC) -o $@ $@.o $(UTIL) $(GNUPLOT) -lseabreeze $(LFLAGS_APP) -lpthread

This allows us to link the gnuplot_i library to the compilation of the files in sample-code/c


The C programm that allows us to plot and save in a csv file in real time

This code uses part of the demo-averaging.c file that can be found in the open-source seabreeze API of Ocean Optics. It is found under sample-code/c

  1. /**************************************************************************//**
  2.  * @file    demo-averaging.c
  3.  * @date    29-Dec-2014
  4.  * @author  Ocean Optics, Inc.
  5.  * @brief   Demonstrate how to perform multi-scan averaging.
  6.  *
  7.  * LICENSE:
  8.  *
  9.  * SeaBreeze Copyright (C) 2014, Ocean Optics Inc
  10.  *
  11.  * Permission is hereby granted, free of charge, to any person obtaining
  12.  * a copy of this software and associated documentation files (the
  13.  * "Software"), to deal in the Software without restriction, including
  14.  * without limitation the rights to use, copy, modify, merge, publish,
  15.  * distribute, sublicense, and/or sell copies of the Software, and to
  16.  * permit persons to whom the Software is furnished to do so, subject
  17.  * to the following conditions:
  18.  *
  19.  * The above copyright notice and this permission notice shall be included
  20.  * in all copies or substantial portions of the Software.
  21.  *
  22.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  25.  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  26.  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  27.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  28.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29.  ******************************************************************************/
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <pthread.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include <sys/time.h>
  38. #include <errno.h>
  39. #include "api/SeaBreezeWrapper.h"
  40. #include "util.h"
  41. #include "gnuplot_i.h"
  42.  
  43. #ifdef _WIN32
  44. #include <windows.h>
  45. #endif
  46.  
  47. #define MAX_LABEL_SIZE     15
  48. double * shiftarray(double *old_array, double new_value, int size_array);
  49. double * blank(double *stor_array, double value, int size_array);
  50. void RemoveSpaces(char* source);
  51. void create_csv(double *big_array_1, double *big_array_2, int iteration, char * fileName, unsigned sleep_time);
  52. int main(int argc, char **argv)
  53. {	
  54.     	int error = 0;
  55.     	logger_header("Output from %s", argv[0]);
  56.  
  57.     ////////////////////////////////////////////////////////////////////////////
  58.     // open spectrometer
  59.     ////////////////////////////////////////////////////////////////////////////
  60.  
  61.     	int specIndex = 0;
  62.     	if(seabreeze_open_spectrometer(specIndex, &error))
  63.     	{
  64.         	logger("no devices found.");
  65.         	exit(1);
  66.     	}
  67.  
  68.     ////////////////////////////////////////////////////////////////////////////
  69.     // describe the unit we're testing
  70.     ////////////////////////////////////////////////////////////////////////////
  71.  
  72.     	char type[MAX_LABEL_SIZE + 1];
  73.     	seabreeze_get_model(specIndex, &error, type, sizeof(type));
  74.     	if (check_error(specIndex, &error, "seabreeze_get_model"))
  75.         	exit(1);
  76.  
  77.     	int pixels = seabreeze_get_formatted_spectrum_length(specIndex, &error);
  78.     	if (check_error(specIndex, &error, "seabreeze_get_formatted_spectrum_length"))
  79.         	exit(1);
  80.  
  81.     	logger("Testing index 0 (%s with %d pixels)", type, pixels);
  82.  
  83.     ////////////////////////////////////////////////////////////////////////////
  84.     // Getting parameters for the run of the experiment			      //
  85.     ////////////////////////////////////////////////////////////////////////////
  86. //	int test = 0;
  87. // how many scans will be performed at each iteration to average	
  88. 	unsigned scans_to_average = 20;
  89. // integration time (how much time pixel "opened")
  90. 	unsigned integ_time_millis = 1;
  91. // nb of iterations
  92. 	unsigned iterations = 0;
  93. // size of time step in seconds
  94. 	unsigned size_time_step = 0;
  95. //	char answer;
  96. // csv file name
  97. 	time_t  stamp_time;
  98. 	time(&stamp_time);	
  99. 	char fileName[200];
  100. 	char *buffer; 
  101. 	buffer = ctime(&stamp_time);
  102. 	RemoveSpaces(buffer);
  103. 	strcat(fileName,"/home/pi/csv_files/");
  104. 	strcat(fileName,buffer);
  105. 	strcat(fileName,"_OD");
  106. 	strcat(fileName,".csv");
  107.  
  108. 	iterations = atoi(argv[1]);
  109. 	size_time_step = atoi(argv[2]);
  110.  
  111. 	double nb_of_seconds;
  112. 	time_t begin_time;
  113. 	time_t current_time;
  114. 	begin_time = time(NULL);
  115.  
  116.     ////////////////////////////////////////////////////////////////////////////
  117.     // configure all arrays 						     //
  118.     ///////////////////////////////////////////////////////////////////////////
  119.  
  120. 	// this is function to send integ time to device     	
  121. 	seabreeze_set_integration_time_microsec(specIndex, &error, integ_time_millis * 1000);    	
  122. 	check_error(specIndex, &error, "seabreeze_set_integration_time_microsec");
  123.  
  124. // malloc is to keep adress in memory for whole programm until free is called     	
  125. 	double *spectrum    = (double*) malloc(pixels * sizeof(double));
  126.     	double *average     = (double*) malloc(pixels * sizeof(double));
  127.     	double *wavelengths = (double*) malloc(pixels * sizeof(double));
  128. 	double *OD_storage     = (double*) malloc(iterations * sizeof(double));
  129. 	double *time_storage     = (double*) malloc(iterations * sizeof(double));
  130. 	memset(time_storage, 0, iterations * sizeof(double));
  131.  
  132. 	seabreeze_get_wavelengths(specIndex, &error, wavelengths, pixels);
  133. 	check_error(specIndex, &error, "seabreeze_get_wavelengths");
  134.  
  135.  
  136. 	  ///////////////////////////////////////////////////////////////////////////
  137. 	 // Running the loop for the length of the experiment		         ///
  138. 	///////////////////////////////////////////////////////////////////////////
  139. 	for (unsigned u= 0; u < iterations; u++)
  140. 	{
  141. 		// memset sets all values of average to 0		
  142. 		memset(average, 0, pixels * sizeof(double));
  143. 		// this wil run and output the average of multiple scans 
  144. 		for (int i = 0; i < scans_to_average; i++) 
  145. 		{
  146. 		        memset(spectrum, 0, pixels * sizeof(double));
  147. 			// gets the whole spectrum of the device		        
  148. 			seabreeze_get_formatted_spectrum(specIndex, &error, spectrum, pixels);
  149. 		        for (unsigned j = 0; j < pixels; j++)
  150. 			{
  151. 			// now just adds all values coming from spectrum for all scasns		           
  152. 			 average[j] += spectrum[j];
  153. 			}
  154. 		}
  155.  
  156. 	    	for (unsigned i = 0; i < pixels; i++)
  157. 		{
  158. 			// divides by nb of scans to get average values	        	
  159. 			average[i] /= scans_to_average;
  160. 		}
  161. 		double avrge = 0;  
  162. 		double count = 0;   
  163.  
  164. 		/* selects wavelenghts 595nm & 605nm contained between positions 542 & 601 (pixels) of array average(this depends on the spectrometer)*/
  165. 		for (unsigned i = 542; i < 601; i++)
  166. 		{      
  167. 			avrge = avrge + average[i];
  168. 			count = count + 1;				
  169. 		}
  170.  
  171. 		OD_storage[u] =10000/(avrge/count)*integ_time_millis;
  172. 		current_time = time(NULL);
  173. 		nb_of_seconds = (double) (current_time - begin_time);
  174. 		time_storage[u] = nb_of_seconds / 60;
  175.  
  176. 		create_csv(time_storage, OD_storage, u, fileName, size_time_step);
  177. 		fprintf(stderr, "OD 600 : %.6lf\n", 10000/(avrge/count)*integ_time_millis);	
  178.  		if (1000/(avrge/count) > 6)
  179. 		{
  180. 			integ_time_millis += 1;
  181. 		}
  182. 	}
  183.  
  184.     ////////////////////////////////////////////////////////////////////////////
  185.     // cleanup
  186.     ////////////////////////////////////////////////////////////////////////////
  187. 	free(OD_storage);
  188. 	free(time_storage);
  189. 	free(average);
  190.     	free(spectrum);
  191.     	seabreeze_close_spectrometer(specIndex, &error);
  192. }
  193.  
  194. void RemoveSpaces(char* source)
  195. {
  196.   char* i = source;
  197.   char* j = source;
  198.   while(*j != 0)
  199.   {
  200.     *i = *j++;
  201.     if(*i != ' ' && *i != ':' && *i != '?' && *i != '\n')
  202.       i++;
  203.   }
  204.   *i = 0;
  205. }
  206.  
  207. void create_csv(double *big_array_1, double *big_array_2, int iteration, char * fileName, unsigned sleep_time)
  208. {
  209.  
  210. 	double adapt_array_1[iteration];
  211. 	double adapt_array_2[iteration];
  212.  
  213. 	gnuplot_ctrl * h1;
  214. 	h1 = gnuplot_init();
  215. 	gnuplot_cmd(h1, "set terminal gif size 800,600 ");
  216. 	gnuplot_set_xlabel(h1,"t (in minutes)");
  217. 	gnuplot_set_ylabel(h1, "OD600");
  218. 	gnuplot_cmd(h1, "set output \'/home/pi/public_html/output.gif\'");
  219. //	gnuplot_cmd(h1,"set grid ytics lc rgb \"#bbbbbb\" lw 1 lt 0");
  220. //	gnuplot_cmd(h1,"set grid xtics lc rgb \"#bbbbbb\" lw 1 lt 0");
  221.  
  222. 	int i = 0;
  223.  
  224. 	while ( i < iteration)
  225. 		{
  226. 			adapt_array_1[i] = big_array_1[i];
  227. 			adapt_array_2[i] = big_array_2[i];
  228. 			i++;
  229. 		}
  230.  
  231. 	gnuplot_plot_xy(h1, adapt_array_1, adapt_array_2, iteration, "OD over time");
  232. 	gnuplot_close(h1);
  233.  
  234. 	gnuplot_write_xy_csv(fileName, adapt_array_1, adapt_array_2, iteration,"OD over time" );
  235. 	sleep(sleep_time);
  236. }
Personal tools