What great timing! I wanted to learn more about OOP (object oriented programming) concepts, and my senior design project just did that!
TL;DR = Tried to access the color signature variable (m_signature) so I could interface the Pixy2 camera with the Arduino. Manage to figure it out by understanding local and global scope, pointers, and member access operators.
Context
Goal = get my servo motor to move based on the color that the Pixy2 camera sees
Plan
- Go through the Pixy2.h
- Find the variable that stores the color signature from the Pixy2 camera
- Access the color signature variable to use it with the servo motor
I had a pretty solid plan. The problem was, I wasn’t sure how to access the color signature variable (m_signature).
But with the power of friendship and Google, I figured it out! Here’s the Arduino C/C++ code below:
What I Did
For testing purposes, I made Color Signature 4 a red color in the PixyMon software.
I went through the code in the Pixy2.h file and couldn’t find ANYTHING related to reading colors. So I played detective and found that the Pixy2 library files were referring to each other.
In other words, I went through Pixy2.h > TPixy2.h > PixyCCC.h
It was in PixyCCC.h where I finally found the color signature variable (m_signature)!!!!
But how to access it?!
Well, I made a Pixy2 pixy object. And m_signature lives within the ccc (color_connected_components) member. But more specifically, m_signature is a local variable within the blocks struct. Well, kinda. I need the pointer, blocks, which is a Block variable (at least, that’s how I understood it). Lastly, the m_signature spits out the number 4, whenever the Pixy2 camera sees the designated red color.
2 hours later, I finally figured out this crucial line of code:
pixy.ccc.blocks->m_signature == 4
I also learned that -> is the arrow notation for a pointer in C.
And with that, I was able to successfully interface the servo motor with the Pixy2 camera! Here’s the Arduino code:
/* Testing Servo motor with Pixy2 camera
by Roselynn Conrady and Tony Lehuy Nguyen
Special shoutout to Abigail Dabu, Raven Tomas, and Christopher Rico Rodriguez
11 Nov 2020
*/
include <Servo.h>
include <Pixy2.h>
Servo myservo; // create servo object
int pos = 0; // variable to store the servo position
Pixy2 pixy;
void setup() {
// put your setup code here, to run once:
myservo.attach(9); // attaches the servo on pion 9 to the servo object
Serial.begin(115200);
Serial.print("Starting…\n");
pixy.init(); // initializes Pixy2
}
void loop() {
// put your main code here, to run repeatedly:
pixy.ccc.getBlocks(); // grabs Pixy blocks
// accessing "m_signature" variable from Pixy2.h
if (pixy.ccc.blocks->m_signature == 4)
{
myservo.write(90); // moves servo 90 degrees
}
else
{
myservo.write(pos); // moves servo back to pos
}
}
Overall, this was a lesson about a concept called scope. I couldn’t just do
if (m_signature === 4)
and expect the Arduino to work. This is because m_signature is a local variable, and only exists in a local scope (which was inside the Blocks struct in the PixyCCC.h file). In other words, Arduino could not access m_signature this way because it was not defined in a global scope.
So I had to finesse my way through pre-existing code to make things work ~