![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
abc
Dependencies: mbed pixy pixy_test
Fork of pixy_test by
main.cpp@2:1354ce60c0b9, 2015-11-24 (annotated)
- Committer:
- jfields
- Date:
- Tue Nov 24 01:26:57 2015 +0000
- Revision:
- 2:1354ce60c0b9
- Parent:
- 1:5302a23c97c4
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 | 1:5302a23c97c4 | 8 | // global vars |
jfields | 1:5302a23c97c4 | 9 | int num_readings = 0; |
jfields | 1:5302a23c97c4 | 10 | int total_y = 0; |
jfields | 1:5302a23c97c4 | 11 | |
jfields | 1:5302a23c97c4 | 12 | // funcs |
jfields | 1:5302a23c97c4 | 13 | void get_volume(int i); |
jfields | 1:5302a23c97c4 | 14 | |
jfields | 0:6618ecb9567e | 15 | int main() { |
jfields | 1:5302a23c97c4 | 16 | |
jfields | 1:5302a23c97c4 | 17 | // init pixy |
jfields | 0:6618ecb9567e | 18 | pc.printf("ready\n\r"); |
jfields | 0:6618ecb9567e | 19 | pixy.setSerialOutput(&pc); |
jfields | 1:5302a23c97c4 | 20 | |
jfields | 0:6618ecb9567e | 21 | while (1) { |
jfields | 1:5302a23c97c4 | 22 | |
jfields | 1:5302a23c97c4 | 23 | // get pixy data |
jfields | 0:6618ecb9567e | 24 | uint16_t blocks; |
jfields | 0:6618ecb9567e | 25 | blocks = pixy.getBlocks(); |
jfields | 1:5302a23c97c4 | 26 | |
jfields | 1:5302a23c97c4 | 27 | // store data |
jfields | 0:6618ecb9567e | 28 | if (blocks) { |
jfields | 1:5302a23c97c4 | 29 | for (int j = 0; j < blocks; j++) { |
jfields | 1:5302a23c97c4 | 30 | get_volume(pixy.blocks[j].y); |
jfields | 0:6618ecb9567e | 31 | } |
jfields | 0:6618ecb9567e | 32 | } |
jfields | 1:5302a23c97c4 | 33 | } |
jfields | 0:6618ecb9567e | 34 | } |
jfields | 1:5302a23c97c4 | 35 | |
jfields | 1:5302a23c97c4 | 36 | void get_volume(int y) { |
jfields | 1:5302a23c97c4 | 37 | |
jfields | 1:5302a23c97c4 | 38 | // update data |
jfields | 1:5302a23c97c4 | 39 | total_y += y; |
jfields | 1:5302a23c97c4 | 40 | num_readings++; |
jfields | 1:5302a23c97c4 | 41 | |
jfields | 1:5302a23c97c4 | 42 | // output results |
jfields | 1:5302a23c97c4 | 43 | if (num_readings > 30) { |
jfields | 2:1354ce60c0b9 | 44 | float average_y = (float)total_y/num_readings; |
jfields | 2:1354ce60c0b9 | 45 | float result = -2.0794*average_y + 351.97; |
jfields | 2:1354ce60c0b9 | 46 | pc.printf("y = %d, num_readings = %d, average = %f, mL = %f\r\n", y, num_readings, average_y, result); |
jfields | 1:5302a23c97c4 | 47 | |
jfields | 1:5302a23c97c4 | 48 | // reset vars |
jfields | 1:5302a23c97c4 | 49 | num_readings = 0; |
jfields | 1:5302a23c97c4 | 50 | total_y = 0; |
jfields | 1:5302a23c97c4 | 51 | } |
jfields | 1:5302a23c97c4 | 52 | } |