Chappie- Self-Balancing Robot

95,089

343

99

About: If you like my work,Please visit my Website for more awesome things

Intro: Chappie- Self-Balancing Robot

After getting so much frustrated over PID tuning of quadcopter, I decided to master PID first on some basic project. Self-balancing robot seems an idle choice. Since it’s not new and still challenging, I decided to go for it.
I never thought it will be easy and I am not disappointed. I learned a lot from it and decided to share it with others.

EDIT - Some people were getting NaN values in their PID readings.The reason was EEPROM use. PID values were loaded from the EEPROM on initialisation. If values haven't already been stored in EEPROM,it will return NaN. People who don't need EEPROM can find a separate version in GITHUB.

Step 1: Concept/Theory

What is Self-balancing robot?

Self-Balance Robot is somewhat which balances itself and automatically corrects its position on disturbance.

How it works?

It will be prevented from falling by giving acceleration to the wheels according to its inclination from the vertical. If the bot gets tilts by an angle, than in the frame of the wheels, the center of mass of the bot will experience a pseudo force which will apply a torque opposite to the direction of tilt.

That’s the technical answer. If you want to know in simple terms, recall the stick balancing game which you used to play in your childhood.Yeah, the one in which you balance stick in your hand. If stick falls forward, you move your hand forward and vice-versa. Now consider the stick your robot and the 2 tire’s as your hands. The same technique you used to balance stick will now be used to balance robot.

Step 2: Materials

First we need some materials to build our chassis. I choose Acrylic sheet. Some screws will be needed according to design which we will discuss shortly plus 2 tires.

For remaining Electronics part, here’s the list

1x Arduino UNO

1x HC-05 Bluetooth Module

1x MPU 6050

1x L293D

1x IC7805

1x Battery

2x 500 rpm Motors

2x Motor clamps with screws

Some jumper wires and breadboard/PCB

We will talk about the choices of these components later

Step 3: Chassis

I designed my chassis using acrylic sheet (just to save trouble of drilling holes). However, it can be designed using Wooden plank, plastic sheet etc. too. In my opinion, wooden plank will be good choice if you want to build a stable chassis and plan to mount something on it. Anyways, I cut sheet to form 3 levels/floors. It’s not mandatory, but is a good practice. No. of levels can be increased if desired. After markings, holes has to be made for screws. For making holes, I simply use Solder iron. (Here acrylic sheet comes to rescue. After making holes, I realized that I made mistakes which I corrected easily) If you are using, wood, double check your markings.

Choice of tires should be ones with good grip (rubber, if available) and large surface area.

Assemble the whole chassis and we are ready to move to electronics part.

Step 4: Connections

Now why these components?

MPU-6050 is used due to its DMP capability. It can process the Raw Accelerometer & Gyro values to give the Yaw, Pitch, and Roll. It greatly decreases the computation overhead.

HC-05 Bluetooth module is used for communication between Bot and Android app.

L293d motor driver is used for controlling motor.

IC7805 voltage Regulator is used for providing 5V power supply to Arduino. However, using this IC is not necessary and battery can be directly plugged to Arduino. This is just used for Safety Purposes

A fritizing file is attached to show the connections. Connect all as shown.

Now as everything set, we can dive into the code for our robot.

Step 5: Software

Arduino is known for its friendly coding environment and large open source user libraries which makes the prototyping really easy.

I first started with separate Accelerometer and Gyroscope. But as things progressed, I find it difficult to balance with computational overhead of calculating pitch & roll (using kalman/Complementary filter).When I switched to MPU-6050, it drastically improved the balancing.

To use the DMP of MPU, I used Jeff Rowberg’sI2CDev libraries. To calibrate the mpu, I used Luis Rodenas’ssketch.

Now, we can determine the angle of bot with vertical. But how can we calculate the amount of force needed to push bot back to vertical when it is unbalanced by some uncertain degrees from vertical. This is where PID kicks in.PID will specify this force which will change according to time and angle by which bot is unbalanced.

I will not go into technical details. Brett Beauregard wrote a nice PID library with explanation if you want.

To further speed up the motor response, I used digitalIOPerformance library for fast writing of pins.

Finally for storing PID values in EEPROM of Arduino, EEPROMex library for efficiency and clean code.

Complete code can be found on the GITHUB.

Step 6: APP

Re-uploading code every time to change PID values is very tiresome. So I decided to attach Bluetooth and change values from my Android phone. I saw many people using MIT App Inventor for building apps for such purposes. I never used it before. So I thought, I must give it a shot. After watching some tutorials and some apps from other people regarding Bluetooth connection, I was all set. I must admit,App inventor revolutionize the app creation by letting you make apps in a way even non-programmers can make it without concerning what is happening behind the scenes.

MIT app inventor was fun except I have to deal large graphical blocks even for simpler tasks. Arrangements of these blocks was tiresome and reading it was pain.

You can find my app and Sample code from here.

Note-Although Multi-scene was introduced in App inventor 2, it treats every scene as separate app.For ex-you started some Bluetooth activity in scene1 and paired a device and then switched to scene2.scene2 will be completely unaware of the the pairing and tries to pair (& obviously fails) a device. A good way of handling it is build a single scene app and hide/show elements just like single page websites.

Step 7: PID Tuning

Frankly, this is the most difficult part of the process and also the most crucial. You will spend many days working on it and yelling “WTH am I supposed to do?” How much frustrating it may be but there is no escaping it? Control engineers set the PID parameters by experience. It’s an art learnt through experience.

There are various methods out there for PID tuning.

The best simple & easy method for PID tuning is-

  • · Set I and D term to 0, and adjust P so that the robot starts to oscillate (move back and forth) about the balance position. P should be large enough for the robot to move but not too large otherwise the movement would not be smooth.
  • · With P set, increase I so that the robot accelerates faster when off balance. With P and I properly tuned, the robot should be able to self-balance for at least a few seconds.
  • · Finally, increase D so that the robot would move about its balanced position more gentle, and there shouldn’t be any significant overshoots.
  • · If first attempt doesn’t give the satisfying results, reset PID values and start over again with different value of P.
  • · Repeat the steps until you find a certain PID value which gives the satisfactory results.
  • · A fine tuning can be done to further increase the performance of PID system.
  • · In fine tuning, PID values are restricted to neighboring values and effects are observed in practical situations.

Important Points

  • · There is no clear boundary for taking P, I or D values and is mostly taken based on experience.
  • · Theoretically, ID values depends on the state of the system.Ex. Mechanical Structure, Physical properties, Electrical properties (if any) etc.
  • · But practically, it also depends on the external conditions.Ex. Atmospheric conditions etc.
  • · PID values and method of choosing PID values depends largely on the characteristics of system. A method producing good result for a system may not work at all for another system with different characteristics.

What P, I & D values means practically?

In case of Self-Balancing Robot-

  • P-P determines the force with which the robot will correct itself. A lower P shows robot’s inability to balance itself and a higher P will shows the violent behavior.
  • I-I determines the response time of robot for correcting itself. Higher the P, Faster it will response.
  • D- D determines the sensitivity of robot to the error in its state. It is used to smoothen/depress the robot oscillations. A lower D is unable to remove oscillations and a higher D will cause violent vibrations.

Depending on your PID tuning, the bot will be able balance itself now.

Congratulation!!! On your first self-balancing robot. Maybe you can name it now, I called mine Chappie.

Step 8: What’s Next?

Well, now you have a self-balancing robot which you can control by Bluetooth. Maybe you can upgrade it to Wi-Fi or use it as replacement to your 4 wheel robot.

You can upgrade the Arduino to Raspberry pi to harness the power of mini-computer e.g. Image processing etc.

You can also use decoder motor or stepper motors.it will also significantly increases the performance. However, you will need to update the code accordingly.

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Audio Contest 2018

      Audio Contest 2018

    99 Discussions

    0
    None
    manpreetsingh80

    2 years ago

    UPDATE:

    digitalIOPerformance library doesn't work on Arduino 1.6. Use Arduino 1.5 for compiling the program.

    1 reply
    0
    None
    vivekdunejamanpreetsingh80

    Reply 1 year ago

    Sir plz guide regarding the version of Arduino 1.5 as there are various other alternatives. I downloaded 1.5.8. will it work if no then plz specify your version too

    0
    None
    h-craft

    1 year ago

    thanx a lot...manpreet bro....i ve made...bt i found that the motor connection to l293d is wrong in the ckt diagram....secondly your app is not working....so I did the PID tuning by connecting to pc....every time I give a command through the app it shows "broken pipe"....I hope you would reply soon...once again thanx a lot!

    P_20170114_113432.gif
    7 replies
    0
    None
    vivekdunejah-craft

    Reply 1 year ago

    Hello sir plz solve my problem give me the correct circuit diagram as well as plz send me your code too.

    My e-mail address is vivekduneja@gmail.com

    0
    None
    vivekdunejah-craft

    Reply 1 year ago

    Please tell the mistake in the circuit diagram as i am also facing the same problem and my dc motors are not moving at all.

    Thanks in advance

    0
    None
    dheerajprabhuswamyh-craft

    Reply 1 year ago

    Bro can u send me the correct circuit diagram. I have made the connections. In serial port I a getting Nan as output and the motor driver IC is not driving the motors. Plz help me out. I have a deadline to complete this. Thank u

    0
    None

    hii dheeraj prabhuswamy can you please send me the correct circuit diagram and code. its urgent, i hope you revert thanks in advance

    0
    None
    Ankit06h-craft

    Reply 1 year ago

    Can u please send me the correct circuit diagram to sharmabasket13@gmai.com. I am working on same project

    0
    None
    John PaulP5h-craft

    Reply 1 year ago

    Can you send mo the codes? Thanks dude. johnpaulpagobayan@yahoo.com

    0
    None
    FreddyH14

    Question 5 months ago

    i do this bot but it do not work correctly please help

    0
    None
    masoodr1

    11 months ago

    hello ...please send your code for me/??

    masood8809@gmail.com

    Thanks

    0
    None
    Kevalrsavla

    2 years ago

    Thanks bro your code work perfectly for me and i got a A+ in my project

    1 reply
    0
    None
    6000214082

    1 year ago

    i have a lipo battery of 11.1v, so instead of using 9v battery can i use that? will the voltage regulator be able to subtend the voltage and be able to produce the required output voltage? please let me know and thanks in advance...

    0
    None
    ounkazmi

    1 year ago

    bro can u send me full coding of your project ???????plz

    0
    None
    aporizga

    1 year ago

    nice project

    plese send me code

    aporizga1313@gmail.com

    0
    None
    John PaulP5

    1 year ago

    Nice project. Can you send me the codes for this? johnpaulpagobayan@yahoo.com thanks!

    0
    None
    souad95

    1 year ago

    can you please send me the code ...

    souad.saidi.95@gmail.com

    0
    None
    AbhishekB113

    1 year ago

    can you plz send me the code ...

    abhi.bassan@gmail.com