/**
 * Read a value from the sensor, map it over the expected range, average it with the N previous readings and return the average.
 */
unsigned short BuddleiaSensor::read() {
  // The raw sensor reading is mapped from the actual range to the expected range.
  unsigned short reading = expand(analogRead(_pin));

  // Average the last N sensor readings to create a smoother effect.
  updateBuffer(reading);
  
  return bufferAverage();
}
示例#2
0
int32_t Counter::filter(int32_t val) {
	double scaled_val = (double)val / (double)*internals->input_scale;
	addSample(internals->positions, (long)read_time, scaled_val);

#if 0
	if (internals->filter_type && *internals->filter_type == 0) {
		internals->last_sent = val;
	}
	else if (internals->filter_type && *internals->filter_type == 1) {
		int32_t mean = (internals->positions.average(internals->buffer_len) + 0.5f);
		if (internals->tolerance) internals->noise_tolerance = *internals->tolerance;
		if ( (uint32_t)abs(mean - internals->last_sent) >= internals->noise_tolerance) {
			internals->last_sent = mean;
		}
	}
	else if (internals->filter_type && *internals->filter_type == 2) {
		long res = (long)internals->filter();
		internals->last_sent = (int32_t)(res / internals->filter_len *2);
	}
#endif
	if (*internals->tolerance>1) {
		int32_t mean = (bufferAverage(internals->positions, *internals->filter_len) + 0.5f);
		long delta = (uint32_t)abs(mean - internals->last_sent);
		if ( delta >= *internals->tolerance) {
			internals->last_sent = mean;
		}
	}
	else
		internals->last_sent = (*internals->input_scale == 1) ? val : (uint32_t)( scaled_val + 0.5 );
	internals->update(read_time);

#if 1
	/* most machines reading sensor values will be prompted when teh
		sensor value changes, depending on whether this filter yields a
		changed value. Some systems such as plugins that operate on 
		their own clock may wish to ignore the filtered value and 
		access the raw io value and read time but note that these 
		values do not cause notifications when they change
	*/

	

	std::list<MachineInstance*>::iterator owners_iter = owners.begin();
	while (owners_iter != owners.end()) {
		MachineInstance *o = *owners_iter++;
		o->properties.add("IOTIME", (long)read_time, SymbolTable::ST_REPLACE);
		o->properties.add("DurationTolerance", internals->rate_len, SymbolTable::ST_REPLACE);
		o->properties.add("VALUE", (long)scaled_val, SymbolTable::ST_REPLACE);
		o->properties.add("Position", (long)internals->last_sent, SymbolTable::ST_REPLACE);
		o->properties.add("Velocity", (long)internals->speeds.average(internals->speeds.length()), SymbolTable::ST_REPLACE);
	}
#endif
	return internals->last_sent;
}
示例#3
0
int32_t AnalogueInput::filter(int32_t raw) {
    if (config->property_changed) {
        config->property_changed = false;
    }
	addSample(config->positions, (long)read_time, (double)raw);

	if (config->filter_type && *config->filter_type == 0 ) {
		config->last_sent = raw;
	}
	else if ( !config->filter_type || (config->filter_type && *config->filter_type == 1))  {
		int32_t mean = (bufferAverage(config->positions, *config->filter_len) + 0.5f);
		long delta = abs(mean - config->last_sent);
		if ( delta >= *config->tolerance) {
			config->last_sent = mean;
		}
	}
	else if (config->filter_type && *config->filter_type == 2) {
		long res = (long)config->filter();
		config->last_sent = (int32_t)(res / config->butterworth_len *2);
	}
	
	config->update(read_time);
	
#if 1
	/* most machines reading sensor values will be prompted when teh
		sensor value changes, depending on whether this filter yields a
		changed value. Some systems such as plugins that operate on 
		their own clock may wish to ignore the filtered value and 
		access the raw io value and read time but note that these 
		values do not cause notifications when they change
	*/

	

	std::list<MachineInstance*>::iterator owners_iter = owners.begin();
	while (owners_iter != owners.end()) {
		MachineInstance *o = *owners_iter++;
		o->properties.add("IOTIME", (long)read_time, SymbolTable::ST_REPLACE);
		o->properties.add("DurationTolerance", config->rate_len, SymbolTable::ST_REPLACE);
		o->properties.add("VALUE", (long)raw, SymbolTable::ST_REPLACE);
		o->properties.add("Position", (long)config->last_sent, SymbolTable::ST_REPLACE);
		double v = config->speeds.average(config->speeds.length());
		if (fabs(v)<1.0) v = 0.0;
		o->properties.add("Velocity", (long)v, SymbolTable::ST_REPLACE);
	}
#endif
	return config->last_sent;
}