Keymer Lab in silico rasperry pi and ocean optics: Difference between revisions
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; | |||
unsigned scans_to_average = | // how many scans will be performed at each iteration to average | ||
unsigned integ_time_millis = | 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 fileName[ | // 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): "); | 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"); | ||
*/ | |||
//////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////// | ||
// configure all arrays // | // configure all arrays // | ||
/////////////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////////////// | ||
// | // 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"); | |||
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 * | 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++) | ||
{ | { | ||
// 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++) | ||
{ | { | ||
// 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)*/ | |||
/* 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; | ||
} | } | ||
// | |||
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; | |||
} | } | ||
} | } | ||
Line 262: | Line 256: | ||
// cleanup | // cleanup | ||
//////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////// | ||
free( | 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
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. ******************************************************************************/
- 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); 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>