How best to control my robot legs

I have built a 4 legged theo jansen walker similar to the one in this picture.

However instead of servos it uses geared electric motors powered via escs to turn the wheels connected to the legs, each leg is seperatley controlled and has a rotary encoder and limit switch to tell how far/fast the leg is moving.

I want my walker to have a similar gait to the walker in this video...

IE each leg will move slowly while on the ground but quickly step forward when off the ground.

To do this I have made a function that gives an ideal position that the leg should be in at a certain time (basically a timer multiplied by a coefficient to give the angle the geared motor should be at)

What is the best way to ensure that (given the information gleaned from the encoder and the ideal position given by the function) that the leg position matches the ideal position as best it can?

I can't just give a constant figure to the esc as when the leg touches the ground it will slow down so I need feedback from the encoder to adjust the escs power.

My first reaction was to pid the difference between the current position (the input) and the ideal position (the setpoint).

However the setpoint is obviously constantly moving so pid gives it this constant lurching movement, not good..

My next idea is to pid the speed; find the speed the leg should move through the slow section of the leg movement arc and ensure it maintains this via pid.
I would also have a higher and lower setpoint speed to be used if the leg current position didn't match the ideal position; ie if the leg is moving at the correct speed but is behind the ideal position due to a slow start the setpoint will be increased till the leg actual position matches the ideal position at which point the speed setpoint will be reduced to the normal ideal speed for the legs slow movement.

This is my plan so far but I would love opinions on it or suggestions on better ways of doing it.

Any thanks in advance.

it appears that that machine has a cam and that all movement is fluid on a constant rotation

here is a possible clue to your answer. sine waves.

we use them to fade LED's

Absolutely and controlling that cams speed is whats giving me the trouble at the moment.

The best analogy I can come up with for what I'm trying to achieve would be this...

Imagine if you had a wheel connected to a geared motor that is sitting in mid air; the motor spins the wheel slowly at about 1 revolution every five seconds and maintains this speed fairly easily as there's little friction on the wheel.

Now imagine I have another second wheel and geared motor setup that I want to turn in such a way that it matches the distance/angle-of-rotation the first wheel has spun.

Both wheels have sensors telling me how far they have turned but here's the issue. The wheel that is trying to mimick the first wheel keeps getting grabbed by someone, things keep rubbing up against it slowing it down and there's no way to predict when this will happen. The wheel has to counter for an unknown factor of friction slowing it down at odd times and still accurately match the uniform rotation of the first wheel.

if you have 4 cams, then your issue is keeping them all in the same timing.
as if you only have one motor that moves and each cam is connected to the motor.

pick one as the master and control the speed of all the others.

as you get more elaborate, you can pick the slowest one to be the master.

if you are trying to use the motor in place of a cam, your movement is still a sinewave and the motor speed has to follow that. timing then becomes where each motor/leg is on the correct curve for that position.

The four legs have to move in lock-step. The easiest way is to use one motor and gear all four legs off of the one drive shaft.