Controller for smartbot and drawbot

Dependencies:   mbed

Committer:
theschrade54
Date:
Fri Dec 14 05:03:08 2012 +0000
Revision:
0:43ba2b4b613b
Etch Sketch Controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
theschrade54 0:43ba2b4b613b 1 #include "mbed.h"
theschrade54 0:43ba2b4b613b 2 #include "ADXL345.h"
theschrade54 0:43ba2b4b613b 3
theschrade54 0:43ba2b4b613b 4 DigitalOut mbedled1(LED1); // signals running
theschrade54 0:43ba2b4b613b 5 DigitalOut mbedled2(LED2); // signals shake warning/detected
theschrade54 0:43ba2b4b613b 6 DigitalOut mbedled3(LED2); // signals +/- y
theschrade54 0:43ba2b4b613b 7 DigitalOut mbedled4(LED2); // signlas +/- x
theschrade54 0:43ba2b4b613b 8
theschrade54 0:43ba2b4b613b 9 PwmOut IRLED(p21);
theschrade54 0:43ba2b4b613b 10
theschrade54 0:43ba2b4b613b 11 Serial pc(USBTX, USBRX); //for debugging
theschrade54 0:43ba2b4b613b 12 Serial drawbot(p13, p14); //change to whatever send method we use
theschrade54 0:43ba2b4b613b 13
theschrade54 0:43ba2b4b613b 14 AnalogIn rightknob(p15);
theschrade54 0:43ba2b4b613b 15 AnalogIn leftknob(p16);
theschrade54 0:43ba2b4b613b 16
theschrade54 0:43ba2b4b613b 17 ADXL345 acc(p5, p6, p7, p8);
theschrade54 0:43ba2b4b613b 18
theschrade54 0:43ba2b4b613b 19 //command vars
theschrade54 0:43ba2b4b613b 20 float rightval, leftval, rightprev, leftprev = 0;
theschrade54 0:43ba2b4b613b 21 int rightdif, leftdif, righttotal, lefttotal = 0;
theschrade54 0:43ba2b4b613b 22 char command = 0; // bit0 = 1 if shake; bit7 = 0 if left, 1 right; bit6 = 0 if fwd, 1 rev; bit 1-5 = magnitude; for drawbot
theschrade54 0:43ba2b4b613b 23 //or bit0 = 1 if shake; bit7 = 0 if y, 1 if x; bit6 = 0 if pos, 1 if neg for smartbot
theschrade54 0:43ba2b4b613b 24
theschrade54 0:43ba2b4b613b 25 //accel and shake vars
theschrade54 0:43ba2b4b613b 26 int z,x,y,zprev,xprev,yprev = 0;
theschrade54 0:43ba2b4b613b 27 int readings[3];
theschrade54 0:43ba2b4b613b 28 int shakeweight = 0;
theschrade54 0:43ba2b4b613b 29
theschrade54 0:43ba2b4b613b 30 int main()
theschrade54 0:43ba2b4b613b 31 {
theschrade54 0:43ba2b4b613b 32 //Initialize accelerometer
theschrade54 0:43ba2b4b613b 33 acc.setPowerControl(0x00);
theschrade54 0:43ba2b4b613b 34 acc.setDataFormatControl(0x0B);
theschrade54 0:43ba2b4b613b 35 acc.setDataRate(ADXL345_3200HZ);
theschrade54 0:43ba2b4b613b 36 acc.setPowerControl(0x08);
theschrade54 0:43ba2b4b613b 37 //Initialize accelerometer readings
theschrade54 0:43ba2b4b613b 38 acc.getOutput(readings);
theschrade54 0:43ba2b4b613b 39 x = (int16_t)readings[0];
theschrade54 0:43ba2b4b613b 40 y = (int16_t)readings[1];
theschrade54 0:43ba2b4b613b 41 z = (int16_t)readings[2];
theschrade54 0:43ba2b4b613b 42
theschrade54 0:43ba2b4b613b 43 //Initialize LEDs & Knobs
theschrade54 0:43ba2b4b613b 44 mbedled1 = 1;
theschrade54 0:43ba2b4b613b 45 mbedled2 = 0;
theschrade54 0:43ba2b4b613b 46 mbedled3 = 0;
theschrade54 0:43ba2b4b613b 47 mbedled4 = 0;
theschrade54 0:43ba2b4b613b 48 rightval = rightknob;
theschrade54 0:43ba2b4b613b 49 leftval = leftknob;
theschrade54 0:43ba2b4b613b 50
theschrade54 0:43ba2b4b613b 51 //IR Setup
theschrade54 0:43ba2b4b613b 52 IRLED.period(1.0/38000.0);
theschrade54 0:43ba2b4b613b 53 IRLED = 0.5;
theschrade54 0:43ba2b4b613b 54 //Drive IR LED data pin with 38Khz PWM Carrier
theschrade54 0:43ba2b4b613b 55 //Drive IR LED gnd pin with serial TX
theschrade54 0:43ba2b4b613b 56 drawbot.baud(2400);
theschrade54 0:43ba2b4b613b 57
theschrade54 0:43ba2b4b613b 58 while(1) {
theschrade54 0:43ba2b4b613b 59
theschrade54 0:43ba2b4b613b 60 //Check for Shake
theschrade54 0:43ba2b4b613b 61 xprev = x;
theschrade54 0:43ba2b4b613b 62 yprev = y;
theschrade54 0:43ba2b4b613b 63 zprev = z;
theschrade54 0:43ba2b4b613b 64 acc.getOutput(readings);
theschrade54 0:43ba2b4b613b 65 x = (int16_t)readings[0];
theschrade54 0:43ba2b4b613b 66 y = (int16_t)readings[1];
theschrade54 0:43ba2b4b613b 67 z = (int16_t)readings[2];
theschrade54 0:43ba2b4b613b 68 if (((abs(xprev - x) > 150) || (abs(yprev - y) > 150) || (abs(zprev - z) > 150)) && (shakeweight == 0)){
theschrade54 0:43ba2b4b613b 69 mbedled2 = 1; //Light LED2
theschrade54 0:43ba2b4b613b 70 shakeweight = 1;} //activate shake wait
theschrade54 0:43ba2b4b613b 71 if (shakeweight > 0) { shakeweight++;} //if shake wait activated, increment
theschrade54 0:43ba2b4b613b 72 if (shakeweight == 8){ //until 10, then check for second shake
theschrade54 0:43ba2b4b613b 73 mbedled2 = 0;
theschrade54 0:43ba2b4b613b 74 if ((abs(xprev - x) > 150) || (abs(yprev - y) > 150) || (abs(zprev - z) > 150)) {
theschrade54 0:43ba2b4b613b 75 mbedled2 = 1; //Light LED2
theschrade54 0:43ba2b4b613b 76 command = 1; //0x01
theschrade54 0:43ba2b4b613b 77 drawbot.putc(command);
theschrade54 0:43ba2b4b613b 78 pc.printf("Shake Detected! Command is %x. Waiting. \n", command); //Send shake signal via Xbee or IR to drawbot
theschrade54 0:43ba2b4b613b 79 wait(5); //Wait long enough for worst case new page set up unless drawbot can talk back
theschrade54 0:43ba2b4b613b 80 mbedled2 = 0;
theschrade54 0:43ba2b4b613b 81 }
theschrade54 0:43ba2b4b613b 82 shakeweight = 0;
theschrade54 0:43ba2b4b613b 83 }
theschrade54 0:43ba2b4b613b 84 command = 0;
theschrade54 0:43ba2b4b613b 85 //Detect Knobs
theschrade54 0:43ba2b4b613b 86 rightprev = rightval;
theschrade54 0:43ba2b4b613b 87 leftprev = leftval;
theschrade54 0:43ba2b4b613b 88 rightval = rightknob;
theschrade54 0:43ba2b4b613b 89 leftval = leftknob;
theschrade54 0:43ba2b4b613b 90 rightdif = 500*rightval - 500*rightprev;
theschrade54 0:43ba2b4b613b 91 leftdif = 500*leftprev - 500*leftval;
theschrade54 0:43ba2b4b613b 92 if ((abs(rightdif) > 1) && (abs(rightdif) < 63)) {
theschrade54 0:43ba2b4b613b 93 mbedled3 = 1;
theschrade54 0:43ba2b4b613b 94 command = 128; //0x80
theschrade54 0:43ba2b4b613b 95 righttotal+=rightdif;
theschrade54 0:43ba2b4b613b 96 if (rightdif < 0) {
theschrade54 0:43ba2b4b613b 97 rightdif = -rightdif;
theschrade54 0:43ba2b4b613b 98 command = 192;}//0xC0
theschrade54 0:43ba2b4b613b 99 command = rightdif | command;
theschrade54 0:43ba2b4b613b 100 pc.printf("RightDif: %d Tot: %d Comm: %x \n", rightdif, righttotal, command); //send right move command and val
theschrade54 0:43ba2b4b613b 101 drawbot.putc(command);
theschrade54 0:43ba2b4b613b 102 mbedled3 = 0;
theschrade54 0:43ba2b4b613b 103 }
theschrade54 0:43ba2b4b613b 104 if ((abs(leftdif) > 1) && (abs(leftdif) < 63)) {
theschrade54 0:43ba2b4b613b 105 mbedled4 = 1;
theschrade54 0:43ba2b4b613b 106 command = 0; //0x00
theschrade54 0:43ba2b4b613b 107 lefttotal+=leftdif;
theschrade54 0:43ba2b4b613b 108 if (leftdif < 0) {
theschrade54 0:43ba2b4b613b 109 leftdif = -leftdif;
theschrade54 0:43ba2b4b613b 110 command = 64;}//0x40
theschrade54 0:43ba2b4b613b 111 command = leftdif | command;
theschrade54 0:43ba2b4b613b 112 pc.printf("LeftDif: %d Tot: %d Comm: %x \n", leftdif, lefttotal, command); //send left move command and val
theschrade54 0:43ba2b4b613b 113 drawbot.putc(command);
theschrade54 0:43ba2b4b613b 114 mbedled4 = 0;
theschrade54 0:43ba2b4b613b 115 }
theschrade54 0:43ba2b4b613b 116 command = 0;
theschrade54 0:43ba2b4b613b 117 wait(.1);
theschrade54 0:43ba2b4b613b 118 //Print State
theschrade54 0:43ba2b4b613b 119 //pc.printf("RightVal: %F, LeftVal: %F", rightval, leftval);
theschrade54 0:43ba2b4b613b 120 //pc.printf("accel: %i, %i, %i\n", x, y, z);
theschrade54 0:43ba2b4b613b 121 }
theschrade54 0:43ba2b4b613b 122 }