Consider the following linear discrete system
$$ \xi_{k+1} = A\xi_k + Bu_k$$
where $x_k \in \mathbb{R} $ and $y_K \in \mathbb{R}$ denote the $x$- and $y$-axis displacements at time $k$, $\xi_k \in \mathbb{R^n}$ denotes the system states and $u_k \in \mathbb{R^{n_u}}$ is the input. The objective is to steer along a continuously differentiable and bounded 2-D geometric path $(x^d(\theta),(y^d(\theta))$. It is assumed that the desired path $(x^d(\theta),(y^d(\theta))$ is parameterised by arc length, i.e. $ds/dθ = 1$, where $s$ denotes the distance travelled along the path.
The contouring error $\epsilon_k^c$ is defined as the normal deviation from the desired path and can be expressed as
$$ \epsilon_k^c = \sin\phi(\theta^r)(x_k - x^d(\theta^r)) - \cos\phi(\theta^r)(y_k - y^d(\theta^r)) $$ Model predictive contour controller is a type of MPD controller that follows a predfined path $X^{ref}$ and $Y^{ref}$.
$$\phi(\theta^r) = \arctan(\frac{\nabla y^d(\theta^r)}{\nabla x^d(\theta^r)}), \nabla f(\theta^r) = \frac{df(\theta)}{d\theta}\bigg|_{\theta=\theta^r}$$
where $\theta^r(x, y)$ is the value of the path parameter at which the distance between the point $(x^d(\theta),y^d(\theta))$ and $(x,y)$ is minimal as per figure.
How do I derive the equation of contouring error $\epsilon_k^c$?