The inverted pendulum is a classic problem in dynamics and control theory that is generally elaborated in high-school and undergraduate physics or math courses. Being a math and science enthusiast myself, I decided to try and implement the concepts that I learned during my classes to build an inverted pendulum. Applying such concepts in real life not only helps strengthen your understanding of the concepts but also exposes you to a whole new dimension of problems and challenges that deal with practicality and real-life situations that one can never encounter in theory classes.
In this instruction, I will firstly introduce the inverted pendulum problem, then cover the theory aspect of the problem, and then discuss the hardware and software required to bring this concept to life.
I suggest you watch the video that is attached above while going through the instructable which will give you a better understanding.
Step 1: The Problem
The inverted pendulum problem is analogous to balancing a broom or a long pole on the palm of your hand, which is something most of us have tried as a kid. When our eyes see the pole falling to a certain side, they send this information over to the brain which performs certain computations and then instructs your arm to move to a certain position with a certain velocity to counter the pole's movement, which would hopefully bring the tipping pole back up to vertical. This process is repeated several hundred times a second which keeps the pole completely under your control. The inverted pendulum functions in a similar manner. The aim is to balance a pendulum upside down on a cart that is allowed to move about. Instead of eyes, a sensor is used to detect the position of the pendulum which sends the information over to a computer which performs certain computations and instructs actuators to move the cart in a way to make the pendulum vertical again.
Step 2: The Solution
This problem of balancing a pendulum upside down requires insight into the movements and forces that are at play in this system. Eventually, this insight will allow us to come up with "equations of motion" of the system which can be used to compute relations between the output that is going to the actuators and the inputs coming from the sensors.
The equations of motion can be derived in two ways depending on your level. They can either be derived using the basic laws of Newton and some high school level mathematics or using Lagrangian mechanics which is generally introduced in undergraduate physics courses. (Note: Deriving the equations of motion using Newton's laws is simple but tedious whereas using Lagrangian mechanics is much more elegant but requires the understanding of Lagrangian mechanics although both approaches eventually lead to the same solution).
Both approaches and their formal derivations are usually covered in high school or undergraduate classes on math or physics, although they can easily be found using a simple google search or by visiting this link. Observing the final equations of motion we notice a relation between four quantities:
The angle of the pendulum to the vertical
The angular velocity of the pendulum
The angular acceleration of the pendulum
The linear acceleration of the cart
Where the first three are quantities that are going to be measured by the sensor and the last quantity is going to be sent to the actuator to perform.
Step 3: Control Theory
Control theory is a subfield of mathematics that deals with controlling and operating dynamical systems in engineered processes and machines. The objective is to develop a control model or a control loop to generally achieve stability. In our case, balance the upside down pendulum.
There are two main types of control loops: open loop control and closed loop control. When implementing an open loop control, the control action or the command from the controller is independent of the system's output. A good example of this is a furnace, where the amount of time that the furnace remains on is purely dependant on the timer.
Whereas in a closed loop system, the controller's command is dependant on the feedback from the state of the system. In our case, the feedback is the angle of the pendulum with reference to the normal which determines the speed and position of the cart, therefore making this system a closed loop system. Attached above is a visual representation in the form of a block diagram of a closed loop system.
There are several feedback mechanism techniques but one of the most widely used is the proportional–integral–derivative controller (PID controller), which is what we are going to use.
Note: Understanding the workings of such controllers is very useful in developing a successful controller although explaining the operations of such a controller is beyond the scope of this instructable. In case you have not come across these types of controllers in your course there are bunches of material online and a simple google search or an online course will help.
Step 4: Implementing This Project in Your Classroom
Age Group: This project is primarily for high-school or undergraduate students, but could also be presented to younger children simply as a demonstration by giving an overview of the concepts.
Concepts Covered: The main concepts that are covered with this project is dynamics and control theory.
Time required: Once all the parts are gathered and fabricated, assembling takes 10 to 15 mins. Creating the control model requires some more time, for this, the students can be given 2 to 3 days. Once each individual student (or groups of students) have developed their respective control models, another day can be used for the individuals or the teams to demonstrate.
One way to implement this project into your classroom would be to build the system (described in following steps), while the batch is working on the subtopics of physics related to dynamics or while they are studying control systems in math classes. In this way, ideas and concepts that they come across during class can be directly implemented into a real-world application making their concepts far more clear because there is no better way to learn a new concept than by implementing it in real life.
A single system can be built, together as a class and then the class can be divided into teams, each building a control model from scratch. Each team can then demonstrate their work in a competition format, where the best control model is the one that can balance the longest and withstand nudges and pushes robustly.
Another way to implement this project in your classroom would be to make older kids (high school level or so), develop this project and demonstrate it to younger children while giving them an overview of dynamics and controls. This may not only spark interest for physics and math for the younger children but will also help the older students crystallize their concepts of the theory because one of the best ways to strengthen your concepts is by explaining it to others, especially younger children as it requires you to formulate your ideas in a very simple and clear manner.
Step 5: Parts and Supplies
The cart will be allowed to freely move on a set of rails giving it a single degree of freedom. Here are the parts and supplies required to make the pendulum and the cart and rails system:
Electronics:
One Arduino compatible board, any will work. I recommend an Uno in case you are not too experienced with electronics because it'll be simpler to follow along.
One Nema17 stepper motor, which will function as the actuator for the cart.
One stepper motor driver, once again anything will work, but I recommend A4988 stepper motor driver because it will just be simpler to follow along.
One MPU-6050 Six-Axis (Gyro + Accelerometer), which will detect the various parameters such as angle and angular velocity of the pendulum.
One 12v 10A power supply, 10A is actually a slight overkill for this specific project, anything above 3A will work, but having the possibility to draw extra current allows for future development where more power may be required.
Hardware:
16 x bearings, I used skateboard bearings and they worked great
2 x GT2 pulleys and belt
About 2.4 meters of 1.5-inch PVC pipe
Bunch of 4mm nuts and bolts
Some of the parts that were used in this project were also 3D printed, therefore having a 3D printer will be very useful, although local or online 3D printing facilities are commonly available.
The total cost of all parts is just a little less than 50$ (excluding the 3D printer)
Step 6: 3D Printed Parts
Some of the parts of the cart and rails system had to be custom made, so I used Autodesk's free to use Fusion360 to model the cad files and 3D print them on a 3D printer.
Some of the parts that were purely 2D shapes, such as the pendulum and the gantry bed, were laser-cut as it was much quicker. All the STL files are attached below in the zipped folder. Here is a complete list of all the parts:
2 x Gantry Roller
4 x End Caps
1 x Stepper Bracket
2 x Idle Pulley Bearing Holder
1 x Pendulum Holder
2 x Belt Attachment
1 x Pendulum Bearing Holder (a)
1 x Pendulum Bearing Holder (b)
1 x Pulley Hole Spacer
4 x Bearing Hole Spacer
1 x Gantry Plate
1 x Stepper Holder Plate
1 x Idle Pulley Holder Plate
1 x Pendulum(a)
1 x Pendulum(b)
In total there are 24 parts, which don't take too long to print as the parts are small and can be printed together. In the course of this instruction, I will be referring to the parts based on the names in this list.
Attachments all stls.rar Download
Step 7: Assembling the Gantry Rollers
The gantry rollers are like the wheels for the cart. These will roll along the PVC track which will allow the cart to move smoothly with minimal friction. For this step, grab the two 3D printed gantry rollers, 12 bearings and a bunch of nuts and bolts. You will require 6 bearings per roller. Attach the bearings to the roller using the nuts and bolts (Use the pictures as a reference). Once each roller is made, slide them onto the PVC pipe.
Step 8: Assembling the Drive System (Stepper Motor)
The cart is going to be driven by a standard Nema17 stepper motor. Clamp the motor into the stepper bracket using the screws that should have come as a set with the stepper. Then screw the bracket onto the stepper holder plate, align the 4 holes on the bracket with the 4 on the plate and use nuts and bolts to secure the two together. Next, mount the GT2 pulley onto the shaft of the motor and attach the 2 endcaps to the stepper holder plate from the bottom using more nuts and bolts. Once done, you can slide the endcaps onto the pipes. In case the fit is too right instead of forcing the endcaps onto the pipes, I recommend sanding the inner surface of the 3D printed endcap until the fit is snug.
Step 9: Assembling the Drive System (Idle Pulley)
The nuts and bolts that I was using were 4mm in diameter although the bores on the pulley and bearings were 6mm, which is why I had to 3D print adaptors and push them into the holes of the pulley and the bearings so that they didn't wobble on the bolt. If you have nuts and bolts of the right size, you will not require this step.
Fit the bearings into the idle pulley bearing holder. Once again if the fit is too tight, use sandpaper to lightly sand the inner wall of the idle pulley bearing holder. Pass a bolt through one of the bearings, then slip a pulley onto the bolt and close the other end of with the second bearing and idle pulley bearing holder set.
Once that's done attach the pair of idle pulley bearing holders onto the idle pulley holder plate and attach the endcaps to the bottom face of this plate, similar to the previous step. Finally, cap the opposite end of the two PVC pipes using these endcaps. With this the rails for your cart are complete.
Step 10: Assembling the Gantry
The next step is to build the cart. Attach the two rollers together using the gantry plate and 4 nuts and bolts. The gantry plates have slots so that you can adjust the position of the plate for slight adjustments.
Next, mount the two belt attachments on both sides of the gantry plate. Make sure to attach them from the bottom otherwise the belt will not be at the same level. Make sure to also pass the bolts in from the bottom, because otherwise, if the bolts are too long they may cause an obstruction for the belt.
Lastly, attach the pendulum holder to the front of the cart using nuts and bolts.
Step 11: Assembling the Pendulum
The pendulum was made in two pieces simply to save on material. You can stick the two pieces together by aligning the teeth and supergluing them. Again push the bearing hole spacers into the two bearings to compensate for the smaller bolt diameters and then push the bearings into the bearing holes of the two pendulum bearing holder pieces. Clamp the two 3D printed parts on each side of the bottom end of the pendulum and secure the 3 together using 3 nuts and bolts passing through the pendulum bearing holders. Pass a bolt through the two bearings and secure the other end with a corresponding nut.
Next, grab your MPU6050 and attach it on the opposite end of the pendulum using mounting screws.
Step 12: Mounting the Pendulum and Belts
The final step is to mount the pendulum onto the cart. Do this by passing the bolt that you had earlier passed through the two pendulum bearings, through the hole on the pendulum holder that is attached to the front of the cart and use a nut on the other end to secure the pendulum onto the cart.
Finally, grab your GT2 belt and firstly secure one end to one of the belt attachments that is clamped onto the cart. For this, I used a neat 3D printable belt clip which clips onto the end of the belt and prevents it from slipping through the narrow slot. The stls for this piece can be found on Thingiverse using this link. Wrap the belt all the way around the stepper pulley and the idle pulley and secure the other end of the belt to the belt attachment piece on the opposite end of the cart. Tension the belt while making sure to not tighten too much or leave it too lose and with this your pendulum and cart is complete!
Step 13: Wiring and Electronics
The wiring consists of connecting the MPU6050 to the Arduino and the wiring of the drive system. Follow the wiring diagram attached above to connect each component.
MPU6050 to Arduino:
GND to GND
+5v to +5v
SDA to A4
SCL to A5
Int to D2
Stepper motor to stepper driver:
Coil 1(a) to 1A
Coil 1(b) to 1B
Coil 2(a) to 2A
Coil 2(b) to 2B
Stepper Driver to Arduino:
GND to GND
VDD to +5v
STEP to D3
DIR to D2
VMOT to power supply's positive terminal
GND to power supply's ground terminal
The Sleep and Reset pins on the stepper driver need to be connected with a jumper. And finally, it is a good idea to connect an electrolytic capacitor of about 100 uF in parallel with the positive and ground terminals of the power supply.
Step 14: Controlling the System (Proportional Control)
Initially, I decided to try out a basic proportional control system, that is, the velocity of the cart is simply proportional by a certain factor to the angle that the pendulum makes with the vertical. This was meant to be simply a test to make sure all the parts functioned correctly. Although, this basic proportional system was robust enough to make the pendulum already balance. The pendulum could even counter gentle pushes and nudges quite robustly. While this control system worked remarkably well, it still had a few problems. If one takes a look at the graph of the IMU readings over a certain time, we can clearly notice oscillations in the sensor readings. This implies that whenever the controller tries to make a correction, it is always overshooting by a certain amount, which is, in fact, the very nature of a proportional control system. This slight error can be corrected by implementing a different type of controller which takes into account all these factors.
The code for the proportional control system is attached below. The code requires the support of a few additional libraries which are the MPU6050 library, the PID library, and the AccelStepper library. These can be downloaded using the Arduino IDE's integrated library manager. Simply go to the Sketch >> Include Library >> Manage Libraries, and then just search PID, MPU6050 and AccelStepper in the search bar and install them by simply clicking on the Install button.
Although, my advise for all those of you that are science and math enthusiasts, would be to try and build a controller of this sort from scratch. This will not only strengthen your concepts about the dynamics and controls theories but will also give you an opportunity to implement your knowledge in real life applications.
Attachments proportional_control_final.ino Download
Step 15: Controlling the System (PID Control)
Generally, in real life, once a control system proves to be robust enough for its application, the engineers usually just complete the project rather than overcomplicating the situations by using more complex control systems. But in our case, we are building this inverted pendulum purely for educational purpose. Therefore we can try to progress to more complex control systems such as PID control, which may prove to be far more robust than a basic proportional control system.
Although PID control was far more complex to implement, once implemented correctly and finding the perfect tuning parameters, the pendulum balanced significantly better. At this point, it could also counter light jolts. The readings from the IMU over a given time (attached above) also prove that the readings never go too far away for the desired setpoint, that is, the vertical, demonstrating that this control system is far more effective and robust than the basic proportional control.
Once again, my advise for all those of you that are science and math enthusiasts, would be to try and build a PID controller from scratch before using the code that is attached below. This can be taken as a challenge, and one never knows, someone could come up with a control system that is far more robust than anything that has been attempted till now. Although a robust PID library is already available for Arduino which was developed by Brett Beauregard that can be installed from the library manager on the Arduino IDE.
Note: Each control system and its outcome is demonstrated in the video that is attached in the very first step.
Attachments PID_control_final.ino Download
Step 16: Further Improvements
One of the things that I wanted to attempt was a "swing-up" function, where the pendulum is initially hanging below the cart and the cart does a few quick up and down movements along the track to swing-up the pendulum from a hanging position to an upside down inverted position. But this was not doable with the current configuration because a long cable had to connect the inertial measurement unit to the Arduino, therefore a full circle done by the pendulum may have caused the cable to twist and snag. This issue can be dealt with by using a rotary encoder attached to the pivot of the pendulum instead of an inertial measurement unit at the very tip of it. With an encoder, its shaft is the only thing that's spinning with the pendulum, while the body stays stationary which means that the cables won't twist.
A second feature I wanted to attempt, was to balance a double pendulum on the cart. This system consists of two pendulums connected one after the other. Although the dynamics of such systems are far more complex and require much more research.
Step 17: Final Results
An experiment like this can transform the mood of a class in a positive manner. Generally, most people prefer being able to apply concepts and ideas to crystallize them, otherwise, the ideas remain "in the air" which makes people tend to forget them more quickly. This was just one example of applying certain concepts learned during class into a real-world application, although this will certainly spark enthusiasm in students to eventually try and come up with their own experiments to test the theories, which will make their future classes far more lively, which will make them want to learn more, which will make them come up with newer experiments and this positive cycle will continue until future classrooms are full of such fun and enjoyable experiments and projects.
0 Comments: