Source code for learning DC motor position control and rotary inverted pendulum control using MATLAB/Simulink.
Motor-Pendulum-Control/
├── docs/ # Figures and reference materials
│ ├── DCmotor_pid_simulink.png
│ ├── DCmotor_PID_withoutFunction.png
│ ├── RotaryPendulum_simulink.png
│ └── RotaryPendulum_StateSpaceController.png
├── matlab/
│ ├── models/ # Simulink models
│ │ ├── DCmotor_PID.slx
│ │ ├── InvertedPendulum_simonly.slx
│ │ └── MotorPD_simonly.slx
│ ├── scripts/ # MATLAB parameter / setup scripts
│ │ ├── init_dc_motor.m
│ │ └── init_rotary_pendulum.m
│ └── results/ # Simulation result plots
│ ├── DC_PID_Final.png
│ ├── DC_P_Control/
│ │ ├── Kp_0point1.png
│ │ ├── Kp_1point0.png
│ │ └── Kp_negative.png
│ ├── DC_PI_Control/
│ │ ├── BigKi.png
│ │ ├── SmallKi.png
│ │ └── unstableKi.png
│ ├── DC_PD_Control/
│ │ ├── case1.png
│ │ ├── case2.png
│ │ ├── case3.png
│ │ └── case4.png
│ └── RotaryPendulum/
│ ├── alpha_result.png
│ └── theta_result.png
├── .gitignore
└── README.md
- Model files:
matlab/models/DCmotor_PID.slx,matlab/models/MotorPD_simonly.slx - Parameter script:
matlab/scripts/init_dc_motor.m
Using the parameters init_dc_motor.m,
the first-order system from input voltage
To obtain motor position
The controller and plant are implemented in a single Simulink model using standard blocks.
P/PI/PD/PID control results are stored in the matlab/results folder.
The figure below shows an example of the final response with PID control:
Main cases:
-
matlab/results/DC_P_Control: responses for different proportional gains$K_p$ -
matlab/results/DC_PI_Control: stable/unstable cases for different integral gains$K_i$ -
matlab/results/DC_PD_Control: several PD parameter cases
- Model file:
matlab/models/InvertedPendulum_simonly.slx - Parameter script:
matlab/scripts/init_rotary_pendulum.m
This project uses a linearized state-space model defined in init_rotary_pendulum.m.
Let the state be
the input
where
The state-space matrices are
The folder matlab/results/RotaryPendulum contains the response plots for the angles.
- Start MATLAB and Simulink.
- Set this repository as the current MATLAB working folder.
- Initialize parameters:
- DC motor : run
init_dc_motor.m - Rotary pendulum : run
init_rotary_pendulum.m
- DC motor : run
- Open the desired
.slxmodel and run the simulation. - Check the simulation results under the corresponding subfolders in
matlab/results.





