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 1:a6c13143b151, committed 2014-07-03
- Comitter:
- bjblazkowicz
- Date:
- Thu Jul 03 09:32:09 2014 +0000
- Parent:
- 0:8a18ceffce1b
- Child:
- 2:c865eac393d2
- Commit message:
- Updated to latest rgb_sensor with low ADC frequency.; C++-ified detection code.
Changed in this revision
--- a/detection.cpp Thu Jul 03 08:07:46 2014 +0000 +++ b/detection.cpp Thu Jul 03 09:32:09 2014 +0000 @@ -2,18 +2,10 @@ #include "detection.h" #define COUNT(x) (sizeof(x)/sizeof(x[0])) -#define RGB_VALUES (15) -extern RGB_Sensor rgb; extern Serial console; - -static uint32_t g_buffer_pos; -static TRGB g_buffer[RGB_VALUES]; -struct Capsule { - const char* name; - double r, g, b; -} g_capsules[] = { +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 }, @@ -23,16 +15,23 @@ { "vivalto", -152.07474747474748, -608.4919191919192, -574.739393939394 } }; -static bool rgb_callback(const TRGB &color) +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) { - if(g_buffer_pos>=RGB_VALUES) - return false; - - g_buffer[g_buffer_pos++] = color; - return true; + // FIXME: Detect double instantiations. Use a thunk. + m_global = this; } -int match_sample_to_capsule(TRGB& rgb_sample) +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; double min_so_far = 1e20; @@ -50,69 +49,83 @@ min_so_far = d; best_index = j; } + + console.printf("Distance to %s is %f\r\n", g_capsules[j].name, d); } return best_index; } -Capsule* read_capsule() +const Capsule* RGB_Detection::read_capsule(void) { - Capsule *capsule = NULL; + Capsule const *capsule = NULL; bool done = false; while (!done) { int histogram[COUNT(g_capsules)] = {0}; - int first_significant_sample = RGB_VALUES / 3; + int first_significant_sample = DETECTION_VALUES / 3; - g_buffer_pos = 0; - rgb.capture(rgb_callback); - rgb.wait(); + m_buffer_pos = 0; + m_rgb.capture(__callback); + m_rgb.wait(); - for (int i = first_significant_sample; i < RGB_VALUES; i++) + for (int i = first_significant_sample; i < DETECTION_VALUES; i++) { - int index = match_sample_to_capsule(g_buffer[i]); + int index = match_sample_to_capsule(m_buffer[i]); histogram[index]++; } for (int i = 0; i < COUNT(histogram); i++) { - if (histogram[i] > 9/*RGB_VALUES / 2*/) + if (histogram[i] > 9/*DETECTION_VALUES / 2*/) { capsule = &g_capsules[i]; done = true; break; } - } + } - //console.printf("------------------------\r\n"); -// for (int i = 0; i < COUNT(histogram); i++) -// { -// console.printf("%s: %d\r\n", g_capsules[i].name, histogram[i]); -// } -// console.printf("------------------------\r\n\r\n"); - + console.printf("------------------------\r\n"); + for (int i = 0; i < COUNT(histogram); i++) + { + console.printf("%s: %d\r\n", g_capsules[i].name, histogram[i]); + } + console.printf("------------------------\r\n\r\n"); } return capsule; } -static Capsule *last_capsule = NULL; +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; -const char* do_detection() + m_buffer[m_buffer_pos++] = color; + return true; +} + + +const char* RGB_Detection::run(void) { - Capsule* this_capsule = read_capsule(); - if (last_capsule) + console.printf("RGB_Detection::read: "); + Capsule const *this_capsule = read_capsule(); + console.printf("%s\r\n", this_capsule->name); + if (m_last_capsule) { - while (this_capsule == last_capsule) + while (this_capsule == m_last_capsule) this_capsule = read_capsule(); - - last_capsule = this_capsule; - } - else - { - last_capsule = this_capsule; - } + } + + m_last_capsule = this_capsule; return this_capsule->name; } +
--- a/detection.h Thu Jul 03 08:07:46 2014 +0000 +++ b/detection.h Thu Jul 03 09:32:09 2014 +0000 @@ -1,8 +1,37 @@ #ifndef __DETECTION_H_ #define __DETECTION_H__ -#include "rgb_sensor.h" +#include <rgb_sensor.h> + +#define DETECTION_VALUES (15) + +struct Capsule { + const char* name; + double r, g, b; +}; + +class RGB_Detection { + public: + RGB_Detection(PinName red, PinName green, PinName blue, PinName adc); + ~RGB_Detection(void); + const char *run(void); + + protected: + int match_sample_to_capsule(TRGB& rgb_sample); + const Capsule *read_capsule(void); + + RGB_Sensor m_rgb; + Capsule const *m_last_capsule; + + uint32_t m_buffer_pos; + TRGB m_buffer[DETECTION_VALUES]; + + 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 08:07:46 2014 +0000 +++ b/rgb_sensor.lib Thu Jul 03 09:32:09 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/meriac/code/rgb_sensor/#fc64a14a2f4a +http://mbed.org/users/meriac/code/rgb_sensor/#0d35392230be