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.
Async_4pin_Stepper.cpp
00001 /* Asynchronous Stepper with 4-pin output 00002 * 00003 * Written by Simon Krogedal 00004 * 31/05/2021 00005 * Team 9 4th Year project 00006 * 00007 * for NUCLEO-F401RE 00008 * 00009 * Version 0.1 00010 * 00011 */ 00012 00013 #include "Async_4pin_Stepper.h " 00014 00015 // Constructor takes 4 pins and a steps/rev in float presicion 00016 Async_4pin_Stepper::Async_4pin_Stepper( PinName StepperPin1, 00017 PinName StepperPin2, 00018 PinName StepperPin3, 00019 PinName StepperPin4, 00020 float StepsPerRev) : 00021 00022 output(StepperPin1, 00023 StepperPin2, 00024 StepperPin3, 00025 StepperPin4) 00026 { 00027 SPR = StepsPerRev; 00028 setSpeed(500); 00029 currentPos = 0; 00030 relax(); 00031 } 00032 00033 void Async_4pin_Stepper::setZeroPos() {currentPos = 0;} 00034 00035 void Async_4pin_Stepper::setSpeed(int speed) { 00036 if(speed > 0) { 00037 speed_ = speed; 00038 stepInterval = 1.0/speed; 00039 } 00040 else { 00041 speed_ = 0; 00042 stepInterval = 0; 00043 } 00044 } 00045 00046 int Async_4pin_Stepper::getPos() {return currentPos;} 00047 int Async_4pin_Stepper::getTarget() {return targetPos;} 00048 int Async_4pin_Stepper::getSpeed() {return speed_;} 00049 int Async_4pin_Stepper::distanceToGo() {return targetPos-currentPos;} 00050 00051 void Async_4pin_Stepper::step(int step) { 00052 switch (step & 0x7) { 00053 case 0: // 1000 00054 output = 0b0001; 00055 break; 00056 00057 case 1: // 1010 00058 output = 0b0101; 00059 break; 00060 00061 case 2: // 0010 00062 output = 0b0100; 00063 break; 00064 00065 case 3: // 0110 00066 output = 0b0110; 00067 break; 00068 00069 case 4: // 0100 00070 output = 0b0010; 00071 break; 00072 00073 case 5: //0101 00074 output = 0b1010; 00075 break; 00076 00077 case 6: // 0001 00078 output = 0b1000; 00079 break; 00080 00081 case 7: //1001 00082 output = 0b1001; 00083 break; 00084 } 00085 } 00086 00087 void Async_4pin_Stepper::stepOnceCCW() { 00088 // Update current position 00089 currentPos++; 00090 // Step to new position 00091 step(currentPos); 00092 } 00093 00094 void Async_4pin_Stepper::stepOnceCW() { 00095 // Update current position 00096 currentPos--; 00097 // Step to new position 00098 step(currentPos); 00099 } 00100 00101 void Async_4pin_Stepper::setTarget(int target) {targetPos = target;} 00102 00103 void Async_4pin_Stepper::goToTarget() { 00104 // Check if at target or standstill 00105 if(!distanceToGo() || !stepInterval || running) 00106 return; 00107 else { // Attach ticker to callback function 00108 ticker.attach(callback(this, &Async_4pin_Stepper::stepCB), stepInterval); 00109 running = true; 00110 } 00111 } 00112 00113 void Async_4pin_Stepper::stepCB() { 00114 // check error value for driction 00115 int error = distanceToGo(); 00116 if(error == 0) { 00117 ticker.detach(); 00118 running = false; 00119 } 00120 else if(error > 0) 00121 stepOnceCCW(); 00122 else 00123 stepOnceCW(); 00124 } 00125 00126 void Async_4pin_Stepper::relax() {output = 0;} 00127 bool Async_4pin_Stepper::isRunning() {return running;} 00128 void Async_4pin_Stepper::stop() {ticker.detach();}
Generated on Sat Jul 23 2022 07:35:00 by
