I'm using the PCA9685 module with its Adafruit library. In my code I have one object of the servodriver class that has 14 channels (controlling 14 servos basically).

void Positioning(){
  ServoDriver.setPWM(right_foot, 0, MapRotation(rotations[0],"1"));
  ServoDriver.setPWM(left_foot, 0, MapRotation(rotations[1],"2")); 
  ServoDriver.setPWM(right_calf, 0, MapRotation(rotations[2],"1")); 
  ServoDriver.setPWM(left_calf, 0, MapRotation(rotations[3],"2")); 
  ServoDriver.setPWM(right_thigh, 0, MapRotation(rotations[4],"1")); 
  ServoDriver.setPWM(left_thigh, 0, MapRotation(rotations[5],"2")); 
  ServoDriver.setPWM(right_pelvis, 0, MapRotation(rotations[6],"1"));
  ServoDriver.setPWM(left_pelvis, 0, MapRotation(rotations[7],"2")); 
  ServoDriver.setPWM(torso, 0, MapRotation(rotations[8], "1"));
  ServoDriver.setPWM(right_shoulder, 0, MapRotation(rotations[9], "1")); 
  ServoDriver.setPWM(left_shoulder, 0, MapRotation(rotations[10], "2")); 
  ServoDriver.setPWM(right_arm, 0, MapRotation(rotations[11], "1")); 
  ServoDriver.setPWM(left_arm, 0, MapRotation(rotations[12], "2")); 
  ServoDriver.setPWM(right_hand, 0, MapRotation(rotations[13], "1")); 
  ServoDriver.setPWM(left_hand, 0, MapRotation(rotations[14], "2")); 

All the function positioning does is just set all the servos to a certain position (they're in a humanoid robot and so this position is for standing). The array rotations[] just contains the servo positions from 0 to 180 which is set elsewhere in the code for each servo respectively. The second argument for map rotation just maps it between the servos pulse min and max values (115 and 560) in normal order or reverse depending on number.

My question is how do I change the speed of the servos? I cannot use delay() or loops because wouldn't that cause all the servos to turn sequentially instead of all at once. a change in any rotations[] value indicates the robot is moving and so how do I make the servos slower instead of turning at their fastest to the new rotations[] positions whenever they are changed (as positioning() is called in the loop() function)?


You cannot simply change the speed of a servo. You can only move step my step to the desired position, waiting inbetween. So for example instead of directly moving from 0° to 90°, you can first move to 5°, wait a bit, move to 10°, wait a bit, move to 15°, wait a bit,... until you reached 90°. You can also go in 1° steps to make it smoother.

You can solve this with delays, but if you want to move the servos at different speeds, you will need a non-blocking coding style like in the BlinkWithoutDelay example of the Arduino IDE (it was described countless times at this site and on the web).

Also, when building a humanoid robot, at some point you will have to consider not only the pose, that you want the robot to reach, but also the way to get there. This can be very important and it can be a complex task. Basically you need formulas for the movements, which return you the exact position for Dach servo at any given time in a movement. Then you can set the servos to this value for the current time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.