-
Notifications
You must be signed in to change notification settings - Fork 0
/
odometry.cpp
72 lines (65 loc) · 1.74 KB
/
odometry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <math.h>
#include <stdio.h>
#include "includes/odometry.h"
#include "includes/log.h"
#include <stdio.h>
#include <math.h>
/*
* Returns a truncated delta between -2^16 and 2^16
*/
static wheels<int> preventOverflow(wheels<int> delta)
{
if (delta.left > 0x8000)
{
delta.left -= 0x10000;
}
else if (delta.left < -0x8000)
{
delta.left += 0x10000;
}
if (delta.right > 0x8000)
{
delta.right -= 0x10000;
}
else if (delta.right < -0x8000)
{
delta.right += 0x10000;
}
return delta;
}
/*
* Converts motor ticks to meters
*/
inline wheels<double> getDistanceFromTicks(odotype* const p, wheels<int> ticks)
{
return ticks * p->metersPerEncoderTick;
}
/*
* Updates the total distance the wheels have traveled and returns
* the distance the wheels traveled in this tick in meters
*/
static wheels<double> updateEncodersPositions(odotype* const p)
{
const wheels<int> delta = preventOverflow(p->wheelsEncoderTicks - p->oldWheelsEncoderTicks);
p->oldWheelsEncoderTicks = p->wheelsEncoderTicks;
const wheels<double> traveledDistance = getDistanceFromTicks(p, delta);
p->wheelsDrivenDistance += traveledDistance;
return traveledDistance;
}
/*
* Updates odomety with new data
*/
void updateOdo(odotype* const p)
{
const wheels<double> movedDist = updateEncodersPositions(p);
//add distance traveled to total distance
p->totalDistance += fabs(movedDist.left + movedDist.right) / 2;
//add changed angle to angle
p->angle += (movedDist.left - movedDist.right) / p->wheelSeparation; // deltaTheta
//update robot position
const double deltaU = (movedDist.left + movedDist.right) / 2;
p->robotPosition.x += deltaU * cos(p->angle);
p->robotPosition.y += deltaU * sin(p->angle);
//printf("%f %f %f\n", p->xpos, p->ypos, p->angle);
logOdo(p);
}