CARLO TARALLO
/
MedicineSettori_2
Medi
main.cpp@1:b974ec8fe646, 2020-03-11 (annotated)
- Committer:
- carlotara
- Date:
- Wed Mar 11 15:12:13 2020 +0000
- Revision:
- 1:b974ec8fe646
- Parent:
- 0:6999a083fb46
Medici
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
XtaticO | 0:6999a083fb46 | 1 | /* |
XtaticO | 0:6999a083fb46 | 2 | ############################################ |
XtaticO | 0:6999a083fb46 | 3 | ## sMotor v0.1 Test Program ## |
XtaticO | 0:6999a083fb46 | 4 | ## created by Samuel Matildes ## |
XtaticO | 0:6999a083fb46 | 5 | ############################################ |
XtaticO | 0:6999a083fb46 | 6 | ---- sam.naeec@gmail.com ----- |
XtaticO | 0:6999a083fb46 | 7 | This library was made for 4-Phase Stepper Motors |
XtaticO | 0:6999a083fb46 | 8 | I don't take any resposability for the damage caused to your equipment. |
XtaticO | 0:6999a083fb46 | 9 | |
XtaticO | 0:6999a083fb46 | 10 | */ |
XtaticO | 0:6999a083fb46 | 11 | |
XtaticO | 0:6999a083fb46 | 12 | #include "mbed.h" |
carlotara | 1:b974ec8fe646 | 13 | |
XtaticO | 0:6999a083fb46 | 14 | |
XtaticO | 0:6999a083fb46 | 15 | |
XtaticO | 0:6999a083fb46 | 16 | Serial pc(USBTX, USBRX); |
XtaticO | 0:6999a083fb46 | 17 | |
carlotara | 1:b974ec8fe646 | 18 | //sMotor motor(D8, D9, D10, D11); // creates new stepper motor: IN1, IN2, IN3, IN4 |
carlotara | 1:b974ec8fe646 | 19 | //sMotor motor(p9, p10, p11, p12); // creates new stepper motor: IN1, IN2, IN3, IN4 |
XtaticO | 0:6999a083fb46 | 20 | int step_speed = 1200 ; // set default motor speed |
carlotara | 1:b974ec8fe646 | 21 | //int numstep = 512 ; // defines full turn of 360 degree |
carlotara | 1:b974ec8fe646 | 22 | int numstep = 514 |
carlotara | 1:b974ec8fe646 | 23 | ; // defines full turn of 360 degree |
XtaticO | 0:6999a083fb46 | 24 | //you might want to calibrate this value according to your motor |
carlotara | 1:b974ec8fe646 | 25 | int statomax =7; |
carlotara | 1:b974ec8fe646 | 26 | //int kmax = 2; |
carlotara | 1:b974ec8fe646 | 27 | //int statomax =7; |
carlotara | 1:b974ec8fe646 | 28 | int kmax = 8; |
carlotara | 1:b974ec8fe646 | 29 | int stato; |
carlotara | 1:b974ec8fe646 | 30 | int ora = 1; |
carlotara | 1:b974ec8fe646 | 31 | int data = 0; |
carlotara | 1:b974ec8fe646 | 32 | //int statomax =7; |
carlotara | 1:b974ec8fe646 | 33 | |
carlotara | 1:b974ec8fe646 | 34 | |
carlotara | 1:b974ec8fe646 | 35 | DigitalOut IN1(D8); |
carlotara | 1:b974ec8fe646 | 36 | DigitalOut IN2(D9); |
carlotara | 1:b974ec8fe646 | 37 | DigitalOut IN3(D10); |
carlotara | 1:b974ec8fe646 | 38 | DigitalOut IN4(D11); |
carlotara | 1:b974ec8fe646 | 39 | |
carlotara | 1:b974ec8fe646 | 40 | /* |
carlotara | 1:b974ec8fe646 | 41 | const int IN1 = 8; // filo motore BLUE |
carlotara | 1:b974ec8fe646 | 42 | const int IN2 = 9; // filo motore ROSA |
carlotara | 1:b974ec8fe646 | 43 | const int IN3 = 10; // filo motore GIALLO |
carlotara | 1:b974ec8fe646 | 44 | const int IN4 = 11; // filo motore ARANCIO |
carlotara | 1:b974ec8fe646 | 45 | */ |
carlotara | 1:b974ec8fe646 | 46 | const int MotoreOFF = 99; // motore spento |
carlotara | 1:b974ec8fe646 | 47 | |
carlotara | 1:b974ec8fe646 | 48 | |
carlotara | 1:b974ec8fe646 | 49 | |
carlotara | 1:b974ec8fe646 | 50 | void Uscita( int i4,int i3,int i2,int i1) |
carlotara | 1:b974ec8fe646 | 51 | { |
carlotara | 1:b974ec8fe646 | 52 | if (i1==1) IN1=1; else IN1=0; |
carlotara | 1:b974ec8fe646 | 53 | if (i2==1) IN2=1; else IN2=0; |
carlotara | 1:b974ec8fe646 | 54 | if (i3==1) IN3=1; else IN3=0; |
carlotara | 1:b974ec8fe646 | 55 | if (i4==1) IN4=1; else IN4=0; |
carlotara | 1:b974ec8fe646 | 56 | /* |
carlotara | 1:b974ec8fe646 | 57 | if (i1==1) digitalWrite(IN1,HIGH); else digitalWrite(IN1,LOW); |
carlotara | 1:b974ec8fe646 | 58 | if (i2==1) digitalWrite(IN2,HIGH); else digitalWrite(IN2,LOW); |
carlotara | 1:b974ec8fe646 | 59 | if (i3==1) digitalWrite(IN3,HIGH); else digitalWrite(IN3,LOW); |
carlotara | 1:b974ec8fe646 | 60 | if (i4==1) digitalWrite(IN4,HIGH); else digitalWrite(IN4,LOW); |
carlotara | 1:b974ec8fe646 | 61 | */ |
carlotara | 1:b974ec8fe646 | 62 | } |
carlotara | 1:b974ec8fe646 | 63 | |
XtaticO | 0:6999a083fb46 | 64 | |
XtaticO | 0:6999a083fb46 | 65 | |
carlotara | 1:b974ec8fe646 | 66 | void EseguiPasso(int stato) |
carlotara | 1:b974ec8fe646 | 67 | { |
carlotara | 1:b974ec8fe646 | 68 | int i1,i2,i3,i4; |
XtaticO | 0:6999a083fb46 | 69 | |
carlotara | 1:b974ec8fe646 | 70 | switch ( stato ) |
carlotara | 1:b974ec8fe646 | 71 | {// vedi tabella nel pdf del motore passo passo |
carlotara | 1:b974ec8fe646 | 72 | case 0: Uscita(0,0,0,1); break; |
carlotara | 1:b974ec8fe646 | 73 | case 1: Uscita(0,0,1,1); break; |
carlotara | 1:b974ec8fe646 | 74 | case 2: Uscita(0,0,1,0); break; |
carlotara | 1:b974ec8fe646 | 75 | case 3: Uscita(0,1,1,0); break; |
carlotara | 1:b974ec8fe646 | 76 | case 4: Uscita(0,1,0,0); break; |
carlotara | 1:b974ec8fe646 | 77 | case 5: Uscita(1,1,0,0); break; |
carlotara | 1:b974ec8fe646 | 78 | case 6: Uscita(1,0,0,0); break; |
carlotara | 1:b974ec8fe646 | 79 | case 7: Uscita(1,0,0,1); break; |
carlotara | 1:b974ec8fe646 | 80 | case 8: Uscita(0,0,0,1); break; |
carlotara | 1:b974ec8fe646 | 81 | case 9: Uscita(0,0,1,1); break; |
carlotara | 1:b974ec8fe646 | 82 | case 10: Uscita(0,0,1,0); break; |
carlotara | 1:b974ec8fe646 | 83 | case 11: Uscita(0,1,1,0); break; |
carlotara | 1:b974ec8fe646 | 84 | case 12: Uscita(0,1,0,0); break; |
carlotara | 1:b974ec8fe646 | 85 | case 13: Uscita(1,1,0,0); break; |
carlotara | 1:b974ec8fe646 | 86 | case 14: Uscita(1,0,0,0); break; |
carlotara | 1:b974ec8fe646 | 87 | // case 7: Uscita(1,0,0,1); break; |
carlotara | 1:b974ec8fe646 | 88 | case MotoreOFF: //OFF |
carlotara | 1:b974ec8fe646 | 89 | Uscita(0,0,0,0); break; |
carlotara | 1:b974ec8fe646 | 90 | } |
carlotara | 1:b974ec8fe646 | 91 | wait_ms(1); |
carlotara | 1:b974ec8fe646 | 92 | // delay(1); //ritardo almeno 1 mS |
carlotara | 1:b974ec8fe646 | 93 | } |
XtaticO | 0:6999a083fb46 | 94 | |
carlotara | 1:b974ec8fe646 | 95 | void RitardoAccensione() |
carlotara | 1:b974ec8fe646 | 96 | { //attesa prima di attivare il motore |
carlotara | 1:b974ec8fe646 | 97 | EseguiPasso(MotoreOFF); |
carlotara | 1:b974ec8fe646 | 98 | for(int i=0; i<10; i++) |
carlotara | 1:b974ec8fe646 | 99 | { |
carlotara | 1:b974ec8fe646 | 100 | // digitalWrite(13,HIGH); |
carlotara | 1:b974ec8fe646 | 101 | // wait_ms(250); |
carlotara | 1:b974ec8fe646 | 102 | // delay(250); |
carlotara | 1:b974ec8fe646 | 103 | // digitalWrite(13,LOW); |
carlotara | 1:b974ec8fe646 | 104 | // wait_ms(250); |
carlotara | 1:b974ec8fe646 | 105 | // delay(250); |
carlotara | 1:b974ec8fe646 | 106 | } |
carlotara | 1:b974ec8fe646 | 107 | } |
XtaticO | 0:6999a083fb46 | 108 | |
carlotara | 1:b974ec8fe646 | 109 | int main() |
carlotara | 1:b974ec8fe646 | 110 | { |
XtaticO | 0:6999a083fb46 | 111 | |
carlotara | 1:b974ec8fe646 | 112 | RitardoAccensione(); //2 secondi di pausa prima di iniziare |
carlotara | 1:b974ec8fe646 | 113 | while (1) { |
carlotara | 1:b974ec8fe646 | 114 | char kmaxCh; |
carlotara | 1:b974ec8fe646 | 115 | printf("Inserisci numero Settore \r\n"); |
carlotara | 1:b974ec8fe646 | 116 | // scanf("%c", &kmaxCh); |
carlotara | 1:b974ec8fe646 | 117 | // printf("%c\n",kmaxCh); |
carlotara | 1:b974ec8fe646 | 118 | scanf("%d", &kmax); |
carlotara | 1:b974ec8fe646 | 119 | printf("%d\n",kmax); |
carlotara | 1:b974ec8fe646 | 120 | //char numberstring[] = "42"; |
carlotara | 1:b974ec8fe646 | 121 | //int number; |
carlotara | 1:b974ec8fe646 | 122 | |
carlotara | 1:b974ec8fe646 | 123 | //sscanf(numberstring, "%d", &number); |
carlotara | 1:b974ec8fe646 | 124 | // kmax = 2; |
carlotara | 1:b974ec8fe646 | 125 | stato=0; //inizio da uno stato arbitrario |
XtaticO | 0:6999a083fb46 | 126 | |
XtaticO | 0:6999a083fb46 | 127 | |
carlotara | 1:b974ec8fe646 | 128 | if (kmax>0) |
carlotara | 1:b974ec8fe646 | 129 | { |
carlotara | 1:b974ec8fe646 | 130 | stato=0; //inizio da uno stato arbitrario |
XtaticO | 0:6999a083fb46 | 131 | |
carlotara | 1:b974ec8fe646 | 132 | for(int k=0; k<kmax; k++) //Rotazione Oraria |
carlotara | 1:b974ec8fe646 | 133 | // for(int k=0; k<4; k++) //Rotazione Oraria |
carlotara | 1:b974ec8fe646 | 134 | { |
carlotara | 1:b974ec8fe646 | 135 | for (int i=0; i<numstep; i++) //45 gradi a ciclo |
carlotara | 1:b974ec8fe646 | 136 | // for (int i=0; i<514; i++) //45 gradi a ciclo |
carlotara | 1:b974ec8fe646 | 137 | // for (int i=0; i<1024; i++) //90 gradi a ciclo |
carlotara | 1:b974ec8fe646 | 138 | { |
carlotara | 1:b974ec8fe646 | 139 | EseguiPasso(stato); |
carlotara | 1:b974ec8fe646 | 140 | stato=stato+1; // avanza nella tabella |
carlotara | 1:b974ec8fe646 | 141 | /* printf("Stato : \r\n"); |
carlotara | 1:b974ec8fe646 | 142 | printf("%d\r\n",stato); |
carlotara | 1:b974ec8fe646 | 143 | printf("k : \r\n"); |
carlotara | 1:b974ec8fe646 | 144 | printf("%d\r\n",k); |
carlotara | 1:b974ec8fe646 | 145 | */ |
carlotara | 1:b974ec8fe646 | 146 | if ((stato)>statomax) |
carlotara | 1:b974ec8fe646 | 147 | { |
carlotara | 1:b974ec8fe646 | 148 | stato=0; |
carlotara | 1:b974ec8fe646 | 149 | // if ((stato)>7) stato=0; |
carlotara | 1:b974ec8fe646 | 150 | } |
carlotara | 1:b974ec8fe646 | 151 | EseguiPasso(MotoreOFF); |
carlotara | 1:b974ec8fe646 | 152 | // wait_ms(500); // pausa di mezzo secondo |
carlotara | 1:b974ec8fe646 | 153 | } |
carlotara | 1:b974ec8fe646 | 154 | wait_ms(1000); // pausa di 1 secondo |
carlotara | 1:b974ec8fe646 | 155 | } |
carlotara | 1:b974ec8fe646 | 156 | for(int k=0; k<kmax; k++) //Rotazione AntiOraria |
carlotara | 1:b974ec8fe646 | 157 | // for(int k=0; k<4; k++) //Rotazione AntiOraria |
carlotara | 1:b974ec8fe646 | 158 | { |
carlotara | 1:b974ec8fe646 | 159 | for (int i=0; i<numstep; i++) //45 gradi a ciclo |
carlotara | 1:b974ec8fe646 | 160 | // for (int i=0; i<514; i++) //45 gradi a ciclo |
carlotara | 1:b974ec8fe646 | 161 | // for (int i=0; i<1024; i++) //90 gradi a ciclo |
carlotara | 1:b974ec8fe646 | 162 | { |
carlotara | 1:b974ec8fe646 | 163 | EseguiPasso(stato); |
carlotara | 1:b974ec8fe646 | 164 | stato=stato-1; //torna indietro nella tabella |
carlotara | 1:b974ec8fe646 | 165 | /* printf("Stato : \r\n"); |
carlotara | 1:b974ec8fe646 | 166 | printf("%d\r\n",stato); |
carlotara | 1:b974ec8fe646 | 167 | printf("k : \r\n"); |
carlotara | 1:b974ec8fe646 | 168 | printf("%d\r\n",k); |
carlotara | 1:b974ec8fe646 | 169 | */ |
carlotara | 1:b974ec8fe646 | 170 | if ((stato)<0) |
carlotara | 1:b974ec8fe646 | 171 | { |
carlotara | 1:b974ec8fe646 | 172 | stato=statomax; |
carlotara | 1:b974ec8fe646 | 173 | // if ((stato)<0) stato=7; |
carlotara | 1:b974ec8fe646 | 174 | } |
carlotara | 1:b974ec8fe646 | 175 | EseguiPasso(MotoreOFF); |
carlotara | 1:b974ec8fe646 | 176 | // wait_ms(500); |
XtaticO | 0:6999a083fb46 | 177 | } |
carlotara | 1:b974ec8fe646 | 178 | wait_ms(1000); |
XtaticO | 0:6999a083fb46 | 179 | } |
carlotara | 1:b974ec8fe646 | 180 | } |
carlotara | 1:b974ec8fe646 | 181 | |
carlotara | 1:b974ec8fe646 | 182 | } |
carlotara | 1:b974ec8fe646 | 183 | } |