As included in Thesis report.

Dependencies:   Encoder MODSERIAL mbed

Committer:
Socrates
Date:
Wed Jun 25 12:25:22 2014 +0000
Revision:
1:79c4c5746a33
Parent:
0:210b81f6c5b6
Zoals geplaatst in verslag.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Socrates 0:210b81f6c5b6 1 #include "mbed.h"
Socrates 0:210b81f6c5b6 2 #include "encoder.h"
Socrates 0:210b81f6c5b6 3 #include "MODSERIAL.h"
Socrates 0:210b81f6c5b6 4
Socrates 0:210b81f6c5b6 5 MODSERIAL pc(USBTX,USBRX);
Socrates 0:210b81f6c5b6 6 Encoder Enc(PTA1,PTA2);
Socrates 0:210b81f6c5b6 7 PwmOut motorplus(PTD3);
Socrates 0:210b81f6c5b6 8 PwmOut motormin(PTC3);
Socrates 0:210b81f6c5b6 9
Socrates 0:210b81f6c5b6 10 double deliver, ts, pos, pos1, move;
Socrates 1:79c4c5746a33 11 volatile bool flag, statloop,viscloop;
Socrates 0:210b81f6c5b6 12 double Move[25];
Socrates 0:210b81f6c5b6 13 int n;
Socrates 0:210b81f6c5b6 14
Socrates 0:210b81f6c5b6 15 void setflag(void)
Socrates 0:210b81f6c5b6 16 {
Socrates 0:210b81f6c5b6 17 flag=true;
Socrates 0:210b81f6c5b6 18 }
Socrates 0:210b81f6c5b6 19
Socrates 0:210b81f6c5b6 20
Socrates 0:210b81f6c5b6 21 int main()
Socrates 0:210b81f6c5b6 22 {
Socrates 0:210b81f6c5b6 23 pc.baud(115200);
Socrates 0:210b81f6c5b6 24
Socrates 0:210b81f6c5b6 25 Ticker Loop;
Socrates 0:210b81f6c5b6 26
Socrates 0:210b81f6c5b6 27 ts=1.0/5000.0;
Socrates 0:210b81f6c5b6 28
Socrates 0:210b81f6c5b6 29 motorplus.period(1.0/60000.0);
Socrates 0:210b81f6c5b6 30 motormin.period(1.0/60000.0);
Socrates 0:210b81f6c5b6 31 flag=true;
Socrates 1:79c4c5746a33 32 statloop=true;
Socrates 1:79c4c5746a33 33 viscloop=true;
Socrates 0:210b81f6c5b6 34 wait(3);
Socrates 1:79c4c5746a33 35
Socrates 0:210b81f6c5b6 36 Loop.attach(setflag,ts);
Socrates 1:79c4c5746a33 37 while(statloop==true) {
Socrates 0:210b81f6c5b6 38 while(flag !=true) {
Socrates 0:210b81f6c5b6 39 }
Socrates 0:210b81f6c5b6 40 flag=false;
Socrates 0:210b81f6c5b6 41 motorplus.write(-deliver);
Socrates 0:210b81f6c5b6 42 motormin.write(deliver);
Socrates 0:210b81f6c5b6 43 pos=Enc.getPosition()/1024.0;
Socrates 0:210b81f6c5b6 44 Move[n]=pos-pos1;
Socrates 0:210b81f6c5b6 45 n += 1;
Socrates 0:210b81f6c5b6 46 if (n >24) {
Socrates 0:210b81f6c5b6 47 n=0;
Socrates 0:210b81f6c5b6 48 }
Socrates 0:210b81f6c5b6 49 move=Move[24]-Move[0];
Socrates 0:210b81f6c5b6 50 if (abs(move)>0.002) {
Socrates 1:79c4c5746a33 51 statloop=false;
Socrates 0:210b81f6c5b6 52 motorplus.write(0.0);
Socrates 0:210b81f6c5b6 53 motormin.write(0.0);
Socrates 1:79c4c5746a33 54 pc.printf("Fs %f\n\r",deliver);
Socrates 1:79c4c5746a33 55 wait(1);
Socrates 1:79c4c5746a33 56 }
Socrates 1:79c4c5746a33 57
Socrates 1:79c4c5746a33 58 deliver = deliver+ts;
Socrates 1:79c4c5746a33 59
Socrates 1:79c4c5746a33 60 }
Socrates 1:79c4c5746a33 61
Socrates 1:79c4c5746a33 62 deliver=0.4;
Socrates 1:79c4c5746a33 63 motorplus.write(-deliver);
Socrates 1:79c4c5746a33 64 motormin.write(deliver);
Socrates 1:79c4c5746a33 65 wait(1.0);
Socrates 1:79c4c5746a33 66 while(viscloop==true) {
Socrates 1:79c4c5746a33 67 while(flag !=true) {
Socrates 1:79c4c5746a33 68 }
Socrates 1:79c4c5746a33 69 flag=false;
Socrates 1:79c4c5746a33 70 motorplus.write(-deliver);
Socrates 1:79c4c5746a33 71 motormin.write(deliver);
Socrates 1:79c4c5746a33 72 pos=Enc.getPosition()/1024.0;
Socrates 1:79c4c5746a33 73 Move[n]=pos-pos1;
Socrates 1:79c4c5746a33 74 n += 1;
Socrates 1:79c4c5746a33 75 if (n >24) {
Socrates 1:79c4c5746a33 76 n=0;
Socrates 1:79c4c5746a33 77 }
Socrates 1:79c4c5746a33 78 move=Move[24]-Move[0];
Socrates 1:79c4c5746a33 79
Socrates 1:79c4c5746a33 80 if (abs(move)==0.0) {
Socrates 1:79c4c5746a33 81 viscloop=false;
Socrates 1:79c4c5746a33 82 motorplus.write(0.0);
Socrates 1:79c4c5746a33 83 motormin.write(0.0);
Socrates 1:79c4c5746a33 84 pc.printf("Fv %f\n\r",deliver);
Socrates 0:210b81f6c5b6 85 return 0;
Socrates 0:210b81f6c5b6 86 }
Socrates 1:79c4c5746a33 87 deliver = deliver-ts;
Socrates 0:210b81f6c5b6 88
Socrates 0:210b81f6c5b6 89 }
Socrates 0:210b81f6c5b6 90
Socrates 0:210b81f6c5b6 91
Socrates 0:210b81f6c5b6 92 }