Adam Resnick / Mbed 2 deprecated CNCAirbrushCode

Dependencies:   StepperMotors mbed CNCAirbrush Servo

Committer:
stretch
Date:
Fri May 04 06:25:59 2012 +0000
Revision:
1:f3fda065e614
Parent:
0:ba40c5f30cf0

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stretch 0:ba40c5f30cf0 1 #include "mbed.h"
stretch 0:ba40c5f30cf0 2 #include "Wifly.h"
stretch 0:ba40c5f30cf0 3 #include "Bitmap.h"
stretch 0:ba40c5f30cf0 4 #include "Stepper.h"
stretch 0:ba40c5f30cf0 5 #include "LinearMotion.h"
stretch 0:ba40c5f30cf0 6 #include "Servo.h"
stretch 0:ba40c5f30cf0 7
stretch 0:ba40c5f30cf0 8 #define PAN_RANGE 0.0009
stretch 0:ba40c5f30cf0 9 #define PAN_DEGREE 90
stretch 0:ba40c5f30cf0 10 #define PAN_CENTER 0.0002
stretch 1:f3fda065e614 11 #define STEPS_PER_PIXEL_DEFAULT 600
stretch 1:f3fda065e614 12 #define INITIAL_DELAY_DEFAULT 1200
stretch 1:f3fda065e614 13 #define STEP_BUFFER_DEFAULT 4
stretch 1:f3fda065e614 14 #define DEFAULT_FEEDRATE 200
stretch 0:ba40c5f30cf0 15
stretch 0:ba40c5f30cf0 16 LocalFileSystem local("local");
stretch 0:ba40c5f30cf0 17 Serial pc(USBTX, USBRX);
stretch 0:ba40c5f30cf0 18 DigitalOut led(LED1);
stretch 0:ba40c5f30cf0 19 DigitalOut solenoid(p29);
stretch 0:ba40c5f30cf0 20 Wifly wifi(p9, p10, p8);
stretch 0:ba40c5f30cf0 21 Command * cmd;
stretch 0:ba40c5f30cf0 22 Bitmap bmp;
stretch 0:ba40c5f30cf0 23 bool paused = false;
stretch 0:ba40c5f30cf0 24 bool stopped = false;
stretch 0:ba40c5f30cf0 25
stretch 0:ba40c5f30cf0 26 Stepper x(p6,p5,p14);
stretch 0:ba40c5f30cf0 27 Stepper y(p24,p27,p28);
stretch 0:ba40c5f30cf0 28 Stepper z(p22,p23,p21);
stretch 0:ba40c5f30cf0 29 LinearMotion linearMotion;
stretch 0:ba40c5f30cf0 30 Servo servoTilt(p25);
stretch 0:ba40c5f30cf0 31 Servo servoPan(p26);
stretch 0:ba40c5f30cf0 32
stretch 0:ba40c5f30cf0 33
stretch 0:ba40c5f30cf0 34
stretch 0:ba40c5f30cf0 35 /*1B:Blue
stretch 0:ba40c5f30cf0 36 1A:Red
stretch 0:ba40c5f30cf0 37 2A:Black
stretch 0:ba40c5f30cf0 38 2B:Green*/
stretch 0:ba40c5f30cf0 39
stretch 0:ba40c5f30cf0 40 void savePosition (Vector position, float pan, float tilt) {
stretch 0:ba40c5f30cf0 41 FILE * fp = fopen("/local/hrmm.settings", "w");
stretch 0:ba40c5f30cf0 42 if (fp != NULL) {
stretch 0:ba40c5f30cf0 43 fwrite(&position.x, sizeof(long), 1, fp);
stretch 0:ba40c5f30cf0 44 fwrite(&position.y, sizeof(long), 1, fp);
stretch 0:ba40c5f30cf0 45 fwrite(&position.z, sizeof(long), 1, fp);
stretch 0:ba40c5f30cf0 46 fwrite(&pan, sizeof(float), 1, fp);
stretch 0:ba40c5f30cf0 47 fwrite(&tilt, sizeof(float), 1, fp);
stretch 0:ba40c5f30cf0 48 fclose(fp);
stretch 0:ba40c5f30cf0 49 }
stretch 0:ba40c5f30cf0 50 }
stretch 0:ba40c5f30cf0 51
stretch 0:ba40c5f30cf0 52
stretch 0:ba40c5f30cf0 53 void pause(Command * c) {
stretch 0:ba40c5f30cf0 54 paused = !paused;
stretch 0:ba40c5f30cf0 55 printf("paused:%d\n\r", paused);
stretch 0:ba40c5f30cf0 56 }
stretch 0:ba40c5f30cf0 57
stretch 0:ba40c5f30cf0 58 void stop(Command * c) {
stretch 0:ba40c5f30cf0 59 stopped = true;
stretch 0:ba40c5f30cf0 60 paused = false;
stretch 0:ba40c5f30cf0 61 }
stretch 0:ba40c5f30cf0 62
stretch 0:ba40c5f30cf0 63 int main() {
stretch 0:ba40c5f30cf0 64 servoPan.calibrate(PAN_RANGE, PAN_DEGREE, PAN_CENTER);
stretch 0:ba40c5f30cf0 65 float pan = .5;
stretch 0:ba40c5f30cf0 66 float tilt = 0;
stretch 0:ba40c5f30cf0 67 Vector pos = {0, 0, 0};
stretch 0:ba40c5f30cf0 68 FILE * fp = fopen("/local/hrmm.settings", "r");
stretch 0:ba40c5f30cf0 69 if (fp != NULL) {
stretch 0:ba40c5f30cf0 70 fread(&pos.x, sizeof(long), 1, fp);
stretch 0:ba40c5f30cf0 71 fread(&pos.y, sizeof(long), 1, fp);
stretch 0:ba40c5f30cf0 72 fread(&pos.z, sizeof(long), 1, fp);
stretch 0:ba40c5f30cf0 73 fread(&pan, sizeof(float), 1, fp);
stretch 0:ba40c5f30cf0 74 fread(&tilt, sizeof(float), 1, fp);
stretch 0:ba40c5f30cf0 75 fclose(fp);
stretch 0:ba40c5f30cf0 76 }
stretch 0:ba40c5f30cf0 77 x.setPosition(pos.x);
stretch 0:ba40c5f30cf0 78 y.setPosition(pos.y);
stretch 0:ba40c5f30cf0 79 z.setPosition(pos.z);
stretch 0:ba40c5f30cf0 80 servoPan = pan;
stretch 0:ba40c5f30cf0 81 servoTilt = tilt;
stretch 0:ba40c5f30cf0 82 solenoid = 0;
stretch 0:ba40c5f30cf0 83 pc.baud(460800);
stretch 0:ba40c5f30cf0 84 printf("Test Airbrush!\r\n");
stretch 0:ba40c5f30cf0 85 linearMotion.setStepMotors(&x, &y, &z, &solenoid, &paused, &stopped);
stretch 1:f3fda065e614 86 linearMotion.updateSettings(STEPS_PER_PIXEL_DEFAULT, INITIAL_DELAY_DEFAULT, STEP_BUFFER_DEFAULT);
stretch 0:ba40c5f30cf0 87 wifi.createAdhocNetwork();
stretch 0:ba40c5f30cf0 88 wifi.attach_interrupt(0x09, pause, 0);
stretch 0:ba40c5f30cf0 89 wifi.attach_interrupt(0x0a, stop, 1);
stretch 0:ba40c5f30cf0 90 led = 1;
stretch 1:f3fda065e614 91 long paintingFeedrate = DEFAULT_FEEDRATE;
stretch 0:ba40c5f30cf0 92 while (1) {
stretch 0:ba40c5f30cf0 93 while (pc.readable()) {
stretch 0:ba40c5f30cf0 94 wifi.putc(pc.getc());
stretch 0:ba40c5f30cf0 95 }
stretch 0:ba40c5f30cf0 96 while (wifi.readable()) {
stretch 0:ba40c5f30cf0 97 pc.putc(wifi.getc());
stretch 0:ba40c5f30cf0 98 }
stretch 0:ba40c5f30cf0 99 if (wifi.hasCmd()) {
stretch 0:ba40c5f30cf0 100 cmd = wifi.getCmd();
stretch 0:ba40c5f30cf0 101 switch (cmd->cmd) {
stretch 0:ba40c5f30cf0 102 case 0x00: { //relative motion
stretch 0:ba40c5f30cf0 103 pc.printf("Move to relative coordinates x: %d, y:%d, z:%d\n\r", cmd->l[0], cmd->l[1], cmd->l[2]);
stretch 0:ba40c5f30cf0 104 Vector finalPos = {cmd->l[0],
stretch 0:ba40c5f30cf0 105 cmd->l[1],
stretch 0:ba40c5f30cf0 106 cmd->l[2]};
stretch 0:ba40c5f30cf0 107 linearMotion.interpolate(finalPos,1);
stretch 0:ba40c5f30cf0 108 break;
stretch 0:ba40c5f30cf0 109 }
stretch 0:ba40c5f30cf0 110 case 0x01: { //move to specified global coordinates
stretch 0:ba40c5f30cf0 111 Vector relative = {cmd->l[0] - x.getPosition(),
stretch 0:ba40c5f30cf0 112 cmd->l[1] - y.getPosition(),
stretch 0:ba40c5f30cf0 113 cmd->l[2] - z.getPosition()};
stretch 0:ba40c5f30cf0 114 pc.printf("Move to global coordinates x:%d, y:%d, z:%d, Tilt:%f, Pan:%f\n\r",cmd->l[0], cmd->l[1], cmd->l[2], z.getPosition(), tilt, pan);
stretch 0:ba40c5f30cf0 115 linearMotion.interpolate(relative, 10);
stretch 0:ba40c5f30cf0 116 break;
stretch 0:ba40c5f30cf0 117 }
stretch 0:ba40c5f30cf0 118 case 0x02: { //pretend paint an image
stretch 0:ba40c5f30cf0 119 Vector baseVector = {cmd->l[0],
stretch 0:ba40c5f30cf0 120 cmd->l[1],
stretch 0:ba40c5f30cf0 121 cmd->l[2]};
stretch 0:ba40c5f30cf0 122
stretch 0:ba40c5f30cf0 123 Vector heightVector = {cmd->l[3],
stretch 0:ba40c5f30cf0 124 cmd->l[4],
stretch 0:ba40c5f30cf0 125 cmd->l[5]};
stretch 0:ba40c5f30cf0 126 pc.printf("Pretend to paint an image with corners at (x:%d, y:%d, z:%d), (x:%d, y:%d, z:%d) and (x:%d, y:%d, z:%d)\n\r", x.getPosition(), y.getPosition(), z.getPosition(), baseVector.x, baseVector.y, baseVector.z, heightVector.x, heightVector.y, heightVector.z);
stretch 0:ba40c5f30cf0 127 linearMotion.interpolateSquare(baseVector, heightVector, 10, false);
stretch 0:ba40c5f30cf0 128 break;
stretch 0:ba40c5f30cf0 129 }
stretch 0:ba40c5f30cf0 130 case 0x03: { //paint an image
stretch 0:ba40c5f30cf0 131 Vector baseVector = {cmd->l[0],
stretch 0:ba40c5f30cf0 132 cmd->l[1],
stretch 0:ba40c5f30cf0 133 cmd->l[2]};
stretch 0:ba40c5f30cf0 134
stretch 0:ba40c5f30cf0 135 Vector heightVector = {cmd->l[3],
stretch 0:ba40c5f30cf0 136 cmd->l[4],
stretch 0:ba40c5f30cf0 137 cmd->l[5]};
stretch 0:ba40c5f30cf0 138 pc.printf("Paint the image with corners at (x:%d, y:%d, z:%d), (x:%d, y:%d, z:%d) and (x:%d, y:%d, z:%d)\n\r", x.getPosition(), y.getPosition(), z.getPosition(),baseVector.x,baseVector.y,baseVector.z,heightVector.x,heightVector.y,heightVector.z);
stretch 1:f3fda065e614 139 linearMotion.interpolateSquare(baseVector,heightVector,paintingFeedrate,true);
stretch 0:ba40c5f30cf0 140 break;
stretch 0:ba40c5f30cf0 141 }
stretch 0:ba40c5f30cf0 142 case 0x04: { //change the pan
stretch 0:ba40c5f30cf0 143 pan = cmd->f[0];
stretch 0:ba40c5f30cf0 144 servoPan = pan;
stretch 0:ba40c5f30cf0 145 pc.printf("Change the pan to %f\n\r",pan);
stretch 0:ba40c5f30cf0 146 break;
stretch 0:ba40c5f30cf0 147 }
stretch 0:ba40c5f30cf0 148 case 0x05: { //change the tilt
stretch 0:ba40c5f30cf0 149 tilt = cmd->f[0];
stretch 0:ba40c5f30cf0 150 servoTilt = tilt;
stretch 0:ba40c5f30cf0 151 pc.printf("Change the tilt to %f\n\r",tilt);
stretch 0:ba40c5f30cf0 152 break;
stretch 0:ba40c5f30cf0 153 }
stretch 1:f3fda065e614 154 case 0x06: {
stretch 1:f3fda065e614 155 linearMotion.updateSettings(cmd->l[0], cmd->l[1], cmd->l[2]);
stretch 1:f3fda065e614 156 paintingFeedrate = cmd->l[3];
stretch 1:f3fda065e614 157 break;
stretch 1:f3fda065e614 158
stretch 1:f3fda065e614 159 }
stretch 0:ba40c5f30cf0 160 case 0x07: { //turn on the solenoid
stretch 0:ba40c5f30cf0 161 long t = cmd->l[0];
stretch 0:ba40c5f30cf0 162 if (t)
stretch 0:ba40c5f30cf0 163 solenoid = 1;
stretch 0:ba40c5f30cf0 164 else
stretch 0:ba40c5f30cf0 165 solenoid = 0;
stretch 0:ba40c5f30cf0 166 break;
stretch 0:ba40c5f30cf0 167 }
stretch 0:ba40c5f30cf0 168 case 0x09: { // We accidentally got to a pause state
stretch 0:ba40c5f30cf0 169 paused = false;
stretch 0:ba40c5f30cf0 170 break;
stretch 0:ba40c5f30cf0 171 }
stretch 0:ba40c5f30cf0 172 default: {
stretch 0:ba40c5f30cf0 173 pc.printf("You should probably do something with case %x\n\r", cmd->cmd);
stretch 0:ba40c5f30cf0 174 break;
stretch 0:ba40c5f30cf0 175 }
stretch 0:ba40c5f30cf0 176 }
stretch 0:ba40c5f30cf0 177 stopped = false;
stretch 0:ba40c5f30cf0 178 Vector globalPos = {x.getPosition(), y.getPosition(), z.getPosition()};
stretch 0:ba40c5f30cf0 179 savePosition(globalPos, pan, tilt);
stretch 0:ba40c5f30cf0 180 cmd->cmd = 8;
stretch 0:ba40c5f30cf0 181 cmd->l[0] = globalPos.x;
stretch 0:ba40c5f30cf0 182 cmd->l[1] = globalPos.y;
stretch 0:ba40c5f30cf0 183 cmd->l[2] = globalPos.z;
stretch 0:ba40c5f30cf0 184 cmd->f[3] = pan;
stretch 0:ba40c5f30cf0 185 cmd->f[4] = tilt;
stretch 0:ba40c5f30cf0 186 wifi.sendCmd(cmd);
stretch 0:ba40c5f30cf0 187 }
stretch 0:ba40c5f30cf0 188 }
stretch 0:ba40c5f30cf0 189 }