Test
stepperMotor.cpp@0:dd286de4bc2f, 2021-01-11 (annotated)
- Committer:
- fermedicius
- Date:
- Mon Jan 11 11:07:32 2021 +0000
- Revision:
- 0:dd286de4bc2f
Test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fermedicius | 0:dd286de4bc2f | 1 | #include "stepperMotor.h" |
fermedicius | 0:dd286de4bc2f | 2 | |
fermedicius | 0:dd286de4bc2f | 3 | #include "mbed.h" |
fermedicius | 0:dd286de4bc2f | 4 | |
fermedicius | 0:dd286de4bc2f | 5 | int motorSpeed; // Steper speed |
fermedicius | 0:dd286de4bc2f | 6 | |
fermedicius | 0:dd286de4bc2f | 7 | sMotor::sMotor(PinName A0, PinName A1, PinName A2, PinName A3) : _A0(A0), _A1(A1), _A2(A2), _A3(A3) { // Defenition of motor pins |
fermedicius | 0:dd286de4bc2f | 8 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 9 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 10 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 11 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 12 | } |
fermedicius | 0:dd286de4bc2f | 13 | |
fermedicius | 0:dd286de4bc2f | 14 | void sMotor::off() |
fermedicius | 0:dd286de4bc2f | 15 | { |
fermedicius | 0:dd286de4bc2f | 16 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 17 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 18 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 19 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 20 | } |
fermedicius | 0:dd286de4bc2f | 21 | void sMotor::anticlockwise() { // rotate the motor 1 step anticlockwise |
fermedicius | 0:dd286de4bc2f | 22 | |
fermedicius | 0:dd286de4bc2f | 23 | |
fermedicius | 0:dd286de4bc2f | 24 | for (int i = 0; i < 8; i++) { |
fermedicius | 0:dd286de4bc2f | 25 | |
fermedicius | 0:dd286de4bc2f | 26 | switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps |
fermedicius | 0:dd286de4bc2f | 27 | case 0: { |
fermedicius | 0:dd286de4bc2f | 28 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 29 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 30 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 31 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 32 | } |
fermedicius | 0:dd286de4bc2f | 33 | break; |
fermedicius | 0:dd286de4bc2f | 34 | case 1: { |
fermedicius | 0:dd286de4bc2f | 35 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 36 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 37 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 38 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 39 | } |
fermedicius | 0:dd286de4bc2f | 40 | break; |
fermedicius | 0:dd286de4bc2f | 41 | case 2: { |
fermedicius | 0:dd286de4bc2f | 42 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 43 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 44 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 45 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 46 | } |
fermedicius | 0:dd286de4bc2f | 47 | break; |
fermedicius | 0:dd286de4bc2f | 48 | case 3: { |
fermedicius | 0:dd286de4bc2f | 49 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 50 | _A1=1; |
fermedicius | 0:dd286de4bc2f | 51 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 52 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 53 | } |
fermedicius | 0:dd286de4bc2f | 54 | break; |
fermedicius | 0:dd286de4bc2f | 55 | case 4: { |
fermedicius | 0:dd286de4bc2f | 56 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 57 | _A1=1; |
fermedicius | 0:dd286de4bc2f | 58 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 59 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 60 | } |
fermedicius | 0:dd286de4bc2f | 61 | break; |
fermedicius | 0:dd286de4bc2f | 62 | case 5: { |
fermedicius | 0:dd286de4bc2f | 63 | _A0=1; |
fermedicius | 0:dd286de4bc2f | 64 | _A1=1; |
fermedicius | 0:dd286de4bc2f | 65 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 66 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 67 | } |
fermedicius | 0:dd286de4bc2f | 68 | break; |
fermedicius | 0:dd286de4bc2f | 69 | case 6: { |
fermedicius | 0:dd286de4bc2f | 70 | _A0=1; |
fermedicius | 0:dd286de4bc2f | 71 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 72 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 73 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 74 | } |
fermedicius | 0:dd286de4bc2f | 75 | break; |
fermedicius | 0:dd286de4bc2f | 76 | case 7: { |
fermedicius | 0:dd286de4bc2f | 77 | _A0=1; |
fermedicius | 0:dd286de4bc2f | 78 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 79 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 80 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 81 | } |
fermedicius | 0:dd286de4bc2f | 82 | break; |
fermedicius | 0:dd286de4bc2f | 83 | } |
fermedicius | 0:dd286de4bc2f | 84 | |
fermedicius | 0:dd286de4bc2f | 85 | |
fermedicius | 0:dd286de4bc2f | 86 | wait_us(motorSpeed); // wait time defines the speed |
fermedicius | 0:dd286de4bc2f | 87 | } |
fermedicius | 0:dd286de4bc2f | 88 | } |
fermedicius | 0:dd286de4bc2f | 89 | |
fermedicius | 0:dd286de4bc2f | 90 | void sMotor::clockwise() { // rotate the motor 1 step clockwise |
fermedicius | 0:dd286de4bc2f | 91 | for (int i = 7; i >= 0; i--) { |
fermedicius | 0:dd286de4bc2f | 92 | |
fermedicius | 0:dd286de4bc2f | 93 | switch (i) { |
fermedicius | 0:dd286de4bc2f | 94 | case 0: { |
fermedicius | 0:dd286de4bc2f | 95 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 96 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 97 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 98 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 99 | } |
fermedicius | 0:dd286de4bc2f | 100 | break; |
fermedicius | 0:dd286de4bc2f | 101 | case 1: { |
fermedicius | 0:dd286de4bc2f | 102 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 103 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 104 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 105 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 106 | } |
fermedicius | 0:dd286de4bc2f | 107 | break; |
fermedicius | 0:dd286de4bc2f | 108 | case 2: { |
fermedicius | 0:dd286de4bc2f | 109 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 110 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 111 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 112 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 113 | } |
fermedicius | 0:dd286de4bc2f | 114 | break; |
fermedicius | 0:dd286de4bc2f | 115 | case 3: { |
fermedicius | 0:dd286de4bc2f | 116 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 117 | _A1=1; |
fermedicius | 0:dd286de4bc2f | 118 | _A2=1; |
fermedicius | 0:dd286de4bc2f | 119 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 120 | } |
fermedicius | 0:dd286de4bc2f | 121 | break; |
fermedicius | 0:dd286de4bc2f | 122 | case 4: { |
fermedicius | 0:dd286de4bc2f | 123 | _A0=0; |
fermedicius | 0:dd286de4bc2f | 124 | _A1=1; |
fermedicius | 0:dd286de4bc2f | 125 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 126 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 127 | } |
fermedicius | 0:dd286de4bc2f | 128 | break; |
fermedicius | 0:dd286de4bc2f | 129 | case 5: { |
fermedicius | 0:dd286de4bc2f | 130 | _A0=1; |
fermedicius | 0:dd286de4bc2f | 131 | _A1=1; |
fermedicius | 0:dd286de4bc2f | 132 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 133 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 134 | } |
fermedicius | 0:dd286de4bc2f | 135 | break; |
fermedicius | 0:dd286de4bc2f | 136 | case 6: { |
fermedicius | 0:dd286de4bc2f | 137 | _A0=1; |
fermedicius | 0:dd286de4bc2f | 138 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 139 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 140 | _A3=0; |
fermedicius | 0:dd286de4bc2f | 141 | } |
fermedicius | 0:dd286de4bc2f | 142 | break; |
fermedicius | 0:dd286de4bc2f | 143 | case 7: { |
fermedicius | 0:dd286de4bc2f | 144 | _A0=1; |
fermedicius | 0:dd286de4bc2f | 145 | _A1=0; |
fermedicius | 0:dd286de4bc2f | 146 | _A2=0; |
fermedicius | 0:dd286de4bc2f | 147 | _A3=1; |
fermedicius | 0:dd286de4bc2f | 148 | } |
fermedicius | 0:dd286de4bc2f | 149 | break; |
fermedicius | 0:dd286de4bc2f | 150 | } |
fermedicius | 0:dd286de4bc2f | 151 | |
fermedicius | 0:dd286de4bc2f | 152 | |
fermedicius | 0:dd286de4bc2f | 153 | wait_us(motorSpeed); // wait time defines the speed |
fermedicius | 0:dd286de4bc2f | 154 | } |
fermedicius | 0:dd286de4bc2f | 155 | } |
fermedicius | 0:dd286de4bc2f | 156 | void sMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200) |
fermedicius | 0:dd286de4bc2f | 157 | int count=0; // initalize step count |
fermedicius | 0:dd286de4bc2f | 158 | motorSpeed=speed; //set motor speed |
fermedicius | 0:dd286de4bc2f | 159 | if (direction==0) // turn clockwise |
fermedicius | 0:dd286de4bc2f | 160 | do { |
fermedicius | 0:dd286de4bc2f | 161 | clockwise(); |
fermedicius | 0:dd286de4bc2f | 162 | count++; |
fermedicius | 0:dd286de4bc2f | 163 | } while (count<num_steps); // turn number of steps applied |
fermedicius | 0:dd286de4bc2f | 164 | |
fermedicius | 0:dd286de4bc2f | 165 | else if (direction==1)// turn anticlockwise |
fermedicius | 0:dd286de4bc2f | 166 | { |
fermedicius | 0:dd286de4bc2f | 167 | count=0; |
fermedicius | 0:dd286de4bc2f | 168 | do { |
fermedicius | 0:dd286de4bc2f | 169 | anticlockwise(); |
fermedicius | 0:dd286de4bc2f | 170 | count++; |
fermedicius | 0:dd286de4bc2f | 171 | } while (count<num_steps);// turn number of steps applied |
fermedicius | 0:dd286de4bc2f | 172 | } |
fermedicius | 0:dd286de4bc2f | 173 | } |