<< return to Pixycam.com

How to adjust pan/tilt servo limits - Pixy2 with Arduino

Hello,

I am using a Pixy2 cam connected to an Arduino via SPI on my hexapod. Servos are connected to the Pixy2 board. I run a modified Arduino pan/tilt code but the Pixy2 head crashes into the front legs in max/min pan position.
I can limit the pan servo in Pixymon while running pan/tilt demo but with Arduino connected the limits do not work. I suppose they need to be set in the Arduino sketch.
The topic Pixy2 Pan/Tilt How to Limit Servo's range of movement did not provide a solution for me.

In TPixy2.h I found:
// RC-servo values
#define PIXY_RCS_MIN_POS 0
#define PIXY_RCS_MAX_POS 1000L
#define PIXY_RCS_CENTER_POS ((PIXY_RCS_MAX_POS-PIXY_RCS_MIN_POS)/2)

Is this the right place to set the limits? How can I set values for each servo separately?
I cannot find a solution for my problem here:
https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:general_api

Can you give me an example of Arduino code how to set the pan/tilt limits?

Thanks for your help! :slight_smile:
Markus

Hello,
Just to confirm, the servos are connected to Pixy and you are controlling them through the Arduino (yes?)
If so, why not just limit the servo in software by limiting the max/min range of values you pass into setServos()?

https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:general_api

Edward

Hi Edward,
thanks for your suggestions. The servos are connected to Pixy2 and I am controlling them through the Arduino.
This is the void loop part of the test programm:

void loop(){
static int i = 0;
int panCommand, tiltCommand;
char buf[64];
int32_t panOffset, tiltOffset;

pixy.ccc.getBlocks();

if (pixy.ccc.numBlocks)
{
i++;

if (i%60==0)
  Serial.println(i);   

panOffset = (int32_t)pixy.frameWidth/2 - (int32_t)pixy.ccc.blocks[0].m_x;
tiltOffset = (int32_t)pixy.ccc.blocks[0].m_y - (int32_t)pixy.frameHeight/2;  

panLoop.update(panOffset);
tiltLoop.update(tiltOffset);

pixy.setServos(panLoop.m_command, tiltLoop.m_command); 

panCommand = round((panLoop.m_command*255)/1000);       
tiltCommand = round((tiltLoop.m_command*255)/1000);    

if (panCommand - 128 < 64){
ROBOT_STOP();
ROBOT_ROTATE_MODE(128,panCommand,tiltCommand,128);
Serial.print(“Robot rotating…\n”);
}
else {
ROBOT_MOVE(128,panCommand,128);
Serial.print(“Robot moving…\n”);
}
}
}

As far as I understood PIXY_RCS_MIN_POS (0) and PIXY_RCS_MAX_POS (1000) are defined in TPixy2.h.
Can you give me a hint how to change setServos in my part of code?
Thanks!

Markus

Hi Markus,

I think what @edge is suggesting is something like this:

if (panLoop.m_command>pan_max_value)
    panLoop.m_command=pan_max_value;

if (tiltLoop.m_command>tilt_max_value)
    tiltLoop.m_command=tilt_max_value;

pixy.setServos(panLoop.m_command, tiltLoop.m_command); 

Make sense?

Cheers,
Jesse

Hi Jesse,

I will give it a try next week! :slight_smile:

Thanks!

Markus