Ejemplo n.º 1
0
/***************************************************************************
Grab sample temperature and calculate running mean and variance
   see http://www.johndcook.com/blog/standard_deviation/ 
***************************************************************************/
void sampleTemp(Measurement* m, int* semaphorePtr) {
   double T, mLast;
   m->n++;
   if (m->n == 1) { //first measurement
      m->M = get_measurement(m->channel, semaphorePtr); //get_measurement first gets internal junction temp then gets TC (20ms delays)
      m->S = 0.0;
      m->max = m->M;
      m->min = m->M;
   } else {
      T = get_measurement_fast(m->channel, semaphorePtr); //get_measurement_fast just grabs TC value and starts next TC sampling
      mLast = m->M;
      m->M = mLast + (T - mLast)/m->n; //running mean
      m->S = m->S + (T - mLast) * (T - m->M);
      m->max = fmax(T, m->max);
      m->min = fmin(T, m->min);
   }
}
Ejemplo n.º 2
0
int
main(int argc, char* argv[])
{
	int ret;
	int i;
	uint8_t spi_config=0;
	double tval;
	int repeat=0;
	int period=1;
	char fname[128];
	char tstring[128];
	char tstring2[128];
	time_t mytime;
	time_t desiredtime;
	struct timespec tstime;
	int not_finished=1;
	int elapsed=0;
	int showtime=0;
	
	// initialise GPIO
	setup_io();
	INP_GPIO(LCD_RS_GPIO); // must use INP_GPIO before we can use OUT_GPIO
  OUT_GPIO(LCD_RS_GPIO);
	
	// parse inputs
	dofile=0;
	if (argc>2)
	{
		if (strcmp(argv[1], "msg")==0) // print to the lcd
		{
			spi_config=0;
			ret=spi_open(&lcd_fd, 0, spi_config);
			if (ret!=0)
			{
				printf("Exiting\n");
			exit(1);
			}
			lcd_display_string(0,argv[2]);
			close(lcd_fd);
			exit(0);
		}
	}
	if (argc>1)
	{
		if (strcmp(argv[1], "-h")==0) // print help
		{
			printf("%s [sec] [filename]\n", argv[0]);
			printf("%s msg <message in quotes>\n", argv[0]);
			exit(0);
		}
		if (strcmp(argv[1], "lcdinit")==0) // initialize the LCD display
		{
			spi_config=0;
			ret=spi_open(&lcd_fd, 0, spi_config);
			if (ret!=0)
			{
				printf("Exiting\n");
				exit(1);
			}
			lcd_init();
			close(lcd_fd);
			exit(0);
		}
		if (strcmp(argv[1], "withtime")==0)
		{
			showtime=1;
		}
		else
		{
			sscanf(argv[1], "%d", &period); // period between measurements
			repeat=1;
		}
		if (argc>3)
		{
			if (strcmp(argv[2], "msg")==0) // print to the lcd
			{
				spi_config=0;
				ret=spi_open(&lcd_fd, 0, spi_config);
				if (ret!=0)
				{
					printf("Exiting\n");
					exit(1);
				}
				lcd_init();
				lcd_display_string(0,argv[3]);
				lcd_initialised=1;
			}
		}
	}
	if (argc>2)
	{
		strcpy(fname, argv[2]); // file name
		dofile=1;
		if (argc>4)
		{
			if (strcmp(argv[3], "msg")==0) // print to the lcd
			{
				spi_config=0;
				ret=spi_open(&lcd_fd, 0, spi_config);
				if (ret!=0)
				{
					printf("Exiting\n");
					exit(1);
				}
				lcd_init();
				lcd_display_string(0,argv[4]);
				lcd_initialised=1;
			}
		}
	}
	
	if (dofile)
	{
		outfile=fopen(fname, "w");
	}
	
	// open SPI for ADS1118
	spi_config |= SPI_CPHA;
	ret=spi_open(&ads_fd, 1, spi_config);
	if (ret!=0) // SPI error
	{
		printf("Exiting\n");
		exit(1);
	}

	if (repeat==0)
	{
		// display a single measurement and exit
		tval=get_measurement();
		if (showtime)
		{
			mytime = time(NULL);
			sprintf(tstring, "%d", mytime);
			unixtime2string(tstring, tstring2);
			printf("%s %#.1f\n", tstring2, tval);
		}
		else
		{
			printf("%#.1f\n", tval);
		}
		close(ads_fd);
		exit(0);
	}
	
	// open up SPI for LCD
  spi_config=0;
	ret=spi_open(&lcd_fd, 0, spi_config);
	if (ret!=0)
	{
		printf("Exiting\n");
		exit(1);
	}

	if (lcd_initialised==0)
		lcd_init();
	
	if (dofile)
	{
		// line 1 of the output file will contain the three column descriptions
		fprintf(outfile, "Time HH:MM:SS,Elapsed Sec,Temp C\n");
	}
	
	// Align on an integer number of seconds and get current time
  mytime = time(NULL);
  tstime.tv_sec=mytime+1;
  tstime.tv_nsec=0;
  clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &tstime, NULL);
  mytime++;
  desiredtime=mytime;

	signal(SIGINT, sig_handler);
	
	while(not_finished)
	{
		tval=get_measurement();
		for (i=1; i<10; i++)
		{
			delay_ms(10);
			tval=tval+get_measurement_fast();
		}
		tval=tval/10;
		
		// print the time, elapsed counter and temperature
		sprintf(tstring, "%d", mytime);
		unixtime2string(tstring, tstring2);
		if (mytime==desiredtime)
		{
			printf("%s %d %#.1f\n", tstring2, elapsed, tval);
			if (dofile)
			{
				fprintf(outfile, "%s,%d,%#.1f\n", tstring2, elapsed, tval);
				fflush(outfile);
			}
			desiredtime=desiredtime+period;
		}
		sprintf(tstring, "%s %#.1f", tstring2, tval);
		lcd_display_string(1, tstring);
		// now we sleep for a certain time
		mytime++;
		tstime.tv_sec=mytime;
		elapsed++;
		clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &tstime, NULL);
	}
	
  close(ads_fd);
    
  // LCD requires opposite clock polarity
  spi_config=0;
	ret=spi_open(&lcd_fd, 0, spi_config);
	if (ret!=0)
	{
		printf("Exiting\n");
		exit(1);
	}


	lcd_init();
	lcd_clear();					// LCD clear
	lcd_display_string(0,"Hello");	// display "ADS1118"
  
  close(lcd_fd);
  
  return(0);
}