## 3 Controller Design  
Switching between PID controllers has been designed for the hybrid model, where the dynamics of the system change whenever the contact between gears changes. Three separate PID controllers are designed here. Whenever the drive undergoes changes in its direction, the backlash mode is activated. One PID controller is tuned for positive contact and the second is tuned for negative contact. Switching controllers allows the system to become operational under the desired operating conditions. The method described in this section for the compensation of backlash operates the controller in these modes. However, switching modes are dependent on the monitoring of the angular position of pendulum, as explained in the algorithm defined in Algorithm 1. Depending on the evaluation of this condition, it assigns a value to the output variable \(p\). The explanation of the logical conditions is given below. The controller has been assigned a reference trajectory against which it compares the angular position of the inverted pendulum. This comparison generates an error signal. The error signal is then utilized to adjust the parameter values of \(K^{p}_{p}\), \(K^{p}_{i}\), and \(K^{p}_{d}\), where \(p\in[0,1,2]\). This compensation aims to effectively eliminate backlash within the vibratory system. \[e(t)=y_{ref}-y\]  
Here, \(y\) is the output of the controller and \(y_{ref}\) is the reference trajectory. The generated error signal is subsequently fed into the controller to prompt the desired response. Three distinct controllers are employed to mitigate the backlash. The selection of the appropriate controller relies on the value of parameter \(p\). A switched-mode PID controller is used to regulate the dynamics of the inverted pendulum. This controller effectively adjusts the voltages for both drive and anti-drive mechanisms, which are subsequently applied to the inverted pendulum system. The activation of the switched-mode PID controller in Algorithm 2, is dependent on the computed value of parameter '\(p\)', derived through a dedicated Algorithm 1. In the context of designing control systems, step responses for three separate controllers have been defined and developed. Each controller utilizes a PID (Proportional-Integral-Derivative) control scheme, tailored with a specific design focus. The design focus, in this case, emphasizes the use of reference trajectory tracking as a primary feature. The drive and anti-drive controllers are defined as \(ctr\_a\) and \(ctr\_a\). Three separate PID controllers and PID selections are defined as \(ctr1,ctr2,ctr3,p\). For \(p=0\), the drive will take the values from controller 0 and the anti-drive will stay off. When the system enters backlash mode, controller 0 will give the input to the drive and controller 1 will give its input to the anti-drive. For \(p=2\), the drive is driven by controller 2. To achieve this, the controllers were tuned with a phase margin set to 80 degrees. This choice of phase margin is indicative of a robust control system design, ensuring stability and performance margins within the control loop. ```
1:Input:\(ctr1,ctr2,ctr3,p\)
2:Output:\(ctr\_d\_ctr\_a\)
3:if\(p=0\)then
4:\(ctr\_d\_ctr0\)
5:\(ctr\_a\gets 0\)
6:elseif\(p=1\)then
7:\(ctr\_d\gets ctr0\)
8:\(ctr\_a\gets ctr1\)
9:elseif\(p=2\)then
10:\(ctr\_d\gets ctr2\)
11:\(ctr\_a\gets 0\)
12:else
13:\(ctr\_d\gets 0\)
14:\(ctr\_a\gets 0\)
15:endif
16:return\(ctr\_d\_ctr\_a\)
```  
**Algorithm 2**\(p\) Mode selection for drive and anti-drive  
The PID controllers were developed using MATLAB R2022's pidtune function, which automates the tuning process based on specified design criteria. In this scenario, the design focus parameter was crucial in guiding the tuning process towards optimizing the controllers for the accurate tracking of reference trajectories.