Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Motor_NIT_Nagaoka_College PID Servo QEI SoftPWM mbed HBridge
main.cpp@8:9d8999740604, 2015-09-14 (annotated)
- Committer:
- WAT34
- Date:
- Mon Sep 14 04:51:25 2015 +0000
- Revision:
- 8:9d8999740604
- Parent:
- 7:920cbfb28112
- Child:
- 9:98ef1eee7ace
- Child:
- 10:fefff533c442
brushless ver
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| WAT34 | 3:d2c733b52600 | 1 | #define pi 3.141593 |
| WAT34 | 0:00fcc71314cf | 2 | #include "mbed.h" |
| WAT34 | 0:00fcc71314cf | 3 | #include "Motor.h" |
| WAT34 | 3:d2c733b52600 | 4 | #include "PID.h" |
| WAT34 | 3:d2c733b52600 | 5 | #include "QEI.h" |
| WAT34 | 8:9d8999740604 | 6 | #include "Servo.h" |
| WAT34 | 4:646562d80dc2 | 7 | #define RATE 0.05 |
| WAT34 | 8:9d8999740604 | 8 | BusOut air(PC_10,PC_11); |
| WAT34 | 8:9d8999740604 | 9 | DigitalOut out(PC_12); |
| WAT34 | 8:9d8999740604 | 10 | Serial conn(PA_15,PB_7); |
| WAT34 | 2:74c543a0a671 | 11 | Serial pc(USBTX,USBRX); |
| WAT34 | 4:646562d80dc2 | 12 | PID Tp(50,40000,0,0.001); |
| WAT34 | 3:d2c733b52600 | 13 | BusOut led(LED1,LED2,LED3,LED4); |
| WAT34 | 8:9d8999740604 | 14 | Motor ot(PB_13,PB_3,PA_10); |
| WAT34 | 8:9d8999740604 | 15 | QEI sensort(PC_3,PC_0,NC,624); |
| WAT34 | 8:9d8999740604 | 16 | Servo L(PC_9); |
| WAT34 | 8:9d8999740604 | 17 | Servo R(PC_8); |
| WAT34 | 3:d2c733b52600 | 18 | Timeout ai; |
| WAT34 | 0:00fcc71314cf | 19 | char read; |
| WAT34 | 3:d2c733b52600 | 20 | int Rs = 0,Ls = 0; |
| WAT34 | 3:d2c733b52600 | 21 | int i = 0; |
| WAT34 | 3:d2c733b52600 | 22 | void zero(){ |
| WAT34 | 3:d2c733b52600 | 23 | air = 0; |
| WAT34 | 3:d2c733b52600 | 24 | i = 0; |
| WAT34 | 3:d2c733b52600 | 25 | out = 1; |
| WAT34 | 3:d2c733b52600 | 26 | } |
| WAT34 | 3:d2c733b52600 | 27 | void rev(){ |
| WAT34 | 3:d2c733b52600 | 28 | air = 2; |
| WAT34 | 3:d2c733b52600 | 29 | ai.attach(&zero,1.0); |
| WAT34 | 3:d2c733b52600 | 30 | out = 0; |
| WAT34 | 3:d2c733b52600 | 31 | } |
| WAT34 | 2:74c543a0a671 | 32 | int main() |
| WAT34 | 0:00fcc71314cf | 33 | { |
| WAT34 | 8:9d8999740604 | 34 | ot.setfrequency(60000); |
| WAT34 | 4:646562d80dc2 | 35 | double tilt = 0,lo = 0,ro = 0; |
| WAT34 | 4:646562d80dc2 | 36 | int8_t ttilt = 0,tmpread = 0,tmpttilt = 0; |
| WAT34 | 5:4b462b9cb255 | 37 | char tro = 0,tlo = 0; |
| WAT34 | 4:646562d80dc2 | 38 | Tp.setInputLimits(-45,45); |
| WAT34 | 3:d2c733b52600 | 39 | Tp.setOutputLimits(-0.9,0.9); |
| WAT34 | 3:d2c733b52600 | 40 | Tp.setMode(1); |
| WAT34 | 4:646562d80dc2 | 41 | Tp.setBias(0.0); |
| WAT34 | 0:00fcc71314cf | 42 | while(1) { |
| WAT34 | 3:d2c733b52600 | 43 | if(conn.getc() == 255) { |
| WAT34 | 4:646562d80dc2 | 44 | tmpread = conn.getc(); |
| WAT34 | 4:646562d80dc2 | 45 | tmpttilt = conn.getc(); |
| WAT34 | 5:4b462b9cb255 | 46 | tro = conn.getc(); |
| WAT34 | 5:4b462b9cb255 | 47 | tlo = conn.getc(); |
| WAT34 | 5:4b462b9cb255 | 48 | if(tmpread^tmpttilt^tro^tlo == conn.getc()){ |
| WAT34 | 4:646562d80dc2 | 49 | ttilt = tmpttilt; |
| WAT34 | 4:646562d80dc2 | 50 | read = tmpread; |
| WAT34 | 5:4b462b9cb255 | 51 | ro = (tro-127)/127.0*0.9; |
| WAT34 | 5:4b462b9cb255 | 52 | lo = (tlo-127)/127.0*0.9; |
| WAT34 | 4:646562d80dc2 | 53 | } |
| WAT34 | 0:00fcc71314cf | 54 | } |
| WAT34 | 3:d2c733b52600 | 55 | if((read>>2)%2 && i == 0){ |
| WAT34 | 3:d2c733b52600 | 56 | air = 1; |
| WAT34 | 3:d2c733b52600 | 57 | ai.attach(&rev,1.0); |
| WAT34 | 3:d2c733b52600 | 58 | i = 1; |
| WAT34 | 0:00fcc71314cf | 59 | } |
| WAT34 | 3:d2c733b52600 | 60 | Tp.setSetPoint(ttilt); |
| WAT34 | 4:646562d80dc2 | 61 | tilt = double(sensort.getPulses()); |
| WAT34 | 4:646562d80dc2 | 62 | tilt = tilt*61/5128.0; |
| WAT34 | 3:d2c733b52600 | 63 | Tp.setProcessValue(tilt); |
| WAT34 | 4:646562d80dc2 | 64 | if (abs(lo) < 0.1){ |
| WAT34 | 4:646562d80dc2 | 65 | lo = 0; |
| WAT34 | 4:646562d80dc2 | 66 | } |
| WAT34 | 4:646562d80dc2 | 67 | if (abs(ro) < 0.1){ |
| WAT34 | 4:646562d80dc2 | 68 | ro = 0; |
| WAT34 | 4:646562d80dc2 | 69 | } |
| WAT34 | 8:9d8999740604 | 70 | L.write((lo+1.0)/2.0); |
| WAT34 | 8:9d8999740604 | 71 | R.write((ro+1.0)/2.0); |
| WAT34 | 3:d2c733b52600 | 72 | ot.speed(Tp.compute()); |
| WAT34 | 7:920cbfb28112 | 73 | //pc.printf("%f %d\n\r",Tp.compute(),read); |
| eil4nyqn | 6:21f6a2216fad | 74 | //pc.printf("%d-%d\r\n",tlo,tro); |
| WAT34 | 4:646562d80dc2 | 75 | wait_ms(1); |
| WAT34 | 0:00fcc71314cf | 76 | } |
| WAT34 | 0:00fcc71314cf | 77 | } |