Upravljanje unipolasnim motorom

Committer:
mario_meh
Date:
Mon Jan 30 00:22:04 2017 +0000
Revision:
1:9cd4fd37eee0
Parent:
0:a3d5b06d790a
Child:
2:b769f13c7523
Ure?ivanje API poja?njenja

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mario_meh 1:9cd4fd37eee0 1 /** Projektiranje ugradbenih računalnih sustava
mario_meh 1:9cd4fd37eee0 2 * Interakcija sa linijom
mario_meh 1:9cd4fd37eee0 3 * @author: mario_meh
mario_meh 1:9cd4fd37eee0 4 * @code
mario_meh 1:9cd4fd37eee0 5 #include "mbed.h"
mario_meh 1:9cd4fd37eee0 6 #include "umotor.h"
mario_meh 1:9cd4fd37eee0 7
mario_meh 1:9cd4fd37eee0 8 * int main() {
mario_meh 1:9cd4fd37eee0 9 * Steper *bipolar = new Steper();
mario_meh 1:9cd4fd37eee0 10 * while(1) {
mario_meh 1:9cd4fd37eee0 11 * bipolar->u_motor();
mario_meh 1:9cd4fd37eee0 12 * }
mario_meh 1:9cd4fd37eee0 13 * }
mario_meh 1:9cd4fd37eee0 14 * @endcode
mario_meh 1:9cd4fd37eee0 15 */
mario_meh 0:a3d5b06d790a 16 #include "mbed.h"
mario_meh 0:a3d5b06d790a 17 #include "umotor.h"
mario_meh 0:a3d5b06d790a 18
mario_meh 0:a3d5b06d790a 19 volatile uint8_t speed_step = 0;
mario_meh 0:a3d5b06d790a 20 uint32_t g_tmr_multiplicator = 5;
mario_meh 0:a3d5b06d790a 21 int koraka;
mario_meh 0:a3d5b06d790a 22 int available = 1;
mario_meh 0:a3d5b06d790a 23 static uint8_t step = 0;
mario_meh 1:9cd4fd37eee0 24 /** Unipolarni motor radi u half-step modu */
mario_meh 0:a3d5b06d790a 25 const uint8_t schrittfolge[] = {0b1001, 0b1010, 0b0110, 0b0101}; // Vollschritt zwei Phasen
mario_meh 1:9cd4fd37eee0 26 /** Konstruktor
mario_meh 1:9cd4fd37eee0 27 * inicijalizacija je: Steper <naziv> = new Steper;
mario_meh 1:9cd4fd37eee0 28 */
mario_meh 0:a3d5b06d790a 29 Steper::Steper() : coil1(PTD2), coil2(PTD0), coil3(PTD5), coil4(PTA13),
mario_meh 0:a3d5b06d790a 30 ledSec(PTB18), riseL(PTD6), fallL(PTD7), pbone(PTA5), pbtwo(PTA4)
mario_meh 0:a3d5b06d790a 31
mario_meh 0:a3d5b06d790a 32 {
mario_meh 0:a3d5b06d790a 33
mario_meh 0:a3d5b06d790a 34 //toggler.attach(this, &Steper::timer_isr, 0.001);
mario_meh 0:a3d5b06d790a 35
mario_meh 0:a3d5b06d790a 36 }
mario_meh 0:a3d5b06d790a 37
mario_meh 0:a3d5b06d790a 38 bool pbonePressed = false;
mario_meh 0:a3d5b06d790a 39 bool pbtwoPressed = false;
mario_meh 0:a3d5b06d790a 40
mario_meh 0:a3d5b06d790a 41 void Steper::stepper_step()
mario_meh 0:a3d5b06d790a 42 {
mario_meh 0:a3d5b06d790a 43
mario_meh 0:a3d5b06d790a 44 static uint8_t step = 0;
mario_meh 0:a3d5b06d790a 45
mario_meh 0:a3d5b06d790a 46 coil1 = 0x01 & (schrittfolge[step] >> 0 );
mario_meh 0:a3d5b06d790a 47 coil2 = 0x01 & (schrittfolge[step] >> 1 );
mario_meh 0:a3d5b06d790a 48 coil3 = 0x01 & (schrittfolge[step] >> 2 );
mario_meh 0:a3d5b06d790a 49 coil4 = 0x01 & (schrittfolge[step] >> 3 );
mario_meh 0:a3d5b06d790a 50
mario_meh 0:a3d5b06d790a 51 step++;
mario_meh 0:a3d5b06d790a 52
mario_meh 0:a3d5b06d790a 53 if (step >= ARRAY_SIZE(schrittfolge))
mario_meh 0:a3d5b06d790a 54 step = 0;
mario_meh 0:a3d5b06d790a 55
mario_meh 0:a3d5b06d790a 56 }
mario_meh 1:9cd4fd37eee0 57 /** <timer_isr()> zove stepper_steo() */
mario_meh 0:a3d5b06d790a 58 void Steper::timer_isr() {
mario_meh 0:a3d5b06d790a 59
mario_meh 0:a3d5b06d790a 60 static uint32_t g_tmr_cnt = 0;
mario_meh 0:a3d5b06d790a 61 g_tmr_cnt++;
mario_meh 0:a3d5b06d790a 62 if (g_tmr_cnt >= g_tmr_multiplicator) {
mario_meh 0:a3d5b06d790a 63 stepper_step();
mario_meh 0:a3d5b06d790a 64 g_tmr_cnt = 0;
mario_meh 0:a3d5b06d790a 65 }
mario_meh 0:a3d5b06d790a 66
mario_meh 0:a3d5b06d790a 67 }
mario_meh 0:a3d5b06d790a 68
mario_meh 0:a3d5b06d790a 69 void Steper::pboneCallback(void) {
mario_meh 0:a3d5b06d790a 70 if(t.read_ms() > 20) {
mario_meh 0:a3d5b06d790a 71 pbonePressed = true;
mario_meh 0:a3d5b06d790a 72 }
mario_meh 0:a3d5b06d790a 73 t.reset();
mario_meh 0:a3d5b06d790a 74 }
mario_meh 0:a3d5b06d790a 75 void Steper::pbtwoCallback(void) {
mario_meh 0:a3d5b06d790a 76 if(t.read_ms() > 20) {
mario_meh 0:a3d5b06d790a 77 pbtwoPressed = true;
mario_meh 0:a3d5b06d790a 78 }
mario_meh 0:a3d5b06d790a 79 t.reset();
mario_meh 0:a3d5b06d790a 80 }
mario_meh 0:a3d5b06d790a 81 void Steper::togglerOff() { ledSec = 0; }
mario_meh 0:a3d5b06d790a 82 void Steper::togglerRed() {
mario_meh 0:a3d5b06d790a 83 ledSec = 1;
mario_meh 0:a3d5b06d790a 84 led.detach();
mario_meh 0:a3d5b06d790a 85 led.attach(this, &Steper::togglerOff, 0.2);
mario_meh 0:a3d5b06d790a 86 }
mario_meh 0:a3d5b06d790a 87
mario_meh 0:a3d5b06d790a 88 void motorStall() {
mario_meh 0:a3d5b06d790a 89 //toggler.detach();
mario_meh 0:a3d5b06d790a 90 }
mario_meh 0:a3d5b06d790a 91
mario_meh 0:a3d5b06d790a 92 void Steper::u_motor() {
mario_meh 0:a3d5b06d790a 93 riseL = 1;
mario_meh 0:a3d5b06d790a 94 fallL = 1;
mario_meh 0:a3d5b06d790a 95 ledSec = 1;
mario_meh 1:9cd4fd37eee0 96 /** Imam flag za 2 tipkala */
mario_meh 0:a3d5b06d790a 97 pbone.fall(this, &Steper::pboneCallback);
mario_meh 0:a3d5b06d790a 98 pbtwo.fall(this, &Steper::pbtwoCallback);
mario_meh 1:9cd4fd37eee0 99 /** Započni timer da bi mogao zvati prekid <timer_isr> */
mario_meh 0:a3d5b06d790a 100 t.start();
mario_meh 0:a3d5b06d790a 101 toggler.attach(this, &Steper::timer_isr, 0.001);
mario_meh 0:a3d5b06d790a 102
mario_meh 0:a3d5b06d790a 103 while(1) {
mario_meh 0:a3d5b06d790a 104 static uint8_t speed_step = 0;
mario_meh 0:a3d5b06d790a 105 if (pbonePressed) {
mario_meh 0:a3d5b06d790a 106 toggler.attach(this, &Steper::timer_isr, 0.001);
mario_meh 0:a3d5b06d790a 107 pbonePressed = !pbonePressed;
mario_meh 0:a3d5b06d790a 108 led.attach(this, &Steper::togglerRed, 1);
mario_meh 0:a3d5b06d790a 109 // Do actions required when button one is pressed.
mario_meh 0:a3d5b06d790a 110 riseL = !riseL;
mario_meh 0:a3d5b06d790a 111
mario_meh 0:a3d5b06d790a 112 switch(speed_step){
mario_meh 0:a3d5b06d790a 113 case 0:
mario_meh 0:a3d5b06d790a 114 g_tmr_multiplicator = 5;
mario_meh 0:a3d5b06d790a 115 speed_step++;
mario_meh 0:a3d5b06d790a 116 break;
mario_meh 0:a3d5b06d790a 117
mario_meh 0:a3d5b06d790a 118 case 1:
mario_meh 0:a3d5b06d790a 119 g_tmr_multiplicator = 10;
mario_meh 0:a3d5b06d790a 120 speed_step++;
mario_meh 0:a3d5b06d790a 121 break;
mario_meh 0:a3d5b06d790a 122
mario_meh 0:a3d5b06d790a 123 case 2:
mario_meh 0:a3d5b06d790a 124 g_tmr_multiplicator = 20;
mario_meh 0:a3d5b06d790a 125 speed_step++;
mario_meh 0:a3d5b06d790a 126 break;
mario_meh 0:a3d5b06d790a 127
mario_meh 0:a3d5b06d790a 128 case 3:
mario_meh 0:a3d5b06d790a 129 g_tmr_multiplicator = 50;
mario_meh 0:a3d5b06d790a 130 speed_step = 0;
mario_meh 0:a3d5b06d790a 131 break;
mario_meh 0:a3d5b06d790a 132
mario_meh 0:a3d5b06d790a 133 default:
mario_meh 0:a3d5b06d790a 134 speed_step = 0;
mario_meh 0:a3d5b06d790a 135 break;
mario_meh 0:a3d5b06d790a 136 }
mario_meh 0:a3d5b06d790a 137 }
mario_meh 0:a3d5b06d790a 138 if (pbtwoPressed) {
mario_meh 0:a3d5b06d790a 139 pbtwoPressed = false;
mario_meh 0:a3d5b06d790a 140 // Do actions required when button two is pressed.
mario_meh 0:a3d5b06d790a 141 fallL = !fallL;
mario_meh 0:a3d5b06d790a 142 toggler.detach();
mario_meh 0:a3d5b06d790a 143
mario_meh 0:a3d5b06d790a 144 //void motorStall();
mario_meh 0:a3d5b06d790a 145 }
mario_meh 0:a3d5b06d790a 146 }
mario_meh 0:a3d5b06d790a 147 }