69 lines
No EOL
2 KiB
Python
69 lines
No EOL
2 KiB
Python
import time
|
|
import math
|
|
from compLib.Encoder import Encoder
|
|
from compLib.Robot import Robot
|
|
|
|
last_run = time.time()
|
|
last_enc_left = 0
|
|
last_enc_right = 0
|
|
|
|
pos_x = 0
|
|
pos_y = 0
|
|
orientation = 0
|
|
|
|
|
|
class Odometry():
|
|
def __init__(self, x, y, orientation):
|
|
self.x = x
|
|
self.y = y
|
|
self.orientation = orientation
|
|
|
|
def __str__(self):
|
|
return f"X: {self.x} Y: {self.y} O: {self.orientation}"
|
|
|
|
class Odom(object):
|
|
|
|
@staticmethod
|
|
def get_odom():
|
|
return Odometry(pos_x, pos_y, orientation)
|
|
|
|
@staticmethod
|
|
def clear():
|
|
global last_run, last_enc_left, last_enc_right, pos_x, pos_y, orientation
|
|
|
|
last_run = 0
|
|
last_enc_left = 0
|
|
last_enc_right = 0
|
|
pos_x = 0
|
|
pos_y = 0
|
|
orientation = 0
|
|
|
|
@staticmethod
|
|
def update():
|
|
global last_run, last_enc_left, last_enc_right, pos_x, pos_y, orientation
|
|
|
|
now = time.time()
|
|
if last_run + 0.01 < now:
|
|
last_run = now
|
|
|
|
encoder_positions = Encoder.read_all()
|
|
current_left = encoder_positions[Robot.LEFT_PORT - 1]
|
|
current_right = -encoder_positions[Robot.RIGHT_PORT - 1]
|
|
distance_left = (current_left - last_enc_left) / Robot.TICKS_PER_METER
|
|
distance_right = (current_right - last_enc_right) / Robot.TICKS_PER_METER
|
|
last_enc_left = current_left
|
|
last_enc_right = current_right
|
|
|
|
distance = (distance_left + distance_right) / 2.0
|
|
theta = (distance_left - distance_right) / Robot.ARBOR_LENGTH_M
|
|
|
|
if distance != 0:
|
|
distance_x = math.cos(theta) * distance
|
|
distance_y = -math.sin(theta) * distance
|
|
|
|
pos_x = pos_x + (math.cos(orientation) * distance_x -
|
|
math.sin(orientation) * distance_y)
|
|
pos_y = pos_y + (math.sin(orientation) * distance_x +
|
|
math.cos(orientation) * distance_y)
|
|
if theta != 0:
|
|
orientation += theta |