![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
abc
Dependencies: mbed-rtos mbed pixy pixy_test
Fork of aUtO_volume_v3 by
main.cpp@3:56a1264e43aa, 2015-12-09 (annotated)
- Committer:
- jfields
- Date:
- Wed Dec 09 02:09:20 2015 +0000
- Revision:
- 3:56a1264e43aa
- Parent:
- 2:1354ce60c0b9
- Child:
- 4:fe091424b406
abc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jfields | 0:6618ecb9567e | 1 | #include "mbed.h" |
jfields | 0:6618ecb9567e | 2 | #include "Pixy.h" |
jfields | 0:6618ecb9567e | 3 | |
jfields | 1:5302a23c97c4 | 4 | // init board |
jfields | 0:6618ecb9567e | 5 | Pixy pixy(Pixy::SPI, p11, p12, p13); |
jfields | 0:6618ecb9567e | 6 | Serial pc(USBTX, USBRX); |
jfields | 0:6618ecb9567e | 7 | |
jfields | 3:56a1264e43aa | 8 | #if defined(TARGET_LPC1768) |
jfields | 3:56a1264e43aa | 9 | Serial blue(p9, p10); // TX, RX |
jfields | 3:56a1264e43aa | 10 | //Serial blue(p13, p14); // TX, RX |
jfields | 3:56a1264e43aa | 11 | #elif defined(TARGET_LPC4330_M4) |
jfields | 3:56a1264e43aa | 12 | Serial blue(P6_4, P6_5); // UART0_TX, UART0_RX |
jfields | 3:56a1264e43aa | 13 | //Serial blue(P2_3, P2_4); // UART3_TX, UART3_RX |
jfields | 3:56a1264e43aa | 14 | #endif |
jfields | 3:56a1264e43aa | 15 | |
jfields | 1:5302a23c97c4 | 16 | // global vars |
jfields | 1:5302a23c97c4 | 17 | int num_readings = 0; |
jfields | 1:5302a23c97c4 | 18 | int total_y = 0; |
jfields | 1:5302a23c97c4 | 19 | |
jfields | 1:5302a23c97c4 | 20 | // funcs |
jfields | 1:5302a23c97c4 | 21 | void get_volume(int i); |
jfields | 1:5302a23c97c4 | 22 | |
jfields | 0:6618ecb9567e | 23 | int main() { |
jfields | 1:5302a23c97c4 | 24 | |
jfields | 3:56a1264e43aa | 25 | // init bluetooth |
jfields | 3:56a1264e43aa | 26 | blue.baud(9600); |
jfields | 3:56a1264e43aa | 27 | |
jfields | 3:56a1264e43aa | 28 | // init pc |
jfields | 3:56a1264e43aa | 29 | pc.baud(9600); |
jfields | 3:56a1264e43aa | 30 | pc.printf("Bluetooth Start\r\n"); |
jfields | 3:56a1264e43aa | 31 | pc.printf("ready\n\r"); |
jfields | 3:56a1264e43aa | 32 | |
jfields | 1:5302a23c97c4 | 33 | // init pixy |
jfields | 0:6618ecb9567e | 34 | pixy.setSerialOutput(&pc); |
jfields | 1:5302a23c97c4 | 35 | |
jfields | 0:6618ecb9567e | 36 | while (1) { |
jfields | 1:5302a23c97c4 | 37 | |
jfields | 1:5302a23c97c4 | 38 | // get pixy data |
jfields | 0:6618ecb9567e | 39 | uint16_t blocks; |
jfields | 0:6618ecb9567e | 40 | blocks = pixy.getBlocks(); |
jfields | 1:5302a23c97c4 | 41 | |
jfields | 1:5302a23c97c4 | 42 | // store data |
jfields | 0:6618ecb9567e | 43 | if (blocks) { |
jfields | 1:5302a23c97c4 | 44 | for (int j = 0; j < blocks; j++) { |
jfields | 1:5302a23c97c4 | 45 | get_volume(pixy.blocks[j].y); |
jfields | 0:6618ecb9567e | 46 | } |
jfields | 0:6618ecb9567e | 47 | } |
jfields | 1:5302a23c97c4 | 48 | } |
jfields | 0:6618ecb9567e | 49 | } |
jfields | 1:5302a23c97c4 | 50 | |
jfields | 1:5302a23c97c4 | 51 | void get_volume(int y) { |
jfields | 1:5302a23c97c4 | 52 | |
jfields | 1:5302a23c97c4 | 53 | // update data |
jfields | 1:5302a23c97c4 | 54 | total_y += y; |
jfields | 1:5302a23c97c4 | 55 | num_readings++; |
jfields | 1:5302a23c97c4 | 56 | |
jfields | 1:5302a23c97c4 | 57 | // output results |
jfields | 3:56a1264e43aa | 58 | if (num_readings >= 20) { |
jfields | 2:1354ce60c0b9 | 59 | float average_y = (float)total_y/num_readings; |
jfields | 3:56a1264e43aa | 60 | float result = -2.0028*average_y + 327.23; |
jfields | 1:5302a23c97c4 | 61 | |
jfields | 3:56a1264e43aa | 62 | // to pc |
jfields | 3:56a1264e43aa | 63 | pc.printf("y = %d, num_readings = %d, average = %.2f, mL = %.2f\r\n", y, num_readings, average_y, result); |
jfields | 3:56a1264e43aa | 64 | pc.printf("%.2f\r\n", result); |
jfields | 3:56a1264e43aa | 65 | |
jfields | 3:56a1264e43aa | 66 | // to bluetooth |
jfields | 3:56a1264e43aa | 67 | //blue.printf("y = %d, num_readings = %d, average = %.2f, mL = %.2f\r\n", y, num_readings, average_y, result); |
jfields | 3:56a1264e43aa | 68 | blue.printf("%.2f\r\n", result); |
jfields | 3:56a1264e43aa | 69 | |
jfields | 1:5302a23c97c4 | 70 | // reset vars |
jfields | 1:5302a23c97c4 | 71 | num_readings = 0; |
jfields | 1:5302a23c97c4 | 72 | total_y = 0; |
jfields | 1:5302a23c97c4 | 73 | } |
jfields | 1:5302a23c97c4 | 74 | } |