Colour binning for Nespresso capsules using rgb_sensor (RGB LED & Photo Diode) - to detect flavour based on colour
Dependencies: rgb_sensor_buffer
Dependents: coffee_capsule_detection
Revision 3:4eefc032a319, committed 2014-07-11
- Comitter:
- bjblazkowicz
- Date:
- Fri Jul 11 13:48:32 2014 +0000
- Parent:
- 2:c865eac393d2
- Commit message:
- Now uses the buffered sensor.
Changed in this revision
--- a/detection.cpp Thu Jul 03 09:37:45 2014 +0000 +++ b/detection.cpp Fri Jul 11 13:48:32 2014 +0000 @@ -1,7 +1,10 @@ #include <mbed.h> #include "detection.h" +#define RGB_TRESHOLD 0 + #define COUNT(x) (sizeof(x)/sizeof(x[0])) +//#define DETECTION_DEBUG #ifdef DETECTION_DEBUG extern Serial console; @@ -9,33 +12,27 @@ static const Capsule g_capsules[] = { { "no_capsule", 0, 0, 0 }, - { "decaffeinato_intenso", -380.97474747474746, -606.4181818181818, -414.5565656565657 }, - { "arpeggio", -84.13939393939394, -391.35656565656564, -405.6222222222222 }, - { "dulsao", -422.4707070707071, -726.2787878787879, -541.3545454545455 }, - { "volluto", -480.3393939393939, -768.0040404040404, -530.9494949494949 }, - { "fortissio", -5.533333333333333, -521.9070707070707, -514.1626262626263 }, - { "vivalto", -152.07474747474748, -608.4919191919192, -574.739393939394 } + { "arpeggio", 182.091796875, 390.759765625, 557.3984375 }, + { "decaffeinato_intenso", 265.15234375, 383.55859375, 464.64453125 }, + { "livanto", 417.59765625, 551.00390625, 522.685546875 }, + { "roma", 159.708984375, 407.90234375, 476.068359375 }, + { "rosabaya", 707.041015625, 1193.072265625, 1382.546875 }, + { "volluto", 736.615234375, 1041.916015625, 849.5234375 } }; -RGB_Detection* RGB_Detection::m_global = NULL; - RGB_Detection::RGB_Detection(PinName red, PinName green, PinName blue, PinName adc) : m_rgb(red, green, blue, adc), m_last_capsule(NULL) { - // FIXME: Detect double instantiations. Use a thunk. - m_global = this; } RGB_Detection::~RGB_Detection() { - // FIXME: Detect double instantiations. Use a thunk. - m_global = NULL; } int RGB_Detection::match_sample_to_capsule(TRGB& rgb_sample) { - const int magic_threshold = 10000; + const int magic_threshold = 7000; double min_so_far = 1e20; int best_index = 0; @@ -64,21 +61,18 @@ while (!done) { int histogram[COUNT(g_capsules)] = {0}; - int first_significant_sample = DETECTION_VALUES / 3; - - m_buffer_pos = 0; - m_rgb.capture(__callback); - m_rgb.wait(); - - for (int i = first_significant_sample; i < DETECTION_VALUES; i++) + int samples_in_buffer = m_rgb.trigger(m_buffer, COUNT(m_buffer), RGB_TRESHOLD); + + for (int i = 0; i < samples_in_buffer; i++) { + // console.printf("buffer[%d] = {%i, %i, %i}\r\n", i, m_buffer[i].data[0] / RGB_OVERSAMPLING, m_buffer[i].data[1] / RGB_OVERSAMPLING, m_buffer[i].data[2] / RGB_OVERSAMPLING); int index = match_sample_to_capsule(m_buffer[i]); histogram[index]++; } for (int i = 0; i < COUNT(histogram); i++) { - if (histogram[i] > 9/*DETECTION_VALUES / 2*/) + if (histogram[i] > 45/*RGB_VALUES / 2*/) { capsule = &g_capsules[i]; done = true; @@ -99,22 +93,6 @@ return capsule; } -bool RGB_Detection::__callback(const TRGB &color) -{ - return m_global ? m_global->callback(color) : false; -} - - -bool RGB_Detection::callback(const TRGB &color) -{ - if(m_buffer_pos>=DETECTION_VALUES) - return false; - - m_buffer[m_buffer_pos++] = color; - return true; -} - - const char* RGB_Detection::run(void) { Capsule const *this_capsule = read_capsule();
--- a/detection.h Thu Jul 03 09:37:45 2014 +0000 +++ b/detection.h Fri Jul 11 13:48:32 2014 +0000 @@ -1,9 +1,9 @@ #ifndef __DETECTION_H_ #define __DETECTION_H__ -#include <rgb_sensor.h> +#include <rgb_sensor_buffer.h> -#define DETECTION_VALUES (15) +#define RGB_VALUES (50) struct Capsule { const char* name; @@ -21,17 +21,15 @@ int match_sample_to_capsule(TRGB& rgb_sample); const Capsule *read_capsule(void); - RGB_Sensor m_rgb; + RGB_SensorBuffer m_rgb; Capsule const *m_last_capsule; - uint32_t m_buffer_pos; - TRGB m_buffer[DETECTION_VALUES]; + TRGB m_buffer[RGB_VALUES]; - private: - static bool __callback(const TRGB &color); - bool callback(const TRGB &color); - static RGB_Detection *m_global; +// private: +// static bool __callback(const TRGB &color); +// bool callback(const TRGB &color); +// static RGB_Detection *m_global; }; -const char* do_detection(); #endif // __DETECTION_H__
--- a/rgb_sensor.lib Thu Jul 03 09:37:45 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/meriac/code/rgb_sensor/#0d35392230be
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgb_sensor_buffer.lib Fri Jul 11 13:48:32 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/meriac/code/rgb_sensor_buffer/#8fbb1cd0c2d9