How do you calculate or update the position of a differential drive robot with incremental sensors?
There is one incremental sensor attatched to each of the two differential wheels. Both sensors determine the distance $\Delta left$ resp. $\Delta right$ their wheel has rolled during a known time $\Delta t$.
First, let's assume the center between both wheels marks the position of the robot. In this case, one could calculate the position as:
$$ x = \frac{x_{left}+x_{right}}{2} \\ y = \frac{y_{left}+y_{right}}{2} $$
"Deriving" those equations under the assumption that both wheels rolled in a straight line (which should be approximately correct for small distances) I get:
$$ \frac{\Delta x}{\Delta t} = \frac{1}{2}\left( \frac{\Delta left}{\Delta t} + \frac{\Delta right}{\Delta t}\right)cos(\theta) \\ \frac{\Delta y}{\Delta t} = \frac{1}{2}\left( \frac{\Delta left}{\Delta t} + \frac{\Delta right}{\Delta t}\right)sin(\theta) $$
Where $\theta$ is the angle of orientation of the robot. For the change of this angle I found the equation
$$ \frac{\Delta \theta}{\Delta t} = \frac{1}{w} \left( \frac{\Delta left}{\Delta t} - \frac{\Delta right}{\Delta t}\right) $$
Where $w$ is the distance between both wheels.
Because $\Delta x$ and $\Delta y$ depend on $\theta$, I wonder whether I should first calculate the new $\theta$ by adding $\Delta \theta$ or if I should rather use the "old" $\theta$ ? Is there any reason to use one over the other?
Then, let's now assume the center between both wheels does not mark the position of the robot. Instead I want to use a point which marks the geometric center of the robot's bounding box. Then $x$ and $y$ change to:
$$ x = \frac{x_{left}+x_{right}}{2} + l\, cos(\theta)\\ y = \frac{y_{left}+y_{right}}{2} + l\, sin(\theta) $$
"Deriving" the first gives:
$$ \frac{\Delta x}{\Delta t} = \frac{1}{2}\left( \frac{\Delta left}{\Delta t} + \frac{\Delta right}{\Delta t}\right)cos(\theta) - l\,sin(\theta)\,\frac{\Delta \theta}{\Delta t} $$
Now there is a dependance on $\Delta \theta$. Is this a reason to use the "new" $\theta$ ?
Is there any better method to do simulatenous update of position and orientation? May be using complex numbers (same approach as with quaternions in 3D?) or homogeneous coordinates?
Here's some sample code with the math simplified: