<< return to Pixycam.com

Pixy allocates memory dynamically?

So I was going trough TPixy.h file and noticed that inside the constructor, as well as resize(), malloc() and realloc() are called.

I was wondering if you could suggest a quick fix to avoid dynamic memory allocation. My arduino program freezes after awhile and I believe this is due to the fact that the memory is being dynamically allocated and eventually the memory gets divided and fragmented in a way that new data cant be stored

I tried tweaking the library a bit, the code compiles but doesnt run :frowning:

Also, I wasnt able to construct/deconstruct a pixy object for some reason. I tried to temporarily clear up some memory on my chip
I declared PixyI2C pixy; before void Setup(){}; then inside of a setup loop I would do pixy.init();

later in a program, I had a method which did this:
@
if( (millis() - last_camera_reset) > 10000){
pixy.~PixyI2C(); //clear up memory, destroy camera object
PixyI2C pixy; //and then create it again
pixy.init();
last_camera_reset=millis();
}
@
essentially i tired to remake pixy object every 10 seconds but that didnt work, it appeared that the camera would stop communicating with the arduino

Thanks!

Hi Vadim,

Why don’t you just remove the malloc/realloc and use a fixed-size array? Once the array is full, you can just ignore the rest of the blocks (although you should probably read them from the buffer, but not store them).

Even better, you could configure Pixy to only send you a certain number of blocks each frame so you don’t need to worry about modifying the TPixy code. This is done with the “Max blocks” parameter in the configuration dialog. The default value is 1000, which is pretty high.

Hope this helps.

Scott

Hello Scott!

Thats what I actually ended up doing yesterday, seems to be working okay now.
I put a serial.println(“Hello”) inside of resize() method and it was getting called way too often. My uneducated guess is that this was fragmenting memory on a chip up to the point where there was no memory blocks left large enough to store data. But Im not a computer scientist or anything, so clearly I have no idea what Im talking about :slight_smile: (its just my wild guess).
Im using all 7 color signatures on a camera (3 of them are CC), this may explain why I may be getting massive block recognition once in a while.
Part of the problem (i think) is that the fact that pixy automatically sets white balance parameter, so every time i turn the camera on and off the color recognition goes a bit differently. It would be nice if you guys could post a tutorial for dummies on how to disable that feature in camera’s firmware!

This also solves this topic [[http://www.cmucam.org/boards/9/topics/4265?r=4284#message-4284]]

Thanks for support man!

Hi Vadim,

Your “wild” guess sounds like it could be correct, but its too difficult to know without more info on the application code. Glad it worked out for you though!

As for setting the white balance parameters yourself (or disabling Pixy from doing it), we are working on that right now. Configuration via Arduino has been requested quite a bit, so its looking like that’ll be included in the Arduino library in the near future.

Good luck!

Scott