/
ArduinoVisionCode.txt
66 lines (66 loc) 路 1.95 KB
/
ArduinoVisionCode.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// begin license header
//
// This file is part of Pixy CMUcam5 or "Pixy" for short
//
// All Pixy source code is provided under the terms of the
// GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
// Those wishing to use Pixy source code, software and/or
// technologies under different licensing terms should contact us at
// cmucam@cs.cmu.edu. Such licensing terms are available for
// all portions of the Pixy codebase presented here.
//
// end license header
//
// This sketch is a good place to start if you're just getting started with
// Pixy and Arduino. This program simply prints the detected object blocks
// (including color codes) through the serial console. It uses the Arduino's
// ICSP SPI port. For more information go here:
//
// https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:hooking_up_pixy_to_a_microcontroller_-28like_an_arduino-29
//
#include <Pixy2.h>
#include <math.h>
// This is the main Pixy object
Pixy2 pixy;
double currentArea = 0, currentX = 0, currentSetPoint = 0;
void setup()
{
Serial.begin(115200);
Serial.print("Starting...\n");
pixy.init();
}
void loop()
{
int i;
char str[80];
// grab blocks!
pixy.ccc.getBlocks();
// If there are detect blocks, print them!
if (pixy.ccc.numBlocks)
{
Serial.print("Detected ");
Serial.println(pixy.ccc.numBlocks+"\n\t");
for (i=0; i<pixy.ccc.numBlocks; i++)
{
if(pixy.ccc.blocks[i].m_width * pixy.ccc.blocks[i].m_height > currentArea)
{
sprintf(str, "%d|%d", pixy.ccc.blocks[i].m_x, 200+50*(pixy.ccc.blocks[i].m_y / 208));
Serial.println(str);
currentArea = pixy.ccc.blocks[i].m_width * pixy.ccc.blocks[i].m_height;
currentX = pixy.ccc.blocks[i].m_x;
currentSetPoint = 200+50*(pixy.ccc.blocks[i].m_y / 208);
}
else
{
sprintf(str, "%d|%d", currentX, currentSetPoint);
Serial.println(str);
}
}
}
else
{
currentArea = 0;
currentX = 0;
}
}