Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: StepperMotors mbed CNCAirbrush Servo
main.cpp@0:ba40c5f30cf0, 2012-04-28 (annotated)
- Committer:
- stretch
- Date:
- Sat Apr 28 21:11:15 2012 +0000
- Revision:
- 0:ba40c5f30cf0
- Child:
- 1:f3fda065e614
Initial Public Release
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:ba40c5f30cf0 | 11 | |
stretch | 0:ba40c5f30cf0 | 12 | LocalFileSystem local("local"); |
stretch | 0:ba40c5f30cf0 | 13 | Serial pc(USBTX, USBRX); |
stretch | 0:ba40c5f30cf0 | 14 | DigitalOut led(LED1); |
stretch | 0:ba40c5f30cf0 | 15 | DigitalOut solenoid(p29); |
stretch | 0:ba40c5f30cf0 | 16 | Wifly wifi(p9, p10, p8); |
stretch | 0:ba40c5f30cf0 | 17 | Command * cmd; |
stretch | 0:ba40c5f30cf0 | 18 | Bitmap bmp; |
stretch | 0:ba40c5f30cf0 | 19 | bool paused = false; |
stretch | 0:ba40c5f30cf0 | 20 | bool stopped = false; |
stretch | 0:ba40c5f30cf0 | 21 | |
stretch | 0:ba40c5f30cf0 | 22 | Stepper x(p6,p5,p14); |
stretch | 0:ba40c5f30cf0 | 23 | Stepper y(p24,p27,p28); |
stretch | 0:ba40c5f30cf0 | 24 | Stepper z(p22,p23,p21); |
stretch | 0:ba40c5f30cf0 | 25 | LinearMotion linearMotion; |
stretch | 0:ba40c5f30cf0 | 26 | Servo servoTilt(p25); |
stretch | 0:ba40c5f30cf0 | 27 | Servo servoPan(p26); |
stretch | 0:ba40c5f30cf0 | 28 | |
stretch | 0:ba40c5f30cf0 | 29 | |
stretch | 0:ba40c5f30cf0 | 30 | |
stretch | 0:ba40c5f30cf0 | 31 | /*1B:Blue |
stretch | 0:ba40c5f30cf0 | 32 | 1A:Red |
stretch | 0:ba40c5f30cf0 | 33 | 2A:Black |
stretch | 0:ba40c5f30cf0 | 34 | 2B:Green*/ |
stretch | 0:ba40c5f30cf0 | 35 | |
stretch | 0:ba40c5f30cf0 | 36 | void savePosition (Vector position, float pan, float tilt) { |
stretch | 0:ba40c5f30cf0 | 37 | FILE * fp = fopen("/local/hrmm.settings", "w"); |
stretch | 0:ba40c5f30cf0 | 38 | if (fp != NULL) { |
stretch | 0:ba40c5f30cf0 | 39 | fwrite(&position.x, sizeof(long), 1, fp); |
stretch | 0:ba40c5f30cf0 | 40 | fwrite(&position.y, sizeof(long), 1, fp); |
stretch | 0:ba40c5f30cf0 | 41 | fwrite(&position.z, sizeof(long), 1, fp); |
stretch | 0:ba40c5f30cf0 | 42 | fwrite(&pan, sizeof(float), 1, fp); |
stretch | 0:ba40c5f30cf0 | 43 | fwrite(&tilt, sizeof(float), 1, fp); |
stretch | 0:ba40c5f30cf0 | 44 | fclose(fp); |
stretch | 0:ba40c5f30cf0 | 45 | } |
stretch | 0:ba40c5f30cf0 | 46 | } |
stretch | 0:ba40c5f30cf0 | 47 | |
stretch | 0:ba40c5f30cf0 | 48 | |
stretch | 0:ba40c5f30cf0 | 49 | void pause(Command * c) { |
stretch | 0:ba40c5f30cf0 | 50 | paused = !paused; |
stretch | 0:ba40c5f30cf0 | 51 | printf("paused:%d\n\r", paused); |
stretch | 0:ba40c5f30cf0 | 52 | } |
stretch | 0:ba40c5f30cf0 | 53 | |
stretch | 0:ba40c5f30cf0 | 54 | void stop(Command * c) { |
stretch | 0:ba40c5f30cf0 | 55 | stopped = true; |
stretch | 0:ba40c5f30cf0 | 56 | paused = false; |
stretch | 0:ba40c5f30cf0 | 57 | } |
stretch | 0:ba40c5f30cf0 | 58 | |
stretch | 0:ba40c5f30cf0 | 59 | int main() { |
stretch | 0:ba40c5f30cf0 | 60 | servoPan.calibrate(PAN_RANGE, PAN_DEGREE, PAN_CENTER); |
stretch | 0:ba40c5f30cf0 | 61 | float pan = .5; |
stretch | 0:ba40c5f30cf0 | 62 | float tilt = 0; |
stretch | 0:ba40c5f30cf0 | 63 | Vector pos = {0, 0, 0}; |
stretch | 0:ba40c5f30cf0 | 64 | FILE * fp = fopen("/local/hrmm.settings", "r"); |
stretch | 0:ba40c5f30cf0 | 65 | if (fp != NULL) { |
stretch | 0:ba40c5f30cf0 | 66 | fread(&pos.x, sizeof(long), 1, fp); |
stretch | 0:ba40c5f30cf0 | 67 | fread(&pos.y, sizeof(long), 1, fp); |
stretch | 0:ba40c5f30cf0 | 68 | fread(&pos.z, sizeof(long), 1, fp); |
stretch | 0:ba40c5f30cf0 | 69 | fread(&pan, sizeof(float), 1, fp); |
stretch | 0:ba40c5f30cf0 | 70 | fread(&tilt, sizeof(float), 1, fp); |
stretch | 0:ba40c5f30cf0 | 71 | fclose(fp); |
stretch | 0:ba40c5f30cf0 | 72 | } |
stretch | 0:ba40c5f30cf0 | 73 | x.setPosition(pos.x); |
stretch | 0:ba40c5f30cf0 | 74 | y.setPosition(pos.y); |
stretch | 0:ba40c5f30cf0 | 75 | z.setPosition(pos.z); |
stretch | 0:ba40c5f30cf0 | 76 | servoPan = pan; |
stretch | 0:ba40c5f30cf0 | 77 | servoTilt = tilt; |
stretch | 0:ba40c5f30cf0 | 78 | solenoid = 0; |
stretch | 0:ba40c5f30cf0 | 79 | pc.baud(460800); |
stretch | 0:ba40c5f30cf0 | 80 | printf("Test Airbrush!\r\n"); |
stretch | 0:ba40c5f30cf0 | 81 | linearMotion.setStepMotors(&x, &y, &z, &solenoid, &paused, &stopped); |
stretch | 0:ba40c5f30cf0 | 82 | wifi.createAdhocNetwork(); |
stretch | 0:ba40c5f30cf0 | 83 | wifi.attach_interrupt(0x09, pause, 0); |
stretch | 0:ba40c5f30cf0 | 84 | wifi.attach_interrupt(0x0a, stop, 1); |
stretch | 0:ba40c5f30cf0 | 85 | led = 1; |
stretch | 0:ba40c5f30cf0 | 86 | while (1) { |
stretch | 0:ba40c5f30cf0 | 87 | while (pc.readable()) { |
stretch | 0:ba40c5f30cf0 | 88 | wifi.putc(pc.getc()); |
stretch | 0:ba40c5f30cf0 | 89 | } |
stretch | 0:ba40c5f30cf0 | 90 | while (wifi.readable()) { |
stretch | 0:ba40c5f30cf0 | 91 | pc.putc(wifi.getc()); |
stretch | 0:ba40c5f30cf0 | 92 | } |
stretch | 0:ba40c5f30cf0 | 93 | if (wifi.hasCmd()) { |
stretch | 0:ba40c5f30cf0 | 94 | cmd = wifi.getCmd(); |
stretch | 0:ba40c5f30cf0 | 95 | switch (cmd->cmd) { |
stretch | 0:ba40c5f30cf0 | 96 | case 0x00: { //relative motion |
stretch | 0:ba40c5f30cf0 | 97 | 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 | 98 | Vector finalPos = {cmd->l[0], |
stretch | 0:ba40c5f30cf0 | 99 | cmd->l[1], |
stretch | 0:ba40c5f30cf0 | 100 | cmd->l[2]}; |
stretch | 0:ba40c5f30cf0 | 101 | linearMotion.interpolate(finalPos,1); |
stretch | 0:ba40c5f30cf0 | 102 | break; |
stretch | 0:ba40c5f30cf0 | 103 | } |
stretch | 0:ba40c5f30cf0 | 104 | case 0x01: { //move to specified global coordinates |
stretch | 0:ba40c5f30cf0 | 105 | Vector relative = {cmd->l[0] - x.getPosition(), |
stretch | 0:ba40c5f30cf0 | 106 | cmd->l[1] - y.getPosition(), |
stretch | 0:ba40c5f30cf0 | 107 | cmd->l[2] - z.getPosition()}; |
stretch | 0:ba40c5f30cf0 | 108 | 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 | 109 | linearMotion.interpolate(relative, 10); |
stretch | 0:ba40c5f30cf0 | 110 | break; |
stretch | 0:ba40c5f30cf0 | 111 | } |
stretch | 0:ba40c5f30cf0 | 112 | case 0x02: { //pretend paint an image |
stretch | 0:ba40c5f30cf0 | 113 | Vector baseVector = {cmd->l[0], |
stretch | 0:ba40c5f30cf0 | 114 | cmd->l[1], |
stretch | 0:ba40c5f30cf0 | 115 | cmd->l[2]}; |
stretch | 0:ba40c5f30cf0 | 116 | |
stretch | 0:ba40c5f30cf0 | 117 | Vector heightVector = {cmd->l[3], |
stretch | 0:ba40c5f30cf0 | 118 | cmd->l[4], |
stretch | 0:ba40c5f30cf0 | 119 | cmd->l[5]}; |
stretch | 0:ba40c5f30cf0 | 120 | 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 | 121 | linearMotion.interpolateSquare(baseVector, heightVector, 10, false); |
stretch | 0:ba40c5f30cf0 | 122 | break; |
stretch | 0:ba40c5f30cf0 | 123 | } |
stretch | 0:ba40c5f30cf0 | 124 | case 0x03: { //paint an image |
stretch | 0:ba40c5f30cf0 | 125 | Vector baseVector = {cmd->l[0], |
stretch | 0:ba40c5f30cf0 | 126 | cmd->l[1], |
stretch | 0:ba40c5f30cf0 | 127 | cmd->l[2]}; |
stretch | 0:ba40c5f30cf0 | 128 | |
stretch | 0:ba40c5f30cf0 | 129 | Vector heightVector = {cmd->l[3], |
stretch | 0:ba40c5f30cf0 | 130 | cmd->l[4], |
stretch | 0:ba40c5f30cf0 | 131 | cmd->l[5]}; |
stretch | 0:ba40c5f30cf0 | 132 | 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 | 0:ba40c5f30cf0 | 133 | linearMotion.interpolateSquare(baseVector,heightVector,200,true); |
stretch | 0:ba40c5f30cf0 | 134 | break; |
stretch | 0:ba40c5f30cf0 | 135 | } |
stretch | 0:ba40c5f30cf0 | 136 | case 0x04: { //change the pan |
stretch | 0:ba40c5f30cf0 | 137 | pan = cmd->f[0]; |
stretch | 0:ba40c5f30cf0 | 138 | servoPan = pan; |
stretch | 0:ba40c5f30cf0 | 139 | pc.printf("Change the pan to %f\n\r",pan); |
stretch | 0:ba40c5f30cf0 | 140 | break; |
stretch | 0:ba40c5f30cf0 | 141 | } |
stretch | 0:ba40c5f30cf0 | 142 | case 0x05: { //change the tilt |
stretch | 0:ba40c5f30cf0 | 143 | tilt = cmd->f[0]; |
stretch | 0:ba40c5f30cf0 | 144 | servoTilt = tilt; |
stretch | 0:ba40c5f30cf0 | 145 | pc.printf("Change the tilt to %f\n\r",tilt); |
stretch | 0:ba40c5f30cf0 | 146 | break; |
stretch | 0:ba40c5f30cf0 | 147 | } |
stretch | 0:ba40c5f30cf0 | 148 | |
stretch | 0:ba40c5f30cf0 | 149 | case 0x07: { //turn on the solenoid |
stretch | 0:ba40c5f30cf0 | 150 | long t = cmd->l[0]; |
stretch | 0:ba40c5f30cf0 | 151 | if (t) |
stretch | 0:ba40c5f30cf0 | 152 | solenoid = 1; |
stretch | 0:ba40c5f30cf0 | 153 | else |
stretch | 0:ba40c5f30cf0 | 154 | solenoid = 0; |
stretch | 0:ba40c5f30cf0 | 155 | break; |
stretch | 0:ba40c5f30cf0 | 156 | } |
stretch | 0:ba40c5f30cf0 | 157 | case 0x09: { // We accidentally got to a pause state |
stretch | 0:ba40c5f30cf0 | 158 | paused = false; |
stretch | 0:ba40c5f30cf0 | 159 | break; |
stretch | 0:ba40c5f30cf0 | 160 | } |
stretch | 0:ba40c5f30cf0 | 161 | default: { |
stretch | 0:ba40c5f30cf0 | 162 | pc.printf("You should probably do something with case %x\n\r", cmd->cmd); |
stretch | 0:ba40c5f30cf0 | 163 | break; |
stretch | 0:ba40c5f30cf0 | 164 | } |
stretch | 0:ba40c5f30cf0 | 165 | } |
stretch | 0:ba40c5f30cf0 | 166 | stopped = false; |
stretch | 0:ba40c5f30cf0 | 167 | Vector globalPos = {x.getPosition(), y.getPosition(), z.getPosition()}; |
stretch | 0:ba40c5f30cf0 | 168 | savePosition(globalPos, pan, tilt); |
stretch | 0:ba40c5f30cf0 | 169 | cmd->cmd = 8; |
stretch | 0:ba40c5f30cf0 | 170 | cmd->l[0] = globalPos.x; |
stretch | 0:ba40c5f30cf0 | 171 | cmd->l[1] = globalPos.y; |
stretch | 0:ba40c5f30cf0 | 172 | cmd->l[2] = globalPos.z; |
stretch | 0:ba40c5f30cf0 | 173 | cmd->f[3] = pan; |
stretch | 0:ba40c5f30cf0 | 174 | cmd->f[4] = tilt; |
stretch | 0:ba40c5f30cf0 | 175 | wifi.sendCmd(cmd); |
stretch | 0:ba40c5f30cf0 | 176 | } |
stretch | 0:ba40c5f30cf0 | 177 | } |
stretch | 0:ba40c5f30cf0 | 178 | } |