-
Notifications
You must be signed in to change notification settings - Fork 0
/
bicycle.py
48 lines (40 loc) · 1.46 KB
/
bicycle.py
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
"""Kinematic model for the robot that deals with the motors and servos."""
import numpy as np
from src.config import TIMESTEP, V_CALIB, G_CALIB, SIMULATION
from src import picar
from src import utils as u
class Bicycle(object):
def __init__(self):
self.pose = None
self.v = 0
self.gamma = 0
self.trajectory_x = []
self.trajectory_y = []
self.turnangles = []
def input(self, pose, v, gamma):
self.pose = pose
self.v = v
self.gamma = gamma
def update_step(self):
self.trajectory_x.append(self.pose[0])
self.trajectory_y.append(self.pose[1])
if not SIMULATION:
if self.v >= 0:
picar.run_action('forward')
else:
picar.run_action('backward')
picar_v = u.real_to_picar_v(np.fabs(self.v))
picar.run_speed(picar_v)
w = self.gamma * G_CALIB
if self.v >= 0:
turnangle = 90 - int(np.round(np.rad2deg(w)))
else:
turnangle = 90 + int(np.round(np.rad2deg(w)))
picar.run_action(f'fwturn:{turnangle}')
self.turnangles.append(turnangle)
self.pose[2] = self.pose[2] + TIMESTEP * self.gamma
self.pose[0] += TIMESTEP * V_CALIB * self.v * np.cos(self.pose[2])
self.pose[1] += TIMESTEP * V_CALIB * self.v * np.sin(self.pose[2])
def output(self):
self.update_step()
return self.pose