-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodometry.py
More file actions
40 lines (29 loc) · 1.25 KB
/
odometry.py
File metadata and controls
40 lines (29 loc) · 1.25 KB
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
from globaldata import *
from osensor import *
from qencoder import *
class Odometry:
leftTicks = 0
rightTicks = 0
lastLeftEncoder = 0
lastRightEncoder = 0
globalX = 0
globalY = 0
def __init__(self, orientationSensor, leftEncoder, rightEncoder):
self.orientationSensor = orientationSensor
self.leftEncoder = leftEncoder
self.rightEncoder = rightEncoder
def getPosition(self):
self.lastLeftEncoder = self.leftTicks
self.lastRightEncoder = self.rightTicks
self.leftTicks = self.leftEncoder.ticks
self.rightTicks = self.rightEncoder.ticks
leftChange = self.leftTicks - self.lastLeftEncoder
rightChange = self.rightTicks - self.lastRightEncoder
leftChangeCm = leftChange * CM_PER_TICK
rightChangeCm = rightChange * CM_PER_TICK
distance = (leftChangeCm + rightChangeCm) / 2
self.yaw = self.orientationSensor.getYaw2()
yawRadians = math.radians(self.yaw)
self.globalX += distance * math.sin(yawRadians)
self.globalY += distance * math.cos(yawRadians)
return (self.globalX, self.globalY, self.yaw)