Keymer Lab in silico rasperry pi and ocean optics: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
No edit summary
Line 55: Line 55:
This code uses part of the demo-averaging.c file that can be found in the open-source [https://oceanoptics.com/api/seabreeze/ seabreeze API] of Ocean Optics. It is found under sample-code/c  
This code uses part of the demo-averaging.c file that can be found in the open-source [https://oceanoptics.com/api/seabreeze/ seabreeze API] of Ocean Optics. It is found under sample-code/c  
<syntaxhighlight lang="c" line="1" >
<syntaxhighlight lang="c" line="1" >
/**************************************************************************//**
* @file    demo-averaging.c
* @date    29-Dec-2014
* @author  Ocean Optics, Inc.
* @brief  Demonstrate how to perform multi-scan averaging.
*
* LICENSE:
*
* SeaBreeze Copyright (C) 2014, Ocean Optics Inc
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 74: Line 104:
double * shiftarray(double *old_array, double new_value, int size_array);
double * shiftarray(double *old_array, double new_value, int size_array);
double * blank(double *stor_array, double value, int size_array);
double * blank(double *stor_array, double value, int size_array);
 
void RemoveSpaces(char* source);
void create_csv(double *big_array_1, double *big_array_2, int iteration, char * fileName, unsigned sleep_time);
int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
Line 109: Line 140:
     // Getting parameters for the run of the experiment       //
     // Getting parameters for the run of the experiment       //
     ////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////
int test = 0;
// int test = 0;
unsigned scans_to_average = 0;
// how many scans will be performed at each iteration to average
unsigned integ_time_millis = 0;
unsigned scans_to_average = 20;
// integration time (how much time pixel "opened")
unsigned integ_time_millis = 1;
// nb of iterations
unsigned iterations = 0;
unsigned iterations = 0;
// size of time step in seconds
unsigned size_time_step = 0;
unsigned size_time_step = 0;
char answer;
// char answer;
char fileName[100];
// csv file name
while(test < 100)
time_t  stamp_time;
{
time(&stamp_time);
char fileName[200];
char *buffer;
buffer = ctime(&stamp_time);
RemoveSpaces(buffer);
strcat(fileName,"/home/pi/csv_files/");
strcat(fileName,buffer);
strcat(fileName,"_OD");
strcat(fileName,".csv");
iterations = atoi(argv[1]);
size_time_step = atoi(argv[2]);
 
double nb_of_seconds;
time_t begin_time;
time_t current_time;
begin_time = time(NULL);
 
/*
    printf("\nEnter Your File name for csv(with .csv): ");
    printf("\nEnter Your File name for csv(with .csv): ");
scanf("%s", fileName);
scanf("%s", fileName);
Line 132: Line 185:
scanf("%u", &iterations);
scanf("%u", &iterations);
printf("\n");
printf("\n");
*/  
printf("Enter the time between measures in seconds: ");
scanf("%u", &size_time_step);
    printf("\n");
printf("This corresponds to a total time in minutes of:%u and in hours:%u \n", (iterations*size_time_step)/60, (iterations*size_time_step)/3600);
// this is just to check or restart entrance of parameters
printf("Do you wish to proceed (y or n or q) ?: \n");
scanf(" %c", &answer);
printf("\n");
if (answer == 'y')
{
test = 200;
}
else if (answer == 'q')
{
exit(0);
}
else
{
test++;
}
 
}
     ////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////
     // configure all arrays     //
     // configure all arrays     //
     ///////////////////////////////////////////////////////////////////////////
     ///////////////////////////////////////////////////////////////////////////


// the integration time is how much time a pixel is "open" it have to be low enough for the device not to saturate   
// this is function to send integ time to device    
seabreeze_set_integration_time_microsec(specIndex, &error, integ_time_millis * 1000);
seabreeze_set_integration_time_microsec(specIndex, &error, integ_time_millis * 1000);  
    check_error(specIndex, &error, "seabreeze_set_integration_time_microsec");
check_error(specIndex, &error, "seabreeze_set_integration_time_microsec");
     double *spectrum    = (double*) malloc(pixels * sizeof(double));
// malloc is to keep adress in memory for whole programm until free is called    
double *spectrum    = (double*) malloc(pixels * sizeof(double));
     double *average    = (double*) malloc(pixels * sizeof(double));
     double *average    = (double*) malloc(pixels * sizeof(double));
     double *wavelengths = (double*) malloc(pixels * sizeof(double));
     double *wavelengths = (double*) malloc(pixels * sizeof(double));
double *storage     = (double*) malloc(iterations * sizeof(double));
double *OD_storage     = (double*) malloc(iterations * sizeof(double));
double *time_storage    = (double*) malloc(iterations * sizeof(double));
memset(time_storage, 0, iterations * sizeof(double));
    
    
seabreeze_get_wavelengths(specIndex, &error, wavelengths, pixels);
seabreeze_get_wavelengths(specIndex, &error, wavelengths, pixels);
Line 188: Line 220:
        for (unsigned j = 0; j < pixels; j++)
        for (unsigned j = 0; j < pixels; j++)
{
{
            average[j] += spectrum[j];
// now just adds all values coming from spectrum for all scasns          
average[j] += spectrum[j];
}
}
}
}
Line 194: Line 227:
    for (unsigned i = 0; i < pixels; i++)
    for (unsigned i = 0; i < pixels; i++)
{
{
        average[i] /= scans_to_average;
// divides by nb of scans to get average values        
average[i] /= scans_to_average;
}
}
double avrge = 0;   
double avrge = 0;   
double count = 0;   
double count = 0;   
////////////////////////////////////////////////////////////////
/* selects wavelenghts 595nm & 605nm contained between positions 542 & 601 (pixels) of array average(this depends on the spectrometer)*/
// Initilisation of GNUPLOT session       //
////////////////////////////////////////////////////////////////
//creating the Gnuplot objetcs
gnuplot_ctrl * h1;
gnuplot_ctrl * h2;
// intilizing gnuplot sessions
h1 = gnuplot_init();
h2 = gnuplot_init();
// setting x and y labels for the two sessions
gnuplot_set_xlabel(h1,"t (in minutes)");
gnuplot_set_ylabel(h1, "OD600");
gnuplot_set_xlabel(h2,"t (in minutes)");
gnuplot_set_ylabel(h2, "OD600");
// setting gnuplot session to output gif at given size
gnuplot_cmd(h2, "set terminal gif size 800,600 ");
// setting gnuplot session to output using the x11 window manager
gnuplot_cmd(h1,"set terminal x11");
// setting grid lines on the plots of both sessions
gnuplot_cmd(h1,"set grid ytics lc rgb "#bbbbbb" lw 1 lt 0");
gnuplot_cmd(h1,"set grid xtics lc rgb "#bbbbbb" lw 1 lt 0");
gnuplot_cmd(h2,"set grid ytics lc rgb "#bbbbbb" lw 1 lt 0");
gnuplot_cmd(h2,"set grid xtics lc rgb "#bbbbbb" lw 1 lt 0");
// saving the output of one gnuplot session to a directory
gnuplot_cmd(h2, "set output \'/home/pi/public_html/output.gif\'");
 
/* selects wavelenghts 595nm & 605nm contained between positions 542 & 601 of array average(this depends on the spectrometer)*/
for (unsigned i = 542; i < 601; i++)
for (unsigned i = 542; i < 601; i++)
{       
{       
Line 232: Line 239:
count = count + 1;
count = count + 1;
}
}
// this initializes the whole array that is being plotted to a blank at t = 0
if(u == 0)
OD_storage[u] =10000/(avrge/count)*integ_time_millis;
current_time = time(NULL);
nb_of_seconds = (double) (current_time - begin_time);
time_storage[u] = nb_of_seconds / 60;
create_csv(time_storage, OD_storage, u, fileName, size_time_step);
fprintf(stderr, "OD 600 : %.6lf\n", 10000/(avrge/count)*integ_time_millis);
if (1000/(avrge/count) > 6)
{
{
storage = blank(storage, 10000/(avrge/count), iterations);
integ_time_millis += 1;
}
}
// this shifts the array to the left and inserts the latest value on the right
else
{
storage = shiftarray(storage, 10000/(avrge/count), iterations);
}
// plotting the arrays using the plot_x function of gnuplot_i library
gnuplot_plot_x(h2, storage, iterations, "OD") ;
gnuplot_plot_x(h1, storage, iterations, "OD") ;
fprintf(stderr, "OD 600 : %.6lf\n", avrge/count);
// sleeps the amount of time between timesteps
sleep(size_time_step);
// creates a csv that can be read by gnuplot for future gnuplot
gnuplot_write_x_csv(fileName,storage,iterations,"Overnight" );
// closing the gnuplot sessions (necessary to close the windows on the windows manager
gnuplot_close(h1);
gnuplot_close(h2);
}
}


Line 262: Line 256:
     // cleanup
     // cleanup
     ////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////
free(storage);
free(OD_storage);
free(time_storage);
free(average);
free(average);
     free(spectrum);
     free(spectrum);
Line 287: Line 282:
return stor_array;
return stor_array;
}
}
void RemoveSpaces(char* source)
{
  char* i = source;
  char* j = source;
  while(*j != 0)
  {
    *i = *j++;
    if(*i != ' ' && *i != ':' && *i != '?' && *i != '\n')
      i++;
  }
  *i = 0;
}
void create_csv(double *big_array_1, double *big_array_2, int iteration, char * fileName, unsigned sleep_time)
{
double adapt_array_1[iteration];
double adapt_array_2[iteration];
gnuplot_ctrl * h1;
h1 = gnuplot_init();
gnuplot_cmd(h1, "set terminal gif size 800,600 ");
gnuplot_set_xlabel(h1,"t (in minutes)");
gnuplot_set_ylabel(h1, "OD600");
gnuplot_cmd(h1, "set output \'/home/pi/public_html/output.gif\'");
// gnuplot_cmd(h1,"set grid ytics lc rgb \"#bbbbbb\" lw 1 lt 0");
// gnuplot_cmd(h1,"set grid xtics lc rgb \"#bbbbbb\" lw 1 lt 0");
int i = 0;
while ( i < iteration)
{
adapt_array_1[i] = big_array_1[i];
adapt_array_2[i] = big_array_2[i];
i++;
}
gnuplot_plot_xy(h1, adapt_array_1, adapt_array_2, iteration, "OD over time");
gnuplot_close(h1);
gnuplot_write_xy_csv(fileName, adapt_array_1, adapt_array_2, iteration,"OD over time" );
sleep(sleep_time);
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 13:26, 21 July 2016

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 the open source seabreeze API and a raspberry pi 2 running NOOBS. It also uses the gnuplot_i C library to plot the results using a gnuplot session from a C programm. 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: <syntaxhighlight lang="powershell" line="1" > $ export LD_LIBRARY_PATH="$PWD/lib" </syntaxhighlight>

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 File: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: <syntaxhighlight lang="make" line="1" > SEABREEZE = ../..


APPS = seabreeze-util $(basename $(wildcard demo-*.c)) OBJS = $(addsuffix .o,$(APPS)) UTIL = util.o GNUPLOT = gnuplot_i.o all: $(APPS)

include $(SEABREEZE)/common.mk

$(APPS) : $(OBJS) $(UTIL) $(GNUPLOT) @echo linking $@ @$(CC) -o $@ $@.o $(UTIL) $(GNUPLOT) -lseabreeze $(LFLAGS_APP) -lpthread </syntaxhighlight>

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 <syntaxhighlight lang="c" line="1" > /**************************************************************************//**

* @file    demo-averaging.c
* @date    29-Dec-2014
* @author  Ocean Optics, Inc.
* @brief   Demonstrate how to perform multi-scan averaging.
*
* LICENSE:
*
* SeaBreeze Copyright (C) 2014, Ocean Optics Inc
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <string.h>
  4. include <pthread.h>
  5. include <time.h>
  6. include <unistd.h>
  7. include <sys/time.h>
  8. include <errno.h>
  9. include "api/SeaBreezeWrapper.h"
  10. include "util.h"
  11. include "gnuplot_i.h"
  1. ifdef _WIN32
  2. include <windows.h>
  3. endif
  1. define MAX_LABEL_SIZE 15

double * shiftarray(double *old_array, double new_value, int size_array); double * blank(double *stor_array, double value, int size_array); void RemoveSpaces(char* source); void create_csv(double *big_array_1, double *big_array_2, int iteration, char * fileName, unsigned sleep_time); int main(int argc, char **argv) {

   	int error = 0;
   	logger_header("Output from %s", argv[0]);
   ////////////////////////////////////////////////////////////////////////////
   // open spectrometer
   ////////////////////////////////////////////////////////////////////////////
   	int specIndex = 0;
   	if(seabreeze_open_spectrometer(specIndex, &error))
   	{
       	logger("no devices found.");
       	exit(1);
   	}
   ////////////////////////////////////////////////////////////////////////////
   // describe the unit we're testing
   ////////////////////////////////////////////////////////////////////////////
   	char type[MAX_LABEL_SIZE + 1];
   	seabreeze_get_model(specIndex, &error, type, sizeof(type));
   	if (check_error(specIndex, &error, "seabreeze_get_model"))
       	exit(1);
   	int pixels = seabreeze_get_formatted_spectrum_length(specIndex, &error);
   	if (check_error(specIndex, &error, "seabreeze_get_formatted_spectrum_length"))
       	exit(1);
   	logger("Testing index 0 (%s with %d pixels)", type, pixels);
   ////////////////////////////////////////////////////////////////////////////
   // Getting parameters for the run of the experiment			      //
   ////////////////////////////////////////////////////////////////////////////

// int test = 0; // how many scans will be performed at each iteration to average unsigned scans_to_average = 20; // integration time (how much time pixel "opened") unsigned integ_time_millis = 1; // nb of iterations unsigned iterations = 0; // size of time step in seconds unsigned size_time_step = 0; // char answer; // csv file name time_t stamp_time; time(&stamp_time); char fileName[200]; char *buffer; buffer = ctime(&stamp_time); RemoveSpaces(buffer); strcat(fileName,"/home/pi/csv_files/"); strcat(fileName,buffer); strcat(fileName,"_OD"); strcat(fileName,".csv");

iterations = atoi(argv[1]); size_time_step = atoi(argv[2]);

double nb_of_seconds; time_t begin_time; time_t current_time; begin_time = time(NULL);

/* printf("\nEnter Your File name for csv(with .csv): "); scanf("%s", fileName);

printf("\nEnter integration time (millisec): "); scanf("%u", &integ_time_millis);


printf("Enter scans to average: "); scanf("%u", &scans_to_average); printf("\n");

printf("Enter number of iterations you wish: "); scanf("%u", &iterations); printf("\n");

  • /
   ////////////////////////////////////////////////////////////////////////////
   // configure all arrays 						     //
   ///////////////////////////////////////////////////////////////////////////

// this is function to send integ time to device seabreeze_set_integration_time_microsec(specIndex, &error, integ_time_millis * 1000); check_error(specIndex, &error, "seabreeze_set_integration_time_microsec"); // malloc is to keep adress in memory for whole programm until free is called double *spectrum = (double*) malloc(pixels * sizeof(double));

   	double *average     = (double*) malloc(pixels * sizeof(double));
   	double *wavelengths = (double*) malloc(pixels * sizeof(double));

double *OD_storage = (double*) malloc(iterations * sizeof(double)); double *time_storage = (double*) malloc(iterations * sizeof(double)); memset(time_storage, 0, iterations * sizeof(double));

seabreeze_get_wavelengths(specIndex, &error, wavelengths, pixels); check_error(specIndex, &error, "seabreeze_get_wavelengths");


/////////////////////////////////////////////////////////////////////////// // Running the loop for the length of the experiment /// /////////////////////////////////////////////////////////////////////////// for (unsigned u= 0; u < iterations; u++) { // memset sets all values of average to 0 memset(average, 0, pixels * sizeof(double)); // this wil run and output the average of multiple scans for (int i = 0; i < scans_to_average; i++) { memset(spectrum, 0, pixels * sizeof(double)); // gets the whole spectrum of the device seabreeze_get_formatted_spectrum(specIndex, &error, spectrum, pixels); for (unsigned j = 0; j < pixels; j++) { // now just adds all values coming from spectrum for all scasns average[j] += spectrum[j]; } }

for (unsigned i = 0; i < pixels; i++) { // divides by nb of scans to get average values average[i] /= scans_to_average; } double avrge = 0; double count = 0;

/* selects wavelenghts 595nm & 605nm contained between positions 542 & 601 (pixels) of array average(this depends on the spectrometer)*/ for (unsigned i = 542; i < 601; i++) { avrge = avrge + average[i]; count = count + 1; }

OD_storage[u] =10000/(avrge/count)*integ_time_millis; current_time = time(NULL); nb_of_seconds = (double) (current_time - begin_time); time_storage[u] = nb_of_seconds / 60;

create_csv(time_storage, OD_storage, u, fileName, size_time_step); fprintf(stderr, "OD 600 : %.6lf\n", 10000/(avrge/count)*integ_time_millis);

		if (1000/(avrge/count) > 6)

{ integ_time_millis += 1; } }

   ////////////////////////////////////////////////////////////////////////////
   // cleanup
   ////////////////////////////////////////////////////////////////////////////

free(OD_storage); free(time_storage); free(average);

   	free(spectrum);
   	seabreeze_close_spectrometer(specIndex, &error);

}

double * shiftarray(double *old_array, double new_value, int size_array) { for(unsigned i = 1; i < size_array; i++) { old_array[i-1] = old_array[i];

} old_array[size_array-1] = new_value; return old_array; }

double * blank(double *stor_array, double value, int size_array) { for(unsigned i = 0; i < size_array ; i++) { stor_array[i] = value; } return stor_array; }

void RemoveSpaces(char* source) {

 char* i = source;
 char* j = source;
 while(*j != 0)
 {
   *i = *j++;
   if(*i != ' ' && *i != ':' && *i != '?' && *i != '\n')
     i++;
 }
 *i = 0;

}

void create_csv(double *big_array_1, double *big_array_2, int iteration, char * fileName, unsigned sleep_time) {

double adapt_array_1[iteration]; double adapt_array_2[iteration];

gnuplot_ctrl * h1; h1 = gnuplot_init(); gnuplot_cmd(h1, "set terminal gif size 800,600 "); gnuplot_set_xlabel(h1,"t (in minutes)"); gnuplot_set_ylabel(h1, "OD600"); gnuplot_cmd(h1, "set output \'/home/pi/public_html/output.gif\'"); // gnuplot_cmd(h1,"set grid ytics lc rgb \"#bbbbbb\" lw 1 lt 0"); // gnuplot_cmd(h1,"set grid xtics lc rgb \"#bbbbbb\" lw 1 lt 0");

int i = 0;

while ( i < iteration) { adapt_array_1[i] = big_array_1[i]; adapt_array_2[i] = big_array_2[i]; i++; }

gnuplot_plot_xy(h1, adapt_array_1, adapt_array_2, iteration, "OD over time"); gnuplot_close(h1);

gnuplot_write_xy_csv(fileName, adapt_array_1, adapt_array_2, iteration,"OD over time" ); sleep(sleep_time); } </syntaxhighlight>