-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_terrain.py
executable file
·164 lines (114 loc) · 4.92 KB
/
gen_terrain.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
"""
Generate csv files with terrain readings
"""
import csv
# Interval between messages, in seconds
# Note: 0.5 will trigger the timeout in ArduSub.
INTERVAL = 0.1
# Nominal seafloor depth
SEAFLOOR_Z = -20.0
# Positive terrain values cause special behavior
DROPOUT = 1.0 # Do not send message
LOW_SIGNAL_QUALITY = 2.0 # Send signal_quality = 10
# csv files are saved in a directory which is not checked in
def csv_path(prefix):
return 'terrain/' + prefix + '.csv'
def write_flat_segment(writer, adj: float, t: float):
for i in range(int(t / INTERVAL)):
writer.writerow([SEAFLOOR_Z + adj])
def write_ramp_segment(writer, start: float, stop: float, rate: float):
step = rate * INTERVAL
num = int((stop - start) / step)
for i in range(num):
adj = round(start + i * step, 2)
writer.writerow([SEAFLOOR_Z + adj])
def write_dropouts(writer, t: float):
for i in range(int(t / INTERVAL)):
writer.writerow([DROPOUT])
def write_low_signal_quality(writer, t: float):
for i in range(int(t / INTERVAL)):
writer.writerow([LOW_SIGNAL_QUALITY])
def gen_zeros():
# Open for writing. Do not translate newlines.
with open(csv_path('zeros'), mode='w', newline='') as csvfile:
datawriter = csv.writer(csvfile, delimiter=',', quotechar='|', lineterminator='\n')
# Write the interval
datawriter.writerow([INTERVAL])
# Write some zeros
write_flat_segment(datawriter, 0.0, 10.0)
def gen_trapezoid(tallest_bump: float, rate: float, t=10.0):
# Open for writing. Do not translate newlines.
with open(csv_path('trapezoid'), mode='w', newline='') as csvfile:
datawriter = csv.writer(csvfile, delimiter=',', quotechar='|', lineterminator='\n')
# Write the interval
datawriter.writerow([INTERVAL])
# Write a trapezoid
write_flat_segment(datawriter, 0.0, t)
write_ramp_segment(datawriter, 0.0, tallest_bump, rate)
write_flat_segment(datawriter, tallest_bump, t)
write_ramp_segment(datawriter, tallest_bump, 0.0, -rate)
def gen_sawtooth(tallest_bump: float, rate: float, t=10.0):
# Open for writing. Do not translate newlines.
with open(csv_path('sawtooth'), mode='w', newline='') as csvfile:
datawriter = csv.writer(csvfile, delimiter=',', quotechar='|', lineterminator='\n')
# Write the interval
datawriter.writerow([INTERVAL])
# Write a sawtooth
write_flat_segment(datawriter, 0.0, t)
write_flat_segment(datawriter, tallest_bump, t)
write_ramp_segment(datawriter, tallest_bump, 0.0, -rate)
def gen_square(tallest_bump: float, t=10.0):
# Open for writing. Do not translate newlines.
with open(csv_path('square'), mode='w', newline='') as csvfile:
datawriter = csv.writer(csvfile, delimiter=',', quotechar='|', lineterminator='\n')
# Write the interval
datawriter.writerow([INTERVAL])
# Write a square wave
write_flat_segment(datawriter, 0.0, t)
write_flat_segment(datawriter, tallest_bump, t)
# Stress PID controllers, test dropout handling
def gen_stress(tallest_bump=4.0, t=2.0):
# Open for writing. Do not translate newlines.
with open(csv_path('stress'), mode='w', newline='') as csvfile:
datawriter = csv.writer(csvfile, delimiter=',', quotechar='|', lineterminator='\n')
# Write the interval
datawriter.writerow([INTERVAL])
# Start with a long low segment
write_flat_segment(datawriter, 0.0, 4 * t)
# Dropout, then resume
write_dropouts(datawriter, t)
write_flat_segment(datawriter, tallest_bump, t)
# Jump to very high segment, with dropout
write_flat_segment(datawriter, tallest_bump, t)
# Dropout, then resume
write_dropouts(datawriter, t)
write_flat_segment(datawriter, tallest_bump, t)
# Dropout, then resume at a very different height
write_dropouts(datawriter, t)
write_flat_segment(datawriter, 0.0, t)
write_flat_segment(datawriter, tallest_bump, t)
# Test low signal_quality
def gen_test_signal_quality():
# Open for writing. Do not translate newlines.
with open(csv_path('test_signal_quality'), mode='w', newline='') as csvfile:
datawriter = csv.writer(csvfile, delimiter=',', quotechar='|', lineterminator='\n')
# Write the interval
datawriter.writerow([INTERVAL])
# Start "on the dock"
write_low_signal_quality(datawriter, t=4)
# Normal, in the water
write_flat_segment(datawriter, adj=0.0, t=4)
# Bad readings
write_low_signal_quality(datawriter, t=4)
# Resume, jump
write_flat_segment(datawriter, adj=2.0, t=4)
def main():
gen_zeros()
gen_trapezoid(5.0, 0.2, 20.0)
gen_sawtooth(1.0, 0.2)
gen_square(1.0, 20.0)
gen_stress()
gen_test_signal_quality()
if __name__ == '__main__':
main()