<< return to Pixycam.com

pixy.init(); command is preventing my Arduino from controlling its servos properly.

I have my Pixy connected to an Arduino Uno which is mounted onto a small robot chassis with two continuous rotation servos to control the wheels. The pan/tilt mechanism is connected on top of the robot assembly, with the pan/tilt servos connected directly to the Arduino, not Pixy. Arduino is able to control all four servos perfectly as I specify, but upon inserting the pixy.init(); command into my program, all four servos start spinning wildly out of control for what seems to be no reason whatsoever. Pixy still returns whatever block characteristics I call from it, but the servos go on the fritz. I can’t figure out why this one line of code is doing this. Could anyone explain why this is the case and how I can fix it?

#include 
#include 
#include 

Servo pan;
Servo tilt;
Servo right;
Servo left;
Pixy pixy;

void setup()
{
  Serial.begin(9600);
  Serial.println("START!");
  
  pan.attach(10);
  tilt.attach(11);
  right.attach(12);
  left.attach(13);
  
  pan.write(110);
  tilt.write(120);
  
  pan.write(180);
  delay(1000);
  pan.write(40);
  delay(1000);
  
  pan.write(110);
  tilt.write(120);
  
}

void loop()
{
  pixy.init(); //<==================== **This line is causing the problem. The servos start spinning out of control past this point.**
  pixy.getBlocks();
  Serial.println(pixy.blocks[0].width);
}

Hello Dakota,

Have you tried commenting out the two lines pixy.getBlocks(); and Serial.println(pixy.blocks[0].width); as an experiment?

It will tell us if it’s the SPI communications or the init routine that is causing the problem.

Edward

I have this same issue and yes, I’ve tested each line individually and the source is definitely the pixy.init.
Any ideas?

Hello Jeff,
Hmm, this is something we’re unable to reproduce on our end. If it’s pixy.init() that’s causing issues, it should be straightforward to track down the specific cause because pixy.init() is a small routine.

In the pixy.h file, this is the relevant code. You should find it in your documents/arduino/libraries/pixy directory:

class LinkSPI
{
  public:
    void init()
    {
      SPI.begin();

      #ifdef __SAM3X8E__
      // DUE clock divider //
      SPI.setClockDivider(84);
      #else
      // Default clock divider //
      SPI.setClockDivider(SPI_CLOCK_DIV16);
      #endif
    }

Try commenting out the 3 lines. (the 2nd line shouldn’t be relevant though because your using probably using an Uno?)

Let us know if there is a particular line that’s causing the issue.

Edward

Hey Edward,

Yes, that was the problem I accidentally found earlier. I switched the DIV16 to DIV4 and it seems to be working better.
The servos still behave strangely but I’m trying to figure out what’s wrong with my code now. At least it’s not skipping the servo code anymore.

Thanks for the help anyways,

-Jeff

Hello Jeff,
That’s interesting. So there seems to be some kind of coupling between the SPI divider and the PWM you are using. How are you controlling your servos?

Edward

Hello again,
Seems it was a false alarm. Changing the divider made it work the command once, but failed to allow the loop to continue.
Turns out, I was using pin 11 which caused some issues… After it was switched to pin 10 it started working great haha well at least it works

Hi Jeff,
Ah, all’s well that ends well :slight_smile:

Edward