Ubicoders - Robotics and AI, Coding, and Software Development!
Helicopter: A 2D Height Control Simulation
No Human Can Play This Game Maintain the Height of a 2D Helicopter by PID Controller in Python.
Elliot Lee - Nov.24, 2023
Simplifying PID Controller Concepts for Engineers and Students
The PID (Proportional-Integral-Derivative) controller is a staple in control engineering, known for its simplicity and effectiveness. However, its technical jargon can be daunting for beginners. This guide is crafted to demystify PID controllers, making them accessible for engineering students and early-career engineers.
python -c "import ubicoders_vrobots.vrobots_bridge.vrobots_bridge_gui as gui; gui.gui_app_run()"
1.d Run the Demo Script
Execute the demo script:
bash
Line #:
python my_helil.py
The script structure:
bash
Line #:
from ubicoders_vrobots import System, Helicopter
heli = Helicopter()
class UbicodersMain:
def __init__(self) -> None:
self.error_sum = 0
def setup(self):
pass
def loop(self):
if heli.states == None:
return
states = heli.states
print(states)
error = 5 - heli.states.pos.z
self.error_sum += error
heli.set_force(error*5 - heli.states.vel.z*5 + self.error_sum*0.08) # Newton
if __name__ == "__main__":
sys = System(heli, UbicodersMain())
sys.start()
Code Insights
The UbicodersMain class, with its functions __init__, setup, and loop, orchestrates the simulation. The loop() function, in particular, is pivotal in controlling the helicopter's thrust by manipulating the states variable.
2. Observing Virtual Helicopter Information
Virtual helicopter's position and velocity data.
The heli.states output provides crucial information such as the helicopter's height and vertical velocity.
Target height for the helicopter in the simulation.
The objective is to align the helicopter with the green bar, which is at a height of 5 meters.
3. Implementing a PID Controller
The aim is to maintain the helicopter at a 5-meter altitude.
Force Calculation
The force exerted by the helicopter is based on the "error" - the difference between the target and current heights:
$$
E = target - h_{current}
$$
In Python:
bash
Line #:
error = 5 - heli.states.pos.z
The updated force equation:
$$
force = error \times 5 - vel \times 5
$$
Python implementation:
bash
Line #:
heli.set_force(error*5 - heli.states.vel.z*5)
Experimenting with different scale factors can lead to varied system responses.
Addressing Steady State Error
To correct persistent errors, the error over time is integrated:
$$
force = error \times 5 - vel \times 5 + \int{error}
$$
Run the script to observe the helicopter maintaining its height at the target.
Helicopter simulation maintaining height at the target level.
Entire Code
bash
Line #:
from ubicoders_vrobots import System, Helicopter
# Create a helicopter object that contains states (position and velocity)
heli = Helicopter()
class UbicodersMain:
def __init__(self) -> None:
# Define the variables here!
self.error_sum = 0
def setup(self):
pass
def loop(self):
if heli.states == None:
return
# Print the helicopter states from the simulator!
states = heli.states
print(states)
error = 5 - heli.states.pos.z
self.error_sum += error
# Set the thrust force to the helicopter!
heli.set_force(error*5 - heli.states.vel.z*5 + self.error_sum*0.08) # Newton
if __name__ == "__main__":
sys = System(heli, UbicodersMain())
sys.start()
4. Conclusion
PID controllers, while initially appearing complex, can be mastered with a basic grasp of their underlying principles. This guide, through a practical virtual helicopter simulation, demonstrates how PID controllers can be implemented effectively. For engineering students and budding engineers, understanding and applying these concepts in simulations paves the way for more advanced applications in control engineering.
Your feedback and experiences with the simulation are invaluable. Share your thoughts and findings in the comments below, and let's foster a collaborative learning environment!