-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslam_03_d_find_cylinders_cartesian_question.py
73 lines (63 loc) · 2.52 KB
/
slam_03_d_find_cylinders_cartesian_question.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
# For each cylinder in the scan, find its cartesian coordinates,
# in the scanner's coordinate system.
# Write the result to a file which contains all cylinders, for all scans.
# 03_d_find_cylinders_cartesian
# Claus Brenner, 09 NOV 2012
from lego_robot import *
from math import sin, cos
# Find the derivative in scan data, ignoring invalid measurements.
def compute_derivative(scan, min_dist):
jumps = [ 0 ]
for i in xrange(1, len(scan) - 1):
l = scan[i-1]
r = scan[i+1]
if l > min_dist and r > min_dist:
derivative = (r - l) / 2.0
jumps.append(derivative)
else:
jumps.append(0)
jumps.append(0)
return jumps
# For each area between a left falling edge and a right rising edge,
# determine the average ray number and the average depth.
def find_cylinders(scan, scan_derivative, jump, min_dist):
cylinder_list = []
on_cylinder = False
sum_ray, sum_depth, rays = 0.0, 0.0, 0
# --->>> Insert here your previous solution from find_cylinders_question.py.
return cylinder_list
def compute_cartesian_coordinates(cylinders, cylinder_offset):
result = []
for c in cylinders:
# --->>> Insert here the conversion from polar to Cartesian coordinates.
# c is a tuple (beam_index, range).
# For converting the beam index to an angle, use
# LegoLogfile.beam_index_to_angle(beam_index)
result.append( (0,0) ) # Replace this by your (x,y)
return result
if __name__ == '__main__':
minimum_valid_distance = 20.0
depth_jump = 100.0
cylinder_offset = 90.0
# Read the logfile which contains all scans.
logfile = LegoLogfile()
logfile.read("robot4_scan.txt")
# Write a result file containing all cylinder records.
# Format is: D C x[in mm] y[in mm] ...
# With zero or more points.
# Note "D C" is also written for otherwise empty lines (no
# cylinders in scan)
out_file = file("cylinders.txt", "w")
for scan in logfile.scan_data:
# Find cylinders.
der = compute_derivative(scan, minimum_valid_distance)
cylinders = find_cylinders(scan, der, depth_jump,
minimum_valid_distance)
cartesian_cylinders = compute_cartesian_coordinates(cylinders,
cylinder_offset)
# Write to file.
print >> out_file, "D C",
for c in cartesian_cylinders:
print >> out_file, "%.1f %.1f" % c,
print >> out_file
out_file.close()