Nespresso RGB Sensor / ColorDetector

Dependencies:   GroveColourSensor

Fork of ColorDetector by Brian Daniels

Files at this revision

API Documentation at this revision

Comitter:
bridadan
Date:
Thu Jul 02 01:43:07 2015 +0000
Parent:
8:e7c198206c5f
Commit message:
Added parameter for number of good readings before triggering change event. Updating docs.

Changed in this revision

ColorDetector.cpp Show annotated file Show diff for this revision Revisions of this file
ColorDetector.h Show annotated file Show diff for this revision Revisions of this file
--- a/ColorDetector.cpp	Thu Jul 02 01:07:28 2015 +0000
+++ b/ColorDetector.cpp	Thu Jul 02 01:43:07 2015 +0000
@@ -2,9 +2,9 @@
 #include "math.h"
 
 
-ColorDetector::ColorDetector(GroveColourSensor *sensor, uint16_t threshold, uint16_t varianceThreshold, uint16_t diffThreshold, uint16_t samplesPerReading)
-    :sensor(sensor), threshold(threshold), varianceThreshold(varianceThreshold), diffThreshold(diffThreshold), changeDetected(false), curBuffPointer(0),
-    samplesPerReading(samplesPerReading) {
+ColorDetector::ColorDetector(GroveColourSensor *sensor, uint16_t threshold, uint16_t varianceThreshold, uint16_t diffThreshold, uint16_t samplesPerReading, uint16_t readingsPerEvent)
+    :sensor(sensor), threshold(threshold), varianceThreshold(varianceThreshold), diffThreshold(diffThreshold), samplesPerReading(samplesPerReading), readingsPerEvent(readingsPerEvent),
+    changeDetected(false), curBuffPointer(0) {
         // Empty
 }
 
@@ -111,11 +111,11 @@
             totalDiff += diff;
         }
         
-        for (int i = 0; i < DIFF_BUFF_LEN - 1; i++) {
+        for (int i = 0; i < readingsPerEvent - 1; i++) {
             diffBuff[i] = diffBuff[i + 1];
         }
         
-        diffBuff[DIFF_BUFF_LEN - 1] = totalDiff;
+        diffBuff[readingsPerEvent - 1] = totalDiff;
         
         printf("totalDiff: %d\r\n", totalDiff);
         if (totalDiff > threshold) {
@@ -136,8 +136,8 @@
 
 int32_t ColorDetector::getTotalPastDiff() {
     int32_t totalDiff = 0;
-    for (int i = 0; i < DIFF_BUFF_LEN - 1; i++) {
-        totalDiff += abs(diffBuff[DIFF_BUFF_LEN - 1] - diffBuff[i]);
+    for (int i = 0; i < readingsPerEvent - 1; i++) {
+        totalDiff += abs(diffBuff[readingsPerEvent - 1] - diffBuff[i]);
     }
     return totalDiff;
 }
--- a/ColorDetector.h	Thu Jul 02 01:07:28 2015 +0000
+++ b/ColorDetector.h	Thu Jul 02 01:43:07 2015 +0000
@@ -5,7 +5,7 @@
 #include "GroveColourSensor.h"
 
 #define CD_BUFF_LEN 512
-#define DIFF_BUFF_LEN 6
+#define DIFF_BUFF_LEN 4
 
 
 /** ColorDetector class.
@@ -17,13 +17,14 @@
      *    Constructor.
      *
      *    @param sensor Pointer to instantiated and powered up Grove colour sensor.
-     *    @param threshold Total change in channel values needed to trigger a "change" event. (default 15)
-     *        This will start to save samples for later use until the total change in channel values dips below this threshold or the sample buffer is filled.
+     *    @param threshold Total change in channel values needed to be considered a "differnet" reading from the baseline. (default 15)
      *    @param varianceThreshold The maximum difference in variance between samples that is allowed for a "steady" reading. (default 3)
-     *    @param diffThreshold The maximum difference between past readings that is allowed for a "steady" reading. (default 2)
+     *    @param diffThreshold The maximum difference between past readings that is allowed for a "consistent" reading. (default 2)
      *    @param samplesPerReading Specifies the number of samples to take per sample() call. (default 256)
+     *    @param readingsPerEvent Number of readings that must be "steady", "consistent", and "different" before triggering a "change detected" event. (default 4)
+     *        NOTE: Must be less than or equal to DIFF_BUFF_LEN (default 4).
      */
-    ColorDetector(GroveColourSensor *sensor, uint16_t threshold = 15, uint16_t varianceThreshold = 3, uint16_t diffThreshold = 2, uint16_t samplesPerReading = 256);
+    ColorDetector(GroveColourSensor *sensor, uint16_t threshold = 15, uint16_t varianceThreshold = 3, uint16_t diffThreshold = 2, uint16_t samplesPerReading = 256, uint16_t readingsPerEvent = 4);
     
     /**
      *    This establishes the average "resting" channel values. This should be called before calling sample().
@@ -31,7 +32,11 @@
     void setBaseline();
     
     /**
-     *    Takes samples from sensor. It trackes whether the change in color rises above the threshold given in the constructor.
+     *    Takes a new reading from the sensor. First, it checks to see if the variance between the samples in the reading is
+     *    less than the varianceThreshold. If it is, the reading is considered "steady" and a candidate "change detected" event.
+     *    Next, it checks to see if the total average difference between the last samples is greater
+     
+     It trackes whether the change in color rises above the threshold given in the constructor.
      *    If the total change in color rises above and then falls bellow this threshold, or if the sample buffer is filled,
      *    this will return a value greater than 0 that indicates how many samples are in the buffer. Otherwise, it returns 0.
      */
@@ -54,6 +59,7 @@
     bool changeDetected;
     uint16_t samplesPerReading;
     int32_t diffBuff[DIFF_BUFF_LEN];
+    uint16_t readingsPerEvent;
     
     /**
      *    Takes the specified number of samples and returns the average color channel values.