Ingmar Loohuis
/
Motor3
controller
main.cpp@0:8dc46da67c9f, 2016-11-01 (annotated)
- Committer:
- IngmarLoohuis
- Date:
- Tue Nov 01 13:07:59 2016 +0000
- Revision:
- 0:8dc46da67c9f
Motor 3 controller
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IngmarLoohuis | 0:8dc46da67c9f | 1 | // 28BYJ-48 stepper motor example |
IngmarLoohuis | 0:8dc46da67c9f | 2 | // showing how to control a unipolar stepper motor by mbed digital output ports. |
IngmarLoohuis | 0:8dc46da67c9f | 3 | // Tested on the Nucleo F103RB Board |
IngmarLoohuis | 0:8dc46da67c9f | 4 | // Based on http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino |
IngmarLoohuis | 0:8dc46da67c9f | 5 | // Using the ULN2003A Driver. |
IngmarLoohuis | 0:8dc46da67c9f | 6 | |
IngmarLoohuis | 0:8dc46da67c9f | 7 | #include "mbed.h" |
IngmarLoohuis | 0:8dc46da67c9f | 8 | Serial pc(USBTX,USBRX); |
IngmarLoohuis | 0:8dc46da67c9f | 9 | Ticker t0; |
IngmarLoohuis | 0:8dc46da67c9f | 10 | Ticker t7; |
IngmarLoohuis | 0:8dc46da67c9f | 11 | |
IngmarLoohuis | 0:8dc46da67c9f | 12 | volatile bool fn0_go = false; |
IngmarLoohuis | 0:8dc46da67c9f | 13 | void fn0_activate(){ fn0_go = true; }; //Activates the go−flag |
IngmarLoohuis | 0:8dc46da67c9f | 14 | volatile bool fn7_go = false; |
IngmarLoohuis | 0:8dc46da67c9f | 15 | void fn7_activate(){ fn7_go = true; }; //Activates the go−flag |
IngmarLoohuis | 0:8dc46da67c9f | 16 | |
IngmarLoohuis | 0:8dc46da67c9f | 17 | BusOut motor_out(D0, D1, D2, D3); // blue - pink - yellow - orange |
IngmarLoohuis | 0:8dc46da67c9f | 18 | |
IngmarLoohuis | 0:8dc46da67c9f | 19 | int up = 1; |
IngmarLoohuis | 0:8dc46da67c9f | 20 | int down = 0; |
IngmarLoohuis | 0:8dc46da67c9f | 21 | |
IngmarLoohuis | 0:8dc46da67c9f | 22 | int begin_step = 0; |
IngmarLoohuis | 0:8dc46da67c9f | 23 | int teller = 0; |
IngmarLoohuis | 0:8dc46da67c9f | 24 | |
IngmarLoohuis | 0:8dc46da67c9f | 25 | int motor3_direction = up; // direction |
IngmarLoohuis | 0:8dc46da67c9f | 26 | |
IngmarLoohuis | 0:8dc46da67c9f | 27 | void motor3_position() |
IngmarLoohuis | 0:8dc46da67c9f | 28 | { |
IngmarLoohuis | 0:8dc46da67c9f | 29 | switch(begin_step) |
IngmarLoohuis | 0:8dc46da67c9f | 30 | { |
IngmarLoohuis | 0:8dc46da67c9f | 31 | case 0: motor_out = 0x1; break; // 0001 |
IngmarLoohuis | 0:8dc46da67c9f | 32 | case 1: motor_out = 0x3; break; // 0011 |
IngmarLoohuis | 0:8dc46da67c9f | 33 | case 2: motor_out = 0x2; break; // 0010 |
IngmarLoohuis | 0:8dc46da67c9f | 34 | case 3: motor_out = 0x6; break; // 0110 |
IngmarLoohuis | 0:8dc46da67c9f | 35 | case 4: motor_out = 0x4; break; // 0100 |
IngmarLoohuis | 0:8dc46da67c9f | 36 | case 5: motor_out = 0xC; break; // 1100 |
IngmarLoohuis | 0:8dc46da67c9f | 37 | case 6: motor_out = 0x8; break; // 1000 |
IngmarLoohuis | 0:8dc46da67c9f | 38 | case 7: motor_out = 0x9; break; // 1001 |
IngmarLoohuis | 0:8dc46da67c9f | 39 | |
IngmarLoohuis | 0:8dc46da67c9f | 40 | default: motor_out = 0x0; break; // 0000 |
IngmarLoohuis | 0:8dc46da67c9f | 41 | } |
IngmarLoohuis | 0:8dc46da67c9f | 42 | |
IngmarLoohuis | 0:8dc46da67c9f | 43 | if(motor3_direction==1) |
IngmarLoohuis | 0:8dc46da67c9f | 44 | { |
IngmarLoohuis | 0:8dc46da67c9f | 45 | begin_step++; |
IngmarLoohuis | 0:8dc46da67c9f | 46 | teller++; |
IngmarLoohuis | 0:8dc46da67c9f | 47 | } |
IngmarLoohuis | 0:8dc46da67c9f | 48 | else |
IngmarLoohuis | 0:8dc46da67c9f | 49 | { |
IngmarLoohuis | 0:8dc46da67c9f | 50 | begin_step--; |
IngmarLoohuis | 0:8dc46da67c9f | 51 | teller--; |
IngmarLoohuis | 0:8dc46da67c9f | 52 | } |
IngmarLoohuis | 0:8dc46da67c9f | 53 | if(begin_step>7)begin_step=0; |
IngmarLoohuis | 0:8dc46da67c9f | 54 | if(begin_step<0)begin_step=7; |
IngmarLoohuis | 0:8dc46da67c9f | 55 | pc.printf("steps = %i/n/r",teller); |
IngmarLoohuis | 0:8dc46da67c9f | 56 | |
IngmarLoohuis | 0:8dc46da67c9f | 57 | // if(teller<=-10000)//WANNEER DE FUNCTIE VAN RICHTING VERANDERD(MOET NOG STOPPEN WORDEN) |
IngmarLoohuis | 0:8dc46da67c9f | 58 | //{ |
IngmarLoohuis | 0:8dc46da67c9f | 59 | //motor3_direction=1; |
IngmarLoohuis | 0:8dc46da67c9f | 60 | // } |
IngmarLoohuis | 0:8dc46da67c9f | 61 | } |
IngmarLoohuis | 0:8dc46da67c9f | 62 | |
IngmarLoohuis | 0:8dc46da67c9f | 63 | void motor3_clean() |
IngmarLoohuis | 0:8dc46da67c9f | 64 | { |
IngmarLoohuis | 0:8dc46da67c9f | 65 | switch(begin_step) |
IngmarLoohuis | 0:8dc46da67c9f | 66 | { |
IngmarLoohuis | 0:8dc46da67c9f | 67 | case 0: motor_out = 0x1; break; // 0001 |
IngmarLoohuis | 0:8dc46da67c9f | 68 | case 1: motor_out = 0x3; break; // 0011 |
IngmarLoohuis | 0:8dc46da67c9f | 69 | case 2: motor_out = 0x2; break; // 0010 |
IngmarLoohuis | 0:8dc46da67c9f | 70 | case 3: motor_out = 0x6; break; // 0110 |
IngmarLoohuis | 0:8dc46da67c9f | 71 | case 4: motor_out = 0x4; break; // 0100 |
IngmarLoohuis | 0:8dc46da67c9f | 72 | case 5: motor_out = 0xC; break; // 1100 |
IngmarLoohuis | 0:8dc46da67c9f | 73 | case 6: motor_out = 0x8; break; // 1000 |
IngmarLoohuis | 0:8dc46da67c9f | 74 | case 7: motor_out = 0x9; break; // 1001 |
IngmarLoohuis | 0:8dc46da67c9f | 75 | |
IngmarLoohuis | 0:8dc46da67c9f | 76 | default: motor_out = 0x0; break; // 0000 |
IngmarLoohuis | 0:8dc46da67c9f | 77 | } |
IngmarLoohuis | 0:8dc46da67c9f | 78 | |
IngmarLoohuis | 0:8dc46da67c9f | 79 | if(motor3_direction==1) |
IngmarLoohuis | 0:8dc46da67c9f | 80 | { |
IngmarLoohuis | 0:8dc46da67c9f | 81 | begin_step++; |
IngmarLoohuis | 0:8dc46da67c9f | 82 | teller++; |
IngmarLoohuis | 0:8dc46da67c9f | 83 | } |
IngmarLoohuis | 0:8dc46da67c9f | 84 | else |
IngmarLoohuis | 0:8dc46da67c9f | 85 | { |
IngmarLoohuis | 0:8dc46da67c9f | 86 | begin_step--; |
IngmarLoohuis | 0:8dc46da67c9f | 87 | teller--; |
IngmarLoohuis | 0:8dc46da67c9f | 88 | } |
IngmarLoohuis | 0:8dc46da67c9f | 89 | if(begin_step>7)begin_step=0; |
IngmarLoohuis | 0:8dc46da67c9f | 90 | if(begin_step<0)begin_step=7; |
IngmarLoohuis | 0:8dc46da67c9f | 91 | pc.printf("steps = %i/n/r",teller); |
IngmarLoohuis | 0:8dc46da67c9f | 92 | |
IngmarLoohuis | 0:8dc46da67c9f | 93 | if(teller<=-1000)//WANNEER DE FUNCTIE VAN RICHTING VERANDERD(MOET NOG STOPPEN WORDEN) |
IngmarLoohuis | 0:8dc46da67c9f | 94 | { |
IngmarLoohuis | 0:8dc46da67c9f | 95 | motor3_direction=up; |
IngmarLoohuis | 0:8dc46da67c9f | 96 | } |
IngmarLoohuis | 0:8dc46da67c9f | 97 | if(teller>=1000)//WANNEER DE FUNCTIE VAN RICHTING VERANDERD(MOET NOG STOPPEN WORDEN) |
IngmarLoohuis | 0:8dc46da67c9f | 98 | { |
IngmarLoohuis | 0:8dc46da67c9f | 99 | motor3_direction=down; |
IngmarLoohuis | 0:8dc46da67c9f | 100 | } |
IngmarLoohuis | 0:8dc46da67c9f | 101 | |
IngmarLoohuis | 0:8dc46da67c9f | 102 | } |
IngmarLoohuis | 0:8dc46da67c9f | 103 | |
IngmarLoohuis | 0:8dc46da67c9f | 104 | int main() |
IngmarLoohuis | 0:8dc46da67c9f | 105 | { |
IngmarLoohuis | 0:8dc46da67c9f | 106 | t0.attach(&fn0_activate, 0.0014f); |
IngmarLoohuis | 0:8dc46da67c9f | 107 | //t7.attach(&fn7_activate, 0.0014f); |
IngmarLoohuis | 0:8dc46da67c9f | 108 | pc.baud(115200); |
IngmarLoohuis | 0:8dc46da67c9f | 109 | while(1) |
IngmarLoohuis | 0:8dc46da67c9f | 110 | { |
IngmarLoohuis | 0:8dc46da67c9f | 111 | if(fn0_go) |
IngmarLoohuis | 0:8dc46da67c9f | 112 | { |
IngmarLoohuis | 0:8dc46da67c9f | 113 | fn0_go = false; |
IngmarLoohuis | 0:8dc46da67c9f | 114 | motor3_position(); |
IngmarLoohuis | 0:8dc46da67c9f | 115 | } |
IngmarLoohuis | 0:8dc46da67c9f | 116 | //if(fn7_go) |
IngmarLoohuis | 0:8dc46da67c9f | 117 | //{ |
IngmarLoohuis | 0:8dc46da67c9f | 118 | //fn7_go = false; |
IngmarLoohuis | 0:8dc46da67c9f | 119 | //motor3_clean(); |
IngmarLoohuis | 0:8dc46da67c9f | 120 | //} |
IngmarLoohuis | 0:8dc46da67c9f | 121 | } |
IngmarLoohuis | 0:8dc46da67c9f | 122 | } |
IngmarLoohuis | 0:8dc46da67c9f | 123 | |
IngmarLoohuis | 0:8dc46da67c9f | 124 | |
IngmarLoohuis | 0:8dc46da67c9f | 125 |