Keymer Lab in silico rasperry pi and ocean optics: Difference between revisions
No edit summary |
No edit summary |
||
Line 13: | Line 13: | ||
'''Testing your installation''' | '''Testing your installation''' | ||
Before running place yourself in the /seabreeze-x.x.x/SeaBreeze/ directory and run the command: | Before running place yourself in the /seabreeze-x.x.x/SeaBreeze/ directory and run the command: | ||
<syntaxhighlight lang="powershell" line="1" > | <syntaxhighlight lang="powershell" line="1" > | ||
$ export LD_LIBRARY_PATH="$PWD/lib" | $ export LD_LIBRARY_PATH="$PWD/lib" | ||
</syntaxhighlight> | </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''' | '''Measuring continuously and plotting in realtime''' |
Revision as of 09:41, 13 June 2016
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.
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" >
- include <stdio.h>
- include <stdlib.h>
- include <string.h>
- include <pthread.h>
- include <time.h>
- include <unistd.h>
- include <sys/time.h>
- include <errno.h>
- include "api/SeaBreezeWrapper.h"
- include "util.h"
- include "gnuplot_i.h"
- ifdef _WIN32
- include <windows.h>
- endif
- 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);
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; unsigned scans_to_average = 0; unsigned integ_time_millis = 0; unsigned iterations = 0; unsigned size_time_step = 0; char answer; char fileName[100]; while(test < 100) { 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");
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 // ///////////////////////////////////////////////////////////////////////////
// the integration time is how much time a pixel is "open" it have to be low enough for the device not to saturate seabreeze_set_integration_time_microsec(specIndex, &error, integ_time_millis * 1000);
check_error(specIndex, &error, "seabreeze_set_integration_time_microsec"); double *spectrum = (double*) malloc(pixels * sizeof(double)); double *average = (double*) malloc(pixels * sizeof(double)); double *wavelengths = (double*) malloc(pixels * sizeof(double));
double *storage = (double*) malloc(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++)
{
average[j] += spectrum[j];
}
}
for (unsigned i = 0; i < pixels; i++) { average[i] /= scans_to_average; } double avrge = 0; double count = 0;
//////////////////////////////////////////////////////////////// // 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++) { avrge = avrge + average[i]; count = count + 1; } // this initializes the whole array that is being plotted to a blank at t = 0 if(u == 0) { storage = blank(storage, 10000/(avrge/count), iterations); } // 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);
}
//////////////////////////////////////////////////////////////////////////// // cleanup ////////////////////////////////////////////////////////////////////////////
free(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; } </syntaxhighlight>