-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.py
69 lines (61 loc) · 2.3 KB
/
Robot.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
import numpy as np
class Robot:
def __init__(self, simulator, name: str, id: int, position: np.array) -> None:
"""The base Robot class
Parameters
----------
simulator: Simsim
Host simulator object
name : str
Robot's name
id : int
Robot's ID
position : np.array
Robot's starting position
"""
# host simulator
self.simulator = simulator
self.name = name
self.id = id
self.log_head = f"{self.name}_{self.id}"
self.position = position
# facing direction
self.ang = np.random.random()*360
self.desired_position = self.position
self.in_position = True
self.speed = 0.0
self.rate = self.simulator.rate
self.max_speed = 100.0 # m/s
def waypoint_ctrl(self, speed=None, desired_pos: np.array = None):
# if got new waypoint, update
if not np.any(desired_pos == None):
# logging.debug(f"{self.name}{self.id} set new desired position")
self.desired_position = desired_pos
if speed is None:
# logging.debug(f"{self.name}_{self.id} set to max speed")
self.speed = self.max_speed
else:
if speed > self.max_speed:
logging.warning("Given speed is over maximum")
self.speed = speed
if np.any(self.position != self.desired_position):
# logging.debug(
# f"{self.name}_{self.id} moving to {self.desired_position}")
self.in_position = False
one_time_step_length = self.speed/self.rate
difference = self.desired_position - self.position
distance = np.linalg.norm(difference)
# if it's close enough to the goal position, make it in position
if distance <= one_time_step_length:
self.position = self.desired_position
self.in_position = True
else:
# move to the goal point at the given speed
direction = difference/distance
self.position = self.position + direction*self.speed/self.rate
# print("MOVED")
return self.in_position
def job(self):
self.waypoint_ctrl()
pass