# Quick Matlab guide to the unicycle control

Let’s get going with a short series of tutorials dedicated to nonlinear control examples with Matlab/Octave implementation. First to go: unicycle.

In order to describe the exact position and orientation of a robot moving on a planar surface you need only:

The mathematical model for the unicycle is quite easy:

Ok, so first we will need a matlab function that returns this exact vector:

function dq = unicycle( t, q ) x = q(1); y = q(2); theta = q(3); v = 1; w = 1; dq = [ v*cos(theta) ; ... v*sin(theta) ; ... w ];

and the simulation script:

function unicycle_test [t,q] = ode45( @unicycle, 0:0.01:10, [5 4 pi/3] ) plot( q(:,1), q(:,2) )

Of course, you don’t need to create two separate files for those – you can place them together, just make sure that unicycle_test is on top and the name of the file is unicycle_test.m

What should be the result? Well, if the unicycle is travelling forward with velocity and steering left with velocity … a circle.

(placeholder for a plot)

Ok, let’s do the drunk driving now:

v=1 w= 0.1 * sin(t)

the steering wheel should now move left and right.

Ok, now is the time for the first feedback – let’s stabilise the angle to zero = let’s go straight to the right. First, set the speed to a constant value:

v=1

now for the angle… basically the last equation of motion reads:

so… you can substitute nearly anything for the control signal, including a function of ! So… what ODE stabilises itself and has a nice eigenvalue of -1? Hmm… ? Let’s enforce it with a control signal:

w = -theta

and if you by any chanve would like to go up:

w = pi/2 - theta

so generally it’s a task of:

w = r - theta

where r is a reference position or a signal.

(placeholder for a plot)

Ok, but that’s just a simple feedback, what about something more complex? Let’s create a control system that stabilises the robot to a specific point – (0,0) for start should be sufficient.

So how far are we? Distance in a straight line will be denoted as and is equal to . And if we are a bit misaligned – to get straight to (0,0) we would need to rotate by … and then by to be exactly horizontal at the end.

Ok, so the task at hand is to get those signals to zero:

using any type of controls .

One of the better ways to do that is just to assume that those three variables are already STABLE (well, it is possible after all). How do we prove stability? Lyapunov! Just propose some Lyapunov (energy-like) function for the system and try to make it meet all the stability criteria.

Ok, so the most simple sketch for the energy is to grab all three and make them square: (a good energy-like function should be and at the origin )

… why all the you ask? Well, in a moment we will be calculating derivatives and the best derivative of the type is of course – no numbers flying around anymore. In case we deal with function of time $x(t)$ we need to take into account the next level of derivatives .

So… the Lyapunov function should be nicely decreasing during the motion and when it hits the 0 value it should stop and stick to it (well, that’s what you should hope for…). So is it falling like the hopes of every nation ever one minute after exit polls results?

\dot{V} = e \dot{e} + \alpha \dot{\alpha} + \beta \dot{\beta}

Now we need to calculate and how they change… so:

From the triangle:

Remember that in Matlab there are two types of tangent function – tan(c) which works in 180 degrees radius and tan2(a,b) which works in the whole circle (where c=a/b).

(to be continued soon …)

where the next chapters?