Dectection take 1. Fixed adaptation step. Detect based on a number of standard deviations away from the mean.

Dependencies:   SI1143 m3pi mbed

Fork of Proximity_Sensor_2_detect by MF Doom

Committer:
mfillinois
Date:
Sat Jun 21 21:02:46 2014 +0000
Revision:
8:f81057354da4
Parent:
7:73a722360cdb
Sweet detector bro!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmcohen24 0:1802ead3667e 1 #include "mbed.h"
dmcohen24 0:1802ead3667e 2 #include "SI1143.h"
mfillinois 5:b77b678ed22f 3 #include "m3pi.h"
mfillinois 7:73a722360cdb 4 #define NN 1000
dmcohen24 0:1802ead3667e 5
dmcohen24 3:0359b918f658 6 //DigitalOut enable(p30);
mfillinois 5:b77b678ed22f 7 //Ticker timer;
dmcohen24 3:0359b918f658 8
dmcohen24 3:0359b918f658 9 I2C* i2c = new I2C(p28, p27);
mfillinois 5:b77b678ed22f 10 Serial bt(p13, p14);
mfillinois 5:b77b678ed22f 11 m3pi pi;
dmcohen24 0:1802ead3667e 12
mfillinois 6:f2c616fa9ffe 13 DigitalOut led1(LED1);
mfillinois 6:f2c616fa9ffe 14 DigitalOut led2(LED2);
mfillinois 6:f2c616fa9ffe 15 DigitalOut led3(LED3);
mfillinois 6:f2c616fa9ffe 16 DigitalOut led4(LED4);
dmcohen24 0:1802ead3667e 17
mfillinois 5:b77b678ed22f 18 InterruptIn event(p26);
dmcohen24 3:0359b918f658 19
dmcohen24 3:0359b918f658 20 SI1143 sensor1(i2c, p30, p29, 0x01);
mfillinois 5:b77b678ed22f 21 //SI1143 sensor2(i2c, p25, p26, 0x02); // p25, p26
dmcohen24 3:0359b918f658 22
dmcohen24 3:0359b918f658 23 //InterruptIn prox_meas(p26); //26
dmcohen24 3:0359b918f658 24 int sense1a, sense1b, sense2a, sense2b;
mfillinois 7:73a722360cdb 25 int sensor1alog[NN];
mfillinois 7:73a722360cdb 26 int sensor1blog[NN];
mfillinois 6:f2c616fa9ffe 27 int whileflag = 1;
mfillinois 5:b77b678ed22f 28 int countingstuffpoop = 0;
mfillinois 5:b77b678ed22f 29
mfillinois 8:f81057354da4 30 float alpha = 0.999;
mfillinois 8:f81057354da4 31 float beta;
mfillinois 8:f81057354da4 32 float floorpoop = 225;
mfillinois 8:f81057354da4 33 float stdpoop = 5;
mfillinois 8:f81057354da4 34 float scalepoop = 5;
mfillinois 8:f81057354da4 35 int cd = 5;
mfillinois 8:f81057354da4 36
mfillinois 5:b77b678ed22f 37 void meas_int()
mfillinois 5:b77b678ed22f 38 {
dmcohen24 2:8798491332ed 39 led1 = 1;
mfillinois 5:b77b678ed22f 40 sense1a = sensor1.read_ps1();
mfillinois 5:b77b678ed22f 41 sense1b = sensor1.read_ps2();
mfillinois 8:f81057354da4 42 if(sense1a > floorpoop)
mfillinois 8:f81057354da4 43 {
mfillinois 8:f81057354da4 44 floorpoop = floorpoop*(1+beta);
mfillinois 8:f81057354da4 45 }
mfillinois 8:f81057354da4 46 else
mfillinois 8:f81057354da4 47 {
mfillinois 8:f81057354da4 48 floorpoop = floorpoop*(1-beta);
mfillinois 8:f81057354da4 49 }
mfillinois 6:f2c616fa9ffe 50
mfillinois 8:f81057354da4 51 if(sqrt(((float) sense1a - floorpoop)*(sense1a - floorpoop)) > stdpoop)
mfillinois 5:b77b678ed22f 52 {
mfillinois 8:f81057354da4 53 stdpoop = stdpoop*(1+beta);
mfillinois 5:b77b678ed22f 54 }
mfillinois 6:f2c616fa9ffe 55 else
mfillinois 6:f2c616fa9ffe 56 {
mfillinois 8:f81057354da4 57 stdpoop = stdpoop*(1-beta);
mfillinois 8:f81057354da4 58 }
mfillinois 8:f81057354da4 59
mfillinois 8:f81057354da4 60 if(sense1a > floorpoop + scalepoop*stdpoop)
mfillinois 8:f81057354da4 61 {
mfillinois 7:73a722360cdb 62 led4 = 1;
mfillinois 8:f81057354da4 63 cd = 20;
mfillinois 6:f2c616fa9ffe 64 }
mfillinois 8:f81057354da4 65 else
mfillinois 8:f81057354da4 66 {
mfillinois 8:f81057354da4 67 cd = cd - 1;
mfillinois 8:f81057354da4 68 if(cd < 0)
mfillinois 8:f81057354da4 69 {
mfillinois 8:f81057354da4 70 cd = - 1;
mfillinois 8:f81057354da4 71 led4 = 0;
mfillinois 8:f81057354da4 72 }
mfillinois 8:f81057354da4 73 }
mfillinois 5:b77b678ed22f 74 sensor1.clear_int();
dmcohen24 3:0359b918f658 75 led1 = 0;
dmcohen24 2:8798491332ed 76 }
dmcohen24 2:8798491332ed 77
mfillinois 7:73a722360cdb 78 void blinkblink(int t, int n)
mfillinois 7:73a722360cdb 79 {
mfillinois 7:73a722360cdb 80 for (int i = 1; i <= n; i++)
mfillinois 7:73a722360cdb 81 {
mfillinois 7:73a722360cdb 82 led4 = 1;
mfillinois 7:73a722360cdb 83 wait(t);
mfillinois 7:73a722360cdb 84 led4 = 0;
mfillinois 7:73a722360cdb 85 wait(t);
mfillinois 7:73a722360cdb 86 }
mfillinois 7:73a722360cdb 87 }
mfillinois 7:73a722360cdb 88
dmcohen24 0:1802ead3667e 89 int main()
mfillinois 6:f2c616fa9ffe 90 {
dmcohen24 3:0359b918f658 91 led2 = 0;
mfillinois 7:73a722360cdb 92 //wait(10);
mfillinois 7:73a722360cdb 93 blinkblink(1,2);
mfillinois 8:f81057354da4 94 beta = 1-alpha;
mfillinois 5:b77b678ed22f 95 event.fall(&meas_int);
mfillinois 5:b77b678ed22f 96 //timer.attach(&meas_int, 0.01);
mfillinois 5:b77b678ed22f 97 sensor1.start_ps_auto();
mfillinois 5:b77b678ed22f 98 led4 = 1;
mfillinois 7:73a722360cdb 99 //pi.left(0.1);
dmcohen24 3:0359b918f658 100
mfillinois 7:73a722360cdb 101 //pi.stop();
mfillinois 7:73a722360cdb 102 blinkblink(1,5);
mfillinois 6:f2c616fa9ffe 103
mfillinois 6:f2c616fa9ffe 104 led1 = 1;
mfillinois 6:f2c616fa9ffe 105 wait(0.3);
mfillinois 6:f2c616fa9ffe 106 led1 = 0;
mfillinois 6:f2c616fa9ffe 107 wait(0.3);
mfillinois 6:f2c616fa9ffe 108 led1 = 1;
mfillinois 8:f81057354da4 109 while(1)
mfillinois 8:f81057354da4 110 {
mfillinois 8:f81057354da4 111
mfillinois 8:f81057354da4 112 }
dmcohen24 3:0359b918f658 113 }