# Arduino Quadruped Robot – Stalker

I am going to build an Arduino Quadruped Robot. As usual, I will share my source code and show as many pictures as possible, to help those of you who are also building Quadruped robots. The way I do things might not be the best ways, and I am sure you can come up with better solutions, please let me know if you do! :-)

This robot is actually the first robot I wanted to build, but I failed miserably. you can check this out and this. After building a working Arduino Hexapod robot, I feel confident that I can pull it off this time! I recycled the parts from the arduino hexapod robot, and build a body with styrene sheets, so there is no new parts. For Inverse Kinematics, Check out my tutorial:

```https://oscarliang.com/inverse-kinematics-and-trigonometry-basics/
```

I made an excel spreadsheet to simulate the movements of a quadruped robot. It shows detailed inverse kinematics calculations, and it might help those who are having problem understanding IK to visualize the complex computations, also it’s a great help to debug your code.

`Download Quadruped Robot Simulation Spreadsheet V1.0`

Update on [31/01/2013]

I call this arduino quadruped robot ‘Stalker’, because of the game unit in Star Craft of the same name. (although they look totally different :p)

## Hardware that I use at the moment:

• Arduino Mega
• 12 Servos
• Wii Nunchuck controller
• a few thick styrene sheets (skeleton)
• 6V Ni-MH 4300mh Battery

## Plan:

1. Build Body and Assemble Robot.
2. Test Wii Nunchuck on it.
3. Write good IK for body rotation, and translation.
4. Get it walking!
5. Write Android App to achieve more complicated task.
6. IR/ Ultra-Sonic Detection and simple AI.

============================================================

## Project Started – Update 31/03/2013

Basically copied most of the codes from my last Hexapod robot. All I did was to remove the variables related to the middle legs, and modifed the preset variable values to work in the Quadruped Robot, and it actually worked! :) But the movements are quite Awkward, a lot of work need to be done about Inverse Kinamatics. Here is how I made the body:

========================================================================

## Quadruped Robot Leg Calibration (Servo Angle Offsets) – Update 03/04/2013

I just didn’t like the body, i thought it was too big! So I spent an evening re-measured, and cut it down from 16cm to 12cm (leg to leg distance).

I have also created a very simple Servo Offset calibration program. As you might know, due to the fact that no servo is perfect, they might tilt to the right a little more than the left, or the other way round. (It could be also caused by the servo brackets) You might want to give it an offset to correct this. It’s very handy when you have a lot of servos like Hexapod/Quadruped Robts have.

[sourcecode language=”cpp”]
// ===========================================================
// blog.OscarLiang.net
// Created on 04/04/2013
//
// ===========================================================
//
// A very simple program to calibrate multiple servo offsets.
// Especially useful if you have a lot of servos, for example
// Hexapod Robots and Quadruped Robots.
//
// ===========================================================
//
// Instructions:
//
// 1. change variable “ServoNum” – number of servos you have.
// 2. change variable “servoPin” – the pins used by your servos.
// 3. upload code to Arduino.
// 4. open serial monitor, use baud rate 9600
// 5. enter + or – in serial monitor to calibrate servo, there
// is a reading printed on the monitor.
// 6. enter n or l to move to next or last servo.
//
// =============================================================

#include

const int ServoNum = 12;

Servo legServo[ServoNum];
int servoPos[ServoNum];
int servoIndex;

int servoPin[ServoNum] = {23, 24, 25,
26, 27, 28,
29, 30, 31,
32, 33, 34};

void setup() {

Serial.begin(9600);
servoIndex = 0;

// ================ Servos ==================
for (int i=0; i<ServoNum; i++){
legServo[i].attach(servoPin[i]);
delay(50);
legServo[i].writeMicroseconds(1500);
servoPos[i] = 1500;
delay(100);
}
delay(500);

Serial.println(“enter + to increment”);
Serial.println(“enter – to decrement”);
Serial.println(“enter n to proceed to next servo”);
Serial.println(“enter l to go back to last servo”);

}

void loop(){

if ( Serial.available()) {

switch(ch){
case ‘+’:
servoPos[servoIndex] += 3;
if(servoPos[servoIndex] >= 2350){
servoIndex = 2350;
Serial.println(“You can’t turn it up anymore (you might damage it!”);
}
else{
legServo[servoIndex].write(servoPos[servoIndex]);
Serial.println(servoPos[servoIndex]);
delay(100);
}
break;

case ‘-‘:
servoPos[servoIndex] -= 3;
if(servoPos[servoIndex] <= 650){
servoIndex = 650;
Serial.println(“You can’t turn it down anymore (you might damage it!”);
}
else{
legServo[servoIndex].write(servoPos[servoIndex]);
Serial.println(servoPos[servoIndex]);
delay(100);
}
break;

case ‘n’:
if(++servoIndex >= 12){
servoIndex = 11;
Serial.println(“we have reached last servo”);
}
else{
Serial.print(“Switched to Pin “);
Serial.println(servoPin[servoIndex]);
}
break;

case ‘l’:
if(–servoIndex < 0){
servoIndex = 0;
Serial.println(“we have reached first servo”);
}
else{
Serial.print(“Switched to Pin “);
Serial.println(servoPin[servoIndex]);
}
break;

default:
Serial.println(“Unknown Command… “);

}
}
}
[/sourcecode]

.

## Creeping Gait – Update 15/04/2013

Some Background Gait Knowledge you might want to know:

`https://oscarliang.com/quadruped-robot-gait-study/`

Tonight I came up with a 20-step Creeping Gait. This gait is how 4 legged animals, for example cats, walk. This is the GaitStep Plan (leg 1 is the top right leg, leg 2 is the bottom right leg… etc…) This follows the idea about creep gait described here:

`https://oscarliang.com/quadruped-robot-gait-study/`

Circled steps are the leg-lifting steps, and the gait cycle starts at step one where leg starts to be lifted. I copied most of the code on how to generate gait sequence from my previous hexapod robot, you can check out the source code. Here is the source code for the gait I have written: Before we start, we call this function to initialize the variables for the Gait:

[sourcecode language=”cpp”]

void GaitSelect (){

//begining of the step cycle for each leg (with reference to the first leg)
GaitLegNr[LR] = 1;
GaitLegNr[LF] = 6;
GaitLegNr[RR] = 11;
GaitLegNr[RF] = 16;

NrLiftedPos = 5; // number of steps that the leg is in the air
TLDivFactor = 15; // number of steps that the leg is on the ground
StepsInGait = 20; // total steps
}
[/sourcecode]

For a complete Gait cycle (e.g. from step 1 to step 20), we will go through loop() function 20 times, each time we call GaitCalculate(), and then increment ‘GaitStep’ variable.

[sourcecode language=”cpp”]
coor GaitCalculate (){
//Calculate Gait positions

for (int LegIndex = 0; LegIndex < 4; LegIndex++){

if (GaitStep == GaitLegNr[LegIndex]) {
GaitPos[LegIndex].Y = -LegLiftHeight/2;
}
else if (GaitStep == GaitLegNr[LegIndex]+1) {
GaitPos[LegIndex].Z = 0;
GaitPos[LegIndex].Y = -LegLiftHeight;
}
else if (GaitStep == GaitLegNr[LegIndex]+2) {
GaitPos[LegIndex].Z = WalkLength/2;
GaitPos[LegIndex].Y = -LegLiftHeight/2;
}
else if (GaitStep == GaitLegNr[LegIndex]+3) {
GaitPos[LegIndex].Y = 0;
}
else{
// move body forward
GaitPos[LegIndex].Z -= WalkLength/TLDivFactor;

}

}

if (++GaitStep > StepsInGait)
GaitStep = 1;

}
[/sourcecode]

This is by no mean the best implementation, but it might give you an idea how to start doing gaits if you still have no clue. I will keep working on Gait, make it more stable, faster, smoother and maybe come up with some more gait style as well, before I publish my full working code.

## Trot Gait – Update 28/04/2013

Right, finally have the time to do some robotics, and this time I made a fist version of Trot Gait for the stalker.

I didn’t look into too much theory stuff to get this gait working, as it pretty straight forward: 2 diagonal legs (1 and 3) lifted and move forward, the other two legs (2 and 4) at the same time move backward, 1 and 3 land. Now 2 and 4 are lifted to move forward while 1 and 3 move backward … And repeat…. Check out my Gait Study post for more detail:

`https://oscarliang.com/quadruped-robot-gait-study/`

## DIY Remote Controller – Update 28/07/2013

I have been working hard on a universal DIY robotic remote controller that uses XBee or Ciesco XRF for wireless communication. I also aim to develop an open source Arduino library that can be easily modified to suit different remote controller configuration. You can find more information on the project post. Hopefully at the end, this remote controller can be used in all sorts of robots and projects that requires manual control.

## Servo Horn orientation

Some one asked me what’s the orientation of the leg servo horns. so here is a very rough drawing.

To raise money for future projects, I am starting a fund raise. To say thank you, anyone who donate more than UK£10, I will send you the source code of this project. Amount does not matter, every little helps. You can donate through paypal via the link on the side bar (right hand side – Donate button). Please drop me a private message as soon as the donation is made and point out which project you are interested in. However I can’t give you any technical support on this project anymore as I am no longer working one it.

(Please don’t expect my source code will work straight after uploading on your robot. It would be best only for your reference. Although it’s possible, but it will take some effort to rewrite the code, therefore I don’t recommend Arduino/programming beginners to get it)

## 106 thoughts on “Arduino Quadruped Robot – Stalker”

1. Yang

Hi Oscar, I’m very interest in your this quadruped robot, can I still donate and get the source code now?

2. Dima

Hi, I am interested in your Quaduped robot. Coulyou tell me how to donate for Inverse Kinematic and code. I have Visa.

3. Phil Desrosiers

4. Christopher Caligiuri

Hi Oscar,
I am a student working on my first robot and was wondering if you could send me your spreadsheet? I am having some difficulty coding my gait.
My email is crcali AT gmail.com.

1. Christopher Caligiuri

Also, if I donate, can I receive all the code mentioned on this blog, including both the spreadsheet and the code for the gaits?
Thanks!

5. markuzzzz

6. Louis Connell

Hi,

I just donated for the source code, do you think you could send me the code? I’m really struggling with smoothing out the movement on my quad, hope your code will point me in the right direction.

Thank you!

Louis

7. Corey

Very nice project. Do you still have the Quadruped Robot Simulation Spreadsheet? The dropbox link doesn’t work.

8. Gabriele

Hi,
can I also use the code on Arduino one?
Is the code the same one used by Sunfounder Quadruped ?
Thanks
Gabry

Hi Oscar

I have donated 12 pounds just to be sure it was enough. Would you be able to send me the code for the Quad and Hex?
Great work on the development of these robot!

10. hagar

Oh i got it , I’ve just trying using my phone , but it;s done by my laptop.
I already donate, so could you please send to me the source code of quadruped robot with remote :)

1. kkw

Hi Oscar, It’s time for me to tackle a quadruped after a couple hexy. Am interested in yr coding n excel spreadsheet.
Just did a contribution. Thks mate.

11. Hagar

Hi Oscar ,
Is donation is still available ?
Becouse I’m very interested in this project and I plan to make it for my B-tech project .

12. Gary Oldham

I am trying to build a quad bot and this would help me very much learn what i am missing. However i cannot find a paypal button anywhere. I am so excited about this since i am a disabled US military veteran and Arduino has been my calming, and i have been fully into coding for about a year. This is what i really want to build.

13. Jack

Hi. Oscar. I am Jack. I just did the donation. Could you send me the source code for both of quadruped one and hexapod? Thanks so much.

14. Jack

Hi. Is the donation still available for the source code? If it is, please let me know as soon as possible. I am learning the programming of four-legged robot. Once you reply me, I will donate immediately. BTW, how can I send you the private messages to tell you which project I am interested in because I think I need both of quadruped robot and hexapod.

15. Faruq Sandi

Hi Oscar!
Great post!
How do you think if i use plastic gear micro servo?
Are they strong enough to lift frame, arduino and small servo driver circuit?
Thanks!

1. Oscar Post author

depends on your payload and robot weight… I tried 9g servos but they are just not strong enough…

16. Kjell B

Hi Oscar!

Thanks
Kjell B
Norway

17. Shubhankar Das

Hello I want a help can I make a basic quadruped with just 12 servos and arduino mega….is servo controller board is necessary?

18. Mike

Hi oscar, can you send me the flow chart of programming of the robot I want to know the sequence of how the robot operate. I already donated for the contribution . Thx :-)

19. Mike

Hi Oscar
I have a question about your Arduino Mega shield what capacitor u using for the both??
If i didnt put the capacitor is it ok ??

20. NC

Very well written arcticle. Congrats on making such a wonderful blog. I have donated the amount please share the source code. Thank you.

21. Mike

Hi Oscar
I m doing the same project as u did
If i donate UK£10 can u send me the full code and also the DIY wireless Remote controller which is link together
and can u list out what electronic component u used in the project THX :)

1. Oscar Post author

Hi Mike, yes once the donation is received you will get the code :)
thanks
Oscar

22. Guilherme Alegre

Hi Oscar,
I was interested in the code for a quadruped.
thank you very much

23. jasongosal

Beside that, im using digital servo hs-5645 mg, i wonder whats wrong with my servo PWM pin to arduino, it only generate about 1.5 volt instead of 3.3volt.

im using arduino mega 2560 , and i attach my 12 servo’s signal pin to 2 until 13 pwm pin in arduino. is that okay to do that? am i need servo shield?

24. jasongosal

Hi, Oscar i just donated today.

Im now in project for quadruped.
can you send your code for the quadruped please? the last one with gait, inverse kinematics, rotation matrix, and coordinate transform. thank you. i want learn from your code.
my email is [email protected], i’ll email to you my donation transaction proof.

25. Ross

Oscar,
Donated today, thank you in advance for the great code examples you are going to send me. I also fly quadcopter and a tricopter. Looking forward to seeing what I can do on the ground with your project as a base. I will try to post some of the results as I get them.

Thanks again.

26. Benj

Hi Oscar,

is the £10 for the code still available? We are planning to build the same robot but IK is just somewhat beyond us. We would like to see how IK and the gait is implemented in your code.

Thanks

27. Charles

Great job man!! I was looking for a good quaduped frame example to start mine and i have to admit i love this one… so please can i have more details about your frame ?? (dimensions, weight…)
thx!

28. Juan Mena

Hi Oscar,
I was interested in the code for a quadruped.
thank you very much

29. Clin

Hey Oscar,

Nicely done mate! Im working on a similar project for my robotic class this semester. What I am stuck on is keeping the creeper balance when walking. Whenever it walks for more than 4 steps, the body would start to collapse and sink from the middle =(. Any suggestion? And how long was your source code? did you code each individual legs with .attach () and .write () and delays? It will be really cool if I can refer your codes.

Thanks !

30. Mudassir

On what basis you are using 6V Ni-MH 4300mh Battery? I mean what are the calculations at the back end. how do we calculate the requirements for our battery?

31. Dorine

Outstanding post however , I was wanting to know if you could write a litte more
on this subject? I’d be very thankful if you could elaborate a
little bit more. Cheers!

32. Dani

Hi Oscar,

Your blog is great! Me and my son are having a blast going through it looking for projects and he got really excited about the robot.

Is there any chance of getting your Quadruped Robot source code mentioned in the top of the page? (gait & IK programming is a little beyond us.. so would be great to see the code)

Going to be exciting to build one with my son using your code!

Thanks!

33. lilianliu

I have to say, I see your design almost two days without sleep, I really love it, and I learn to do quadruped robot for my son .but i study to do it just now,i really want to get your help.tks

1. lilianliu

thanks Mr. Oscar

Lilian

34. william

Hey oscar,
I have staretd the development of arduino based Quadruped robot for my college project.I initially started with the purchase of hardware like servo brackets,robot leg
and i purchased it at a very cheap cost compared to others.I purchaesd robot parts form
.I also bought the quadruped robot chasis for the same website .Its awesum and my full assembled robot is complete
and it looks perfect.Now i want to start with the programming of the same robot using arduino.I want to know which dimensional measurements of the robot to feed to the IK program
and if u can tell me according to your program which parameters i need to measure as per my chassis.If u show it in pictorial format i will be gratefu to u.

Thanks

35. laximikant

thanks Mr. Oscar,

we are working on quadrupped robot and we are done with the leg IK,i.e.finding Alpha ,beta ,gamma and we dont know how to get the body IK and relate it int full program…ur reply wil be appreciated

regards,
laximikant.

36. laximikant

hi Oscar,
can u please tell me the steps to be followed in coding a quadrupped…like wat i understood by reading previous post that we should first callibrate the servos…so wat is the stepwise implementation….

regards
laximikant

1. Oscar

1. decide on the frame, and then the servo, control board, battery…
2. assembling (servo + frame + electronics)
3. servo testing, make sure all servos are working as expected, and servo calibration.
4. implement Inverse Kinematics (IK)
5. with IK, you can implement body stretching (pitch, roll, yaw – moving center of body to all directions)
6. you can now try to implement gait

that’s all I have done so far, from here you can use your imagination, and do more advance stuff, like AI, object avoidance etc…

Hi Oscar, Would it perhaps be possible to get an explanation on how your balancing works?
Would it perhaps also be possible for me to send you some emails asking you about how some of your functions work? Just drop me an email if it is.
Thanks a lot

38. Martin

Hi Oscar,

Here’s hoping you’re keeping well. I’ve spent hours in your hexapod code since my last post on here. Things are coming along nicely. I’m turning my attention to your BodyBalance() function now. I’ve got everything else working ok at this point. I’d really appreciate a little explanation on the BodyBalance function. For example, I don’t see how TotalBal is used to do anything useful in the code. Also, how is that Quad code coming along? Can we expect a release sometime soon?:) I’ve tried making monetary contributions to your cause, but unfortunately I don’t have credit card facilities at this point. Any suggestions in that regard are welcome.

Thank you Oscar, always appreciate your efforts.

Kind Regards,
Martin

39. DieDen

Hey, I got a ques plz
1-What is the torque of the servos? I’m using a 10 kg/cm^3 servos , is this torque enough?
2- Are u willing to attach a camera so that u can get a video stream or pictures? If yes, is it possible to use arduino for real time streaming? :/
3- I love ur robot’s legs :D did u make them urself? they are super nice, for me i’m planning to make the whole body on my own any advises? :D

1. Oscar

Hi,
1. Those servos are hitec mg645. it’s about 9 kg/cm^3, but it’s already a over kill for this kind of robot.
2. Arduino can’t be used for video streaming (not even pictures), because it’s not powerful enough. You will need a separate system to handle that type of business, e.g. a smart phone, or raspberry pi.
3. those legs are actually very rubbish! it’s kinematically restrained. I am looking for a better design. You can DIY it, I would suggest 3D printing, it could save you a lot of time. or if you have a laser cutter, you can use some other stronger and cheap materials.

40. DVS

Hi Oscar, Is there any chance that you could post your source code?
I would like to see how you implemented the gaits.
Have you managed to get the creep gait to be able to not just walk in a straight line yet?

1. Oscar

Yes! I will do that.
perhaps in a couple of weeks because i need to find the time to clean the code and add comments (so i don’t get blamed :( )

I haven’t looked into gait transition yet, but i will post the code before i do that so you don’t have to wait another year haha

41. Martin

Martin

1. Oscar

Hi Martin, thanks for the kind words.
I can share the code with you that’s fine, I know how much it would help with your final year project because I had been there before myself!

i will email you to let you know.

regards
Oscar

1. Martin

Hi Oscar,

Kindly let me know when you receive my donation. Here’s hoping you’re well Oscar.

Regards,
Martin

2. Oscar

Hi Martin, thank you very much and yes I received your contribution!
I am currently working very hard on my second/third websites and not have much time spent on robots!
but should be back to it soon!

I hope you are doing well too.
Regards
Oscar

42. Mahmoud

Hello, first let me express my admiration for ur project, I really like it and i’m aiming to make a similar one as my graduation project with android app to control it + a camera for face detection,i have some question plz.
did u use a servo controller circuit or u plugged the servos directly to arduino mega? (this confuses me alot as i see other projects using a servo controller circuts that costs a fortune!)
where is the power source that u used? i’m not seeing any battery in the pics :D

1. Oscar

Thank you!
I connected everything directly to the Arduino Mega, but you will need external power source for the servos, so no servo controller is required.
Battery is a 6V NiMH Battery, it’s inside the body (under the arduino). You can also use LiPo, it’s lighter but less capacitance and more expensive.

43. Brian

Your robot looks great! I am trying to modify your hexapod code to work with a quadruped as you have done here but I am seeing a difference between the two GaitCalculate functions. In your hexapod code you pass in a coor object called WalkLength and use the x y and z values determined by nunchuck. In your quadruped GaitCalculate you just directly use the walklength variable. Could you explain more how these two variants differ and why? Thanks for the help in advance and for sharing your awesome project!

1. Oscar

Thanks Brian!
that’s a good question. I am using WalkLength as a variable (literally just the z direction) because I still don’t know how to turn turn right or left during walking forward with this gait (creep gait), so I have ignore the x input (going right or left). How embarrassing…. ;-p

But for Trot Gait (the last video, or third video), I used the same the same WalkLength variable as the Hexapod (a coordinate).

So yea, WalkLenghth input should awlays be a coordinate… hope that make senses..

1. Martin

Hey Oscar,

After reading Brian’s comment regarding the variable WalkLength, and your comment about WalkLength always being a coordinate, I have a question.

I get an error “no match for ‘operator/’ ” when I leave WalkLength as a variable as follows:

GaitPos[LegIndex].Z = WalkLength/2;

However, when used as a coordinate:

GaitPos[LegIndex].Z = WalkLength.Z/2

Then I have no problems in terms of compiling. May I do this? I’m not sure what I’m missing since Brian didn’t seem to have a problem using WalkLength as a variable or a coordinate. All I have done is replaced the hexapod “GaitSelect” and “GaitCalculate” with your quadruped versions of those functions. Your assistance is appreciated as usual.

Kind Regards,
Martin

2. Oscar

Hi Martin,

It depends on what variable you use to get the input.

If you declare “WalkLength” as int, you should be using
GaitPos[LegIndex].Z = WalkLength/2;

If you declare it as coor (a data type I define in a separate library file), then you should use this
GaitPos[LegIndex].Z = WalkLength.Z/2;

Yes, from the video I posted, I pretty much copied the code from the hexapod robot, so you are doing the same thing as I have done so far.
But there is something extra we should do with Quadruped robot. Because the COG (center of gravity) has to be inside of the tripod (formed by the 3 landing legs) before the other leg can lift. We need to have a function that makes sure of that, and tells the quadruped robot to shift the body toward that point to maintain balance.

Of course that could be the next step forward if you get the quadruped robot moving walking.

Regards
Oscar

44. Jodgey

Hey Oscar,

Could you please post your entire source code for both of your gaits? That would be awesome! Thank you!

1. Jodgey

I should’ve mentioned before. We are trying to make an autonomous quadruped for a senior project. We have been having a lot of issues with our project so far (like getting a properly stable frame, finding the right type of servo controller, etc) We solved these issues but now we are stuck on the gait with only a couple weeks left before our deadline. For now, we would appreciate it if we could get it to walk by typing commanding into the serial command window in the Arduino program. Is there any way that we can:
1) get you source code for the quadruped
2) get some helpful tips for how to use this code without having the nunchuck?
We greatly appreciate your help and how well organized your code is. Thank you

Jodgey

45. rahim

Could you post the code or the algorithm you use for walking.

or a bit explanation on how you choose angles.

I read your IK post but I don’t quit understand on how to apply them.

Thanks

46. Enrique

Hi Oscar. First of all congratulate you four your extraordinary work with this quadruped robot. I’m working in a quadruped robot too, and i am wondering if you can explain me the mechanic part, i mean the design of the legs and how you fixed the servos to the body and the legs.
Sorry for my bad english and thank you!

For prompt technical support, please use our forum IntoFPV.com. I check blog comments weekly.