# Motion Control of Segway Robot. Simulation in ImGui C++

In this article, I will present a comprehensive approach to further expanding upon my previous work by incorporating simulation capabilities into the development of a dynamic model for a self-balance robot, specifically a Segway. The model will be described using second-order motion equations based on Lagrangian mechanics.

While our focus will be on providing a step-by-step guide for achieving a solution, we will not delve into the details of motion physical phenomena or mathematical relationships, as they primarily serve the purpose of the control system. The motion equations will be solved using the numerical method known as the Runge Kuta method, which we discussed in detail in one of our earlier articles. The implementation of the solution involved the utilization of the C++ programming language for the simulation, while the symbolic equations were computed using the SymPy library in Python.

For your convenience, the project files can be found on my **GitHub** repository.

Expected robot motion,

Furthermore, in addition to comprehending the construction of a physical model for a robot or any other dynamic system, it is crucial to possess the skills required to solve differential equations. Within this project, we provide an accessible approach to solving such equations. I encourage you to study this material diligently, as it will equip you to tackle similar or even more complex challenges that may arise in the future.

The definition of the control signal for the robot's motor torque can be found in my article.

In order for the robot to maintain balance and move in a straight line, the robot controller (specifically, the PD controller) needs to minimize the difference between the robot's actual position and its desired position. This requires adjusting the control signal and incorporating the PD controller to influence the total motor torque.

where Kp and Kd are the coefficients of the PD controller.

I would like to draw your attention to the way the PD controller has been integrated into the code. It follows a simple principle -** when solving differential equations, the torque for the current state of the robot is computed.**

The expected motion for the initial position of the robot at 50 degrees is as follows.

Thank you for reading.