Servo pause when position loop restarts

I have animated a leg in a 3D software and wrote out position data for my servos that are hard coded position arrays. I'm delaying by the FPS I animated at, but strangely when the loop completes there is an additional delay in the servo which is causing the motion to not be smooth. Even if I add more position data and change the FPS delay, there is still a blip in the motion. Is there anything that could be causing this?

#include <Servo.h>

Servo hips_servo;

int i = 0;
int fps = 42; //  1000 ms / 24 frames per second
int hips_pos[] = {70,65,64,64,64,64,64,64,52,42,33,25,19,15,15,15,18,23,35,52,71,93,109,120,129,136,141,144,144,143,139,134,127,119,111,101,91,80};

void setup() {
  Serial.begin(9600);
  hips_servo.attach(4);
}

void loop() {
  // SET HIP POSITIONS
  for ( i = 0; i < 38; i++ ) {
    int val = hips_pos[i];
    hips_servo.write(val);
    delay(fps);
  }  
  
}

make shorter delays and less movements.

by the way, get rid of delays and use the "Blink Without Delay" concept.
Here I have an example:
https://werner.rothschopf.net/microcontroller/202207_millis_slow_servo.htm

More details please :smiley:

Shorter delays and less movements? Could you explain why this would fix the issue because it seems like that sort of kills the exercise of animating in 3D and sending to motors? I did try adding subsamples so the position array was longer and the delay was shorter, but I got the same results.

If I reduce the delay then things do go smoother ( but ruins incoming animation data ), but I guess I'm curious why there is an EXTRA delay when the for loop finishes when the delay length is longer.

your sketch is doing something like

move_a_long_distance
block_with_long_delay
move_a_long_distance
block_with_long_delay
move_a_long_distance
block_with_long_delay

if you would program like

move_a_short_distance
give_other_things_a_chance_to_do_something_or_just_do_nothing_for_5_millis
move_a_short_distance
give_other_things_a_chance_to_do_something_or_just_do_nothing_for_5_millis
move_a_short_distance
give_other_things_a_chance_to_do_something_or_just_do_nothing_for_5_millis

the movement is smoother.

Cool thanks for the response. I thought maybe I'd have to try switching to millis() over delay(), but though doing something simple like this I could get away with a simpler sketch.

Cheers!

Just tried your sketch:
on wokwi

it seems not the end of the loop makes your jump, but the sequence of 64 64 64 64 64 within your sequence!
6 x 42ms ... is something you will see as "the servo does nothing"

there's a number of points with the same values

It's interesting to see that graph representation of the position array. In houdini that is not present in the rotation attribute I'm using. I'll double check things after I fit the rotation range into the integer positions. Just curious how did you generate that graph?

You can do it with the Serial Plotter in the IDE. Print the values and simply plot them

xgraph

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.