todbot / ServoEaser

Arduino library for servo easing
73 stars 17 forks source link

Exact timing and example of multiple servos #9

Closed FedericoBusero closed 3 years ago

FedericoBusero commented 3 years ago

Problem

The implementation of the servo easer cumulates little timing mistakes. As a result it is difficult to implement multiple servos, and they need to be synchronised.

Issues

This pull request relates to following issues:

Solution

Testing code

To see the difference between the 2 implementations, you can check the code below. In the original code, the behaviour is changed a lot because of the random delay. The proposed changes make the execution independent of the random delays.

You can make use of my fork of the library https://github.com/FedericoBusero/ServoEaser if you want to test this fix together with my 2 other proposed changes (call by reference & ESP32).

#include <Servo.h>
#include "ServoEaser.h"

const int servo1Pin = 7;
const int servo2Pin = 6;

int servoFrameMillis = 10;  // time between servo updates

Servo servo1; 
Servo servo2;

ServoEaser servo1Easer;
ServoEaser servo2Easer;

int myServoMovesCount = 8;
// configurable list of servo moves
ServoMove myServoMoves[] = {
// angle, duration
    {0,   3000},
    {45,  1000},
    {20,   500},
    {90,  1000},
    {45,  2000},
    {135, 2000},
    {75,  1000},
    {165, 1000},
};

//
void setup()
{
  Serial.begin(19200);
  Serial.println("ServoEaser test with random delays");

  servo1.attach( servo1Pin );
  servo2.attach( servo2Pin );

  servo1Easer.begin( servo1, servoFrameMillis );
  servo2Easer.begin( servo2, servoFrameMillis );
  servo2Easer.setFlipped( true );

  servo1Easer.play( myServoMoves, myServoMovesCount );
  servo2Easer.play( myServoMoves, myServoMovesCount );
}

//
void loop()
{
  servo1Easer.update();
  servo2Easer.update();

  delay(random(20));
}
todbot commented 3 years ago

Thank you! I think I originally went with 'ticks' instead of uint32_t millis because of memory constraints of the Arduinos available back in 2011.