This is the code repository for MATLAB Drone Coding for Beginners Book 2, First Edition, published by Packt.
Dr Ma Mohin
A controller that holds a fixed altitude is only a starting point. Tracking changing references under disturbances and thrust limits requires deeper insight. MATLAB Drone Coding for Beginners Book 2 helps you build that understanding through progressively developed altitude-control simulations. Building on the first book in the series, MATLAB Drone Coding for Beginners: From Hover Thrust to PID Altitude Control, this book advances to trajectory tracking and realistic PID control. You'll refactor PID code into reusable functions, track time-varying altitude references, and add feedforward control. Position and velocity tracking logic adds realism. You'll connect PD gains to natural frequency and damping ratio, use second-order response concepts to guide tuning, and introduce integral action to reduce steady-state error. With actuator saturation in the one-dimensional altitude model, you will examine integral windup and compare conditional integration and clamped-integrator anti-windup. Plots and metrics like overshoot, rise time, and mean absolute error help you assess results. Guided by Dr Ma Mohin, a Chartered Engineer and Lecturer at the University of Greater Manchester, UK, you'll learn to explain why responses change and how to assess MATLAB PID altitude-tracking simulations.
- Refactor MATLAB controller code into reusable functions
- Track time-varying altitude references with PID control
- Apply feedforward control to support trajectory tracking
- Implement position and velocity tracking logic
- Model disturbances and thrust limits in altitude simulations
- Derive PD gains from natural frequency and damping ratio
- Add integral action to reduce steady-state error
- Compare conditional integration and clamped-integrator anti-windup
- Refactoring Drone PID Code into Reusable MATLAB Functions
- PID Altitude Trajectory Tracking with a Time-Varying Reference
- Feedforward PID Control for Better Altitude Trajectory Tracking
- Position and Velocity Tracking for More Realistic UAV Control Logic
- Disturbance, Actuator Saturation, and Anti-Windup in UAV Altitude Control
- Controller Tuning Using Natural Frequency and Damping Ratio
- From PD to Second-Order System Thinking for Drone Control
- From PD Tuning to Control Design Using Natural Frequency and Damping Ratio
- Adding Integral Action for Full PID Control and Steady-State Error Reduction
- Saturation and Anti-Windup: Making PID Control More Realistic
No advanced control theory knowledge is required to get started. However, a basic understanding of altitude error, thrust, velocity, acceleration, and PID control will be helpful. The chapters follow a step-by-step approach, with each concept building on the previous one.
- MATLAB is required to run the examples.
- Chapter 7 requires the Control System Toolbox for the
tfandstepcommands. - All other chapters use core MATLAB functionality.
To get the most out of this book, follow these recommendations:
- Read in sequence: The chapters are interconnected, so reading them in order will provide the best learning experience.
- Run the MATLAB examples: Execute the code, examine Command Window outputs, and carefully inspect the generated figures.
- Practice actively: Attempt the common mistakes and practice tasks provided in each chapter.
- Experiment with parameters: Change one parameter at a time and observe how the system response changes.
- Understand the variables: Focus on what each variable represents rather than simply copying the code. For example:
z– Altitudevz– Vertical velocityaz– Vertical accelerationT– Thruste– Altitude error
- Compare control strategies: Pay attention to the differences between:
- Feedback-only and feedforward tracking.
- PD and PID control under disturbance.
- PID control with and without anti-windup.
- Conditional integration and clamped integrator approaches.
Do not expect every result to be perfect initially. Overshoot, steady-state error, oscillations, actuator saturation, and integral windup are important learning opportunities. Understanding why these behaviors occur is essential to developing engineering intuition and designing effective real-world control systems.
Dr Ma Mohin, PhD, CEng, MIMechE, FHEA, is a Lecturer in Mechanical Engineering and Programme Leader for MSc Mechanical Engineering at the University of Greater Manchester, UK. He also serves as a Director of Studies, supervising doctoral research in engineering. He is a Chartered Engineer, a Member of the Institution of Mechanical Engineers, and a Fellow of Advance HE. His teaching and research span mathematical modeling, simulation, control systems, additive manufacturing, and drone technology. His books help students and engineers connect engineering concepts with MATLAB code and simulations to develop their understanding of modeling and drone control.