# How to inverse gait quadruped?

thank you brother the sequence of your code as follow
while coax moves backward the femur and tibia do lift and down steps and when the coax moves forward the femur and the tibia still

but the paulRb do

if you dont mind i wonder what the coding style in this link
is it C++??
again thank you

You are welcome. But it is important to me that you understand how I changed the code and why I changed it like that. Can you describe to me those things?

I agree with the answers on the other topic. Your code could be attempting to move the servos to positions that they cannot reach because of mechanical limitations, causing their motors to stall and overheat.

i think i could understand 60% of your code.
variable step that comes from int direction as parameter of moveLeg3 replace

``````  i1L1
i2L1
i3L1
i4L1
``````

also i could understand the parameter of if() as follows

``````// rotate coax backward
if (step <= 30) {
sc3.write(100 + step);
}

``````

the sc3 will step from 100 to 130 as long step < = 30
but couldnot with this if

``````  if (step <= 7) {
st3.write(60 + step * 2);
sf3.write(55 + step * 3);
}
``````

the code is up step of tibia and femur
why its happen when step <= 7 ?
as my trial code which also do same time lift up for femur and tibia when coax start forward moves but when i1L1 > = 30

Correct. Because it is only one variable, not 4 variables, it is equally easy to run forward or backward. 1 variable is simpler than 4. Simpler is almost always better.

It was the same in your original code:

For these values, step and i1L1 are the same:

``````Step  0: i1L1 =  0 i2L1 =  0 i3L1 =  0 i4L1 =  0
Step  1: i1L1 =  1 i2L1 =  1 i3L1 =  0 i4L1 =  0
Step  2: i1L1 =  2 i2L1 =  2 i3L1 =  0 i4L1 =  0
Step  3: i1L1 =  3 i2L1 =  3 i3L1 =  0 i4L1 =  0
Step  4: i1L1 =  4 i2L1 =  4 i3L1 =  0 i4L1 =  0
Step  5: i1L1 =  5 i2L1 =  5 i3L1 =  0 i4L1 =  0
Step  6: i1L1 =  6 i2L1 =  6 i3L1 =  0 i4L1 =  0
Step  7: i1L1 =  7 i2L1 =  7 i3L1 =  0 i4L1 =  0
``````
``````if (i1L1 >= 30)
``````

also do the same
btw could you suggest me how to run for loop in void setup to make the coax, femur and tibia move more naturally to its initial stall value/ initial position!

``````sc3.write(99);// diagonal to body
sf3.write(55);
st3.write(72);
//sf3 and st3 value create 90 degree legs to its foot step
``````

because when the power switch on the servos moves suddenly.

I cannot think of an easy way to do that. It is difficult because when the power is switched on, the Arduino does not know what the current position of the servos is. That current position would be needed to make a slow movement towards the start position. Without that, the servos will move at their normal, fast speed to reach the start position.

If the Arduino had sensors to inform it of the position of each servo, it would be possible. Maybe replacement servos are available which will allow the Arduino to read each servo's position, I do not know.

Another possibility is to use the Arduino's eeprom memory to record the servo positions as they move. Data in the eeprom memory is not lost when power is switched off. When power is switched on, the Arduino could read the eeprom to find the servo positions and move them slowly to the start position. One problem with this idea is that the eeprom' life is used up a little each time data is written to it, so it will not last forever.

An on/off switch could be connected to the Arduino. This switch would not cut the power. The Arduino would read the switch, and if it has been changed to off, move the servos to their start position before going into a low power sleep mode until the switch is changed to the on position.

None of these ideas are simple or easy. Maybe they are not worth it to fix the startup problem.

1 Like

oke brother. thank you very much for your help. i'll take consideration towards your suggestion on initial position. btw i am not familiar with this code syntax is it C++ or arduino style code? thank you

Arduino code is C++, with some extra libraries added for functions like digitalWrite(). The code that @johnwasser posted is defining a new Class. This is something that is not often seen in an Arduino sketch because the Classes are usually defined in the libraries that the sketch uses. But there is no problem with defining Classes in your sketch also.

1 Like

You're thinking of a DIFFERENT "moving a quadruped leg" forum post. The method in this thread just uses a table of servo positions.

1 Like

Yes indeed, I'm thinking of the thread that @donimart gave the link to in post #29, not this thread. That is what @donimart asked about.

hi brother i have question. how to halt the servo for 500 ms in its angle when the step is =58. thank you

I guess you could change moveLeg3() to return the step it has reached:

``````int moveLeg3(int direction) {
...
...
...
return step;
}
``````

Then, in loop() you can check the step and execute your delay:

``````void loop() {

int leg3step = moveLeg3(+1);
if (leg3step == 58)
delay(500);
else
delay(speedV);
}
``````
1 Like

1st 58 it stop for 500ms 2nd 58 it is not stop the 3rd 58 its stop again. the pattern runs repeatedly

``````void loop(){
int leg3step = moveLeg3(+1);
if (leg3step == 58)
delay(500);
else
delay(speedV);
}
}
int moveLeg3(int direction) {
static int step = 0;
//-----rise the leg
if (step <= 7) {
st3.write(60 + step * 2);
sf3.write(55 + step * 3);
}
// rotate coax backward
if (step <= 30) {
sc3.write(100 + step);
}
// step down the tibia
if (step > 20 & step <= 30) {
st3.write(90 - (step-20) * 2);
sf3.write(87 - (step-20) * 3);// value
}
// rotate coax forward
if (step >= 30) {
sc3.write(120 - (step-30));
}

step += direction;
if (step < 0) step = 58;
else if (step > 58) step = 0;
return step;
}
``````

What does this show?

``````void loop(){
int leg3step = moveLeg3(+1);
Serial.print("Step ");
Serial.print(leg3step);
if (leg3step == 58) {
Serial.println(" long delay");
delay(500);
}
else {
Serial.println(" short delay");
delay(speedV);
}
}
``````
``````Step 1 short delay
Step 2 short delay
Step 3 short delay
Step 4 short delay
Step 5 short delay
Step 6 short delay
Step 7 short delay
Step 8 short delay
Step 9 short delay
Step 10 short delay
Step 11 short delay
Step 12 short delay
Step 13 short delay
Step 14 short delay
Step 15 short delay
Step 16 short delay
Step 17 short delay
Step 18 short delay
Step 19 short delay
Step 20 short delay
Step 21 short delay
Step 22 short delay
Step 23 short delay
Step 24 short delay
Step 25 short delay
Step 26 short delay
Step 27 short delay
Step 28 short delay
Step 29 short delay
Step 30 short delay
Step 31 short delay
Step 32 short delay
Step 33 short delay
Step 34 short delay
Step 35 short delay
Step 36 short delay
Step 37 short delay
Step 38 short delay
Step 39 short delay
Step 40 short delay
Step 41 short delay
Step 42 short delay
Step 43 short delay
Step 44 short delay
Step 45 short delay
Step 46 short delay
Step 47 short delay
Step 48 short delay
Step 49 short delay
Step 50 short delay
Step 51 short delay
Step 52 short delay
Step 53 short delay
Step 54 short delay
Step 55 short delay
Step 56 short delay
Step 57 short delay
Step 58 long delay

``````

dont know why now its halt when step is 58 as expected. thank you

Me too!

Does it still work ok if you remove all Serial.print()?

yes still working without serial print

Post the latest code in full.

i will ASAP

hi brother the actually the code only work for 1 legs. if i combined 4leg (12 servo) the delay will halt/ effect all the servos. i come to idea to speed up sc3 step

``````   if (step <= 30) {
sc3.write(100 + step);
}

``````

3 times faster than

``````  if (step >= 30) {
sc3.write(120 - (step-30));
}
``````

would you like to help me how to do that. thank you