# A Wheeled Inverted Pendulum Learning Stable and Accurate Control from Demonstrations
## 4 Learning Path Following and Balance Controlling Simultaneously
### Teaching the Control Model with the Nonholonomic and Underactuated Constraints  
Instead of directly learning the controller of the wheeled inverted pendulum in the form of Equation (5), we chose to first teach the dynamic system and to subsequently transfer the learned dynamic system into a controller. In order to let the dynamic system sufficiently capture the control features implicated in the demonstrated trajectories (i.e., the sequences of \(x\) and \(y\)), we added an extra-dimensional component \(z\) for the demonstrated trajectories based on our previous work [33], and therefore, acquired transformed trajectories denoted as \(\left\{x^{\mathbf{t},\mathbf{n}},y^{\mathbf{t},\mathbf{n}},z^{\mathbf{t}, \mathbf{n}},\theta^{\mathbf{t},\mathbf{n}},\varphi^{\mathbf{t},\mathbf{n}} \right\}_{t=0,n=1}^{T^{\mathbf{t}},N}\). Therefore, we taught a dynamic system denoted as  
Figure 3: The control scheme of learning from demonstrations for a wheeled inverted pendulum. Figure 2: The control constraints of a wheeled inverted pendulum. \[\left\{ \begin{array}{l}\hat{x}=\hat{f}_{1}\left(x,y,z,\theta,\varphi\left|\beta \right.\right)\\ \hat{y}=\hat{f}_{2}\left(x,y,z,\theta,\varphi\left|\beta\right.\right)\\ \hat{z}=\hat{f}_{3}\left(x,y,z,\theta,\varphi\left|\beta\right.\right)\\ \hat{\theta}=\hat{f}_{4}\left(x,y,z,\theta,\varphi\left|\beta\right.\right)\\ \hat{\phi}=\hat{f}_{5}\left(x,y,z,\theta,\varphi\left|\beta\right.\right)\\ \end{array} \right., \tag{6}\]  
where \(\beta\) are the parameters to be learned during the learning process. The first loss term for learning is given as Equation (7) to fit the dynamic system that generate the transformed demonstration examples \(\left\{x^{\mathbf{t},\mathbf{n}},y^{\mathbf{t},\mathbf{n}},z^{\mathbf{t}, \mathbf{n}},\theta^{\mathbf{t},\mathbf{n}},\varphi^{\mathbf{t},\mathbf{n}} \right\}_{t=0,n=1}^{T^{n},N}\). \[ \begin{array}{l}J_{1}=\sum\limits_{n=1}^{N}\sum\limits_{t=0}^{T^{n}}\left( \left\|\dot{x}^{t,n}-\hat{f}_{1}\left(x^{t,n},y^{t,n},z^{t,n},\theta^{t,n}, \varphi^{t,n}\left|\beta\right.\right)\right\|^{2}+\left\|\dot{y}^{t,n}-\hat{f }_{2}\left(x^{t,n},y^{t,n},z^{t,n},\theta^{t,n},\varphi^{t,n}\left|\beta \right.\right)\right\|^{2}\\ +\left\|\dot{z}^{t,n}-\hat{f}_{3}\left(x^{t,n},y^{t,n},z^{t,n},\theta^{t,n}, \varphi^{t,n}\left|\beta\right.\right)\right\|^{2}+\left\|\dot{\theta}^{t,n}- \hat{f}_{4}\left(x^{t,n},y^{t,n},\theta^{t,n},\varphi^{t,n}\left|\beta\right. \right)\right\|^{2}\\ +\left\|\dot{\varphi}^{t,n}-\hat{f}_{5}\left(x^{t,n},y^{t,n},z^{t,n},\theta^{ t,n},\varphi^{t,n}\left|\beta\right.\right)\right\|^{2}\right).\end{array}  \tag{7}\]  
Due to the existence of the nonholonomic constraints, we add a loss term  
\[J_{2}=\sum\limits_{n=1}^{N}\sum\limits_{t=0}^{T^{n}}\left\|\tan\left(\theta^{ \mathbf{t}\mathbf{n}}\right)\cdot\hat{f}_{1}\left(x^{\mathbf{t},\mathbf{n}},y ^{\mathbf{t},\mathbf{n}},z^{\mathbf{t},\mathbf{n}},\theta^{\mathbf{t}, \mathbf{n}},\varphi^{\mathbf{t},\mathbf{n}}\left|\beta\right.\right)-\hat{f}_{2 }\left(x^{\mathbf{t},\mathbf{n}},y^{\mathbf{t},\mathbf{n}},z^{\mathbf{t}, \mathbf{n}},\theta^{\mathbf{t},\mathbf{n}},\varphi^{\mathbf{t},\mathbf{n}} \left|\beta\right.\right)\right\|^{2}, \tag{8}\]  
which is equivalent to adding mobile robot motion constraints to the learned model.