<?xml version="1.0"?>

<?xml-stylesheet type="text/css" href="myMathml.css"?>

<!DOCTYPE html SYSTEM "mathml.dtd" [
]>

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:xlink="http://www.w3.org/1999/xlink">
<head>
<link rel="StyleSheet" type="text/css" href="/~njh/njhStyle.css"/>
<link rel="shortcut icon" href="/~njh/favicon.ico"/>
<title>Nathan Hurst: Generalising quadrature</title>
</head>
<body>
<h1>Generalising quadrature</h1>

<h2>Basic idea</h2>

<p>Suppose we have a sensor that gives us a measure of some quantity
such as distance.  Clearly distance is unbounded, but a device that
measures it might work by measuring the angle of a wheel moving along
the surface.  The interface of these devices are often a register
giving the angle of the wheel.</p>

<img src="gq-diagram.png" align="right"/>

<p>The problem is that we want to use the device to measure more than
the distance of one rotation (
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mn>2</mn>
  <mi>&pi;</mi>
  <mn>r</mn>
</math>).  To do this we want to keep a tally of how many times the wheel has turned.  This is easy if the wheel only turns in one direction:</p>

<pre>
// Here we have 'value', a byte from the sensor describing its current angle
static char oldvalue = 0;
char dx = value - oldvalue;
static int x = 0;
x += dx;
</pre>

<p>We obviously need to make sure that we read the value of the sensor
more often than it can turn one whole revolution, otherwise we might
see 10, when actually it was 1010. </p>

<p>How do we deal with wheels that
can go backwards as well as forwards?  Handily, the same code will
work if the wheel can go backwards, although lets be specific about
the type of the integers:</p>

<pre>
// Here we have 'value', a byte from the sensor describing its current angle
static signed char oldvalue = 0;
signed char dx = value - oldvalue;
static signed int x = 0;
x += dx;
</pre>

<p>Again we have to worry about the sampling rate - how do we know
that the wheel has turned forwards 3/4 rather than backwards 1/4?
Easy, never let it be a problem.  We have to make sure that the
maximum distance(|value - oldvalue|) between two samples is always
less than 1/2.  If this is true then we know that we will never get
3/4, it always means -1/4.</p>

<p>Why am I calling this generalised quadrature?  quadrature is the
method whereby a wheel rotating can be encoded using two out of phase
edge detectors.  They generate a signal consisting of squarewaves
which 'rotate':</p> 

<img src="quadrature.png"/>

<p>This idea can be extended to an arbitrary number of squarewaves, as
long as we can track the direction of rotation.  sampling at more than
twice maximum resolution gives us this.  Hardware quadrature decoders
use the edges to ensure they are always moving at or faster than 1/2 a
rotation, but with a microprocessor we are better off using a repeated
task.  Note that there is no requirement for the samples to be done at
a fixed rate, although that may be useful for the purpose the signal
was generated.</p>

</body>
</html>
