Crystal Huynh
/
pixy_test
code for testing the pixy
main.cpp@0:214d306295fa, 2017-05-30 (annotated)
- Committer:
- huynh270
- Date:
- Tue May 30 09:56:04 2017 +0000
- Revision:
- 0:214d306295fa
hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
huynh270 | 0:214d306295fa | 1 | #include "mbed.h" |
huynh270 | 0:214d306295fa | 2 | #include "Pixy.h" |
huynh270 | 0:214d306295fa | 3 | #include "PID_Control.h" |
huynh270 | 0:214d306295fa | 4 | |
huynh270 | 0:214d306295fa | 5 | //------------------------------------ |
huynh270 | 0:214d306295fa | 6 | // Example for PIxy UART connection. |
huynh270 | 0:214d306295fa | 7 | // Driver version 0.2 |
huynh270 | 0:214d306295fa | 8 | // BUGS:: |
huynh270 | 0:214d306295fa | 9 | // DO NOT USE PRINTF IN MAIN LOOP FOR PROPER OPERATION |
huynh270 | 0:214d306295fa | 10 | // Pixy connected by UART (PA_9 and PA_10) with 460800 Baud |
huynh270 | 0:214d306295fa | 11 | // Sample tries to center the recognised object between the pixy cam |
huynh270 | 0:214d306295fa | 12 | //------------------------------------ |
huynh270 | 0:214d306295fa | 13 | |
huynh270 | 0:214d306295fa | 14 | //communication |
huynh270 | 0:214d306295fa | 15 | Serial pc(USBTX, USBRX); |
huynh270 | 0:214d306295fa | 16 | Serial cam(PA_9, PA_10); |
huynh270 | 0:214d306295fa | 17 | |
huynh270 | 0:214d306295fa | 18 | DigitalOut myled(LED1); |
huynh270 | 0:214d306295fa | 19 | |
huynh270 | 0:214d306295fa | 20 | //motor stuff |
huynh270 | 0:214d306295fa | 21 | DigitalOut enableMotorDriver(PB_2); |
huynh270 | 0:214d306295fa | 22 | PwmOut pwmL(PA_8); |
huynh270 | 0:214d306295fa | 23 | PwmOut pwmR(PA_9); |
huynh270 | 0:214d306295fa | 24 | |
huynh270 | 0:214d306295fa | 25 | Pixy pixy(cam); |
huynh270 | 0:214d306295fa | 26 | |
huynh270 | 0:214d306295fa | 27 | int main() |
huynh270 | 0:214d306295fa | 28 | { |
huynh270 | 0:214d306295fa | 29 | PID_Control pid; |
huynh270 | 0:214d306295fa | 30 | |
huynh270 | 0:214d306295fa | 31 | pid.setPIDValues( 0.01f, 0.0000f, 0.000000f, 0.3f, -0.3f, 1000); |
huynh270 | 0:214d306295fa | 32 | |
huynh270 | 0:214d306295fa | 33 | pc.baud( 115200 ); |
huynh270 | 0:214d306295fa | 34 | pwmL.period(0.00005f); // Setzt die Periode auf 50 μs |
huynh270 | 0:214d306295fa | 35 | pwmR.period(0.00005f); |
huynh270 | 0:214d306295fa | 36 | |
huynh270 | 0:214d306295fa | 37 | //Wait untill pixy is ready |
huynh270 | 0:214d306295fa | 38 | wait( 2.0f); |
huynh270 | 0:214d306295fa | 39 | |
huynh270 | 0:214d306295fa | 40 | //enable motors |
huynh270 | 0:214d306295fa | 41 | enableMotorDriver = 1; |
huynh270 | 0:214d306295fa | 42 | |
huynh270 | 0:214d306295fa | 43 | //start while loop |
huynh270 | 0:214d306295fa | 44 | while(1) { |
huynh270 | 0:214d306295fa | 45 | wait( 0.005f ); |
huynh270 | 0:214d306295fa | 46 | if( pixy.objectDetected() && pixy.getSignature() == 1) { |
huynh270 | 0:214d306295fa | 47 | float e = 160-pixy.getX(); |
huynh270 | 0:214d306295fa | 48 | float diff = pid.calc( e, 0.005f ); |
huynh270 | 0:214d306295fa | 49 | pwmL = 0.5f - diff; |
huynh270 | 0:214d306295fa | 50 | pwmR = 0.5f - diff; |
huynh270 | 0:214d306295fa | 51 | } else { |
huynh270 | 0:214d306295fa | 52 | pwmL = 0.5f ; |
huynh270 | 0:214d306295fa | 53 | pwmR = 0.5f; |
huynh270 | 0:214d306295fa | 54 | } |
huynh270 | 0:214d306295fa | 55 | } |
huynh270 | 0:214d306295fa | 56 | } |