forked from simondlevy/PyRoboViz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomwalk.py
executable file
·74 lines (55 loc) · 2.25 KB
/
randomwalk.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
#!/usr/bin/env python3
'''
randomwalk.py: PyRoboViz test with random walk
Copyright (C) 2018 Simon D. Levy
This file is part of PyRoboViz.
PyRoboViz is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
PyRoboViz is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http:#www.gnu.org/licenses/>.
'''
from roboviz import Visualizer
from time import time
import numpy as np
import argparse
MAP_SIZE_PIXELS = 800
MAP_SIZE_METERS = 32
SPEED_MPS = 1
class _MyArgumentParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(1)
if __name__ == '__main__':
# Parse optional command-line arguments
parser = _MyArgumentParser(description='Visualize a random walk.')
parser.add_argument('-s', '--seed', help='set seed for pseudo-random number generator')
cmdargs = parser.parse_args()
# Set seed for pseudo-random number generator if indicated
if not cmdargs.seed is None:
np.random.seed(int(cmdargs.seed))
# Create a Visualizer object with a trajectory, centered at 0,0
viz = Visualizer(MAP_SIZE_PIXELS, MAP_SIZE_METERS, 'Random Walk', True)
# Start in the center of the map with a random heading
pose = np.array([0,0,360*np.random.random()])
# Start timing
prevtime = time()
# Loop till user closes the display
while True:
# Set current pose in visualizer the display, exiting gracefully if user closes it
if not viz.display(*pose):
exit(0)
# Rotate randomly and move forward
currtime = time()
s = SPEED_MPS * (currtime - prevtime)
prevtime = currtime
theta = np.radians(pose[2])
pose[0] += s * np.cos(theta)
pose[1] += s * np.sin(theta)
pose[2] += 10 * np.random.randn()