The Balancing Robot

Now avaliable as a kit
A balancing robot kit is now avaliable via Kickstarter: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit. Check out the blog post as well: http://blog.tkjelectronics.dk/2013/03/balanduino-balancing-robot-kit-kickstarter/.


Hello everybody
I have for a long time wanted to build a remote controllable balancing robot aka Segway – that’s was actually the main reason why I created the PS3 Bluetooth Library both for Arduino and the FEZ Devices. It has been a long time since the sneak peak and the performance has been improved a lot since then. The original one had a FEZ Rhino as the main processor, but I discovered that it was not fast enough to read the encoders, as it is not running embedded code. Also I was already using more than 10ms per loop, which I used as a fixed time loop, so I decided to step up a notch and go for a much more powerful device: the mbed microcontroller, which is an ARM Cortex-M3 running 96MHz.

It might have been possible with just a normal Arduino (NB: I have now ported the code to Arduino, see update for the code), but I didn’t want the speed of the processor to be an issue, so I decided to go for the mbed. The robot also features an Arduino Duemilanove with a USB Host Shield on top running a sketch based on my PS3 Bluetooth Library. The mbed board actually has USB Host functionality, but I decided not to port the PS3 Bluetooth Library as my original thought were to use an Arduino Due, but as you might know it hasn’t been released yet, despite the Arduino team announced, that it would be released by the end of 2011. But as soon as it is released I think I will port the code to it instead.

Video Demonstration
Here is a short video demonstration of the robot and me explaining some of the concepts of the design and how it works:


The Hardware
Here are some pictures of the robot:


Here is a list of all the hardware I used:

I also used:

The robot itself is made of three pieces of 215x75x7.5mm MDF wood and four threaded rods. The distance between the plates is 7cm at the bottom and 7.5 at the top. In total the robot is 27cm high including the battery.
See the 3D model for more information.

3D Model
I have created a 3D model in Autodesk Inventor with true dimensions, this will hopefully inspire other people for there robot design. All files can be found at github.
The 3D model can also be viewed at the following site: http://grabcad.com/library/balancing-robot.

Check out these rendered images of the robot:

The Code
All the code and 3D model can be found at our github. Here is a list of hyperlinks for all the repositories:

Also check out the wiki.

Update
I have now ported the code to Arduino. The code can be found at github: https://github.com/TKJElectronics/BalancingRobotArduino.

Improvements
I have thought about how I could improve the performance of the robot. First of all I could try to use an accelerometer with a smaller resolution, as the one I got is a ±3g and ±1.5g would be sufficient for my needs. Also my gyro got a resolution of ±300 deg/s and I have seen people use gyro with a resolution as low as ±50 deg/s.

Another aspect would to use belts to minimize backlash, instead of connecting the wheels directly to the motors – a bit like this one.

Also I don’t compensate for the battery level in the code – so it behaves differently depending on the battery level.

Conclusion
Overall I am really happy about the end result – it balances pretty well and the remote control works perfect!

It has been a really good learning experience for me and a really fun project to do, but also very time-consuming – I have spend many nights tweaking the PID values and adjusting tiny bits of the code before I accomplished the end result.

The next step would to build a full size one, but I don’t know if I will do it the near future – but hopefully some day :)

That’s all for now. Hope you like my robot. Feel free to post a comment below and I will answer as quickly as possible.

  1. December 31st, 2013 at 01:39 | #1

    @Blanco Lucas Ezequiel
    Sorry, but I don’t understand what you are trying to say?
    Anyway you will find the Balanduino code here: https://github.com/TKJElectronics/Balanduino/tree/master/Firmware/Balanduino. It is the newest version of the code and uses the MPU-6050, which you are trying to use, as indicated by your code above.

  2. Venugopal
    February 12th, 2014 at 11:34 | #2

    I am a final year student of Aeronautical engg,. from India
    I would be happy if i get an aid from you for doing a project on SELF BALANCING ABILITY OF THE AIRCRAFT using this method. kindly give me a reply Sir

  3. February 19th, 2014 at 16:49 | #3

    @Venugopal
    What do you got problems with? You should checkout the Balanduino repo: https://github.com/TKJElectronics/Balanduino where you will find all the hardware drawings and firmware.

  4. March 10th, 2014 at 09:56 | #4

    Hi
    Do you have the schematic of the robot circuit. I didnt see any schematic and block diagram of the robot. If it present, can you please inform me and give its link

    Regards
    Lentin

  5. March 10th, 2014 at 22:38 | #5

    @Lentin Joseph
    You should take a look at the Balanduino schematic: https://github.com/TKJElectronics/Balanduino/tree/master/PCB.

  6. March 25th, 2014 at 10:46 | #6

    I’m not sure why but thos weblog is loading very slow for me.
    Is anyone else having this problem oor is it a issue on my end?
    I’ll check back laater and see if the problem still exists.

  7. March 25th, 2014 at 17:53 | #7

    @Google
    It might be slow at peak times, but in general it should respond pretty fast. Are you still experiencing problem?

  8. Warman
    April 13th, 2014 at 04:19 | #8

    Hi!

    A nice piece of work. I am curious to know about the PID controller architecture. Do you have any information about the controller design. (E.g: what is/are your reference commands? what variables are used to calculate the PID error terms? Inner/Outer loop architecture etc? Any information about the design of the controller for your specific robot would be of great help. Please let me know.

    Thanks!

  9. April 17th, 2014 at 15:05 | #9

    @Warman
    You should take a the source code here: https://github.com/TKJElectronics/Balanduino/blob/master/Firmware/Balanduino/Motor.ino.
    Currently I use kind of a PD controller for the encoders, the output of this is then used as the input for a second PID controller which outputs the PWM value.

    I did not calculate the PID values. I tuned it manually.

  10. MKB
    May 3rd, 2014 at 12:29 | #10

    Hello Lauszus!
    I bought a motor DC 12V speed load 220rpm weight 250g, current load 1200mA, you think that it has enough torque to robot balancing ? I try to look for value PID but my robot not balancing.
    Thanks you!

  11. May 3rd, 2014 at 13:20 | #11

    @MKB
    Please send me the link to the motors. I need to know the torque.
    But they sound like they would work.
    What code are you using?

  12. MKB
    May 3rd, 2014 at 18:09 | #12

    @Lauszus
    Torque : 5kgf.cm. This is code:

    #define   GUARD_GAIN   20.0

    float K = 1.4;
    int   Kp =11;                      
    int   Ki = 0;                  
    int   Kd = 10;  
    int last_error = 0;
    int integrated_error = 0;
    int pTerm = 0, iTerm = 0, dTerm = 0;

    int updatePid(int targetPosition, int currentPosition)   {
      int error = targetPosition - currentPosition;
      pTerm = Kp * error;
      integrated_error += error;                                      
      iTerm = Ki * constrain(integrated_error, -GUARD_GAIN, GUARD_GAIN);
      dTerm = Kd * (error - last_error);                            
      last_error = error;
      return -constrain(K*(pTerm + iTerm + dTerm), -255, 255);
    }

    I was very impressed with your robot. Thanks you.

  13. MKB
    May 3rd, 2014 at 18:11 | #13

    @MKB

    int Drive_Motor(int PWM_val)  {
    if (PWM_val& > 255)
      {PWM_val=255;}
    if ((kalAngleY > 150) && (kalAngleY < 166))
    {PWM_val=255;}
    if((kalAngleY191))
    {PWM_val=255;}
      if ((kalAngleY > 166) && (kalAngleYsetPoint)
    {
      analogWrite(PWM1, PWM_val);
      analogWrite(PWM2, PWM_val);
      digitalWrite(InA1, LOW);
      digitalWrite(InB1, LOW);
    }
    else
    {
      analogWrite(PWM1, PWM_val);
      analogWrite(PWM2, PWM_val);
      digitalWrite(InA1, HIGH);
      digitalWrite(InB1, HIGH);
    }
      }
      else
     {
      analogWrite(PWM1, 0);
      analogWrite(PWM2, 0);
      digitalWrite(InA1, HIGH);
      digitalWrite(InB1, HIGH);
     }
     }
  14. slo3
    May 28th, 2014 at 05:13 | #14

    Great site and thanks for sharing the code… but I’m a bit confused about how everything “fits together”… What code runs where?
    The mbed code runs on the mbed (or a second Arduino in the updated version?)
    But does that mean that the Kalman filtering code runs on an Arduino alongside the USB shield and the PS3 control input? How? I’ve only ever run a single piece of code on an Arduino at once… What am I missing?

  15. June 6th, 2014 at 09:25 | #15

    @slo3
    In this video demonstration all the real-time control and regulation (stabilization) of the robot is done with the Arduino. The mbed is only used for the USB control with the PS3 controller thru Bluetooth.

    In our new version, the Balanduino, the main board takes care of all this in one single processor, an ATmega1284P. This Arduino compatible board includes both the regulator design and the USB Host control application, embedded into one system and one project.

  16. July 10th, 2014 at 19:48 | #16

    Hi,

    I am a high school student who is building this self-balancing robot, and I was wondering for the Kalman filter Arduino library, how did you calculate the GYRO_SCALE and ACC_SCALE? My raw gyro values are fairly small, and dividing them by the even smaller GYRO_SCALE makes them very large and noisy. Where should I look on the IMU’s data sheet? Thank you very much!

    Sincerely,
    Claire

  17. July 11th, 2014 at 23:58 | #17

    @Claire
    You should read my guide here: http://arduino.cc/forum/index.php/topic,58048.0.html. I believe it will answer your questions.

Comment pages
1 9 10 11 2196
  1. March 4th, 2012 at 21:03 | #1
  2. March 7th, 2012 at 21:22 | #2
  3. March 7th, 2012 at 21:29 | #3
  4. March 7th, 2012 at 23:33 | #4
  5. March 8th, 2012 at 02:49 | #5
  6. March 8th, 2012 at 05:03 | #6
  7. March 8th, 2012 at 08:31 | #7
  8. May 14th, 2012 at 15:14 | #8
  9. May 23rd, 2012 at 20:02 | #9
  10. February 17th, 2013 at 15:13 | #10
  11. February 17th, 2013 at 15:24 | #11
  12. March 18th, 2013 at 23:18 | #12
  13. March 24th, 2013 at 17:58 | #13