-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simulator.py
103 lines (80 loc) · 3.66 KB
/
Simulator.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
from Robot import Robot
from Plotter import Plotter
class Simulator():
'''Simulate the movements of robots in arena'''
def __init__(self, time_step = 0.01):
self.sim_log = []
self.robots = {}
self.sim_plot = Plotter()
self.time_step = time_step
def add_robot(
self, name, color, wheel_seperation,
wheel_diameter, sample_time = 0.01, start_pose = [0, 0, 0]):
'''Add a robot to be simulated'''
self.robots[name] = (Robot(
name, color, wheel_seperation, wheel_diameter,
sample_time, start_pose))
def live_sim(self, sim_time):
'''Carry out a simulation for a set amount of time and plot'''
# add the robots in the sim to the plot
for robot in self.robots.values():
self.sim_plot.add_robot(robot)
robot.set_command(robot.go_to_pose([10,10, 1]))
now = time.time()
while time.time() - now < sim_time:
# clear the pos_ax to remove previous arrow
Plotter.pos_ax.cla()
# iterate through robots in sim, get thier poses
for robot in self.robots.values():
try:
# this is where we instruct the robot
next(robot.command)
except StopIteration:
# if the simulation stops freeze the plot
x_pos, y_pos, theta = robot.pose
self.sim_plot.update_data(
robot.name, x_pos, y_pos, theta % (2*np.pi),
robot.actual_sim_time, robot.r_speed[-1],
robot.l_speed[-1])
x_pos, y_pos, theta = robot.pose
self.sim_plot.update_data(
robot.name, x_pos, y_pos, theta % (2*np.pi),
robot.actual_sim_time, robot.r_speed[-1], robot.l_speed[-1])
# draw on the canvas with the updated data
self.sim_plot.update_canvas()
time.sleep(0.01)
# keep the plotting window open until user closes it
input('Press ENTER to exit')
return
def set_commands(self, robot_name, commands):
'''Sets the mission of a given robot'''
self.robots[robot_name].commands = commands
def sim_missions(self, sim_time):
'''Runs the mission associated with each robot and plots'''
# add the robots in the sim to the plot
for robot in self.robots.values():
self.sim_plot.add_robot(robot)
now = time.time()
while time.time() - now < sim_time:
# clear the position axis so new arrow can be plotted
Plotter.pos_ax.cla()
Plotter.pos_ax.grid()
# iterate through robots in sim, get thier poses
for robot in self.robots.values():
robot.run()
x_pos, y_pos, theta = robot.pose
self.sim_plot.update_data(
robot.name, x_pos, y_pos, theta % (2*np.pi),
time.time() - now, robot.r_speed[-1], robot.l_speed[-1])
time.sleep(self.time_step)
# draw on the canvas with the updated data
self.sim_plot.update_canvas()
# keep the plotting window open until user closes it
input('Press ENTER to exit')
return
if __name__ == "__main__":
print("Running Simulator.py")