colorrrrr

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
akudohune
Date:
Thu Aug 29 13:50:59 2013 +0000
Commit message:
color

Changed in this revision

ColorSensor.cpp Show annotated file Show diff for this revision Revisions of this file
ColorSensor.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 7269a89e1d57 ColorSensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ColorSensor.cpp	Thu Aug 29 13:50:59 2013 +0000
@@ -0,0 +1,147 @@
+#include "ColorSensor.h"
+// カラーセンサーテストプログラム
+// 赤、緑、青を判定する。
+#define coef 1.05
+
+ColorSensor::ColorSensor(
+    PinName dout, PinName range, 
+    PinName ck, PinName gate, int tm)
+{
+  Dout  = new DigitalIn(dout);
+  Range = new DigitalOut(range);
+  CK    = new DigitalOut(ck);
+  Gate  = new DigitalOut(gate);
+  time  = tm;       // 積算時間(ms)
+
+  *CK = 0;
+  *Gate = 0;
+  *Range= 1;    // 高感度モード
+  setWhite();
+}
+
+void ColorSensor::setWhite()
+{
+  unsigned short RGB[3];
+  getRGB(RGB);
+  R = RGB[0];
+  G = RGB[1];
+  B = RGB[2];
+}
+
+ColorSensor::ColorSensor(void) {
+  unsigned short RGB[3];
+  double R, G, B, r,g,b;
+
+  Dout  = new DigitalIn(p5);    // Dout
+  Range = new DigitalOut(p6);   // Range
+  CK    = new DigitalOut(p7);   // CK
+  Gate  = new DigitalOut(p8);   // Gate
+
+  *CK   = 0; //ck
+  *Gate = 0; //gate
+  *Range = 1;  // 高感度モード range
+  time = 10;   // 10ms
+  setWhite();
+}
+
+void ColorSensor::getRGB(unsigned short RGB[])
+{
+    unsigned short i, j, coldata;
+
+     *Gate = 1; 
+     wait_ms(time);      // 積算時間
+     *Gate  = 0; 
+     for(i=0; i<3; i++) {
+       coldata=0;
+       for(j=0; j<12; j++) {
+         *CK = 1; 
+         wait_us(1);
+         coldata>>=1;
+         if(*Dout) coldata|=0x800;
+         *CK = 0;
+         wait_us(1);
+      }
+      RGB[i]=coldata;
+    }
+}
+
+void ColorSensor::getRGB(
+  unsigned& R, unsigned& G, unsigned& B)
+{
+    unsigned i, j, coldata;
+
+     *Gate = 1; 
+     wait_ms(time);       //  積算時間
+     *Gate = 0;
+     for(i=0; i<3; i++) {
+       coldata=0;
+       for(j=0; j<12; j++) {
+         *CK = 1;
+         wait_us(1);
+         coldata>>=1;
+         if(*Dout) coldata|=0x800;
+         *CK = 0;
+         wait_us(1);
+       }
+       switch(i) {
+        case 0: R=coldata; break;
+        case 1: G=coldata; break;
+        case 2: B=coldata; break;
+      }
+    }
+  }
+
+  // 比率を%で返す
+unsigned ColorSensor::checkRGB(
+  unsigned& R, unsigned& G, unsigned& B)
+{
+     unsigned i, j, coldata, I;
+
+     *Gate = 1; 
+     wait_ms(time);      //  積算時間
+     *Gate = 0;
+     for(i=0; i<3; i++) {
+       coldata=0;
+       for(j=0; j<12; j++) {
+         *CK = 1; 
+         wait_us(1);
+         coldata>>=1;
+         if(*Dout) coldata|=0x800;
+         *CK = false;
+         wait_us(1);
+       }
+       switch(i) {
+        case 0: R=coldata; break;
+        case 1: G=coldata; break;
+        case 2: B=coldata; break;
+      }
+    }
+    I = R + G + B;
+    R = R*100/I;
+    G = G*100/I;
+    B = B*100/I;
+    return I;
+}
+
+int ColorSensor::judge()
+{
+  unsigned short RGB[3];
+  double r,g,b;
+  *CK   = 0; //ck
+  *Gate = 0; //gate
+  *Range = 1;  // 高感度モード range
+
+  getRGB(RGB);
+  r = RGB[0]*100/R;
+  g = RGB[1]*100/G;
+  b = RGB[2]*100/B;
+  if(r > g) {
+       if(r > b*coef) return RED;
+       else if(b > r*coef) return GREEN;
+  }
+   else {
+     if(g > b*coef) return GREEN;
+     else if(b > g*coef) return BLUE;
+   }
+  return OTHER;
+}
\ No newline at end of file
diff -r 000000000000 -r 7269a89e1d57 ColorSensor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ColorSensor.h	Thu Aug 29 13:50:59 2013 +0000
@@ -0,0 +1,23 @@
+#include "mbed.h"
+#define RED   0
+#define GREEN 1
+#define BLUE  2
+#define OTHER 3
+class ColorSensor
+{
+  private:
+    DigitalIn *Dout;
+    DigitalOut *Range;
+    DigitalOut *CK;
+    DigitalOut *Gate;
+    int time;
+    double R, G, B;
+  public:
+    ColorSensor(PinName Dout, PinName Range, PinName CK, PinName Gate, int time);
+    ColorSensor(void);
+    void getRGB(unsigned short RGB[]);
+    void getRGB(unsigned& R, unsigned& G, unsigned& B);
+    unsigned checkRGB(unsigned& R, unsigned& G, unsigned& B);
+    int judge();
+    void setWhite();
+};
\ No newline at end of file
diff -r 000000000000 -r 7269a89e1d57 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Aug 29 13:50:59 2013 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "ColorSensor.h"
+
+ColorSensor color(p17, p18, p19, p20, 10);
+//ColorSensor color = ColorSensor();
+Serial pc(USBTX, USBRX); // tx, rx
+
+int main()
+{
+  unsigned R, G, B;
+  //pc.baud(115200);
+  while(1)
+  {
+    color.getRGB(R, G, B);
+    pc.printf("%4d %4d %4d\n", R, G, B);
+    /*
+    switch(color.judge()){
+      case RED: pc.printf("RED\n"); break;
+      case GREEN: pc.printf("GREEN\n"); break;
+      case BLUE: pc.printf("BLUE\n"); break;
+      default: break;
+    }
+    */
+    wait_ms(500);
+  }
+}
\ No newline at end of file
diff -r 000000000000 -r 7269a89e1d57 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Aug 29 13:50:59 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17
\ No newline at end of file