Reading servo angle while using HCPCA9685 for controlling servos

Hi. I'm controlling multiple servo motors with PCA9685. I want to read the angle of each servos when I need. can any one help? This is my code:

#include "HCPCA9685.h"

/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
  is 0x40 */

#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41

int pos1 = 200;
int pos2 = 220;
int pos3 = 180;
int pos4 = 0;
int pos5 = 130;
int pos6 = 100;
int pos7=50;
const float delay1 = 0.5;
int delay2 = 500;
const float posx = 80.0 / pos5;

/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup()
{
  /* Initialise the library and set it to 'servo mode' */

  // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);

  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);

  unsigned int Pos;

/* standing */
  HCPCA9685_1.Servo(2, pos4);
  HCPCA9685_1.Servo(5, pos4);
  HCPCA9685_1.Servo(8, pos4);
  HCPCA9685_1.Servo(11, pos4);
  HCPCA9685_1.Servo(14, pos4);
  HCPCA9685_2.Servo(1, pos4);

  HCPCA9685_1.Servo(1, pos4);
  HCPCA9685_1.Servo(4, pos4);
  HCPCA9685_1.Servo(7, pos4);
  HCPCA9685_1.Servo(10, pos4);
  HCPCA9685_1.Servo(13, pos4);
  HCPCA9685_2.Servo(0, pos4);

  HCPCA9685_1.Servo(0, pos2);
  HCPCA9685_1.Servo(3, pos3);
  HCPCA9685_1.Servo(6, pos2);
  HCPCA9685_1.Servo(9, pos2);
  HCPCA9685_1.Servo(12, pos3);
  HCPCA9685_1.Servo(15, pos2);

  HCPCA9685_2.Servo(2, 200);  // sensor movement

  delay(delay2);
  
  /*  */
  for (int Pos = pos4; Pos < pos5; Pos++)
  {
    HCPCA9685_1.Servo(1, Pos);
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(7, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_1.Servo(13, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(delay1);
  }

  delay(delay2);

  for (int Pos = pos4; Pos < pos6; Pos++)
  {
    HCPCA9685_1.Servo(2, Pos);
    HCPCA9685_1.Servo(5, Pos);
    HCPCA9685_1.Servo(8, Pos);
    HCPCA9685_1.Servo(11, Pos);
    HCPCA9685_1.Servo(14, Pos);
    HCPCA9685_2.Servo(1, Pos);
    delay(1);
  }

}

void loop()
{

}
1 Like

Would it not be a lot easier to just remember where you sent the servo?

Your topic does not indicate a problem with the IDE and hence has been moved to a more suitable location on the forum.

It's not possible, at least with the servos sold for use with Arduino. The Arduino can command a servo to go to an angle, but it can't know whether that angle was achieved or what angle has actually been reached.

No I'm controlling a robot. For example when I press 'F' button robot moves forward and when I want to turn the robot to left, I don't know what is the latest position of the servos.

Ok no problem

It doesn't matter if I know that the servo has actually reached the angular direction. It is important to know what angle the arduino has given to the servo.I'm controlling a robot. For example when I press 'F' button robot moves forward and when I want to turn the robot to left, I don't know what is the latest position of the servos.

That's not a problem, then. Nothing needs to be read at all. Problem solved.

How? do you offer a command to read servo angle?

No, as already stated, no such read is possible, or necessary. Simply assume the servo has achieved the last angle that was sent to it.

I control the robot with bluetooth. when the user presses 'F' button robot moves forward, when presses 'B' button robot moves backward when presses 'L' button robot turns left and when presses 'R' button robot turns right. so there are for states that user may choose one of them. and in these four states the servos last angles are different. and when the user chooses the next state I don't know what are the last angles to move the servos from that positions to another positions.

But you do know those angles, the final angles depend only on what the user selected as the previous state.

For example, with the code you posted above, at the end of setup(), you know all servos are at angle "pos6".

Why do you need to know those previous angles anyway?

This seems a strange conversation, I am not sure we are understanding each other.

Do not pay attention to the code I sent. Suppose someone really needs to read the angle of the servo motor, is there a command?

@sterretje told you that answer.
Your only hope is to keep track of the last angle you sent to the servo. Since it's your code, that should be possible.
Per post #2. Also answered.

Study the documentation for your servo. Is there mention of the ability to read an angle?

@alija22 as per your recent threads, you either keep asking because you don't like our answers, or because you can't understand them, or because you don't think you've asked the same question enough times.
No, there is no readback of actual position
Yes, you can keep track of the position commanded
Some servo libraries will do the latter for you, and provide a fn to read back the position. But it's still the position sent, not the actual position.

I need the position sent. Witch library has this ability?

Can you please help me to get the zip file of HCPCA9685 Library

Can you please help me to get the zip file of HCPCA9685 Library.

Did you try "(please log in to download the library)"?