Solutions for the Stepper Motor experiments for LPC812 MAX

Dependencies:   lpc812_exp_lib_PCF8591 mbed

Committer:
embeddedartists
Date:
Mon Nov 25 14:47:27 2013 +0000
Revision:
0:9a590fe3a1a1
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:9a590fe3a1a1 1 #include "mbed.h"
embeddedartists 0:9a590fe3a1a1 2 #include "PCF8591.h"
embeddedartists 0:9a590fe3a1a1 3
embeddedartists 0:9a590fe3a1a1 4 DigitalOut ph1(D10);
embeddedartists 0:9a590fe3a1a1 5 DigitalOut ph2(D11);
embeddedartists 0:9a590fe3a1a1 6 DigitalOut ph3(D12);
embeddedartists 0:9a590fe3a1a1 7 DigitalOut ph4(D13);
embeddedartists 0:9a590fe3a1a1 8
embeddedartists 0:9a590fe3a1a1 9 PCF8591 adc;
embeddedartists 0:9a590fe3a1a1 10
embeddedartists 0:9a590fe3a1a1 11 Serial pc(USBTX, USBRX);
embeddedartists 0:9a590fe3a1a1 12
embeddedartists 0:9a590fe3a1a1 13 static void experiment1()
embeddedartists 0:9a590fe3a1a1 14 {
embeddedartists 0:9a590fe3a1a1 15 // Delay between steps
embeddedartists 0:9a590fe3a1a1 16 float number = 0.5;
embeddedartists 0:9a590fe3a1a1 17
embeddedartists 0:9a590fe3a1a1 18 // Initialize outputs
embeddedartists 0:9a590fe3a1a1 19 ph1=1;
embeddedartists 0:9a590fe3a1a1 20 ph2=1;
embeddedartists 0:9a590fe3a1a1 21 ph3=1;
embeddedartists 0:9a590fe3a1a1 22 ph4=1;
embeddedartists 0:9a590fe3a1a1 23 pc.printf("\nStarting program...\n");
embeddedartists 0:9a590fe3a1a1 24
embeddedartists 0:9a590fe3a1a1 25 while(1) {
embeddedartists 0:9a590fe3a1a1 26 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 27 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 28 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 29 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 30 wait(number);
embeddedartists 0:9a590fe3a1a1 31 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 32 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 33 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 34 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 35 wait(number);
embeddedartists 0:9a590fe3a1a1 36 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 37 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 38 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 39 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 40 wait(number);
embeddedartists 0:9a590fe3a1a1 41 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 42 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 43 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 44 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 45 wait(number);
embeddedartists 0:9a590fe3a1a1 46 }
embeddedartists 0:9a590fe3a1a1 47 }
embeddedartists 0:9a590fe3a1a1 48
embeddedartists 0:9a590fe3a1a1 49 static void experiment2_alt1()
embeddedartists 0:9a590fe3a1a1 50 {
embeddedartists 0:9a590fe3a1a1 51 // Delay between steps
embeddedartists 0:9a590fe3a1a1 52 float number = 0.5;
embeddedartists 0:9a590fe3a1a1 53
embeddedartists 0:9a590fe3a1a1 54 // Initialize outputs
embeddedartists 0:9a590fe3a1a1 55 ph1=1;
embeddedartists 0:9a590fe3a1a1 56 ph2=1;
embeddedartists 0:9a590fe3a1a1 57 ph3=1;
embeddedartists 0:9a590fe3a1a1 58 ph4=1;
embeddedartists 0:9a590fe3a1a1 59 pc.printf("\nStarting program...\n");
embeddedartists 0:9a590fe3a1a1 60
embeddedartists 0:9a590fe3a1a1 61 while(1) {
embeddedartists 0:9a590fe3a1a1 62 //read trimming potentiometer and convert to value in the 0-0.5 range
embeddedartists 0:9a590fe3a1a1 63 number = adc.read(PCF8591::A0);
embeddedartists 0:9a590fe3a1a1 64 number = number/512; //
embeddedartists 0:9a590fe3a1a1 65 pc.printf("%f\n", number);
embeddedartists 0:9a590fe3a1a1 66
embeddedartists 0:9a590fe3a1a1 67 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 68 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 69 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 70 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 71 wait(number);
embeddedartists 0:9a590fe3a1a1 72 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 73 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 74 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 75 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 76 wait(number);
embeddedartists 0:9a590fe3a1a1 77 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 78 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 79 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 80 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 81 wait(number);
embeddedartists 0:9a590fe3a1a1 82 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 83 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 84 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 85 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 86 wait(number);
embeddedartists 0:9a590fe3a1a1 87 }
embeddedartists 0:9a590fe3a1a1 88 }
embeddedartists 0:9a590fe3a1a1 89
embeddedartists 0:9a590fe3a1a1 90 #define ALPHA 0.6
embeddedartists 0:9a590fe3a1a1 91 float getDelayAndDirection()
embeddedartists 0:9a590fe3a1a1 92 {
embeddedartists 0:9a590fe3a1a1 93 static float lastValue = 0;
embeddedartists 0:9a590fe3a1a1 94
embeddedartists 0:9a590fe3a1a1 95 // read a value
embeddedartists 0:9a590fe3a1a1 96 int v = adc.read(PCF8591::A0);
embeddedartists 0:9a590fe3a1a1 97
embeddedartists 0:9a590fe3a1a1 98 // convert from 0..255 to -0.5..0.5
embeddedartists 0:9a590fe3a1a1 99 float f = v - 128;
embeddedartists 0:9a590fe3a1a1 100 f = f/256;
embeddedartists 0:9a590fe3a1a1 101
embeddedartists 0:9a590fe3a1a1 102 // apply filter
embeddedartists 0:9a590fe3a1a1 103 lastValue = (ALPHA * lastValue) + (1-ALPHA) * f;
embeddedartists 0:9a590fe3a1a1 104
embeddedartists 0:9a590fe3a1a1 105 return f;
embeddedartists 0:9a590fe3a1a1 106 }
embeddedartists 0:9a590fe3a1a1 107
embeddedartists 0:9a590fe3a1a1 108 static void experiment2_alt2()
embeddedartists 0:9a590fe3a1a1 109 {
embeddedartists 0:9a590fe3a1a1 110 // Delay between steps
embeddedartists 0:9a590fe3a1a1 111 float delay = 0.5;
embeddedartists 0:9a590fe3a1a1 112
embeddedartists 0:9a590fe3a1a1 113 // Direction
embeddedartists 0:9a590fe3a1a1 114 bool clockwise = true;
embeddedartists 0:9a590fe3a1a1 115
embeddedartists 0:9a590fe3a1a1 116 // Initialize outputs
embeddedartists 0:9a590fe3a1a1 117 ph1=1;
embeddedartists 0:9a590fe3a1a1 118 ph2=1;
embeddedartists 0:9a590fe3a1a1 119 ph3=1;
embeddedartists 0:9a590fe3a1a1 120 ph4=1;
embeddedartists 0:9a590fe3a1a1 121 pc.printf("\nStarting program...\n");
embeddedartists 0:9a590fe3a1a1 122
embeddedartists 0:9a590fe3a1a1 123 while(1) {
embeddedartists 0:9a590fe3a1a1 124 delay = getDelayAndDirection();
embeddedartists 0:9a590fe3a1a1 125 pc.printf("%f\n", delay);
embeddedartists 0:9a590fe3a1a1 126
embeddedartists 0:9a590fe3a1a1 127 // determine direction and make sure speed is lowest around 0
embeddedartists 0:9a590fe3a1a1 128 clockwise = (delay < 0);
embeddedartists 0:9a590fe3a1a1 129 delay = 0.5 - abs(delay);
embeddedartists 0:9a590fe3a1a1 130
embeddedartists 0:9a590fe3a1a1 131 if (clockwise) {
embeddedartists 0:9a590fe3a1a1 132 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 133 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 134 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 135 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 136 wait(delay);
embeddedartists 0:9a590fe3a1a1 137 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 138 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 139 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 140 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 141 wait(delay);
embeddedartists 0:9a590fe3a1a1 142 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 143 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 144 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 145 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 146 wait(delay);
embeddedartists 0:9a590fe3a1a1 147 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 148 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 149 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 150 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 151 wait(delay);
embeddedartists 0:9a590fe3a1a1 152 } else {
embeddedartists 0:9a590fe3a1a1 153 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 154 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 155 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 156 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 157 wait(delay);
embeddedartists 0:9a590fe3a1a1 158 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 159 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 160 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 161 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 162 wait(delay);
embeddedartists 0:9a590fe3a1a1 163 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 164 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 165 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 166 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 167 wait(delay);
embeddedartists 0:9a590fe3a1a1 168 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 169 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 170 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 171 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 172 wait(delay);
embeddedartists 0:9a590fe3a1a1 173 }
embeddedartists 0:9a590fe3a1a1 174 }
embeddedartists 0:9a590fe3a1a1 175 }
embeddedartists 0:9a590fe3a1a1 176
embeddedartists 0:9a590fe3a1a1 177 int getTargetStep(bool filter)
embeddedartists 0:9a590fe3a1a1 178 {
embeddedartists 0:9a590fe3a1a1 179 static int lastValue = -1;
embeddedartists 0:9a590fe3a1a1 180
embeddedartists 0:9a590fe3a1a1 181 // read a value
embeddedartists 0:9a590fe3a1a1 182 int v = adc.read(PCF8591::A0);
embeddedartists 0:9a590fe3a1a1 183
embeddedartists 0:9a590fe3a1a1 184 if (lastValue == -1) {
embeddedartists 0:9a590fe3a1a1 185 lastValue = v;
embeddedartists 0:9a590fe3a1a1 186 }
embeddedartists 0:9a590fe3a1a1 187
embeddedartists 0:9a590fe3a1a1 188 // apply filter
embeddedartists 0:9a590fe3a1a1 189 if (filter) {
embeddedartists 0:9a590fe3a1a1 190 lastValue = ((3*lastValue) + v) >> 2;
embeddedartists 0:9a590fe3a1a1 191 } else {
embeddedartists 0:9a590fe3a1a1 192 lastValue = v;
embeddedartists 0:9a590fe3a1a1 193 }
embeddedartists 0:9a590fe3a1a1 194
embeddedartists 0:9a590fe3a1a1 195 // convert from 0..255 to 0..19
embeddedartists 0:9a590fe3a1a1 196 return lastValue/13;
embeddedartists 0:9a590fe3a1a1 197 }
embeddedartists 0:9a590fe3a1a1 198
embeddedartists 0:9a590fe3a1a1 199 static void experiment3()
embeddedartists 0:9a590fe3a1a1 200 {
embeddedartists 0:9a590fe3a1a1 201 // Delay between steps
embeddedartists 0:9a590fe3a1a1 202 float delay = 0.05;
embeddedartists 0:9a590fe3a1a1 203
embeddedartists 0:9a590fe3a1a1 204 // Current step
embeddedartists 0:9a590fe3a1a1 205 int current = 0;
embeddedartists 0:9a590fe3a1a1 206
embeddedartists 0:9a590fe3a1a1 207 // Initialize outputs
embeddedartists 0:9a590fe3a1a1 208 ph1=1;
embeddedartists 0:9a590fe3a1a1 209 ph2=1;
embeddedartists 0:9a590fe3a1a1 210 ph3=1;
embeddedartists 0:9a590fe3a1a1 211 ph4=1;
embeddedartists 0:9a590fe3a1a1 212 pc.printf("\nStarting program...\n");
embeddedartists 0:9a590fe3a1a1 213
embeddedartists 0:9a590fe3a1a1 214
embeddedartists 0:9a590fe3a1a1 215 while(1) {
embeddedartists 0:9a590fe3a1a1 216 int target = getTargetStep(true);
embeddedartists 0:9a590fe3a1a1 217
embeddedartists 0:9a590fe3a1a1 218 if (target == current) {
embeddedartists 0:9a590fe3a1a1 219 wait(0.02);
embeddedartists 0:9a590fe3a1a1 220 continue;
embeddedartists 0:9a590fe3a1a1 221 }
embeddedartists 0:9a590fe3a1a1 222 //pc.printf("%d -> %d\n", current, target);
embeddedartists 0:9a590fe3a1a1 223
embeddedartists 0:9a590fe3a1a1 224 int clockwise = (target + 20 - current) % 20;
embeddedartists 0:9a590fe3a1a1 225 int anticlockwise = (current + 20 - target) % 20;
embeddedartists 0:9a590fe3a1a1 226 if (clockwise < anticlockwise) {
embeddedartists 0:9a590fe3a1a1 227 for (int i = 0; i < clockwise; i++) {
embeddedartists 0:9a590fe3a1a1 228 switch (current%4) {
embeddedartists 0:9a590fe3a1a1 229 case 0:
embeddedartists 0:9a590fe3a1a1 230 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 231 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 232 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 233 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 234 break;
embeddedartists 0:9a590fe3a1a1 235 case 1:
embeddedartists 0:9a590fe3a1a1 236 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 237 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 238 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 239 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 240 break;
embeddedartists 0:9a590fe3a1a1 241 case 2:
embeddedartists 0:9a590fe3a1a1 242 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 243 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 244 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 245 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 246 break;
embeddedartists 0:9a590fe3a1a1 247 case 3:
embeddedartists 0:9a590fe3a1a1 248 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 249 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 250 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 251 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 252 break;
embeddedartists 0:9a590fe3a1a1 253 }
embeddedartists 0:9a590fe3a1a1 254 wait(delay);
embeddedartists 0:9a590fe3a1a1 255 current++;
embeddedartists 0:9a590fe3a1a1 256 }
embeddedartists 0:9a590fe3a1a1 257 } else {
embeddedartists 0:9a590fe3a1a1 258 for (int i = 0; i < anticlockwise; i++) {
embeddedartists 0:9a590fe3a1a1 259 switch (current%4) {
embeddedartists 0:9a590fe3a1a1 260 case 0:
embeddedartists 0:9a590fe3a1a1 261 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 262 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 263 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 264 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 265 break;
embeddedartists 0:9a590fe3a1a1 266 case 1:
embeddedartists 0:9a590fe3a1a1 267 ph1 = 0;
embeddedartists 0:9a590fe3a1a1 268 ph2 = 1;
embeddedartists 0:9a590fe3a1a1 269 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 270 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 271 break;
embeddedartists 0:9a590fe3a1a1 272 case 2:
embeddedartists 0:9a590fe3a1a1 273 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 274 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 275 ph3 = 0;
embeddedartists 0:9a590fe3a1a1 276 ph4 = 1;
embeddedartists 0:9a590fe3a1a1 277 break;
embeddedartists 0:9a590fe3a1a1 278 case 3:
embeddedartists 0:9a590fe3a1a1 279 ph1 = 1;
embeddedartists 0:9a590fe3a1a1 280 ph2 = 0;
embeddedartists 0:9a590fe3a1a1 281 ph3 = 1;
embeddedartists 0:9a590fe3a1a1 282 ph4 = 0;
embeddedartists 0:9a590fe3a1a1 283 break;
embeddedartists 0:9a590fe3a1a1 284 }
embeddedartists 0:9a590fe3a1a1 285 wait(delay);
embeddedartists 0:9a590fe3a1a1 286 current+=19;
embeddedartists 0:9a590fe3a1a1 287 }
embeddedartists 0:9a590fe3a1a1 288 }
embeddedartists 0:9a590fe3a1a1 289 current = current % 20;
embeddedartists 0:9a590fe3a1a1 290 }
embeddedartists 0:9a590fe3a1a1 291 }
embeddedartists 0:9a590fe3a1a1 292
embeddedartists 0:9a590fe3a1a1 293 int main()
embeddedartists 0:9a590fe3a1a1 294 {
embeddedartists 0:9a590fe3a1a1 295 //experiment1(); // Fixed delay
embeddedartists 0:9a590fe3a1a1 296 //experiment2_alt1(); // Trimming pot controls delay
embeddedartists 0:9a590fe3a1a1 297 experiment2_alt2(); // Trimming pot controls delay and direction
embeddedartists 0:9a590fe3a1a1 298 //experiment3(); // Absolute positioning
embeddedartists 0:9a590fe3a1a1 299 }