hfdh
Dependencies: Encoder MODSERIAL mbed
Fork of BO by
Revision 6:7ff223862008, committed 2014-06-30
- Comitter:
- lubbermans
- Date:
- Mon Jun 30 12:42:21 2014 +0000
- Parent:
- 5:31120c4c08f0
- Commit message:
- hoi
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 31120c4c08f0 -r 7ff223862008 main.cpp --- a/main.cpp Thu Jun 12 12:59:32 2014 +0000 +++ b/main.cpp Mon Jun 30 12:42:21 2014 +0000 @@ -1,200 +1,48 @@ #include "mbed.h" #include "encoder.h" #include "MODSERIAL.h" -#include "time.h" -#include "ref.h" -#include "Tijd.h" - -MODSERIAL pc(USBTX,USBRX); -Encoder Enc(PTA1,PTA2); -PwmOut motorplus(PTD3); -PwmOut motormin(PTC3); -double rpm, pos, pos1, t, Ref, Error, deliver; -double Prop, Kp, Int, Int1, Ki, Ctrl; -double pwmotor; -double speed, move, size; -volatile bool looptimerflag, calflag, scriptflag; -double Move[25]; -//double Tijd[9]; -//double Referentie[9]; -int n; +PwmOut pwmA(PTA1); +PwmOut pwmB(PTD2); +DigitalOut dirA(PTD3); +DigitalOut dirB(PTD1); +Encoder motor1(PTC3,PTB23); +MODSERIAL pc(USBTX,USBRX); +Ticker timer; +Ticker scripttimer; -void setlooptimerflag(void) +int x ; +volatile bool scriptflag; + +void looper() { - looptimerflag = true; + pc.printf("%i\n", x); + } -void setscriptflag(void) -{ - scriptflag = true; -} -void setcalflag(void) +void scripter() { - calflag = true; -} -void printdata(void) -{ - pc.printf("Speed %f Pos %f del %f\n\r",speed, pos, deliver); + scriptflag=true; } -void printdata2(void) -{ - pc.printf("Del %f Ctrl %f\n\r",deliver,Ctrl); -} -void printdata3(void) -{ - pc.printf("P %f R %f E %f\n\r",pos, Ref,Error); -} -void printdata4(void) -{ - pc.printf("%f\n",Error); -} - int main() { + pwmA.period(1.0/20000.0); + pwmB.period(1.0/20000.0); pc.baud(115200); - wait(1); - pc.printf("start\n\r"); - wait(1); - //Inladen data. Courtesy of http://runnable.com/UpSpP5ymsslIAAFo/reading-a-file-into-an-array-for-c%2B%2B-and-fstream - - //Nu gaan we weer verder met het normale programma - Ticker looptimer; - Ticker printer; - Ticker script; - Ticker calibratie; - double ts=(1.0/5000.0); - double tsd=(1.0/5.0); - motorplus.period(1.0/60000.0); - motormin.period(1.0/60000.0); - - //Kp = 1.1471/1; - //Ki = 4.9157/1; - Kp=1.5; - Ki=0.5; - pwmotor=-0.9/2.0; - deliver=0.01*pwmotor; - pos1=0; - n=0; - size=sizeof(Referentie)/sizeof(float); - t=0; - Move[24]=pwmotor*-100.0; - pos=Enc.getPosition(); - speed=Enc.getSpeed(); +pc.printf("hoi"); + scripttimer.attach(scripter,1.0/5000.0); + timer.attach(looper, 0.1); + while(1) { - /* - Referentie[0]=20.0; - Referentie[1]=17.0; - Referentie[2]=14.0; - Referentie[3]=20.0; - Referentie[4]=16.0; - Referentie[5]=20.0; - Referentie[6]=14.0; - Referentie[7]=25.0; - Referentie[8]=23.0; - */ - /* - Tijd[0]=0; - Tijd[1]=5.5; - Tijd[2]=7.0; - Tijd[3]=9.25; - Tijd[4]=12.0; - Tijd[5]=15.5; - Tijd[6]=17.5; - Tijd[7]=23.0; - Tijd[8]=25.0; - */ - wait(1); - pc.printf("start2\n\r"); - wait(1); + while(scriptflag==false) { + } + scriptflag=false; + - scriptflag = true; - //printer.attach(printdata,tsd); - //script.attach(setscriptflag, ts); - calibratie.attach(setcalflag,ts); - while(scriptflag == true) { - while(calflag != true) { - } - calflag=false; - if (abs(deliver) <= abs(pwmotor)) { - deliver=deliver*1.01; - } - motorplus.write(0.5+deliver); - motormin.write(0.5-deliver); - //pc.printf("pwm %f\n\r",deliver); - speed=Enc.getSpeed(); - pos1=pos; - pos=Enc.getPosition()/1024.0; - Move[n]=pos-pos1; - n += 1; - if (n >24) { - n=0; - } - move=Move[24]-Move[0]; - //pc.printf("Pwm %f Pos %f\n\r",deliver,pos); + x=motor1.getPosition(); - if(Move[24] == 0.0 && Move[0] == 0.0 && abs(pos) > 0.5) { - motorplus.write(0.5); - motormin.write(0.5); - //pwmotor = pwmotor*-1.0; - //deliver=0.01*pwmotor; - pc.printf("Einde\n\r"); - calibratie.detach(); - printer.detach(); - calflag=false; - scriptflag=false; - wait(1); - } + dirA=1; + pwmA=0.5; + } - - - - Enc.setPosition(0.0); - pwmotor=0.0; - deliver=0.01; - pc.printf("Begin loop\n\r"); - wait(1); - printer.attach(printdata4,tsd); - looptimer.attach(setlooptimerflag,ts); - Ref=Referentie[0]; - while(1) { - while (looptimerflag != true) {} - looptimerflag = false; -//Encoder - pos=Enc.getPosition()/1024.0; - speed=Enc.getSpeed(); - - if (t >= Tijd[n]) { - Ref=Referentie[n]; - n +=1; - if (n >=size) { - n=size; - } - } - t = t + ts; - -//Regelaar - Error=Ref-pos; - Prop=Error*Kp; - Int1=Int; - Int=(Int1+ts*Error)*Ki; - Ctrl=Prop+Int; - - pwmotor=Ctrl; - deliver=pwmotor; - motorplus.write(0.5+deliver); - motormin.write(0.5-deliver); - if (t >= size/100) { - motorplus.write(0.0); - motormin.write(0.0); - pc.printf("\n\rBeindigd door tijd"); - return 0; - } - if (abs(pos) >= 30 || (t >=5.0 && pos <=0.5)) { - motorplus.write(0.0); - motormin.write(0.0); - pc.printf("\n\rBeindigd door positie\n\r"); - return 0; - } - } } \ No newline at end of file