-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·70 lines (51 loc) · 1.85 KB
/
main.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
#!/bin/env python3.2
# coding=utf-8
#
import os
import train.route
import train.drive
def main():
route = train.route.RailRoute('data/sample_A.route')
drive_unit = train.drive.DriveUnit()
# There is the simulation core
result = drive_unit.run(route)
print ('Travel time is {:g} minute.'.format(result[-1].time/60))
# We will make some visualization (gnuplot is needed)
# --- results/sample_A.profile ---
try:
with open('results/sample_A.profile', 'w') as fh:
x = 0.0
while (route.getSpeedLimit(x) > 0.0):
fh.write('{:e}\t{:e}\t{:e}\n'.format(x, route.getSpeedLimit(x), route.getSlope(x) ))
x += 1.0
fh.write('{:e}\t{:e}\t{:e}\n'.format(x, route.getSpeedLimit(x), route.getSlope(x) ))
except EnvironmentError as err:
print (err)
# --- results/sample_A.drive ---
try:
with open('results/sample_A.drive', 'w') as fh:
for d in result:
fh.write("{:e}\t{:e}\t{:e}\n".format(d.time, d.distance, d.speed))
except EnvironmentError as err:
print (err)
# --- results/gnuplot.dat ---
try:
with open('results/gnuplot.dat', 'w') as fh:
fh.write('reset\n')
fh.write('set terminal "png"\n')
fh.write('set output "results/sample_A.png"\n')
fh.write('set title "driving profile"\n')
fh.write('set xlabel "distance [m]"\n')
fh.write('set yrange [0:160]\n')
fh.write('set ylabel "velocity [km/h]"\n')
fh.write('set y2range [-20:140]\n')
fh.write('set y2tics 0,10,20\n')
fh.write('set y2label "slope [ppt]"\n')
fh.write('plot "results/sample_A.profile" u 1:2 w l title "speedlimit", "results/sample_A.drive" u 2:3 w l title "actual speed", "results/sample_A.profile" u 1:3 w l axis x1y2 title "slope"\n')
fh.write('reset\n')
except EnvironmentError as err:
print (err)
# !!! PLEASE, comment out this line if you do not gnuplot
os.system('gnuplot results/gnuplot.dat')
if __name__ == "__main__":
main()