Empty program for LPC4088
main.cpp@0:1f5dc83e66d6, 2016-02-17 (annotated)
- Committer:
- redbird
- Date:
- Wed Feb 17 16:57:43 2016 +0000
- Revision:
- 0:1f5dc83e66d6
Empty template for LPC4088 Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
redbird | 0:1f5dc83e66d6 | 1 | #include "mbed.h" |
redbird | 0:1f5dc83e66d6 | 2 | |
redbird | 0:1f5dc83e66d6 | 3 | /*****Globals*****/ |
redbird | 0:1f5dc83e66d6 | 4 | |
redbird | 0:1f5dc83e66d6 | 5 | // Motor Selectors |
redbird | 0:1f5dc83e66d6 | 6 | #define x 'x' |
redbird | 0:1f5dc83e66d6 | 7 | #define y 'y' |
redbird | 0:1f5dc83e66d6 | 8 | #define f 'f' |
redbird | 0:1f5dc83e66d6 | 9 | |
redbird | 0:1f5dc83e66d6 | 10 | // Direction Selectors |
redbird | 0:1f5dc83e66d6 | 11 | #define cw 0 // = 0 |
redbird | 0:1f5dc83e66d6 | 12 | #define ccw 1 // = 1 |
redbird | 0:1f5dc83e66d6 | 13 | |
redbird | 0:1f5dc83e66d6 | 14 | // Motor Speed |
redbird | 0:1f5dc83e66d6 | 15 | #define xMotorPeriod 4 // 25kHz |
redbird | 0:1f5dc83e66d6 | 16 | #define yMotorPeriod 4 // 25kHz |
redbird | 0:1f5dc83e66d6 | 17 | #define fMotorPeriod 15 |
redbird | 0:1f5dc83e66d6 | 18 | |
redbird | 0:1f5dc83e66d6 | 19 | #define xFreq 25000 |
redbird | 0:1f5dc83e66d6 | 20 | #define yFreq 25000 |
redbird | 0:1f5dc83e66d6 | 21 | |
redbird | 0:1f5dc83e66d6 | 22 | class Drive{ |
redbird | 0:1f5dc83e66d6 | 23 | public: |
redbird | 0:1f5dc83e66d6 | 24 | int stepCount, position, distance, dir, period; |
redbird | 0:1f5dc83e66d6 | 25 | Drive(PinName pulse, PinName direction, int motorPeriod) : _pulse(pulse), _direction(direction) { |
redbird | 0:1f5dc83e66d6 | 26 | _pulse.write(0.0f); |
redbird | 0:1f5dc83e66d6 | 27 | _direction = 0; |
redbird | 0:1f5dc83e66d6 | 28 | distance = 1; |
redbird | 0:1f5dc83e66d6 | 29 | stepCount = 0; |
redbird | 0:1f5dc83e66d6 | 30 | period = motorPeriod; |
redbird | 0:1f5dc83e66d6 | 31 | dir = cw; |
redbird | 0:1f5dc83e66d6 | 32 | } |
redbird | 0:1f5dc83e66d6 | 33 | void on(){ |
redbird | 0:1f5dc83e66d6 | 34 | _pulse.period_us(period); // set period |
redbird | 0:1f5dc83e66d6 | 35 | _pulse.write(0.50f); // 50% duty cycle |
redbird | 0:1f5dc83e66d6 | 36 | } |
redbird | 0:1f5dc83e66d6 | 37 | // edit members |
redbird | 0:1f5dc83e66d6 | 38 | void setDistance(int value){ |
redbird | 0:1f5dc83e66d6 | 39 | distance = value; |
redbird | 0:1f5dc83e66d6 | 40 | } |
redbird | 0:1f5dc83e66d6 | 41 | void setDirection(int value){ |
redbird | 0:1f5dc83e66d6 | 42 | dir = value; |
redbird | 0:1f5dc83e66d6 | 43 | } |
redbird | 0:1f5dc83e66d6 | 44 | void stepIncr(){ |
redbird | 0:1f5dc83e66d6 | 45 | stepCount++; |
redbird | 0:1f5dc83e66d6 | 46 | if(stepCount == distance){ // stop motor |
redbird | 0:1f5dc83e66d6 | 47 | _pulse.write(0.0f); |
redbird | 0:1f5dc83e66d6 | 48 | stepCount = 0; |
redbird | 0:1f5dc83e66d6 | 49 | } |
redbird | 0:1f5dc83e66d6 | 50 | } |
redbird | 0:1f5dc83e66d6 | 51 | void resetCount(){ |
redbird | 0:1f5dc83e66d6 | 52 | stepCount = 0; |
redbird | 0:1f5dc83e66d6 | 53 | } |
redbird | 0:1f5dc83e66d6 | 54 | private: |
redbird | 0:1f5dc83e66d6 | 55 | PwmOut _pulse; |
redbird | 0:1f5dc83e66d6 | 56 | DigitalOut _direction; |
redbird | 0:1f5dc83e66d6 | 57 | }; |
redbird | 0:1f5dc83e66d6 | 58 | |
redbird | 0:1f5dc83e66d6 | 59 | // Motors |
redbird | 0:1f5dc83e66d6 | 60 | Drive xMotor(p7, p10, xMotorPeriod); |
redbird | 0:1f5dc83e66d6 | 61 | Drive yMotor(p8, p11, yMotorPeriod); |
redbird | 0:1f5dc83e66d6 | 62 | Drive fMotor(p9, p12, fMotorPeriod); |
redbird | 0:1f5dc83e66d6 | 63 | |
redbird | 0:1f5dc83e66d6 | 64 | InterruptIn xStepRead(p1); |
redbird | 0:1f5dc83e66d6 | 65 | InterruptIn yStepRead(p2); |
redbird | 0:1f5dc83e66d6 | 66 | InterruptIn fStepRead(p3); |
redbird | 0:1f5dc83e66d6 | 67 | |
redbird | 0:1f5dc83e66d6 | 68 | // LEDs |
redbird | 0:1f5dc83e66d6 | 69 | DigitalOut myled1(LED1); |
redbird | 0:1f5dc83e66d6 | 70 | DigitalOut myled2(LED2); |
redbird | 0:1f5dc83e66d6 | 71 | DigitalOut myled3(LED3); |
redbird | 0:1f5dc83e66d6 | 72 | DigitalOut myled4(LED4); |
redbird | 0:1f5dc83e66d6 | 73 | |
redbird | 0:1f5dc83e66d6 | 74 | /*****Functions*****/ |
redbird | 0:1f5dc83e66d6 | 75 | |
redbird | 0:1f5dc83e66d6 | 76 | // motor drive functions |
redbird | 0:1f5dc83e66d6 | 77 | void motorDrive(int steps, char motor, int direction){ |
redbird | 0:1f5dc83e66d6 | 78 | // Input int pulse for distance // 1 pulse = |
redbird | 0:1f5dc83e66d6 | 79 | // Input char motor as the drive motor |
redbird | 0:1f5dc83e66d6 | 80 | // Return value of 0 means freq input was negative, 1 success |
redbird | 0:1f5dc83e66d6 | 81 | // No more than one motor can run at a time (PWM limit) |
redbird | 0:1f5dc83e66d6 | 82 | |
redbird | 0:1f5dc83e66d6 | 83 | /* Example |
redbird | 0:1f5dc83e66d6 | 84 | int main(){ // Generates a 25 kHz PWM for 15 seconds |
redbird | 0:1f5dc83e66d6 | 85 | motorPWM(500, 'x'); |
redbird | 0:1f5dc83e66d6 | 86 | wait(15.0); |
redbird | 0:1f5dc83e66d6 | 87 | motorStop('x'); |
redbird | 0:1f5dc83e66d6 | 88 | } |
redbird | 0:1f5dc83e66d6 | 89 | */ |
redbird | 0:1f5dc83e66d6 | 90 | switch(motor){ |
redbird | 0:1f5dc83e66d6 | 91 | case 'x': // x track motor |
redbird | 0:1f5dc83e66d6 | 92 | xMotor.resetCount(); |
redbird | 0:1f5dc83e66d6 | 93 | xMotor.setDistance(steps); |
redbird | 0:1f5dc83e66d6 | 94 | xMotor.setDirection(direction); |
redbird | 0:1f5dc83e66d6 | 95 | |
redbird | 0:1f5dc83e66d6 | 96 | xStepRead.rise(&xMotor, &Drive::stepIncr); |
redbird | 0:1f5dc83e66d6 | 97 | wait_us(5); |
redbird | 0:1f5dc83e66d6 | 98 | xMotor.on(); |
redbird | 0:1f5dc83e66d6 | 99 | break; |
redbird | 0:1f5dc83e66d6 | 100 | case 'y': // y track motor |
redbird | 0:1f5dc83e66d6 | 101 | yMotor.resetCount(); |
redbird | 0:1f5dc83e66d6 | 102 | yMotor.setDistance(steps); |
redbird | 0:1f5dc83e66d6 | 103 | yMotor.setDirection(direction); |
redbird | 0:1f5dc83e66d6 | 104 | |
redbird | 0:1f5dc83e66d6 | 105 | yStepRead.rise(&yMotor, &Drive::stepIncr); |
redbird | 0:1f5dc83e66d6 | 106 | wait_us(5); |
redbird | 0:1f5dc83e66d6 | 107 | yMotor.on(); |
redbird | 0:1f5dc83e66d6 | 108 | break; |
redbird | 0:1f5dc83e66d6 | 109 | case 'f': // friction motor |
redbird | 0:1f5dc83e66d6 | 110 | fMotor.resetCount(); |
redbird | 0:1f5dc83e66d6 | 111 | fMotor.setDistance(steps); |
redbird | 0:1f5dc83e66d6 | 112 | fMotor.setDirection(direction); |
redbird | 0:1f5dc83e66d6 | 113 | |
redbird | 0:1f5dc83e66d6 | 114 | fStepRead.rise(&fMotor, &Drive::stepIncr); |
redbird | 0:1f5dc83e66d6 | 115 | wait_us(5); |
redbird | 0:1f5dc83e66d6 | 116 | fMotor.on(); |
redbird | 0:1f5dc83e66d6 | 117 | break; |
redbird | 0:1f5dc83e66d6 | 118 | } |
redbird | 0:1f5dc83e66d6 | 119 | } |
redbird | 0:1f5dc83e66d6 | 120 | |
redbird | 0:1f5dc83e66d6 | 121 | int main() { |
redbird | 0:1f5dc83e66d6 | 122 | myled1 = 1; |
redbird | 0:1f5dc83e66d6 | 123 | myled2 = 1; |
redbird | 0:1f5dc83e66d6 | 124 | myled3 = 1; |
redbird | 0:1f5dc83e66d6 | 125 | myled4 = 1; |
redbird | 0:1f5dc83e66d6 | 126 | |
redbird | 0:1f5dc83e66d6 | 127 | while(1){ |
redbird | 0:1f5dc83e66d6 | 128 | motorDrive(xFreq*5, x, cw); |
redbird | 0:1f5dc83e66d6 | 129 | wait(10.0); |
redbird | 0:1f5dc83e66d6 | 130 | motorDrive(yFreq*5, y, cw); |
redbird | 0:1f5dc83e66d6 | 131 | wait(10.0); |
redbird | 0:1f5dc83e66d6 | 132 | } |
redbird | 0:1f5dc83e66d6 | 133 | } |