The angkle servo not running. arduino quadruped

hi. how to move ankle servo since all ankles servo not running of my quadruped robot. bellow is my code. and thank you for any help

#include <Servo.h>
#include <math.h>

// Define servo objects
Servo hipServo1, kneeServo1, ankleServo1;
Servo hipServo2, kneeServo2, ankleServo2;
Servo hipServo3, kneeServo3, ankleServo3;
Servo hipServo4, kneeServo4, ankleServo4;

// Constants for leg dimensions (lengths in millimeters)
const float femurLength = 50.0;
const float tibiaLength = 70.0;
const float coxaLength = 30.0;

// Variables for joint angles
float hipAngle, kneeAngle, ankleAngle;

// Time variables for sinusoidal function
unsigned long startTime;
const float stepDuration = 1000.0;  // Time for one step in milliseconds

void setup() {
  // Attach servos to respective pins
  // int servoPins[numServos] = {42, 13, 7, 45, 46, 47, 48, 25,50, 51, 33, 53}; // Replace with your actual pin numbers

  hipServo1.attach(42);
  kneeServo1.attach(13);
  ankleServo1.attach(7);

  hipServo2.attach(45);
  kneeServo2.attach(46);
  ankleServo2.attach(47);

  hipServo3.attach(48);
  kneeServo3.attach(25);
  ankleServo3.attach(50);

  hipServo4.attach(51);
  kneeServo4.attach(33);
  ankleServo4.attach(53);

  // Set initial leg positions
  resetLegs();
  startTime = millis();
}

void loop() {
  // Calculate the elapsed time since the last step
  unsigned long elapsedTime = millis() - startTime;

  // Move all legs forward in a sinusoidal pattern
  moveLeg(hipServo1, kneeServo1, ankleServo1, sinMovement(elapsedTime));
  moveLeg(hipServo2, kneeServo2, ankleServo2, sinMovement(elapsedTime));
  moveLeg(hipServo3, kneeServo3, ankleServo3, sinMovement(elapsedTime));
  moveLeg(hipServo4, kneeServo4, ankleServo4, sinMovement(elapsedTime));

  // If one step is completed, reset the start time
  if (elapsedTime > stepDuration) {
    startTime = millis();
  }
}

void moveLeg(Servo hip, Servo knee, Servo ankle, float hipOffset) {
  // Calculate servo angles using inverse kinematics with sinusoidal function
  inverseKinematics(hipOffset, -45, 0);  // targetHip, targetKnee, targetAnkle

  // Move servos to the calculated angles
  hip.write(hipAngle);
  knee.write(kneeAngle);
  ankle.write(ankleAngle);
}

float sinMovement(unsigned long elapsedTime) {
  // Use a sinusoidal function to generate hip joint movement
  float period = 2 * PI * elapsedTime / stepDuration;
  return 30 * sin(period);  // Adjust amplitude and frequency for desired motion
}

void inverseKinematics(float targetHip, float targetKnee, float targetAnkle) {
  // Implement inverse kinematics equations here
  // This can vary depending on your robot's leg structure
  // For a simple example, you can use trigonometric functions

  // Calculate joint angles using trigonometry
  hipAngle = atan2(-femurLength * sin(targetHip), femurLength * cos(targetHip) - coxaLength) * RAD_TO_DEG;
  kneeAngle = atan2(femurLength * sin(targetHip), femurLength * cos(targetHip) - coxaLength - tibiaLength) * RAD_TO_DEG;
  ankleAngle = targetAnkle;  // Assuming direct control of ankle angle
}

void resetLegs() {
  // Reset all legs to their initial positions
  moveLeg(hipServo1, kneeServo1, ankleServo1, 0);
  moveLeg(hipServo2, kneeServo2, ankleServo2, 0);
  moveLeg(hipServo3, kneeServo3, ankleServo3, 0);
  moveLeg(hipServo4, kneeServo4, ankleServo4, 0);

  delay(100);  // Delay for stability
}

Does the value of ankleAngle ever changes in the sketch ?

You should read up about structs, enums and arrays.

They will make your code shorter and easier to read, modify & debug.

why isn't there a similar calculation for the ankleAngle?

1 Like

i'm curious ... it looks like you're determining the angles of the joints based on a desired position, at least for the knee

  • don't understand why the hip angle isn't a given
  • if the knee angle is calculated relative to it's position (??), why isn't the hip angle added to determine the servo angle
  • same can be said for the ankle, although wouldn't it be reasonable to set the ankle angle so that the foot is parallel with floor?

and then

  • shouldn't the hip angle be from some forward angle to some rearward angle, not simply the sine of a complete rotation
  • shouldn't the relative angles of the knee and ankle joints depend on whether the leg is moving rearward in contact with the floor or moving forward without contacting the floor
  • shouldn't the motions be the same for the front-right and back-left legs, and the opposite for the front-left/back-right legs

Hello donimart

Take a search engine of your choice and ask the WWW for 'quadruped robot +arduino' to collect some data to be sorted out to get the needed information.

Have a nice day and enjoy coding in C++.

in addition to the points i made above

  • it seems that at least for constant motion, the joint angles can be calculated apriori and put into a table. the code then uses those values to drive the movement of each leg with an appropriate offset for left/right

  • the other thing to consider is that the angle of each servo depends the angle of the joint which depends on the angles of all previous joints

  • in image below, consider joint angles depending on CW/CCW motion

Are they all on a PWM pin?

Why does it matter whether they on a PWM pin or not ? The Servo library does not depend on that

Ah, right. My mistake. I forgot that even I used digital on/off timing to make a servo move.

hi all. i try find this code from chatGpt and try the code in wokwi doniQuadruped - Wokwi ESP32, STM32, Arduino Simulator and also add ankleAngle joint calculation ass @gcjr mentioned and the angkle servos runs. if you dont mind would you like to visit the wokwi link.

1 Like

Here we go again down the rabbit hole!

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