Gripper and Color sensor basic functionality testing.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
z_g13
Date:
Sat Mar 26 23:30:29 2016 +0000
Commit message:
3/26/2016 - ZG;

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
driver.cpp Show annotated file Show diff for this revision Revisions of this file
gripper.cpp Show annotated file Show diff for this revision Revisions of this file
gripper.h 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 b30091b30223 ColorSensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ColorSensor.cpp	Sat Mar 26 23:30:29 2016 +0000
@@ -0,0 +1,162 @@
+#include "ColorSensor.h"
+#include "mbed.h"
+ 
+//sda and scl are for your i2c pins
+ColorSensor::ColorSensor(PinName sda, PinName scl) : i2c(sda, scl), gain(4), scale(1)
+{
+    //power on device and ADCs
+    i2c.write(ADDR, CTRL, 1);
+    i2c.write(ADDR, CTRL_INIT, 1);
+    
+    //intial gain is 4x, scale is 1x
+    i2c.write(ADDR, GAIN, 1);
+    i2c.write(ADDR, GAIN_INIT, 1);
+}
+ 
+//returns red value
+int ColorSensor::getR()
+{
+    int red;
+    char data;
+    
+    i2c.write(ADDR, RED_LO, 1);
+    i2c.read(ADDR, &data, 1);
+    red = data;
+    i2c.write(ADDR, RED_HI, 1);
+    i2c.read(ADDR, &data, 1);
+    red += 256*data;
+    
+    return red;
+}
+ 
+//returns green value
+int ColorSensor::getG()
+{
+    int grn;
+    char data;
+    
+    i2c.write(ADDR, GRN_LO, 1);
+    i2c.read(ADDR, &data, 1);
+    grn = data;
+    i2c.write(ADDR, GRN_HI, 1);
+    i2c.read(ADDR, &data, 1);
+    grn += 256*data;
+    
+    return grn;
+}
+ 
+//returns blue value
+int ColorSensor::getB()
+{
+    int blu;
+    char data;
+    
+    i2c.write(ADDR, BLU_LO, 1);
+    i2c.read(ADDR, &data, 1);
+    blu = data;
+    i2c.write(ADDR, BLU_HI, 1);
+    i2c.read(ADDR, &data, 1);
+    blu += 256*data;
+    
+    return blu;
+}
+ 
+//returns clear value
+int ColorSensor::getC()
+{
+    int clr;
+    char data;
+    
+    i2c.write(ADDR, CLR_LO, 1);
+    i2c.read(ADDR, &data, 1);
+    clr = data;
+    i2c.write(ADDR, CLR_HI, 1);
+    i2c.read(ADDR, &data, 1);
+    clr += 256*data;
+    
+    return clr;
+}
+ 
+//sets ADC gain
+//valid values 1, 4, 16, 64
+void ColorSensor::setGain(int g)
+{       
+    char newGain;
+    
+    switch(g)
+    {
+        case 1: newGain = 0x00;
+            break;
+        case 4: newGain = 0x10;
+            break;
+        case 16: newGain = 0x20;
+            break;
+        case 64: newGain = 0x30;
+            break;
+        default: return;
+    }
+    
+    gain = g;
+    
+    switch(scale)
+    {
+        case 1: newGain += 0x00;
+            break;
+        case 2: newGain += 0x01;
+            break;
+        case 4: newGain += 0x02;
+            break;
+        case 8: newGain += 0x03;
+            break;
+        case 16: newGain += 0x04;
+            break;
+        case 32: newGain += 0x05;
+            break;
+        case 64: newGain += 0x06;
+    }
+    
+    i2c.write(ADDR, GAIN, 1);
+    i2c.write(ADDR, &newGain, 1);
+}
+ 
+//sets divides ADC sensitivity by scaler
+//valid values 1, 2, 4, 8, 16, 32, 64
+void ColorSensor::setScaler(int s)
+{
+    char newGain;
+    
+    switch(s)
+    {
+        case 1: newGain = 0x00;
+            break;
+        case 2: newGain = 0x01;
+            break;
+        case 4: newGain = 0x02;
+            break;
+        case 8: newGain = 0x03;
+            break;
+        case 16: newGain = 0x04;
+            break;
+        case 32: newGain = 0x05;
+            break;
+        case 64: newGain = 0x06;
+            break;
+        default: return;
+    }
+    
+    scale = s;
+    
+    switch(gain)
+    {
+        case 1: newGain += 0x00;
+            break;
+        case 4: newGain += 0x10;
+            break;
+        case 16: newGain += 0x20;
+            break;
+        case 64: newGain += 0x30;
+    }
+    
+    i2c.write(ADDR, GAIN, 1);
+    i2c.write(ADDR, &newGain, 1);
+}
\ No newline at end of file
diff -r 000000000000 -r b30091b30223 ColorSensor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ColorSensor.h	Sat Mar 26 23:30:29 2016 +0000
@@ -0,0 +1,49 @@
+#ifndef COLORSENSOR_H
+#define COLORSENSOR_H
+ 
+#include "mbed.h"
+ 
+//constants for i2c register communication
+const int ADDR = 0x72;
+const char CTRL_VAL = 0x80;
+const char* const CTRL = &CTRL_VAL;
+const char CTRL_INIT_VAL = 0x03;
+const char* const CTRL_INIT = &CTRL_INIT_VAL;
+const char GAIN_VAL = 0x87;
+const char* const GAIN = &GAIN_VAL;
+const char GAIN_INIT_VAL = 0x10;
+const char* const GAIN_INIT = &GAIN_INIT_VAL;
+const char RED_LO_VAL = 0x92;
+const char* const RED_LO = &RED_LO_VAL;    
+const char RED_HI_VAL = 0x93;
+const char* const RED_HI = &RED_HI_VAL;    
+const char GRN_LO_VAL = 0x90;
+const char* const GRN_LO = &GRN_LO_VAL;    
+const char GRN_HI_VAL = 0x91;
+const char* const GRN_HI = &GRN_HI_VAL;    
+const char BLU_LO_VAL = 0x94;
+const char* const BLU_LO = &BLU_LO_VAL;    
+const char BLU_HI_VAL = 0x95;
+const char* const BLU_HI = &BLU_HI_VAL;    
+const char CLR_LO_VAL = 0x96;
+const char* const CLR_LO = &CLR_LO_VAL;    
+const char CLR_HI_VAL = 0x97;
+const char* const CLR_HI = &CLR_HI_VAL;
+ 
+class ColorSensor
+{
+    public:
+    ColorSensor(PinName sda, PinName scl);
+    int getR();
+    int getG();
+    int getB();
+    int getC();
+    void setGain(int g);
+    void setScaler(int s);
+    
+    private:
+    I2C i2c;
+    int gain;
+    int scale;
+};
+#endif
\ No newline at end of file
diff -r 000000000000 -r b30091b30223 driver.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/driver.cpp	Sat Mar 26 23:30:29 2016 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h"
+#include "gripper.h"
+#include "ColorSensor.h"
+
+Serial vcom(USBTX, USBRX);
+
+float frequency = 50.0f;
+float duty_cycle = 0.50f;
+float period = ( 1.0f / frequency );
+
+int main()
+{
+    vcom.baud(115200);
+    
+    vcom.printf("Hello World!");
+    
+    Gripper gripper(PTB0, PTB1);
+    ColorSensor colorSensor(PTC2, PTC1);
+    
+    /*
+    gripper.grip();
+    wait(1.0);
+    gripper.lift();
+    wait(1.0);
+    gripper.lower();
+    wait(1.0);
+    gripper.release();
+    wait(1.0);
+    */
+    
+    
+    
+    colorSensor.getR();
+    vcom.printf("%i", colorSensor.getR());
+    wait(0.5);
+    
+}
\ No newline at end of file
diff -r 000000000000 -r b30091b30223 gripper.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gripper.cpp	Sat Mar 26 23:30:29 2016 +0000
@@ -0,0 +1,18 @@
+#include "gripper.h"
+#include "mbed.h"
+ 
+Gripper::Gripper(PinName gripPin, PinName wristPin) : gripPwm(gripPin), wristPwm(wristPin)
+{
+    gripPwm.period_us(PWM_PER);
+    wristPwm.period_us(PWM_PER);
+    gripPwm.pulsewidth_us(GRIP_DEF);
+    wristPwm.pulsewidth_us(WRIST_DEF);
+}
+ 
+void Gripper::grip() { gripPwm.pulsewidth_us(GRIP_CLOSE); }
+ 
+void Gripper::release() { gripPwm.pulsewidth_us(GRIP_OPEN); }
+ 
+void Gripper::lift() { wristPwm.pulsewidth_us(WRIST_UP); }
+ 
+void Gripper::lower() { wristPwm.pulsewidth_us(WRIST_DOWN); }
\ No newline at end of file
diff -r 000000000000 -r b30091b30223 gripper.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gripper.h	Sat Mar 26 23:30:29 2016 +0000
@@ -0,0 +1,33 @@
+#ifndef GRIPPER_H
+#define GRIPPER_H
+ 
+#include "mbed.h"
+ 
+//definitions for servo PWM settings in us
+#define GRIP_DEF 1500
+#define GRIP_OPEN 1700
+#define GRIP_CLOSE 1120
+#define WRIST_DEF 1500
+#define WRIST_UP 2000
+#define WRIST_DOWN 1500
+#define PWM_PER 20000
+ 
+class Gripper
+{
+    public:
+    //gripPin is pwm pin connected to gripper servo, wristPin is pwm pin connected to wrist servo
+    Gripper(PinName gripPin, PinName wristPin);
+    //closes gripper to fit target
+    void grip();
+    //fully opens gripper
+    void release();
+    //raises wrist to target angle
+    void lift();
+    //lowers wrist to grabbing angle
+    void lower();
+    
+    private:
+    PwmOut gripPwm;
+    PwmOut wristPwm;
+};
+#endif
\ No newline at end of file
diff -r 000000000000 -r b30091b30223 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Mar 26 23:30:29 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/c0f6e94411f5
\ No newline at end of file