forked from guyzmo/polluxnzcity
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fernandez Lucas
committed
Sep 3, 2012
1 parent
3d5ec77
commit e6fc233
Showing
10 changed files
with
1,464 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Pollux'NZ City Project | ||
* Copyright (c) 2012 CKAB, hackable:Devices | ||
* Copyright (c) 2012 Bernard Pratz <guyzmo{at}hackable-devices{dot}org> | ||
* Copyright (c) 2012 Lucas Fernandez <kasey{at}hackable-devices{dot}org> | ||
* | ||
* Pollux'NZ City is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Pollux'NZ City is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this project. If not, see <http://www.gnu.org/licenses/>. | ||
+-\/-+ | ||
(RESET) 1| |8 VCC (2.7-5.5V) | ||
Temp Read 2| |7 I2C SCK -> Uno A5 | ||
NA 3| |6 (PB1) NA | ||
GND 4| |5 I2C SDA -> Uno A4 | ||
+----+ | ||
*/ | ||
#include <Statistic.h> | ||
#include <TinyWire.h> | ||
|
||
#define I2C_SLAVE_ADDR 0x25 // I2C slave address | ||
|
||
#define TEMP_READ 3 // Dust read on pin 2 (ADC3) | ||
#define DELAY_TIME 10 // Wait 10 ms before reread sensor value | ||
#define NB_READ 100 | ||
|
||
TinyWire Wire; | ||
|
||
prog_uint32_t offset=(float)-0.5; | ||
|
||
void sensor_calibrate() { | ||
offset = Wire.read(0x20); | ||
offset = Wire.read(0x21); | ||
offset = Wire.read(0x22); | ||
offset = Wire.read(0x23); | ||
} | ||
|
||
void sensor_meas() { | ||
Statistic stat; | ||
float result; | ||
uint8_t* float_ptr; | ||
|
||
for(uint8_t i=0;i<NB_READ;i++) { // make 100 temp sample | ||
stat.add(float(analogRead(TEMP_READ))); // add the sensor value in statistics | ||
delay(DELAY_TIME); // wait 1ms to lets ADC converge | ||
} | ||
|
||
result = (stat.average()*0.0049)/62.9 ; | ||
result = result + 0.556792873; | ||
result = 8980 * result; | ||
result = 35678500 / result; | ||
result = result - 4715 ; | ||
|
||
if(result < 0) result = 0; // avoid eratic datas | ||
stat.clear(); // clear statistics to avoid leack and data stacking | ||
|
||
float_ptr = (uint8_t*)&result; | ||
|
||
Wire.write(0x11,*(float_ptr)); // send 1st byte of float | ||
Wire.write(0x12,*(float_ptr+1)); // send 2nd byte of float | ||
Wire.write(0x13,*(float_ptr+2)); // send 3rd byte of float | ||
Wire.write(0x14,*(float_ptr+3)); // send 4th byte of float | ||
} | ||
|
||
void setup() { | ||
pinMode(TEMP_READ,INPUT); | ||
Wire.begin(I2C_SLAVE_ADDR); // slave address | ||
} | ||
|
||
|
||
void loop() { | ||
// on I2C bus : (addr=0x25,reg=0x00,val=1) | ||
if (Wire.read(0x00) != 0) | ||
sensor_meas(); | ||
|
||
// on I2C bus : (addr=0x26,reg=0x10) | ||
if (Wire.read(0x01) != 0) | ||
sensor_calibrate(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Board for dust detection for final fing pollux. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// | ||
// FILE: Statistic.cpp | ||
// AUTHOR: Rob dot Tillaart at gmail dot com | ||
// modified at 0.3 by Gil Ross at physics dot org | ||
// VERSION: 0.3.00 | ||
// PURPOSE: Recursive statistical library for Arduino | ||
// | ||
// NOTE: 2011-01-07 Gill Ross | ||
// Rob Tillaart's Statistic library uses one-pass of the data (allowing | ||
// each value to be discarded), but expands the Sum of Squares Differences to | ||
// difference the Sum of Squares and the Average Squared. This is susceptible | ||
// to bit length precision errors with the float type (only 5 or 6 digits | ||
// absolute precision) so for long runs and high ratios of | ||
// the average value to standard deviation the estimate of the | ||
// standard error (deviation) becomes the difference of two large | ||
// numbers and will tend to zero. | ||
// | ||
// For small numbers of iterations and small Average/SE th original code is | ||
// likely to work fine. | ||
// It should also be recognised that for very large samples, questions | ||
// of stability of the sample assume greater importance than the | ||
// correctnness of the asymptotic estimators. | ||
// | ||
// This recursive algorithm, which takes slightly more computation per | ||
// iteration is numerically stable. | ||
// It updates the number, mean, max, min and SumOfSquaresDiff each step to | ||
// deliver max min average, population standard error (standard deviation) and | ||
// unbiassed SE. | ||
// ------------- | ||
// | ||
// HISTORY: | ||
// 0.1 - 2010-10-29 initial version | ||
// 0.2 - 2010-10-29 stripped to minimal functionality | ||
// 0.2.01 - 2010-10-30 | ||
// added minimim, maximum, unbiased stdev, | ||
// changed counter to long -> int overflows @32K samples | ||
// | ||
// 0.3 - branched from 0.2.01 version of Rob Tillaart's code | ||
// Released to the public domain | ||
// | ||
|
||
#include "Statistic.h" | ||
|
||
Statistic::Statistic() | ||
{ | ||
clear(); | ||
} | ||
|
||
// resets all counters | ||
void Statistic::clear() | ||
{ | ||
_cnt = 0; // count at N stored, becoming N+1 at a new iteration | ||
_sum = 0; | ||
_min = 0.0; | ||
_max = 0.0; | ||
#ifdef STAT_USE_STDEV | ||
_ssqdif = 0.0; // not _ssq but sum of square differences | ||
// which is SUM(from i = 1 to N) of | ||
// (f(i)-_ave_N)**2 | ||
#endif | ||
} | ||
|
||
// adds a new value to the data-set | ||
void Statistic::add(float f) | ||
{ | ||
if (_cnt == 0) | ||
{ | ||
_min = f; | ||
_max = f; | ||
} else { | ||
if (f < _min) _min = f; | ||
if (f > _max) _max = f; | ||
} // end of if (_cnt == 0) else | ||
_sum += f; | ||
_cnt++; | ||
#ifdef STAT_USE_STDEV | ||
if (_cnt >1) { | ||
_store = (_sum / _cnt - f); | ||
_ssqdif = _ssqdif + _cnt * _store * _store / (_cnt-1); | ||
} // end if > 1 | ||
|
||
#endif | ||
|
||
|
||
} | ||
|
||
// returns the number of values added | ||
long Statistic::count() | ||
{ | ||
return _cnt; | ||
} | ||
|
||
// returns the average of the data-set added sofar | ||
float Statistic::average() | ||
{ | ||
if (_cnt == 0) return -1; // original code returned 0 | ||
return _sum / _cnt; | ||
} | ||
|
||
// returns the sum of the data-set (0 if no values added) | ||
float Statistic::sum() | ||
{ | ||
return _sum; | ||
} | ||
|
||
// returns the sum of the data-set (0 if no values added) | ||
float Statistic::minimum() | ||
{ | ||
return _min; | ||
} | ||
|
||
// returns the sum of the data-set (0 if no values added) | ||
float Statistic::maximum() | ||
{ | ||
return _max; | ||
} | ||
|
||
|
||
// Population standard deviation = s = sqrt [ S ( Xi - µ )2 / N ] | ||
// http://www.suite101.com/content/how-is-standard-deviation-used-a99084 | ||
#ifdef STAT_USE_STDEV | ||
float Statistic::pop_stdev() | ||
{ | ||
if (_cnt < 1) return -1; // otherwise DIV0 error | ||
return sqrt( _ssqdif / _cnt); | ||
} | ||
|
||
float Statistic::unbiased_stdev() | ||
{ | ||
if (_cnt < 2) return -1; // otherwise DIV0 error | ||
return sqrt( _ssqdif / (_cnt - 1)); | ||
} | ||
#endif | ||
// END OF FILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef Statistic_h | ||
#define Statistic_h | ||
// | ||
// FILE: Statistic.h | ||
// AUTHOR: Rob dot Tillaart at gmail dot com | ||
// modified at 0.3 by Gil Ross at physics dot org | ||
// PURPOSE: Recursive Statistical library for Arduino | ||
// VERSION: 0.3.00 | ||
// URL: http://www.arduino.cc/playground/Main/Statistics | ||
// HISTORY: See Statistic.cpp | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
// the standard deviation increases the lib (<100 bytes) | ||
// it can be in/excluded by un/commenting next line | ||
#define STAT_USE_STDEV | ||
|
||
#ifdef STAT_USE_STDEV | ||
#include <math.h> | ||
#endif | ||
|
||
class Statistic | ||
{ | ||
public: | ||
Statistic(); | ||
void clear(); | ||
void add(float); | ||
long count(); | ||
float sum(); | ||
float average(); | ||
float minimum(); | ||
float maximum(); | ||
|
||
|
||
#ifdef STAT_USE_STDEV | ||
float pop_stdev(); // population stdev | ||
float unbiased_stdev(); | ||
#endif | ||
|
||
protected: | ||
long _cnt; | ||
float _store; // store to minimise computation | ||
float _sum; | ||
float _min; | ||
float _max; | ||
#ifdef STAT_USE_STDEV | ||
float _ssqdif; // sum of squares difference | ||
#endif | ||
}; | ||
|
||
#endif | ||
// END OF FILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Pollux'NZ City Project | ||
* Copyright (c) 2012 CKAB, hackable:Devices | ||
* Copyright (c) 2012 Bernard Pratz <guyzmo{at}hackable-devices{dot}org> | ||
* Copyright (c) 2012 Lucas Fernandez <kasey{at}hackable-devices{dot}org> | ||
* | ||
* Copyright (c) 2009 Andrew Rapp. For the XBee example origins. | ||
* | ||
* Pollux'NZ City is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Pollux'NZ City is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this project. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
////////////////////////////////////////////////////////////////////////////////////////// | ||
//// TinyWire.cpp | ||
|
||
|
||
extern "C" { | ||
#include <usiTwiSlave.h> | ||
} | ||
#include "TinyWire.h" | ||
|
||
prog_uint8_t mem_buffer[I2C_ADDR_SIZE]; | ||
|
||
void TinyWire::begin(uint8_t i2c_slave_address) { | ||
usiTwiSlaveInit(i2c_slave_address, | ||
_on_request_handler, | ||
_on_receive_handler); | ||
for (int i=0;i<I2C_ADDR_SIZE;++i) | ||
mem_buffer[i] = 0; | ||
} | ||
|
||
uint8_t TinyWire::read(uint8_t reg) { | ||
return mem_buffer[reg]; | ||
} | ||
void TinyWire::write(uint8_t reg, uint8_t value) { | ||
mem_buffer[reg] = value; | ||
} | ||
|
||
void TinyWire::_on_receive_handler(uint8_t reg, uint8_t value) { | ||
mem_buffer[reg] = value; | ||
} | ||
|
||
uint8_t TinyWire::_on_request_handler(uint8_t reg) { | ||
return mem_buffer[reg]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef __TINYWIRE_H__ | ||
#define __TINYWIRE_H__ | ||
|
||
/** | ||
* Pollux'NZ City Project | ||
* Copyright (c) 2012 CKAB, hackable:Devices | ||
* Copyright (c) 2012 Bernard Pratz <guyzmo{at}hackable-devices{dot}org> | ||
* Copyright (c) 2012 Lucas Fernandez <kasey{at}hackable-devices{dot}org> | ||
* | ||
* Copyright (c) 2009 Andrew Rapp. For the XBee example origins. | ||
* | ||
* Pollux'NZ City is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Pollux'NZ City is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this project. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
extern "C" { | ||
#include "usiTwiSlave.h" | ||
} | ||
#define I2C_ADDR_SIZE 256 | ||
|
||
#include <avr/pgmspace.h> | ||
|
||
class TinyWire { | ||
|
||
static void _on_receive_handler(uint8_t reg, uint8_t value); | ||
static uint8_t _on_request_handler(uint8_t reg); | ||
|
||
public: | ||
void begin(uint8_t i2c_slave_address); | ||
|
||
uint8_t read(uint8_t reg); | ||
void write(uint8_t reg, uint8_t value); | ||
|
||
}; | ||
|
||
#endif // __TINYWIRE_H__ |
Oops, something went wrong.