Library to run 4-wire half stepper in asynchronous mode
Async_4pin_Stepper.cpp@0:252d645cfc5d, 2021-06-03 (annotated)
- Committer:
- krogedal
- Date:
- Thu Jun 03 21:38:34 2021 +0000
- Revision:
- 0:252d645cfc5d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krogedal | 0:252d645cfc5d | 1 | /* Asynchronous Stepper with 4-pin output |
krogedal | 0:252d645cfc5d | 2 | * |
krogedal | 0:252d645cfc5d | 3 | * Written by Simon Krogedal |
krogedal | 0:252d645cfc5d | 4 | * 31/05/2021 |
krogedal | 0:252d645cfc5d | 5 | * Team 9 4th Year project |
krogedal | 0:252d645cfc5d | 6 | * |
krogedal | 0:252d645cfc5d | 7 | * for NUCLEO-F401RE |
krogedal | 0:252d645cfc5d | 8 | * |
krogedal | 0:252d645cfc5d | 9 | * Version 0.1 |
krogedal | 0:252d645cfc5d | 10 | * |
krogedal | 0:252d645cfc5d | 11 | */ |
krogedal | 0:252d645cfc5d | 12 | |
krogedal | 0:252d645cfc5d | 13 | #include "Async_4pin_Stepper.h" |
krogedal | 0:252d645cfc5d | 14 | |
krogedal | 0:252d645cfc5d | 15 | // Constructor takes 4 pins and a steps/rev in float presicion |
krogedal | 0:252d645cfc5d | 16 | Async_4pin_Stepper::Async_4pin_Stepper( PinName StepperPin1, |
krogedal | 0:252d645cfc5d | 17 | PinName StepperPin2, |
krogedal | 0:252d645cfc5d | 18 | PinName StepperPin3, |
krogedal | 0:252d645cfc5d | 19 | PinName StepperPin4, |
krogedal | 0:252d645cfc5d | 20 | float StepsPerRev) : |
krogedal | 0:252d645cfc5d | 21 | |
krogedal | 0:252d645cfc5d | 22 | output(StepperPin1, |
krogedal | 0:252d645cfc5d | 23 | StepperPin2, |
krogedal | 0:252d645cfc5d | 24 | StepperPin3, |
krogedal | 0:252d645cfc5d | 25 | StepperPin4) |
krogedal | 0:252d645cfc5d | 26 | { |
krogedal | 0:252d645cfc5d | 27 | SPR = StepsPerRev; |
krogedal | 0:252d645cfc5d | 28 | setSpeed(500); |
krogedal | 0:252d645cfc5d | 29 | currentPos = 0; |
krogedal | 0:252d645cfc5d | 30 | relax(); |
krogedal | 0:252d645cfc5d | 31 | } |
krogedal | 0:252d645cfc5d | 32 | |
krogedal | 0:252d645cfc5d | 33 | void Async_4pin_Stepper::setZeroPos() {currentPos = 0;} |
krogedal | 0:252d645cfc5d | 34 | |
krogedal | 0:252d645cfc5d | 35 | void Async_4pin_Stepper::setSpeed(int speed) { |
krogedal | 0:252d645cfc5d | 36 | if(speed > 0) { |
krogedal | 0:252d645cfc5d | 37 | speed_ = speed; |
krogedal | 0:252d645cfc5d | 38 | stepInterval = 1.0/speed; |
krogedal | 0:252d645cfc5d | 39 | } |
krogedal | 0:252d645cfc5d | 40 | else { |
krogedal | 0:252d645cfc5d | 41 | speed_ = 0; |
krogedal | 0:252d645cfc5d | 42 | stepInterval = 0; |
krogedal | 0:252d645cfc5d | 43 | } |
krogedal | 0:252d645cfc5d | 44 | } |
krogedal | 0:252d645cfc5d | 45 | |
krogedal | 0:252d645cfc5d | 46 | int Async_4pin_Stepper::getPos() {return currentPos;} |
krogedal | 0:252d645cfc5d | 47 | int Async_4pin_Stepper::getTarget() {return targetPos;} |
krogedal | 0:252d645cfc5d | 48 | int Async_4pin_Stepper::getSpeed() {return speed_;} |
krogedal | 0:252d645cfc5d | 49 | int Async_4pin_Stepper::distanceToGo() {return targetPos-currentPos;} |
krogedal | 0:252d645cfc5d | 50 | |
krogedal | 0:252d645cfc5d | 51 | void Async_4pin_Stepper::step(int step) { |
krogedal | 0:252d645cfc5d | 52 | switch (step & 0x7) { |
krogedal | 0:252d645cfc5d | 53 | case 0: // 1000 |
krogedal | 0:252d645cfc5d | 54 | output = 0b0001; |
krogedal | 0:252d645cfc5d | 55 | break; |
krogedal | 0:252d645cfc5d | 56 | |
krogedal | 0:252d645cfc5d | 57 | case 1: // 1010 |
krogedal | 0:252d645cfc5d | 58 | output = 0b0101; |
krogedal | 0:252d645cfc5d | 59 | break; |
krogedal | 0:252d645cfc5d | 60 | |
krogedal | 0:252d645cfc5d | 61 | case 2: // 0010 |
krogedal | 0:252d645cfc5d | 62 | output = 0b0100; |
krogedal | 0:252d645cfc5d | 63 | break; |
krogedal | 0:252d645cfc5d | 64 | |
krogedal | 0:252d645cfc5d | 65 | case 3: // 0110 |
krogedal | 0:252d645cfc5d | 66 | output = 0b0110; |
krogedal | 0:252d645cfc5d | 67 | break; |
krogedal | 0:252d645cfc5d | 68 | |
krogedal | 0:252d645cfc5d | 69 | case 4: // 0100 |
krogedal | 0:252d645cfc5d | 70 | output = 0b0010; |
krogedal | 0:252d645cfc5d | 71 | break; |
krogedal | 0:252d645cfc5d | 72 | |
krogedal | 0:252d645cfc5d | 73 | case 5: //0101 |
krogedal | 0:252d645cfc5d | 74 | output = 0b1010; |
krogedal | 0:252d645cfc5d | 75 | break; |
krogedal | 0:252d645cfc5d | 76 | |
krogedal | 0:252d645cfc5d | 77 | case 6: // 0001 |
krogedal | 0:252d645cfc5d | 78 | output = 0b1000; |
krogedal | 0:252d645cfc5d | 79 | break; |
krogedal | 0:252d645cfc5d | 80 | |
krogedal | 0:252d645cfc5d | 81 | case 7: //1001 |
krogedal | 0:252d645cfc5d | 82 | output = 0b1001; |
krogedal | 0:252d645cfc5d | 83 | break; |
krogedal | 0:252d645cfc5d | 84 | } |
krogedal | 0:252d645cfc5d | 85 | } |
krogedal | 0:252d645cfc5d | 86 | |
krogedal | 0:252d645cfc5d | 87 | void Async_4pin_Stepper::stepOnceCCW() { |
krogedal | 0:252d645cfc5d | 88 | // Update current position |
krogedal | 0:252d645cfc5d | 89 | currentPos++; |
krogedal | 0:252d645cfc5d | 90 | // Step to new position |
krogedal | 0:252d645cfc5d | 91 | step(currentPos); |
krogedal | 0:252d645cfc5d | 92 | } |
krogedal | 0:252d645cfc5d | 93 | |
krogedal | 0:252d645cfc5d | 94 | void Async_4pin_Stepper::stepOnceCW() { |
krogedal | 0:252d645cfc5d | 95 | // Update current position |
krogedal | 0:252d645cfc5d | 96 | currentPos--; |
krogedal | 0:252d645cfc5d | 97 | // Step to new position |
krogedal | 0:252d645cfc5d | 98 | step(currentPos); |
krogedal | 0:252d645cfc5d | 99 | } |
krogedal | 0:252d645cfc5d | 100 | |
krogedal | 0:252d645cfc5d | 101 | void Async_4pin_Stepper::setTarget(int target) {targetPos = target;} |
krogedal | 0:252d645cfc5d | 102 | |
krogedal | 0:252d645cfc5d | 103 | void Async_4pin_Stepper::goToTarget() { |
krogedal | 0:252d645cfc5d | 104 | // Check if at target or standstill |
krogedal | 0:252d645cfc5d | 105 | if(!distanceToGo() || !stepInterval || running) |
krogedal | 0:252d645cfc5d | 106 | return; |
krogedal | 0:252d645cfc5d | 107 | else { // Attach ticker to callback function |
krogedal | 0:252d645cfc5d | 108 | ticker.attach(callback(this, &Async_4pin_Stepper::stepCB), stepInterval); |
krogedal | 0:252d645cfc5d | 109 | running = true; |
krogedal | 0:252d645cfc5d | 110 | } |
krogedal | 0:252d645cfc5d | 111 | } |
krogedal | 0:252d645cfc5d | 112 | |
krogedal | 0:252d645cfc5d | 113 | void Async_4pin_Stepper::stepCB() { |
krogedal | 0:252d645cfc5d | 114 | // check error value for driction |
krogedal | 0:252d645cfc5d | 115 | int error = distanceToGo(); |
krogedal | 0:252d645cfc5d | 116 | if(error == 0) { |
krogedal | 0:252d645cfc5d | 117 | ticker.detach(); |
krogedal | 0:252d645cfc5d | 118 | running = false; |
krogedal | 0:252d645cfc5d | 119 | } |
krogedal | 0:252d645cfc5d | 120 | else if(error > 0) |
krogedal | 0:252d645cfc5d | 121 | stepOnceCCW(); |
krogedal | 0:252d645cfc5d | 122 | else |
krogedal | 0:252d645cfc5d | 123 | stepOnceCW(); |
krogedal | 0:252d645cfc5d | 124 | } |
krogedal | 0:252d645cfc5d | 125 | |
krogedal | 0:252d645cfc5d | 126 | void Async_4pin_Stepper::relax() {output = 0;} |
krogedal | 0:252d645cfc5d | 127 | bool Async_4pin_Stepper::isRunning() {return running;} |
krogedal | 0:252d645cfc5d | 128 | void Async_4pin_Stepper::stop() {ticker.detach();} |