Ejemplo n.º 1
0
void temp_tick(void)
{

    /* Read and average temperatures */
    current_temp[EXTRUDER_0] = read_temp(EXTRUDER_0);
    current_temp[HEATED_BED_0] = read_temp(HEATED_BED_0);

    ticks ++;
    if (ticks == NUM_TICKS)
    {
        /* Manage heater using simple ON/OFF logic, no PID */
        if (current_temp[EXTRUDER_0] < target_temp[EXTRUDER_0])
        {
            extruder_heater_on();
        }
        else
        {
            extruder_heater_off();
        }

        /* Manage heater using simple ON/OFF logic, no PID */
        if (current_temp[HEATED_BED_0] < target_temp[HEATED_BED_0])
        {
            heated_bed_on();
        }
        else
        {
            heated_bed_off();
        }

        ticks = 0;
    }
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[])
{
	int		 i, c, done;
	int		 offset;
	char	 buf[128], str[64];
	short	 temp;

	i = 0;
	offset = 0;
	init_board();

read_input:
	if (IS_PRESSED(BUTTON1)) {
		UART0_puts("_BALANCE:1_", 64);
		UART0_gets(str, sizeof(str));
		LCD_command(1);
		LCD_puts(str, sizeof(str));
		LCD_putchar('\n');
		temp = read_temp();
		show_temperature(temp);
	} else if (IS_PRESSED(BUTTON2)) {
		UART0_puts("_RECHARGE:1:", 64);
		escrevenum(leds_state);
		UART0_puts("_", 64);
		UART0_gets(str, sizeof(str));
		LCD_command(1);
		LCD_puts(str, sizeof(str));
		LCD_putchar('\n');
		temp = read_temp();
		show_temperature(temp);
	} else if (IS_PRESSED(BUTTON3)) {
		UART0_puts("_DISCHARGE:1:", 64);
		escrevenum(leds_state);
		UART0_puts("_", 64);
		UART0_gets(str, sizeof(str));
		LCD_command(1);
		LCD_puts(str, sizeof(str));
		LCD_putchar('\n');
		temp = read_temp();
		show_temperature(temp);
	} else if (IS_PRESSED(BUTTON4)) {
		leds_state++;
		UPDATE_LED();
		uwait(1000);
	} else if (IS_PRESSED(BUTTON5)) {
		leds_state--;
		UPDATE_LED();
		uwait(1000);
	}
	goto read_input;

	/* NOTREACHED */
	return (0);
}
Ejemplo n.º 3
0
Archivo: temp.c Proyecto: lwalkera/R2C2
void temp_tick(void)
{

	/* Define a value for sequencial number of reads of ADC, to average the readed
	 * value and try filter high frequency noise.
	 */
#define ADC_READ_TIMES 4

	/* Read and average the ADC input signal */
	current_temp[EXTRUDER_0] = 0;
	for (uint8_t c = 0; c < ADC_READ_TIMES; c++)
	{
		/* Read EXTRUDER_0 temperature value and manage heater */
		current_temp[EXTRUDER_0] += read_temp(EXTRUDER_0);
	}
	current_temp[EXTRUDER_0] = current_temp[EXTRUDER_0] / ADC_READ_TIMES;

	/* Manage heater using simple ON/OFF logic, no PID */
	if (current_temp[EXTRUDER_0] < target_temp[EXTRUDER_0])
	{
		extruder_heater_on();
	}
	else
	{
		extruder_heater_off();
	}


	/* Read and average the ADC input signal */
	current_temp[HEATED_BED_0] = 0;
	for (uint8_t c = 0; c < ADC_READ_TIMES; c++)
	{
		/* Read HEATED_BED_0 temperature value and manage heater */
		current_temp[HEATED_BED_0] += read_temp(HEATED_BED_0);
	}
	current_temp[HEATED_BED_0] = current_temp[HEATED_BED_0] / ADC_READ_TIMES;

	/* Manage heater using simple ON/OFF logic, no PID */
	if (current_temp[HEATED_BED_0] < target_temp[HEATED_BED_0])
	{
		heated_bed_on();
	}
	else
	{
		heated_bed_off();
	}
}
Ejemplo n.º 4
0
/*
 ***************************************************************************
 * Read temperature statistics.
 *
 * IN:
 * @a  Activity structure.
 *
 * OUT:
 * @a  Activity structure with statistics.
 ***************************************************************************
 */
__read_funct_t wrap_read_temp(struct activity *a)
{
	struct stats_pwr_temp *st_pwr_temp
		= (struct stats_pwr_temp *) a->_buf0;

	/* Read temperature stats */
	read_temp(st_pwr_temp, a->nr);

	return;
}
Ejemplo n.º 5
0
int main(void)
{
   int rectimetemp, adc2, adc3, bat, i, accx, accy, accz;
   float temp=0;
   accx=accy=accz=adc2=bat=adc3=0;
   uint8_t receivedflag, j=0;
   Broentech_Package_Header header;


   	initALL();   // Initialize all the peripherals
	/* Check I2C device and tell me who is connected and who is not
	 for (j=0; j<255; j+=2){
		for (i=0; i<1000; i++);   //Delay
		 if(I2C_check_dev(I2C2, j)==SUCCESS)
			 printf("device %d ok \n\r", j);
	 }

	 for (i=0; i<100000; i++);   //Delay
	 printf("Initialization Status: Ok, Devices checked! \n\r");	//Send string over USART1
	 for (i=0; i<1000000; i++);   //Delay
	 */


   while(1)
   {
	//wakeup_RF();		// Wake up the RF chip

	/*read ADC values*/
	bat=(3*readADCbat());  //read battery value
	temp=read_temp();
	// adc3=readADC3(); //read ADC3 Sensors
	// adc2=readADC2(); //read ADC2 Sensor
	sendDatagramfloat(temp, 0.0, 0.0);
	//  sendDatagramint(adc2,0,0);
	//  sendDatagramint(adc3,0,0);

	//build header and sent datagram for bat
	memcpy(&header.sensID,		"BATT",4);
	memcpy(&header.dataType,	"AINT",4);
	memcpy(&header.dataID,		"B---",4);
	sendDatagramint(header,bat,0,0);


	temp=0.0;
	bat=adc3=adc2=0;
	//go to sleep
	//sleep_RF();
	PWR_EnterSleepMode(PWR_Regulator_LowPower, PWR_SLEEPEntry_WFI);

   }
}
void AP_BattMonitor_SMBus_Maxell::timer()
{
	// check if PEC is supported
    if (!check_pec_support()) {
        return;
    }

    uint16_t data;
    uint32_t tnow = AP_HAL::micros();

    // read voltage (V)
    if (read_word(BATTMONITOR_SMBUS_MAXELL_VOLTAGE, data)) {
        _state.voltage = (float)data / 1000.0f;
        _state.last_time_micros = tnow;
        _state.healthy = true;
    }

    // read cell voltages
    for (uint8_t i = 0; i < BATTMONITOR_SMBUS_MAXELL_NUM_CELLS; i++) {
        if (read_word(maxell_cell_ids[i], data)) {
            _has_cell_voltages = true;
            _state.cell_voltages.cells[i] = data;
        } else {
            _state.cell_voltages.cells[i] = UINT16_MAX;
        }
    }

    // timeout after 5 seconds
    if ((tnow - _state.last_time_micros) > AP_BATTMONITOR_SMBUS_TIMEOUT_MICROS) {
        _state.healthy = false;
        return;
    }

    // read current (A)
    if (read_word(BATTMONITOR_SMBUS_MAXELL_CURRENT, data)) {
        _state.current_amps = -(float)((int16_t)data) / 1000.0f;
        _state.last_time_micros = tnow;
    }

    read_full_charge_capacity();

    // FIXME: Preform current integration if the remaining capacity can't be requested
    read_remaining_capacity();

    read_temp();

    read_serial_number();
}
Ejemplo n.º 7
0
static
uint8_t ds18b20_request(uint8_t channel_id, uint8_t address, uint8_t* reply)
{
	(void)channel_id;

	if (address != HA_CAN_Node_DS18B20_Temperature)
		return NO_RESPONSE;

	if (!read_temp(reply))
	{
		reply[0] = 0xff;
		reply[1] = 0xff;
	}

	return 2;
}
Ejemplo n.º 8
0
void scroll_test(){
loop_count = 0;
loop_average = 0;
while (loop_count < 10)
{
read_current();
read_temp();
printf("mV===>(%4.3f)",current_float);
loop_average = loop_average + current_float;
printf("    deg C===>(%3.2f)",temp_float);
printf("    deg F===>(%3.2f)\n\r",temp_float_faren);
loop_count ++;
delay_ms(1000);
}
loop_average = loop_average / 10;
printf("mV average===>(%4.3f)\n\r",loop_average);

}
Ejemplo n.º 9
0
int main(int argc, char *argv[]){
    if (argc == 6 && atoi(argv[5]) == 1){
        daemon(0, 0);
    }

    if (wiringPiSetup() == -1) 
        exit(1);

    if(argc < 5){
        printf("usage: %s <DHT11 pin#> <temp high pin#> <trigger pin#> <temp high limit> [bool daemonize]\n", argv[0]);
        exit(0);
    }

    int dhtPin = atoi(argv[1]);
    int tempErrPin = atoi(argv[2]); 
    int triggerPin = atoi(argv[3]);
    float tempErrLimit = atof(argv[4]); 

    signal(SIGINT, intHandler);

    while (keepRunning){
        float fTemp = read_temp(dhtPin, tempErrPin, triggerPin, tempErrLimit);

        if (fTemp > tempErrLimit){
            time_t current_time;
            char* c_time_string;
            current_time = time(NULL);
            c_time_string = ctime(&current_time);

            printf("temp error: %.1f > %.1f at %s", fTemp, tempErrLimit, c_time_string);
        }
        delay(3000);
    }

    cleanup(dhtPin, tempErrPin, triggerPin);    

    return(0);
}
Ejemplo n.º 10
0
main() {

   init_temp();
   lcd_init();
   delay_ms(6);
   
   reset_temp();

   while(TRUE)
   {
      current_temp = read_temp();

      if(input(RESET_BUTTON)==0)
         reset_temp();
      else if(current_temp>max_temp)
         max_temp=current_temp;
      else if(current_temp<min_temp)
         min_temp=current_temp;

      printf(lcd_putc,"\fCurrent Temp: %U F\nMin: %U Max: %U",                        current_temp,min_temp,max_temp);
      delay_ms(500);
   }
}
Ejemplo n.º 11
0
/**
 * @brief User entry point in driver/simulator ioctl routine.
 *
 * @param proceed -- if standard code execution should be proceed
 * @param sptr    -- statics table pointer
 * @param f       -- file pointer. Lynx/Linux specific.
 *                   See (sys/file.h) for Lynx and (linux/fs.h) for Linux.
 * @param lun     -- minor number (LUN)
 * @param com     -- ioctl number
 * @param arg     -- ioctl arguments
 *
 * It's up to user to set kernel-level errno (by means of @e pseterr call).
 * @e proceed parameter denotes if further standard actions should be proceed
 * after function returns. @b FALSE - means that user-desired operation done
 * all that user wants and there is no further necessaty to perfom any standard
 * operations that follow function call. @b TRUE - means that code that follows
 * function call will be executed.
 *
 * @return return value is the same as in entry point function\n
 *         OK     - if succeed.\n
 *         SYSERR - in case of failure.
 */
int CvorbUserIoctl(int *proceed, register CVORBStatics_t *sptr,
			 struct file *f, int lun, int com, char *arg)
{
	CVORBUserStatics_t *usp = sptr->usrst; /* user statistics table */
	ushort edp[3]; /* [select/get function] ioctl parameters
			  [0] -- module idx
			  [1] -- chan idx
			  [2] -- func idx */
	*proceed = FALSE;

	switch (com) {
	case CVORB_VHDL:
		return read_vhdl(usp, arg);
	case CVORB_PCB:
		return read_pcb(usp, arg);
	case CVORB_TEMP:
		return read_temp(usp, arg);
	case CVORB_MOD_CFG_RD:
		return read_mod_config_reg(usp, arg);
	case CVORB_MOD_CFG_WR:
		return write_mod_config_reg(usp, arg);
	case CVORB_CH_CFG_RD:
		return read_ch_config_reg(usp, arg);
	case CVORB_CH_CFG_WR:
		return write_ch_config_reg(usp, arg);
	case CVORB_MOD_STAT:
		return read_mod_stat(usp, arg);
	case CVORB_CH_STAT:
		return read_ch_stat(usp, arg);
	case CVORB_LOAD_SRAM:
		return load_sram(usp, arg);
	case CVORB_READ_SRAM:
		return read_sram(usp, arg);
	case CVORB_FEN: /* enable function in the function bitmask */
		return enable_function(usp, arg);
	case CVORB_FDIS: /* disable function in the function bitmask */
		return disable_function(usp, arg);
	case CVORB_FEN_RD: /* read Funciton Enable Mask */
		{
			uint m[2]; /* [0] -- bits[63-32]
				      [1] -- bits[31-0] */

			/* ioctl parameters */
			struct {
				ushort m; /* module idx */
				ushort c; /* channel idx */
				uint  *p; /* results goes here */
			} par;

			if (cdcm_copy_from_user(&par, arg, sizeof(par)))
				return SYSERR;

			m[0] = _rcr(par.m, par.c, FCT_EM_H);
			m[1] = _rcr(par.m, par.c, FCT_EM_L);
			return cdcm_copy_to_user(par.p, m, sizeof(m));
		}
	case CVORB_FEN_WR: /* write Function Enable Mask */
		return write_fem_regs(usp, arg);
	case CVORB_FUNC_SEL: /* select function to be played */
		if (cdcm_copy_from_user(&edp, arg, sizeof(edp)))
			return SYSERR;
		_wcr(edp[0], edp[1], FUNC_SEL, edp[2]);
		/* Should wait on Channel Status register bit[9] -- function
		   copy in progress, when data is copying into local SRAM. */
		while(_rcr(edp[0], edp[1], CH_STAT) & 1<<9)
			usec_sleep(1);
		return OK;
	case CVORB_FUNC_GET:	/* get currently selected function */
		if (cdcm_copy_from_user(&edp, arg, sizeof(edp)))
			return SYSERR;
		return _rcr(edp[0], edp[1], FUNC_SEL);
	case CVORB_WRSWP: /* action register.
			     Simulate front panel pulse inputs */
		return write_swp(usp, arg);
	case CVORB_RC_RD:
		return read_recurrent_cycles_reg(usp, arg);
	case CVORB_RC_WR:
		return write_recurrent_cycles_reg(usp, arg);
	case CVORB_DAC_ON:
		return dac_on(usp, arg);
	case CVORB_DAC_OFF:
		/* disable on-board clock generator */
		_wr(0, CLK_GEN_CNTL, AD9516_OFF);
		return OK;
	case AD9516_GET_PLL:
		return get_pll(usp, arg);
	case CVORB_WR_SAR:
		return write_sar(usp, arg);
	default:
		*proceed = TRUE; /* continue standard code execution */
	}

	return OK;
}
Ejemplo n.º 12
0
void main(void)
{
	unsigned char n, m, delta;
	signed int th, change = 0, eis5temp;
	signed char korrektur;
	unsigned int exponent, eis5lux, rest;

	// start watchdog 2,6 sec
	WDL = 0xFF;
	EA = 0;
	WDCON = 0xE5;
	WFEED1 = 0xA5;
	WFEED2 = 0x5A;
	EA = 1;

	restart_hw();				// Hardware zuruecksetzen

	for (n = 0; n < 50; n++)
	{		// Warten bis Bus stabil, nach Busspannungswiederkehr
		TR0 = 0;					// Timer 0 anhalten
		TH0 = eeprom[ADDRTAB + 1];// Timer 0 setzen mit phys. Adr. damit Geräte unterschiedlich beginnen zu senden
		TL0 = eeprom[ADDRTAB + 2];
		TF0 = 0;					// Überlauf-Flag zurücksetzen
		TR0 = 1;					// Timer 0 starten
		while (!TF0)
			;
	}
	restart_app();			// Anwendungsspezifische Einstellungen zuruecksetzen

	// feed watchdog
	EA = 0;
	WFEED1 = 0xA5;
	WFEED2 = 0x5A;
	EA = 1;

	do
	{
		if (eeprom[0x0D] == 0xFF && fb_state == 0 && !connected)
		{	// Nur wenn im run-mode und statemachine idle

			ET1 = 0;									// statemachine stoppen
			switch (sequence)
			{
			case 1:
				if ((timer & 0x3F) == 0x30)
				{	// nur alle 10 Sekunden wandeln
					interrupted = 0;
					start_tempconversion();				// Konvertierung starten
					if (!interrupted)
						sequence = 2;
				}
				ET1 = 1;						// statemachine starten
				break;
			case 2:
				if ((timer & 0x07) == 0x07)
				{	// nur ein mal pro Sekunde pollen
					interrupted = 0;
					if (ow_read_bit() && !interrupted)
						sequence = 3;	// Konvertierung abgeschlossen
				}
				ET1 = 1;						// statemachine starten
				break;
			case 3:
				interrupted = 0;
#ifdef DEBUG
				th = ti;
				ti+=100;
				if (ti>2800) ti=2000;
#else
				th = read_temp();					// Temperatur einlesen
#endif
				ET1 = 1;						// statemachine starten
				korrektur = (signed char) eeprom[TEMPCORR];	// Parameter Korrekturwert Temperatur
				for (n = 0; n < 10; n++)
					th += korrektur;

				if (!interrupted)
				{
					temp = th;
					if (temp != lasttemp)
					{
						eis5temp = (temp >> 3) & 0x07FF;// durch 8 teilen, da später Exponent 3 dazukommt
						eis5temp = eis5temp + (0x18 << 8);
						if (temp < 0)
							eis5temp += 0x8000;	// Vorzeichen
						write_obj_value(1, eis5temp);

						schwelle(6); // Temperaturschwellen prüfen und ggf. reagieren
						schwelle(7);
					}
					sequence = 4;
				}
				break;
			case 4:				// Helligkeitswert konvertieren
				interrupted = 0;
				Get_ADC(3);		// ADC-Wert holen
				ET1 = 1;			// statemachine starten
				if (!interrupted)
				{
					n = 0;
					if (HighByte >= 112)
					{
						lux = 65535;
					}
					else
					{
						/*
						 while (HighByte >= logtable[n]) n++;

						 if (n>1) {
						 lux=8;
						 lux=lux<<(n-1);	// unterer Wert
						 }

						 else lux=0;
						 */
						lux = 2;
						while (HighByte >= logtable[n])
						{
							n++;
							lux = lux * 2;
						}
						if (n <= 1)
							lux = 0;

						rest = HighByte - logtable[n - 1];
						delta = logtable[n] - logtable[n - 1];

						/*
						 if (n<11) lux+=_divuint(rest<<(n+2),delta);
						 else lux+=_divuint(rest<<(n-2),delta)<<4;
						 */
						if (n < 11)
							m = n + 2;
						else
							m = n - 2;
						rest = rest << m;
						rest = _divuint(rest, delta);
						if (n < 11)
							lux += rest;
						else
							lux += rest << 4;

						if (n < 7)
							lux += (_divuint(LowByte << (n + 2), delta) >> 8);

					}
					if (lux != lastlux)
					{
						exponent = 0x3800;	// Exponent 7

						eis5lux = lux >> 1;
						eis5lux += lux >> 2;
						eis5lux += lux >> 5;

						while (eis5lux > 0x07FF)
						{	// Exponent erhöhen falls Mantisse zu groß
							eis5lux = eis5lux >> 1;
							exponent += 0x0800;
						}
						eis5lux += exponent;

						write_obj_value(0, eis5lux);// Lux Wert im userram speichern
						schwelle(4);             // Helligkeitsschwellen 2 und 3
						schwelle(5);
					}
Ejemplo n.º 13
0
/* 
 * main function - reads instantaneous power values (in W) from a trace
 * file (e.g. "gcc.ptrace") and outputs instantaneous temperature values (in C) to
 * a trace file("gcc.ttrace"). also outputs steady state temperature values
 * (including those of the internal nodes of the model) onto stdout. the
 * trace files are 2-d matrices with each column representing a functional
 * functional block and each row representing a time unit(sampling_intvl).
 * columns are tab-separated and each row is a separate line. the first
 * line contains the names of the functional blocks. the order in which
 * the columns are specified doesn't have to match that of the floorplan 
 * file.
 */
int main(int argc, char **argv)
{
	int i, j, idx, base = 0, count = 0, n = 0;
	int num, size, lines = 0, do_transient = TRUE;
	char **names;
	double *vals;
	/* trace file pointers	*/
	FILE *pin, *tout = NULL;
	/* floorplan	*/
	flp_t *flp;
	/* hotspot temperature model	*/
	RC_model_t *model;
	/* instantaneous temperature and power values	*/
	double *temp = NULL, *power;
	double total_power = 0.0;
	
	/* steady state temperature and power values	*/
	double *overall_power, *steady_temp;
	/* thermal model configuration parameters	*/
	thermal_config_t thermal_config;
	/* global configuration parameters	*/
	global_config_t global_config;
	/* table to hold options and configuration */
	str_pair table[MAX_ENTRIES];
	
	/* variables for natural convection iterations */
	int natural = 0; 
	double avg_sink_temp = 0;
	int natural_convergence = 0;
	double r_convec_old;

	if (!(argc >= 5 && argc % 2)) {
		usage(argc, argv);
		return 1;
	}
	
	size = parse_cmdline(table, MAX_ENTRIES, argc, argv);
	global_config_from_strs(&global_config, table, size);

	/* no transient simulation, only steady state	*/
	if(!strcmp(global_config.t_outfile, NULLFILE))
		do_transient = FALSE;

	/* read configuration file	*/
	if (strcmp(global_config.config, NULLFILE))
		size += read_str_pairs(&table[size], MAX_ENTRIES, global_config.config);

	/* 
	 * earlier entries override later ones. so, command line options 
	 * have priority over config file 
	 */
	size = str_pairs_remove_duplicates(table, size);

	/* get defaults */
	thermal_config = default_thermal_config();
	/* modify according to command line / config file	*/
	thermal_config_add_from_strs(&thermal_config, table, size);
	
	/* if package model is used, run package model */
	if (((idx = get_str_index(table, size, "package_model_used")) >= 0) && !(table[idx].value==0)) {
		if (thermal_config.package_model_used) {
			avg_sink_temp = thermal_config.ambient + SMALL_FOR_CONVEC;
			natural = package_model(&thermal_config, table, size, avg_sink_temp);
			if (thermal_config.r_convec<R_CONVEC_LOW || thermal_config.r_convec>R_CONVEC_HIGH)
				printf("Warning: Heatsink convection resistance is not realistic, double-check your package settings...\n"); 
		}
	}

	/* dump configuration if specified	*/
	if (strcmp(global_config.dump_config, NULLFILE)) {
		size = global_config_to_strs(&global_config, table, MAX_ENTRIES);
		size += thermal_config_to_strs(&thermal_config, &table[size], MAX_ENTRIES-size);
		/* prefix the name of the variable with a '-'	*/
		dump_str_pairs(table, size, global_config.dump_config, "-");
	}

	/* initialization: the flp_file global configuration 
	 * parameter is overridden by the layer configuration 
	 * file in the grid model when the latter is specified.
	 */
	flp = read_flp(global_config.flp_file, FALSE);

	/* allocate and initialize the RC model	*/
	model = alloc_RC_model(&thermal_config, flp);
	populate_R_model(model, flp);
	
	if (do_transient)
		populate_C_model(model, flp);

	#if VERBOSE > 2
	debug_print_model(model);
	#endif

	/* allocate the temp and power arrays	*/
	/* using hotspot_vector to internally allocate any extra nodes needed	*/
	if (do_transient)
		temp = hotspot_vector(model);
	power = hotspot_vector(model);
	steady_temp = hotspot_vector(model);
	overall_power = hotspot_vector(model);
	
	/* set up initial instantaneous temperatures */
	if (do_transient && strcmp(model->config->init_file, NULLFILE)) {
		if (!model->config->dtm_used)	/* initial T = steady T for no DTM	*/
			read_temp(model, temp, model->config->init_file, FALSE);
		else	/* initial T = clipped steady T with DTM	*/
			read_temp(model, temp, model->config->init_file, TRUE);
	} else if (do_transient)	/* no input file - use init_temp as the common temperature	*/
		set_temp(model, temp, model->config->init_temp);

	/* n is the number of functional blocks in the block model
	 * while it is the sum total of the number of functional blocks
	 * of all the floorplans in the power dissipating layers of the 
	 * grid model. 
	 */
	if (model->type == BLOCK_MODEL)
		n = model->block->flp->n_units;
	else if (model->type == GRID_MODEL) {
		for(i=0; i < model->grid->n_layers; i++)
			if (model->grid->layers[i].has_power)
				n += model->grid->layers[i].flp->n_units;
	} else 
		fatal("unknown model type\n");

	if(!(pin = fopen(global_config.p_infile, "r")))
		fatal("unable to open power trace input file\n");
	if(do_transient && !(tout = fopen(global_config.t_outfile, "w")))
		fatal("unable to open temperature trace file for output\n");

	/* names of functional units	*/
	names = alloc_names(MAX_UNITS, STR_SIZE);
	if(read_names(pin, names) != n)
		fatal("no. of units in floorplan and trace file differ\n");

	/* header line of temperature trace	*/
	if (do_transient)
		write_names(tout, names, n);

	/* read the instantaneous power trace	*/
	vals = dvector(MAX_UNITS);
	while ((num=read_vals(pin, vals)) != 0) {
		if(num != n)
			fatal("invalid trace file format\n");

		/* permute the power numbers according to the floorplan order	*/
		if (model->type == BLOCK_MODEL)
			for(i=0; i < n; i++)
				power[get_blk_index(flp, names[i])] = vals[i];
		else
			for(i=0, base=0, count=0; i < model->grid->n_layers; i++) {
				if(model->grid->layers[i].has_power) {
					for(j=0; j < model->grid->layers[i].flp->n_units; j++) {
						idx = get_blk_index(model->grid->layers[i].flp, names[count+j]);
						power[base+idx] = vals[count+j];
					}
					count += model->grid->layers[i].flp->n_units;
				}	
				base += model->grid->layers[i].flp->n_units;	
			}

		/* compute temperature	*/
		if (do_transient) {
			/* if natural convection is considered, update transient convection resistance first */
			if (natural) {
				avg_sink_temp = calc_sink_temp(model, temp);
				natural = package_model(model->config, table, size, avg_sink_temp);
				populate_R_model(model, flp);
			}
			/* for the grid model, only the first call to compute_temp
			 * passes a non-null 'temp' array. if 'temp' is  NULL, 
			 * compute_temp remembers it from the last non-null call. 
			 * this is used to maintain the internal grid temperatures 
			 * across multiple calls of compute_temp
			 */
			if (model->type == BLOCK_MODEL || lines == 0)
				compute_temp(model, power, temp, model->config->sampling_intvl);
			else
				compute_temp(model, power, NULL, model->config->sampling_intvl);
	
			/* permute back to the trace file order	*/
			if (model->type == BLOCK_MODEL)
				for(i=0; i < n; i++)
					vals[i] = temp[get_blk_index(flp, names[i])];
			else
				for(i=0, base=0, count=0; i < model->grid->n_layers; i++) {
					if(model->grid->layers[i].has_power) {
						for(j=0; j < model->grid->layers[i].flp->n_units; j++) {
							idx = get_blk_index(model->grid->layers[i].flp, names[count+j]);
							vals[count+j] = temp[base+idx];
						}
						count += model->grid->layers[i].flp->n_units;	
					}	
					base += model->grid->layers[i].flp->n_units;	
				}
		
			/* output instantaneous temperature trace	*/
			write_vals(tout, vals, n);
		}		
	
		/* for computing average	*/
		if (model->type == BLOCK_MODEL)
			for(i=0; i < n; i++)
				overall_power[i] += power[i];
		else
			for(i=0, base=0; i < model->grid->n_layers; i++) {
				if(model->grid->layers[i].has_power)
					for(j=0; j < model->grid->layers[i].flp->n_units; j++)
						overall_power[base+j] += power[base+j];
				base += model->grid->layers[i].flp->n_units;	
			}

		lines++;
	}    
	if(!lines)
		fatal("no power numbers in trace file\n");
		
	/* for computing average	*/
	if (model->type == BLOCK_MODEL)
		for(i=0; i < n; i++) {
			overall_power[i] /= lines;
			//overall_power[i] /=150; //reduce input power for natural convection
			total_power += overall_power[i];
		}
	else
		for(i=0, base=0; i < model->grid->n_layers; i++) {
			if(model->grid->layers[i].has_power)
				for(j=0; j < model->grid->layers[i].flp->n_units; j++) {
					overall_power[base+j] /= lines;
					total_power += overall_power[base+j];
				}
			base += model->grid->layers[i].flp->n_units;	
		}
		
	/* natural convection r_convec iteration, for steady-state only */ 		
	natural_convergence = 0;
	if (natural) { /* natural convection is used */
		while (!natural_convergence) {
			r_convec_old = model->config->r_convec;
			/* steady state temperature	*/
			steady_state_temp(model, overall_power, steady_temp);
			avg_sink_temp = calc_sink_temp(model, steady_temp) + SMALL_FOR_CONVEC;
			natural = package_model(model->config, table, size, avg_sink_temp);
			populate_R_model(model, flp);
			if (avg_sink_temp > MAX_SINK_TEMP)
				fatal("too high power for a natural convection package -- possible thermal runaway\n");
			if (fabs(model->config->r_convec-r_convec_old)<NATURAL_CONVEC_TOL) 
				natural_convergence = 1;
		}
	}	else /* natural convection is not used, no need for iterations */
	/* steady state temperature	*/
	steady_state_temp(model, overall_power, steady_temp);

	/* print steady state results	*/
	fprintf(stdout, "Unit\tSteady(Kelvin)\n");
	dump_temp(model, steady_temp, "stdout");

	/* dump steady state temperatures on to file if needed	*/
	if (strcmp(model->config->steady_file, NULLFILE))
		dump_temp(model, steady_temp, model->config->steady_file);

	/* for the grid model, optionally dump the most recent 
	 * steady state temperatures of the grid cells	
	 */
	if (model->type == GRID_MODEL &&
		strcmp(model->config->grid_steady_file, NULLFILE))
		dump_steady_temp_grid(model->grid, model->config->grid_steady_file);

	#if VERBOSE > 2
	if (model->type == BLOCK_MODEL) {
		if (do_transient) {
			fprintf(stdout, "printing temp...\n");
			dump_dvector(temp, model->block->n_nodes);
		}
		fprintf(stdout, "printing steady_temp...\n");
		dump_dvector(steady_temp, model->block->n_nodes);
	} else {
		if (do_transient) {
			fprintf(stdout, "printing temp...\n");
			dump_dvector(temp, model->grid->total_n_blocks + EXTRA);
		}
		fprintf(stdout, "printing steady_temp...\n");
		dump_dvector(steady_temp, model->grid->total_n_blocks + EXTRA);
	}
	#endif

	/* cleanup	*/
	fclose(pin);
	if (do_transient)
		fclose(tout);
	delete_RC_model(model);
	free_flp(flp, FALSE);
	if (do_transient)
		free_dvector(temp);
	free_dvector(power);
	free_dvector(steady_temp);
	free_dvector(overall_power);
	free_names(names);
	free_dvector(vals);

	return 0;
}
Ejemplo n.º 14
0
void main(){
   setup_oscillator( OSC_8MHZ );
   setup_adc_ports(sAN6|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

//Software workaround for the power switch floating
onewire_init();
onewire_sendbyte(0xCC);
onewire_sendbyte(0x6C);    //Write Data Command
onewire_sendbyte(0x31);    //Eeprom address but actually gets written to Shadow Ram
onewire_sendbyte(0xE7);    //Value to make PMOD1 SWEN=0 RNAOP=0

//Copy the shadow Ram written above over to actual EEPROM
onewire_init();
onewire_sendbyte(0xCC);
onewire_sendbyte(0x48);    //send the copy command
onewire_sendbyte(0x30);    //copy shadow ram to the block containing 31

while(true){
/*-------------------------------------------------------------------
Pull Reading From Temp Probe
-------------------------------------------------------------------*/
//Use the following to determine the state of the one wire net
//Will report if device present, not, or shorted
//Comment out rest of code
//onewire_init_with_error_check();
//read_status();
//printf("status byte is ====>(%x)\n\r",status);
printf("Please enter a command (h for help):\n\r");

command = getc();  //Gets a key from the keyboard
   switch (command){
   case 'h' :
         printf("Type any of the following commands:\n\r");
         printf("h     This Help Message\n\r");
         printf("C     Ambiant Temp in deg. C\n\r");
         printf("c     Ambiant Temp in deg. C(No Formatting)\n\r");
         printf("F     Ambiant Temp in deg. F\n\r");
         printf("f     Ambiant Temp in deg. F(No Formatting)\n\r");
         printf("N     64 bit node address in Hex\n\r");
         printf("K     Thermo millivolts\n\r");
         printf("k     Thermo millivolts(No Formatting)\n\r");
         printf("s     One line scroll test\n\r");
         break;
   case 'C' :
         read_temp();
         printf("    deg C===>(%3.2f)\n\r",temp_float);
         break;
   case 'c' :
         read_temp();
         printf("%3.2f\n\r",temp_float);
         break;
   case 'F' :
         read_temp();
         printf("    deg F===>(%3.2f)\n\r",temp_float_faren);
         break;
   case 'f' :
         read_temp();
         printf("%4.2f",temp_float_faren);
         break;
   case 'K' :
         read_current();
         printf("mV===>(%4.3f)\n\r",current_float);
         break;
   case 'k' :
         read_current();
         printf("%4.3f\n\r",current_float);
         break;
   case 's' :
         scroll_test();
         break;
   default :
         printf("Not a valid command:\n\r");
         }
         
delay_ms(1000);
}
}
Ejemplo n.º 15
0
int main(int argc, char **argv) {
  /* Sensor values */

#if defined(CONFIG_WEDGE)
  int intake_temp;
  int exhaust_temp;
  int switch_temp;
  int userver_temp;
#else
  float intake_temp;
  float exhaust_temp;
  float userver_temp;
#endif

  int fan_speed = fan_high;
  int bad_reads = 0;
  int fan_failure = 0;
  int fan_speed_changes = 0;
  int old_speed;

  int fan_bad[FANS];
  int fan;

  unsigned log_count = 0; // How many times have we logged our temps?
  int opt;
  int prev_fans_bad = 0;

  struct sigaction sa;

  sa.sa_handler = fand_interrupt;
  sa.sa_flags = 0;
  sigemptyset(&sa.sa_mask);

  sigaction(SIGTERM, &sa, NULL);
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGUSR1, &sa, NULL);

  // Start writing to syslog as early as possible for diag purposes.

  openlog("fand", LOG_CONS, LOG_DAEMON);

#if defined(CONFIG_WEDGE) && !defined(CONFIG_WEDGE100)
  if (is_two_fan_board(false)) {
    /* Alternate, two fan configuration */
    total_fans = 2;
    fan_offset = 2; /* fan 3 is the first */

    fan_low = SIXPACK_FAN_LOW;
    fan_medium = SIXPACK_FAN_MEDIUM;
    fan_high = SIXPACK_FAN_HIGH;
    fan_max = SIXPACK_FAN_MAX;
    fan_speed = fan_high;
  }
#endif

  while ((opt = getopt(argc, argv, "l:m:h:b:t:r:v")) != -1) {
    switch (opt) {
    case 'l':
      fan_low = atoi(optarg);
      break;
    case 'm':
      fan_medium = atoi(optarg);
      break;
    case 'h':
      fan_high = atoi(optarg);
      break;
    case 'b':
      temp_bottom = INTERNAL_TEMPS(atoi(optarg));
      break;
    case 't':
      temp_top = INTERNAL_TEMPS(atoi(optarg));
      break;
    case 'r':
      report_temp = atoi(optarg);
      break;
    case 'v':
      verbose = true;
      break;
    default:
      usage();
      break;
    }
  }

  if (optind > argc) {
    usage();
  }

  if (temp_bottom > temp_top) {
    fprintf(stderr,
            "Should temp-bottom (%d) be higher than "
            "temp-top (%d)?  Starting anyway.\n",
            EXTERNAL_TEMPS(temp_bottom),
            EXTERNAL_TEMPS(temp_top));
  }

  if (fan_low > fan_medium || fan_low > fan_high || fan_medium > fan_high) {
    fprintf(stderr,
            "fan RPMs not strictly increasing "
            "-- %d, %d, %d, starting anyway\n",
            fan_low,
            fan_medium,
            fan_high);
  }

  daemon(1, 0);

  if (verbose) {
    syslog(LOG_DEBUG, "Starting up;  system should have %d fans.",
           total_fans);
  }

  for (fan = 0; fan < total_fans; fan++) {
    fan_bad[fan] = 0;
    write_fan_speed(fan + fan_offset, fan_speed);
    write_fan_led(fan + fan_offset, FAN_LED_BLUE);
  }

#if defined(CONFIG_YOSEMITE)
  /* Ensure that we can read from sensors before proceeding. */

  int found = 0;
  userver_temp = 100;
  while (!found) {
    for (int node = 1; node <= TOTAL_1S_SERVERS && !found; node++) {
      if (!yosemite_sensor_read(node, BIC_SENSOR_SOC_THERM_MARGIN,
                               &userver_temp) &&
          userver_temp < 0) {
        syslog(LOG_DEBUG, "SOC_THERM_MARGIN first valid read of %f.",
               userver_temp);
        found = 1;
      }
      sleep(5);
    }
    // XXX:  Will it ever be a problem that we don't exit this until
    //       we see a valid value?
  }
#endif

  /* Start watchdog in manual mode */
  start_watchdog(0);

  /* Set watchdog to persistent mode so timer expiry will happen independent
   * of this process's liveliness. */
  set_persistent_watchdog(WATCHDOG_SET_PERSISTENT);

  sleep(5);  /* Give the fans time to come up to speed */

  while (1) {
    int max_temp;
    old_speed = fan_speed;

    /* Read sensors */

#if defined(CONFIG_WEDGE) || defined(CONFIG_WEDGE100)
    read_temp(INTAKE_TEMP_DEVICE, &intake_temp);
    read_temp(EXHAUST_TEMP_DEVICE, &exhaust_temp);
    read_temp(CHIP_TEMP_DEVICE, &switch_temp);
    read_temp(USERVER_TEMP_DEVICE, &userver_temp);

    /*
     * uServer can be powered down, but all of the rest of the sensors
     * should be readable at any time.
     */

    if ((intake_temp == BAD_TEMP || exhaust_temp == BAD_TEMP ||
         switch_temp == BAD_TEMP)) {
      bad_reads++;
    }
#else
    intake_temp = exhaust_temp = userver_temp = BAD_TEMP;
    if (yosemite_sensor_read(FRU_SPB, SP_SENSOR_INLET_TEMP, &intake_temp) ||
        yosemite_sensor_read(FRU_SPB, SP_SENSOR_OUTLET_TEMP, &exhaust_temp))
      bad_reads++;

    /*
     * There are a number of 1S servers;  any or all of them
     * could be powered off and returning no values.  Ignore these
     * invalid values.
     */
    for (int node = 1; node <= TOTAL_1S_SERVERS; node++) {
      float new_temp;
      if (!yosemite_sensor_read(node, BIC_SENSOR_SOC_THERM_MARGIN,
			        &new_temp)) {
        if (userver_temp < new_temp) {
          userver_temp = new_temp;
        }
      }

      // Since the yosemite_sensor_read() times out after 8secs, keep WDT from expiring
      kick_watchdog();
    }
#endif

    if (bad_reads > BAD_READ_THRESHOLD) {
      server_shutdown("Some sensors couldn't be read");
    }

    if (log_count++ % report_temp == 0) {
      syslog(LOG_DEBUG,
#if defined(CONFIG_WEDGE) || defined(CONFIG_WEDGE100)
             "Temp intake %d, t2 %d, "
             " userver %d, exhaust %d, "
             "fan speed %d, speed changes %d",
#else
             "Temp intake %f, max server %f, exhaust %f, "
             "fan speed %d, speed changes %d",
#endif
             intake_temp,
#if defined(CONFIG_WEDGE) || defined(CONFIG_WEDGE100)
             switch_temp,
#endif
             userver_temp,
             exhaust_temp,
             fan_speed,
             fan_speed_changes);
    }

    /* Protection heuristics */

    if (intake_temp > INTAKE_LIMIT) {
      server_shutdown("Intake temp limit reached");
    }

#if defined(CONFIG_WEDGE) || defined(CONFIG_WEDGE100)
    if (switch_temp > SWITCH_LIMIT) {
      server_shutdown("T2 temp limit reached");
    }
#endif

    if (userver_temp + USERVER_TEMP_FUDGE > USERVER_LIMIT) {
      server_shutdown("uServer temp limit reached");
    }

    /*
     * Calculate change needed -- we should eventually
     * do something more sophisticated, like PID.
     *
     * We should use the intake temperature to adjust this
     * as well.
     */

#if defined(CONFIG_YOSEMITE)
    /* Use tables to lookup the new fan speed for Yosemite. */

    int intake_speed = temp_to_fan_speed(intake_temp, intake_map,
                                         INTAKE_MAP_SIZE);
    int cpu_speed = temp_to_fan_speed(userver_temp, cpu_map, CPU_MAP_SIZE);

    if (fan_speed == fan_max && fan_failure != 0) {
      /* Don't change a thing */
    } else if (intake_speed > cpu_speed) {
      fan_speed = intake_speed;
    } else {
      fan_speed = cpu_speed;
    }
#else
    /* Other systems use a simpler built-in table to determine fan speed. */

    if (switch_temp > userver_temp + USERVER_TEMP_FUDGE) {
      max_temp = switch_temp;
    } else {
      max_temp = userver_temp + USERVER_TEMP_FUDGE;
    }

    /*
     * If recovering from a fan problem, spin down fans gradually in case
     * temperatures are still high. Gradual spin down also reduces wear on
     * the fans.
     */
    if (fan_speed == fan_max) {
      if (fan_failure == 0) {
        fan_speed = fan_high;
      }
    } else if (fan_speed == fan_high) {
      if (max_temp + COOLDOWN_SLOP < temp_top) {
        fan_speed = fan_medium;
      }
    } else if (fan_speed == fan_medium) {
      if (max_temp > temp_top) {
        fan_speed = fan_high;
      } else if (max_temp + COOLDOWN_SLOP < temp_bottom) {
        fan_speed = fan_low;
      }
    } else {/* low */
      if (max_temp > temp_bottom) {
        fan_speed = fan_medium;
      }
    }
#endif

    /*
     * Update fans only if there are no failed ones. If any fans failed
     * earlier, all remaining fans should continue to run at max speed.
     */

    if (fan_failure == 0 && fan_speed != old_speed) {
      syslog(LOG_NOTICE,
             "Fan speed changing from %d to %d",
             old_speed,
             fan_speed);
      fan_speed_changes++;
      for (fan = 0; fan < total_fans; fan++) {
        write_fan_speed(fan + fan_offset, fan_speed);
      }
    }

    /*
     * Wait for some change.  Typical I2C temperature sensors
     * only provide a new value every second and a half, so
     * checking again more quickly than that is a waste.
     *
     * We also have to wait for the fan changes to take effect
     * before measuring them.
     */

    sleep(5);

    /* Check fan RPMs */

    for (fan = 0; fan < total_fans; fan++) {
      /*
       * Make sure that we're within some percentage
       * of the requested speed.
       */
      if (fan_speed_okay(fan + fan_offset, fan_speed, FAN_FAILURE_OFFSET)) {
        if (fan_bad[fan] > FAN_FAILURE_THRESHOLD) {
          write_fan_led(fan + fan_offset, FAN_LED_BLUE);
          syslog(LOG_CRIT,
                 "Fan %d has recovered",
                 fan);
        }
        fan_bad[fan] = 0;
      } else {
        fan_bad[fan]++;
      }
    }

    fan_failure = 0;
    for (fan = 0; fan < total_fans; fan++) {
      if (fan_bad[fan] > FAN_FAILURE_THRESHOLD) {
        fan_failure++;
        write_fan_led(fan + fan_offset, FAN_LED_RED);
      }
    }

    if (fan_failure > 0) {
      if (prev_fans_bad != fan_failure) {
        syslog(LOG_CRIT, "%d fans failed", fan_failure);
      }

      /*
       * If fans are bad, we need to blast all of the
       * fans at 100%;  we don't bother to turn off
       * the bad fans, in case they are all that is left.
       *
       * Note that we have a temporary bug with setting fans to
       * 100% so we only do fan_max = 99%.
       */

      fan_speed = fan_max;
      for (fan = 0; fan < total_fans; fan++) {
        write_fan_speed(fan + fan_offset, fan_speed);
      }

#if defined(CONFIG_WEDGE) || defined(CONFIG_WEDGE100)
      /*
       * On Wedge, we want to shut down everything if none of the fans
       * are visible, since there isn't automatic protection to shut
       * off the server or switch chip.  On other platforms, the CPUs
       * generating the heat will automatically turn off, so this is
       * unnecessary.
       */

      if (fan_failure == total_fans) {
        int count = 0;
        for (fan = 0; fan < total_fans; fan++) {
          if (fan_bad[fan] > FAN_SHUTDOWN_THRESHOLD)
            count++;
        }
        if (count == total_fans) {
          server_shutdown("all fans are bad for more than 12 cycles");
        }
      }
#endif

      /*
       * Fans can be hot swapped and replaced; in which case the fan daemon
       * will automatically detect the new fan and (assuming the new fan isn't
       * itself faulty), automatically readjust the speeds for all fans down
       * to a more suitable rpm. The fan daemon does not need to be restarted.
       */
    }

    /* Suppress multiple warnings for similar number of fan failures. */
    prev_fans_bad = fan_failure;

    /* if everything is fine, restart the watchdog countdown. If this process
     * is terminated, the persistent watchdog setting will cause the system
     * to reboot after the watchdog timeout. */
    kick_watchdog();
  }
}
Ejemplo n.º 16
0
void reset_temp()  {

   current_temp = read_temp();
   min_temp=current_temp;
   max_temp=current_temp;
}
Ejemplo n.º 17
0
int main()
{
	uint8 i2c_flag = 0;
	uint16 tmp_volt_3v3 = 0, tmp_volt_vg = 0, tmp_volt_vb = 0;
	uint16 extend_error_pulse = 0;
	
	//Initialize and start peripherals:
    init_peripherals();
	
	// Enable global interrupts
	CyGlobalIntEnable;
	
	//PWM pass-through (ToDo program safety feature)
	PWM_Enable_Write(1);	
	
	//Disable shorted leads
	SL_EN_Write(0);
	
    while(1)
    {
		//For now we always generate the -4V5:
		SL_CLK_Write(1);
		CyDelayUs(5);
		SL_CLK_Write(0);
		
		//Update sensor data every 10ms, call safety functions:
		if(flag_tb10ms)
		{
			flag_tb10ms = 0;
			
			//Update shared memory:
			
			ezI2Cbuf[MEM_R_VB_SNS] = read_vb();			
			ezI2Cbuf[MEM_R_VG_SNS] = read_vg();		
			ezI2Cbuf[MEM_R_5V_SNS] = 0;					//	TBD
			ezI2Cbuf[MEM_R_3V3_SNS] = read_3v3();			
			ezI2Cbuf[MEM_R_TEMPERATURE] = read_temp();
			
			//ezI2Cbuf[MEM_R_STATUS1]  = STATUS1_GOOD;
			
			//Safety functions:
			
			err_temp = safety_temp(ezI2Cbuf[MEM_R_TEMPERATURE]);
			err_v_3v3 = safety_volt(ezI2Cbuf[MEM_R_3V3_SNS], M_3V3_LOW, M_3V3_HIGH);
			err_v_vg = safety_volt(ezI2Cbuf[MEM_R_VG_SNS], M_VG_LOW, M_VG_HIGH);
			err_v_vb = safety_volt(ezI2Cbuf[MEM_R_VB_SNS], M_VB_LOW, M_VB_HIGH);
			err_discon = safety_disconnection(ezI2Cbuf[MEM_R_VB_SNS]);
			ezI2Cbuf[MEM_R_STATUS1] = CMB_FLAGS_STATUS1(err_wdclk, err_discon, err_temp, err_v_vb, err_v_vg);
			ezI2Cbuf[MEM_R_STATUS2] = CMB_FLAGS_STATUS2(err_v_3v3);
		}
		
		if(flag_tb_1ms)
		{
			flag_tb_1ms = 0;
			
			if(flag_wdclk == 1)	//Watchdog clock expired
			{
				extend_error_pulse = 1000;
				led_period = LED_PERIOD_ERROR;
				flag_wdclk = 2;	//2 means "being processed"
				err_wdclk = 1;
			}
			else
			{
				led_period = LED_PERIOD_NORM;
			}
			
			if(extend_error_pulse >= 1)
			{
				extend_error_pulse--;
			}
			else
			{
				extend_error_pulse = 0;
				flag_wdclk = 0;
				err_wdclk = 0;
			}
		}
		
		//EZI2C Write complete
        if (0u != (I2C_1_EzI2CGetActivity() & I2C_1_EZI2C_STATUS_WRITE1))
        {
			//...
			i2c_flag = 1;
			i2c_flag = 0;			
        }
    }
}
Ejemplo n.º 18
0
double temp_get(uint8_t sensor_number)
{
  current_temp[sensor_number] = read_temp(sensor_number);
  return current_temp[sensor_number];
}
Ejemplo n.º 19
0
void temp_tick(void)
{
  double pid_error = 0;

  /* Read and average temperatures */
  current_temp[EXTRUDER_0] = read_temp(EXTRUDER_0);
#ifdef EXP_Board
  current_temp[HEATED_BED_0] = read_temp(HEATED_BED_0);

  extruderBlockTemp = current_temp[HEATED_BED_0];
  current_temp_r2c2 = read_R2C2_temp();
#endif

  pid_error = target_temp[EXTRUDER_0] - current_temp[EXTRUDER_0];

  pterm = config.kp * pid_error;
  iterm += (config.ki*pid_error);
  //pterm = kp * pid_error;
  //iterm += (ki*pid_error);

  if(iterm > PID_FUNTIONAL_RANGE){
      iterm = PID_FUNTIONAL_RANGE;
  }else if(iterm < 0){
      iterm = 0;
  }

  dterm_temp = pid_error - last_error;
  dterm = config.kd * dterm_temp;
  //dterm = kd * dterm_temp;

  output = pterm + iterm + dterm;
  //output *= 0.95937 + 0.00907*currenBWSpeed + 0.01662*extruderFanSpeed + 0.000009*currenBWSpeed*currenBWSpeed - 0.000035*currenBWSpeed*extruderFanSpeed - 0.000068*extruderFanSpeed*extruderFanSpeed;
  //output += output*config.kVent*extruderFanSpeed;
  //output += output*config.kBlower*currenBWSpeed;

  output = pterm + iterm + dterm;
/*
#ifdef EXP_Board
  double p00 = -0.02242;
  double p10 = -0.001512*extruderFanSpeed;
  double p01 = 0.01811*currenBWSpeed;
  double p20 = 0.0003169*extruderFanSpeed*extruderFanSpeed;
  double p11 = -0.00006381*extruderFanSpeed*currenBWSpeed;
  double p02 = -0.00008276*currenBWSpeed*currenBWSpeed;
  double p30 = -0.000002056*extruderFanSpeed*extruderFanSpeed*extruderFanSpeed;
  double p21 = -0.000000008015*currenBWSpeed*extruderFanSpeed*extruderFanSpeed;
  double p12 = 0.0000002986*extruderFanSpeed*currenBWSpeed*currenBWSpeed;

  double pxy = p00 + p10 + p01 + p20 + p11 + p02 + p30 + p21 + p12;


  output = output*(1 + pxy);
#endif
*/
  last_error = pid_error;

  if(output > 100) {
      output = 100;
  }else if(output<0 ) {
      output = 0;
  }

  if(target_temp[EXTRUDER_0] == 0){
      output = 0;
      pterm = 0;
      iterm = 0;
      dterm = 0;
      pid_error = 0;
      dterm_temp = 0;
  }

  pwm_set_duty_cycle(5, output);
  pwm_set_enable(5);
}
Ejemplo n.º 20
0
PROCESS_THREAD(shell_owtest_process, ev, data) {
	int err;

	PROCESS_BEGIN();

	// Attempt to acquire 1-Wire lock
	while (!ow_lock()) {
		PROCESS_PAUSE();
	}

	// Reset the bus
	err = ow_reset();
	if (err < 0) {
		shell_output_P(&owtest_command, PSTR("Bus reset failed.\n"));
		PROCESS_EXIT();
	}
	else if (err == 0) {
		shell_output_P(&owtest_command, PSTR("No presence detected.\n"));
		PROCESS_EXIT();
	}

	// Start the search
	err = ow_search_first(&search, 0);
	do {
		if (err < 0) {
			shell_output_P(&owtest_command, PSTR("Search error: %d\n"), err);
			PROCESS_EXIT();
		}
		else if (err == 0) {
			shell_output_P(&owtest_command, PSTR("No devices found.\n"));
			break;
		}

		// Print search result
		shell_output_P(&owtest_command,
			PSTR("Found: %02x.%02x%02x%02x%02x%02x%02x\n"),
			search.rom_no.family, // family code
			search.rom_no.id[0], search.rom_no.id[1], search.rom_no.id[2],
			search.rom_no.id[3], search.rom_no.id[4], search.rom_no.id[5]);

		// If it's a DS18B20, read it
		if (search.rom_no.family == 0x28) {
			shell_output_P(&owtest_command, PSTR("Reading temperature...\n"));
			PROCESS_PT_SPAWN(&ow_pt, read_temp(&ow_pt, &search.rom_no));
		}

		// If we found the last device on the bus, break out of the loop
		if (search.last_device_flag) {
			break;
		}

		// Find the next device on the bus
		err = ow_search_next(&search);
	} while (1);

	// Relinquish bus lock
	ow_unlock();

	shell_output_P(&owtest_command, PSTR("Search complete.\n"));

	PROCESS_END();
}
Ejemplo n.º 21
0
int main(void)
{
	DDRF = 0;
	char GPS[100];
	char dylos[10];
	char GPS_line = 0;
	char dylos_line = 0;
	char sd_buff[150];
	char buffer[30];
	int GPS_data[15];
	int dylos_data[2];
	int C3H8, CH4, CO, H2S, F;
	char new_GPS_data = 0;
	char new_dylos_data = 0;
	FRESULT fr;

	int ppm2[151];
	ppm2[0] =	10478;
	ppm2[1] =	9706;
	ppm2[2] =	9015;
	ppm2[3] =	8394;
	ppm2[4] =	7834;
	ppm2[5] =	7327;
	ppm2[6] =	6868;
	ppm2[7] =	6449;
	ppm2[8] =	6068;
	ppm2[9] =	5718;
	ppm2[10] =	5398;
	ppm2[11] =	5103;
	ppm2[12] =	4831;
	ppm2[13] =	4581;
	ppm2[14] =	4349;
	ppm2[15] =	4133;
	ppm2[16] =	3934;
	ppm2[17] =	3748;
	ppm2[18] =	3575;
	ppm2[19] =	3413;
	ppm2[20] =	3262;
	ppm2[21] =	3121;
	ppm2[22] =	2988;
	ppm2[23] =	2864;
	ppm2[24] =	2747;
	ppm2[25] =	2637;
	ppm2[26] =	2533;
	ppm2[27] =	2435;
	ppm2[28] =	2343;
	ppm2[29] =	2256;
	ppm2[30] =	2173;
	ppm2[31] =	2095;
	ppm2[32] =	2021;
	ppm2[33] =	1951;
	ppm2[34] =	1884;
	ppm2[35] =	1821;
	ppm2[36] =	1760;
	ppm2[37] =	1703;
	ppm2[38] =	1648;
	ppm2[39] =	1596;
	ppm2[40] =	1546;
	ppm2[41] =	1499;
	ppm2[42] =	1454;
	ppm2[43] =	1410;
	ppm2[44] =	1369;
	ppm2[45] =	1329;
	ppm2[46] =	1291;
	ppm2[47] =	1254;
	ppm2[48] =	1220;
	ppm2[49] =	1186;
	ppm2[50] =	1154;
	ppm2[51] =	1123;
	ppm2[52] =	1093;
	ppm2[53] =	1065;
	ppm2[54] =	1037;
	ppm2[55] =	1011;
	ppm2[56] =	985;
	ppm2[57] =	961;
	ppm2[58] =	937;
	ppm2[59] =	914;
	ppm2[60] =	892;
	ppm2[61] =	871;
	ppm2[62] =	851;
	ppm2[63] =	831;
	ppm2[64] =	812;
	ppm2[65] =	793;
	ppm2[66] =	775;
	ppm2[67] =	758;
	ppm2[68] =	741;
	ppm2[69] =	725;
	ppm2[70] =	710;
	ppm2[71] =	694;
	ppm2[72] =	680;
	ppm2[73] =	667;
	ppm2[74] =	652;
	ppm2[75] =	638;
	ppm2[76] =	625;
	ppm2[77] =	613;
	ppm2[78] =	601;
	ppm2[79] =	589;
	ppm2[80] =	577;
	ppm2[81] =	566;
	ppm2[82] =	555;
	ppm2[83] =	545;
	ppm2[84] =	534;
	ppm2[85] =	524;
	ppm2[86] =	515;
	ppm2[87] =	505;
	ppm2[88] =	496;
	ppm2[89] =	487;
	ppm2[90] =	478;
	ppm2[91] =	470;
	ppm2[92] =	461;
	ppm2[93] =	453;
	ppm2[94] =	446;
	ppm2[95] =	438;
	ppm2[96] =	430;
	ppm2[97] =	423;
	ppm2[98] =	416;
	ppm2[99] =	409;
	ppm2[100] =	402;
	ppm2[101] =	396;
	ppm2[102] =	389;
	ppm2[103] =	383;
	ppm2[104] =	377;
	ppm2[105] =	371;
	ppm2[106] =	365;
	ppm2[107] =	359;
	ppm2[108] =	354;
	ppm2[109] =	348;
	ppm2[110] =	343;
	ppm2[111] =	338;
	ppm2[112] =	333;
	ppm2[113] =	328;
	ppm2[114] =	323;
	ppm2[115] =	318;
	ppm2[116] =	313;
	ppm2[117] =	309;
	ppm2[118] =	304;
	ppm2[119] =	300;
	ppm2[120] =	296;
	ppm2[121] =	292;
	ppm2[122] =	287;
	ppm2[123] =	283;
	ppm2[124] =	279;
	ppm2[125] =	276;
	ppm2[126] =	272;
	ppm2[127] =	268;
	ppm2[128] =	265;
	ppm2[129] =	261;
	ppm2[130] =	257;
	ppm2[131] =	254;
	ppm2[132] =	251;
	ppm2[133] =	247;
	ppm2[134] =	244;
	ppm2[135] =	241;
	ppm2[136] =	238;
	ppm2[137] =	235;
	ppm2[138] =	232;
	ppm2[139] =	229;
	ppm2[140] =	226;
	ppm2[141] =	223;
	ppm2[142] =	220;
	ppm2[143] =	218;
	ppm2[144] =	215;
	ppm2[145] =	212;
	ppm2[146] =	210;
	ppm2[147] =	207;
	ppm2[148] =	205;
	ppm2[149] =	202;
	ppm2[150] =	200;

	int ppm5[151];
	ppm5[0] =	9867;
	ppm5[1] =	9291;
	ppm5[2] =	8760;
	ppm5[3] =	8271;
	ppm5[4] =	7819;
	ppm5[5] =	7400;
	ppm5[6] =	7012;
	ppm5[7] =	6652;
	ppm5[8] =	6317;
	ppm5[9] =	6005;
	ppm5[10] =	5714;
	ppm5[11] =	5443;
	ppm5[12] =	5189;
	ppm5[13] =	4951;
	ppm5[14] =	4728;
	ppm5[15] =	4518;
	ppm5[16] =	4322;
	ppm5[17] =	4137;
	ppm5[18] =	3963;
	ppm5[19] =	3799;
	ppm5[20] =	3644;
	ppm5[21] =	3498;
	ppm5[22] =	3360;
	ppm5[23] =	3229;
	ppm5[24] =	3106;
	ppm5[25] =	2988;
	ppm5[26] =	2772;
	ppm5[27] =	2672;
	ppm5[28] =	2577;
	ppm5[29] =	2486;
	ppm5[30] =	2400;
	ppm5[31] =	2318;
	ppm5[32] =	2240;
	ppm5[33] =	2165;
	ppm5[34] =	2094;
	ppm5[35] =	2026;
	ppm5[36] =	1961;
	ppm5[37] =	1899;
	ppm5[38] =	1839;
	ppm5[39] =	1783;
	ppm5[40] =	1728;
	ppm5[41] =	1676;
	ppm5[42] =	1626;
	ppm5[43] =	1578;
	ppm5[44] =	1532;
	ppm5[45] =	1488;
	ppm5[46] =	1446;
	ppm5[47] =	1405;
	ppm5[48] =	1366;
	ppm5[49] =	1328;
	ppm5[50] =	1292;
	ppm5[51] =	1257;
	ppm5[52] =	1223;
	ppm5[53] =	1191;
	ppm5[54] =	1160;
	ppm5[55] =	1130;
	ppm5[56] =	1101;
	ppm5[57] =	1073;
	ppm5[58] =	1046;
	ppm5[59] =	1020;
	ppm5[60] =	995;
	ppm5[61] =	971;
	ppm5[62] =	947;
	ppm5[63] =	924;
	ppm5[64] =	903;
	ppm5[65] =	881;
	ppm5[66] =	861;
	ppm5[67] =	841;
	ppm5[68] =	822;
	ppm5[69] =	803;
	ppm5[70] =	785;
	ppm5[71] =	768;
	ppm5[72] =	751;
	ppm5[73] =	734;
	ppm5[74] =	718;
	ppm5[75] =	703;
	ppm5[76] =	688;
	ppm5[77] =	673;
	ppm5[78] =	659;
	ppm5[79] =	645;
	ppm5[80] =	632;
	ppm5[81] =	619;
	ppm5[82] =	606;
	ppm5[83] =	594;
	ppm5[84] =	582;
	ppm5[85] =	571;
	ppm5[86] =	559;
	ppm5[87] =	549;
	ppm5[88] =	538;
	ppm5[89] =	528;
	ppm5[90] =	517;
	ppm5[91] =	508;
	ppm5[92] =	498;
	ppm5[93] =	489;
	ppm5[94] =	480;
	ppm5[95] =	471;
	ppm5[96] =	462;
	ppm5[97] =	454;
	ppm5[98] =	446;
	ppm5[99] =	438;
	ppm5[100] =	430;
	ppm5[101] =	422;
	ppm5[102] =	415;
	ppm5[103] =	408;
	ppm5[104] =	401;
	ppm5[105] =	394;
	ppm5[106] =	387;
	ppm5[107] =	380;
	ppm5[108] =	374;
	ppm5[109] =	368;
	ppm5[110] =	362;
	ppm5[111] =	356;
	ppm5[112] =	350;
	ppm5[113] =	344;
	ppm5[114] =	339;
	ppm5[115] =	333;
	ppm5[116] =	328;
	ppm5[117] =	323;
	ppm5[118] =	317;
	ppm5[119] =	312;
	ppm5[120] =	308;
	ppm5[121] =	303;
	ppm5[122] =	298;
	ppm5[123] =	294;
	ppm5[124] =	289;
	ppm5[125] =	285;
	ppm5[126] =	280;
	ppm5[127] =	276;
	ppm5[128] =	272;
	ppm5[129] =	268;
	ppm5[130] =	264;
	ppm5[131] =	260;
	ppm5[132] =	256;
	ppm5[133] =	253;
	ppm5[134] =	249;
	ppm5[135] =	246;
	ppm5[136] =	242;
	ppm5[137] =	239;
	ppm5[138] =	235;
	ppm5[139] =	232;
	ppm5[140] =	229;
	ppm5[141] =	226;
	ppm5[142] =	222;
	ppm5[143] =	219;
	ppm5[144] =	216;
	ppm5[145] =	214;
	ppm5[146] =	211;
	ppm5[147] =	208;
	ppm5[148] =	205;
	ppm5[149] =	202;
	ppm5[150] =	200;	

	int ppm7[151];
	ppm7[0] = 4112;
	ppm7[1] = 3495;
	ppm7[2] = 3017;
	ppm7[3] = 2638;
	ppm7[4] = 2332;
	ppm7[5] = 2080;
	ppm7[6] = 1870;
	ppm7[7] = 1692;
	ppm7[8] = 1541;
	ppm7[9] = 1411;
	ppm7[10] = 1298;
	ppm7[11] = 1199;
	ppm7[12] = 1112;
	ppm7[13] = 1035;
	ppm7[14] = 967;
	ppm7[15] = 905;
	ppm7[16] = 850;
	ppm7[17] = 800;
	ppm7[18] = 755;
	ppm7[19] = 714;
	ppm7[20] = 676;
	ppm7[21] = 641;
	ppm7[22] = 610;
	ppm7[23] = 581;
	ppm7[24] = 554;
	ppm7[25] = 529;
	ppm7[26] = 506;
	ppm7[27] = 484;
	ppm7[28] = 464;
	ppm7[29] = 445;
	ppm7[30] = 428;
	ppm7[31] = 412;
	ppm7[32] = 396;
	ppm7[33] = 382;
	ppm7[34] = 368;
	ppm7[35] = 355;
	ppm7[36] = 343;
	ppm7[37] = 332;
	ppm7[38] = 321;
	ppm7[39] = 311;
	ppm7[40] = 301;
	ppm7[41] = 292;
	ppm7[42] = 283;
	ppm7[43] = 275;
	ppm7[44] = 267;
	ppm7[45] = 259;
	ppm7[46] = 252;
	ppm7[47] = 245;
	ppm7[48] = 238;
	ppm7[49] = 232;
	ppm7[50] = 226;
	ppm7[51] = 220;
	ppm7[52] = 215;
	ppm7[53] = 209;
	ppm7[54] = 204;
	ppm7[55] = 199;
	ppm7[56] = 195;
	ppm7[57] = 190;
	ppm7[58] = 186;
	ppm7[59] = 181;
	ppm7[60] = 177;
	ppm7[61] = 174;
	ppm7[62] = 170;
	ppm7[63] = 166;
	ppm7[64] = 163;
	ppm7[65] = 159;
	ppm7[66] = 156;
	ppm7[67] = 153;
	ppm7[68] = 150;
	ppm7[69] = 147;
	ppm7[70] = 144;
	ppm7[71] = 141;
	ppm7[72] = 139;
	ppm7[73] = 136;
	ppm7[74] = 133;
	ppm7[75] = 131;
	ppm7[76] = 129;
	ppm7[77] = 126;
	ppm7[78] = 124;
	ppm7[79] = 122;
	ppm7[80] = 120;
	ppm7[81] = 118;
	ppm7[82] = 116;
	ppm7[83] = 114;
	ppm7[84] = 112;
	ppm7[85] = 110;
	ppm7[86] = 108;
	ppm7[87] = 107;
	ppm7[88] = 105;
	ppm7[89] = 103;
	ppm7[90] = 102;
	ppm7[91] = 100;
	ppm7[92] = 99;
	ppm7[93] = 97;
	ppm7[94] = 96;
	ppm7[95] = 94;
	ppm7[96] = 93;
	ppm7[97] = 91;
	ppm7[98] = 90;
	ppm7[99] = 89;
	ppm7[100] = 88;
	ppm7[101] = 86;
	ppm7[102] = 85;
	ppm7[103] = 84;
	ppm7[104] = 83;
	ppm7[105] = 82;
	ppm7[106] = 81;
	ppm7[107] = 80;
	ppm7[108] = 79;
	ppm7[109] = 78;
	ppm7[110] = 77;
	ppm7[111] = 76;
	ppm7[112] = 75;
	ppm7[113] = 74;
	ppm7[114] = 73;
	ppm7[115] = 72;
	ppm7[116] = 71;
	ppm7[117] = 70;
	ppm7[118] = 69;
	ppm7[119] = 68;
	ppm7[120] = 68;
	ppm7[121] = 67;
	ppm7[122] = 66;
	ppm7[123] = 65;
	ppm7[124] = 64;
	ppm7[125] = 64;
	ppm7[126] = 63;
	ppm7[127] = 62;
	ppm7[128] = 62;
	ppm7[129] = 61;
	ppm7[130] = 60;
	ppm7[131] = 60;
	ppm7[132] = 59;
	ppm7[133] = 58;
	ppm7[134] = 58;
	ppm7[135] = 57;
	ppm7[136] = 56;
	ppm7[137] = 56;
	ppm7[138] = 55;
	ppm7[139] = 55;
	ppm7[140] = 54;
	ppm7[141] = 54;
	ppm7[142] = 53;
	ppm7[143] = 52;
	ppm7[144] = 52;
	ppm7[145] = 51;
	ppm7[146] = 51;
	ppm7[147] = 50;
	ppm7[148] = 50;
	ppm7[149] = 49;
	ppm7[150] = 49;
	
	int ppm136[161];
	ppm136[0] = 199;
	ppm136[1] = 194;
	ppm136[2] = 188;
	ppm136[3] = 183;
	ppm136[4] = 178;
	ppm136[5] = 173;
	ppm136[6] = 168;
	ppm136[7] = 163;
	ppm136[8] = 159;
	ppm136[9] = 154;
	ppm136[10] = 150;
	ppm136[11] = 146;
	ppm136[12] = 142;
	ppm136[13] = 139;
	ppm136[14] = 135;
	ppm136[15] = 131;
	ppm136[16] = 128;
	ppm136[17] = 125;
	ppm136[18] = 122;
	ppm136[19] = 119;
	ppm136[20] = 116;
	ppm136[21] = 113;
	ppm136[22] = 110;
	ppm136[23] = 107;
	ppm136[24] = 105;
	ppm136[25] = 102;
	ppm136[26] = 100;
	ppm136[27] = 97;
	ppm136[28] = 95;
	ppm136[29] = 93;
	ppm136[30] = 91;
	ppm136[31] = 89;
	ppm136[32] = 86;
	ppm136[33] = 84;
	ppm136[34] = 83;
	ppm136[35] = 81;
	ppm136[36] = 79;
	ppm136[37] = 77;
	ppm136[38] = 75;
	ppm136[39] = 74;
	ppm136[40] = 72;
	ppm136[41] = 71;
	ppm136[42] = 69;
	ppm136[43] = 68;
	ppm136[44] = 66;
	ppm136[45] = 65;
	ppm136[46] = 63;
	ppm136[47] = 62;
	ppm136[48] = 61;
	ppm136[49] = 59;
	ppm136[50] = 58;
	ppm136[51] = 57;
	ppm136[52] = 56;
	ppm136[53] = 55;
	ppm136[54] = 54;
	ppm136[55] = 53;
	ppm136[56] = 52;
	ppm136[57] = 50;
	ppm136[58] = 49;
	ppm136[59] = 49;
	ppm136[60] = 48;
	ppm136[61] = 47;
	ppm136[62] = 46;
	ppm136[63] = 45;
	ppm136[64] = 44;
	ppm136[65] = 43;
	ppm136[66] = 42;
	ppm136[67] = 42;
	ppm136[68] = 41;
	ppm136[69] = 40;
	ppm136[70] = 39;
	ppm136[71] = 39;
	ppm136[72] = 38;
	ppm136[73] = 37;
	ppm136[74] = 37;
	ppm136[75] = 36;
	ppm136[76] = 35;
	ppm136[77] = 35;
	ppm136[78] = 34;
	ppm136[79] = 33;
	ppm136[80] = 33;
	ppm136[81] = 32;
	ppm136[82] = 32;
	ppm136[83] = 31;
	ppm136[84] = 31;
	ppm136[85] = 30;
	ppm136[86] = 30;
	ppm136[87] = 29;
	ppm136[88] = 29;
	ppm136[89] = 28;
	ppm136[90] = 28;
	ppm136[91] = 27;
	ppm136[92] = 27;
	ppm136[93] = 26;
	ppm136[94] = 26;
	ppm136[95] = 25;
	ppm136[96] = 25;
	ppm136[97] = 25;
	ppm136[98] = 24;
	ppm136[99] = 24;
	ppm136[100] = 23;
	ppm136[101] = 23;
	ppm136[102] = 23;
	ppm136[103] = 22;
	ppm136[104] = 22;
	ppm136[105] = 22;
	ppm136[106] = 21;
	ppm136[107] = 21;
	ppm136[108] = 21;
	ppm136[109] = 20;
	ppm136[110] = 20;
	ppm136[111] = 20;
	ppm136[112] = 19;
	ppm136[113] = 19;
	ppm136[114] = 19;
	ppm136[115] = 19;
	ppm136[116] = 18;
	ppm136[117] = 18;
	ppm136[118] = 18;
	ppm136[119] = 17;
	ppm136[120] = 17;
	ppm136[121] = 17;
	ppm136[122] = 17;
	ppm136[123] = 16;
	ppm136[124] = 16;
	ppm136[125] = 16;
	ppm136[126] = 16;
	ppm136[127] = 16;
	ppm136[128] = 15;
	ppm136[129] = 15;
	ppm136[130] = 15;
	ppm136[131] = 15;
	ppm136[132] = 14;
	ppm136[133] = 14;
	ppm136[134] = 14;
	ppm136[135] = 14;
	ppm136[136] = 14;
	ppm136[137] = 14;
	ppm136[138] = 13;
	ppm136[139] = 13;
	ppm136[140] = 13;
	ppm136[141] = 13;
	ppm136[142] = 13;
	ppm136[143] = 12;
	ppm136[144] = 12;
	ppm136[145] = 12;
	ppm136[146] = 12;
	ppm136[147] = 12;
	ppm136[148] = 12;
	ppm136[149] = 12;
	ppm136[150] = 11;
	ppm136[151] = 11;
	ppm136[152] = 11;
	ppm136[153] = 11;
	ppm136[154] = 11;
	ppm136[155] = 11;
	ppm136[156] = 11;
	ppm136[157] = 10;
	ppm136[158] = 10;
	ppm136[159] = 10;
	ppm136[160] = 10;

	TCNT1 = 57723;
	TCCR1B = (1<<CS12) | (1<<CS10);
	TIMSK1 = (1<<TOIE1);
	usart_init(0);
	usart_init(1);
	usart_init(2);
	usart_init(3);
	init_GPS();
	F = 0;
	int f_num = 0;
	char tick = 10;
	char sd_valid = 0;
	f_mount(&FatFs, "", 0);		/* Give a work area to the default drive */
	sei();

    while (1) 
	{
		if(sd_valid == 0)
		{
			sprintf(fil_nm, "data%d.txt",f_num);
			if((fr = f_open(&Fil, fil_nm, FA_WRITE | FA_CREATE_NEW)) && f_num < 9999)
				f_num++;
			else
				sd_valid = 1;
		}

		if(rec_dylos_flag == 1)
		{
			rec_dylos_flag = 0;
			new_dylos_data = 1;
			tick = 0;
			if(rec_dylos[strlen(rec_dylos) - 1] == '\n')
			{
				dylos_line = 1;
				strncpy(dylos, rec_dylos, strlen(rec_dylos));
				strclr(rec_dylos);			
			}
		}

		if(rec_GPS_flag == 1)
		{
			rec_GPS_flag = 0;
			new_GPS_data = 1;
			if(rec_GPS[strlen(rec_GPS) - 1] == '\n')
			{
				GPS_line = 1;
				strncpy(GPS, rec_GPS, strlen(rec_GPS));
				strclr(rec_GPS);
			}
		}
		
		if(dylos_line == 1)
		{
			parse_dylos(dylos, dylos_data);
			dylos_line = 0;
		}
		
		if(GPS_line == 1)
		{
			parse_GPS(GPS, GPS_data);
			GPS_line = 0;
		}
	
		if(ov_flag == 1)
		{
			ov_flag = 0;
			tick++;
			if(new_GPS_data == 1)
			{
				if(GPS_data[9] == 1)
				{
					sprintf(sd_buff, "Date: %.2d/%.2d/%.2d\nTime: %.2d:%.2d:%.2d\nLatitude: %d %d.%d\nLongitude: %d %d.%d\nAltitude %d.%d\n", GPS_data[12], GPS_data[13], GPS_data[14], GPS_data[0], GPS_data[1], GPS_data[2], GPS_data[3], GPS_data[4], GPS_data[5], GPS_data[6], GPS_data[7], GPS_data[8], GPS_data[10], GPS_data[11]);
					sprintf(buffer,"%.2d:%.2d:%.2d,%d,%d.%d,%d,%d.%d,%d.%d,",GPS_data[0], GPS_data[1], GPS_data[2], GPS_data[3], GPS_data[4], GPS_data[5], GPS_data[6], GPS_data[7], GPS_data[8], GPS_data[10], GPS_data[11]);
				}
				else
				{
					sprintf(sd_buff, "Time: %.2d:%.2d:%.2d\nNO GPS DATA AVAILABLE\n", GPS_data[0], GPS_data[1], GPS_data[2]);
					sprintf(buffer,"%.2d:%.2d:%.2d,0,0.0,0,0.0,0.0,", GPS_data[0], GPS_data[1], GPS_data[2]);
				}
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
				usart_send(3, buffer);
				new_GPS_data = 0;
			}
			else
				usart_send(3,"00:00:00,0,0.0,0,0.0,0.0,");
			if(new_dylos_data == 1 || tick < 8)
			{
				sprintf(sd_buff, "Small: %d\nLarge: %d\n", dylos_data[0], dylos_data[1]);
				sprintf(buffer, "%d,%d,", dylos_data[0], dylos_data[1]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
				new_dylos_data = 0;
			}
			else
				usart_send(3,"0,0,");			
			F = read_temp();
			sprintf(sd_buff, "Temperature %dF\n", F);
			sprintf(buffer, "%d,", F);
			usart_send(3, buffer);
			sd_write(sd_buff, &Fil, fil_nm, sd_valid);			
			C3H8 = read_MQ2();
			if(C3H8 < -1)
			{
				usart_send(3, "-1,");
				sd_write("C3H8: ERROR - MQ2 reading out of range\n", &Fil, fil_nm, sd_valid);
			}
			else if(C3H8 == -1)
			{
				sprintf(sd_buff, "C3H8: %d ppm \n",0);
				usart_send(3, "0,");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "C3H8: %d ppm \n",ppm2[C3H8]);
				sprintf(buffer, "%d,", ppm2[C3H8]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			CH4 = read_MQ5();
			if(CH4 < -1)
			{
				usart_send(3, "-1,");
				sd_write("CH4 : ERROR - MQ5 reading out of range\n", &Fil, fil_nm, sd_valid);
			}
			else if(CH4 == -1)
			{
				sprintf(sd_buff, "CH4 : %d ppm \n",0);
				usart_send(3, "0,");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "CH4 : %d ppm \n",ppm5[CH4]);
				sprintf(buffer, "%d,", ppm5[CH4]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			CO = read_MQ7();
			if(CO < -1)
			{
				usart_send(3, "-1,");
				sd_write("CO  : ERROR - MQ7 reading out of range\n", &Fil, fil_nm, sd_valid);
			}
			else if(CO == -1)
			{
				sprintf(sd_buff, "CO  : %d ppm \n",0);
				usart_send(3, "0,");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "CO  : %d ppm \n",ppm7[CO]);
				sprintf(buffer, "%d,", ppm7[CO]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}			
			H2S = read_MQ136();
			if(H2S < -1)
			{
				usart_send(3, "-1\n");
				sd_write("H2S : ERROR - MQ136 reading out of range\n\n", &Fil, fil_nm, sd_valid);
			}
			else if(H2S == -1)
			{
				sprintf(sd_buff, "H2S : %d ppm \n\n",0);
				usart_send(3, "0\n");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "H2S : %d ppm \n\n",ppm136[H2S]);
				sprintf(buffer, "%d\n", ppm136[H2S]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
		}
	}
	return 0;
}
Ejemplo n.º 22
0
static void * check_temperature(void * data) 
{
	thdata 			*thread_data = (thdata *)data;
	temp_update_t 	event_data;
	int 			ret;
	float 			old_ambient,old_wort;
	int 			fp, fp1;
	int 			a,b;
	char 			hex_string[6];
	int 			dec_val = 0;
	float 			temp_read;
	while (1) 
	{
		fp = open(temp_wort, O_RDONLY);
		fp1 = open(temp_ambient, O_RDONLY);
		if ( fp != -1 && fp1 != -1 )
		{
			old_wort = event_data.temp1;
			old_ambient = event_data.temp2;
			
			// read wort then read ambient
			temp_read = read_temp(fp);
			if ( temp_read != -999.0 ) {
				event_data.temp1 = temp_read;
			} else {
				event_data.temp1 = old_wort; 
				syslog(LOG_ERR, "one wire read failed for Wort, using old temp of %f",event_data.temp1);
			}
			temp_read = read_temp(fp1);
			if ( temp_read != -999.0 ) {
				event_data.temp2 = temp_read;
				thread_data->good_read = 1;
			} else {
				event_data.temp2 = old_ambient;
				syslog(LOG_ERR, "one wire read failed for Ambient, using old temp of %f",event_data.temp2);
			}
			if ( event_data.temp1 != old_wort || event_data.temp2 != old_ambient ) {
				thread_data->temp_data.temp1 = event_data.temp1;
				thread_data->temp_data.temp2 = event_data.temp2;
				syslog(LOG_DEBUG,"GOT WORT TEMP OF : %f and AMBIENT OF %f and heatcool is %d", thread_data->temp_data.temp1,thread_data->temp_data.temp2,thread_data->heatcool);
			//	ret = temp_update(thread_data->send_handle, NULL, &event_data);
			}
		
			if ( thread_data->temp_data.temp1 < (thread_data->temp_data.set_point-0.3) && thread_data->temp_data.temp1 != -1 ) {
				syslog(LOG_NOTICE,"Heat Enabled [ Setpoint %f : Wort Temp : %f\n", thread_data->temp_data.set_point,thread_data->temp_data.temp1);
				if ( control_temp("1","0") ) {
					thread_data->heatcool=2;
				} else {
					syslog(LOG_ERR,"Error, unable to enable heating");
				}
			}
			if ( thread_data->temp_data.temp1 == thread_data->temp_data.set_point || thread_data->temp_data.temp1 == (thread_data->temp_data.set_point+0.1) || thread_data->temp_data.temp1 == (thread_data->temp_data.set_point-0.1) ) {
				syslog(LOG_NOTICE,"Heating and Cooling disabled");
				if ( control_temp("0","0") ) {
					thread_data->heatcool=1;
				} else {
					syslog(LOG_ERR,"Unable to disable heating and cooling\n");
				}
			}
			if ( thread_data->temp_data.temp1 > (thread_data->temp_data.set_point + 0.4)  && thread_data->temp_data.temp2 >= 3 && thread_data->temp_data.temp1 != -1) {
				syslog(LOG_NOTICE,"Chill Enabled [ Setpoint %f : Wort Temp : %f\n", thread_data->temp_data.set_point,thread_data->temp_data.temp1);
				if ( control_temp("0","1") ) {
					thread_data->heatcool=3;
				} else {
					syslog(LOG_ERR,"Unable to enable the chiller\n");
				}
			}
			close(fp);
			close(fp1);
			sleep(thread_data->read_freq);
		 } else {
			syslog(LOG_ERR, "unable to open temperature sensors");
		}
	}
}