Skip to content

Commit 840a5a7

Browse files
committed
Add debug tool to follow_wall_angle.
1 parent c2c120e commit 840a5a7

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

osgar/explore.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def tangent_circle(dist, radius):
3535
return math.radians(100)
3636

3737

38-
def follow_wall_angle(laser_data, radius, right_wall=False):
38+
def follow_wall_angle(laser_data, radius, right_wall=False, debug=False):
3939
"""
4040
Find the angle to the closest point in laser scan (either on the left or right side).
4141
Then calculate an angle to a free space as tangent to circle of given radius.
@@ -107,12 +107,48 @@ def follow_wall_angle(laser_data, radius, right_wall=False):
107107
else:
108108
tangent_angle = tangent_circle(last_wall_distance, radius)
109109

110+
def deg(index):
111+
ret = -135 + index * deg_resolution
112+
return ret if right_wall else -ret
113+
114+
def rad(index):
115+
return math.radians(deg(index))
110116

111117
laser_angle = math.radians(-135 + last_wall_idx * deg_resolution)
112118
total_angle = laser_angle + tangent_angle
113119
if not right_wall:
114120
total_angle = -total_angle
115121

122+
if debug:
123+
from osgar.lib.drawscan import draw_scan
124+
import cv2
125+
img = draw_scan(laser_data, max_obstacle_distance=4.0)
126+
width_px, height_px = img.shape[1], img.shape[0]
127+
scale = 500
128+
dest_x = math.cos(rad(wall_start_idx))
129+
dest_y = math.sin(rad(wall_start_idx))
130+
point = (width_px//2 - int(dest_y*scale), height_px//2 - int(dest_x*scale))
131+
#print(point, f"start {deg(wall_start_idx)})")
132+
cv2.line(img, (width_px//2, height_px//2), point, color=(255,255,255))
133+
134+
dest_x = math.cos(rad(last_wall_idx))
135+
dest_y = math.sin(rad(last_wall_idx))
136+
point = (width_px//2 - int(dest_y*scale), height_px//2 - int(dest_x*scale))
137+
cv2.line(img, (width_px//2, height_px//2), point, color=(120,255,255))
138+
139+
dest_x = math.cos(total_angle)
140+
dest_y = math.sin(total_angle)
141+
point = (width_px//2 - int(dest_y*scale), height_px//2 - int(dest_x*scale))
142+
cv2.line(img, (width_px//2, height_px//2), point, color=(120,120,255))
143+
144+
point = (width_px//2, height_px//4)
145+
cv2.line(img, (width_px//2, height_px//2), point, color=(120,120,120))
146+
147+
cv2.imshow("follow_wall_angle", img)
148+
cv2.waitKey()
149+
150+
#print("start wall", wall_start_idx, f"deg({deg(wall_start_idx)})", "last wall", last_wall_idx, f"deg({deg(last_wall_idx)})", math.degrees(tangent_angle))
151+
116152
return total_angle
117153

118154

osgar/lib/drawscan.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import cv2
2+
import numpy as np
3+
import math
4+
import osgar.explore
5+
6+
NO_MEASUREMENT = 0
7+
MAX_OBSTACLE_DISTANCE = 20
8+
9+
10+
def draw_scan(scan, max_obstacle_distance=None, scan_left=135, scan_right=-135):
11+
if max_obstacle_distance is None:
12+
max_obstacle_distance = MAX_OBSTACLE_DISTANCE
13+
n = len(scan)
14+
scan = np.asarray(scan) / 1000
15+
16+
angles = np.linspace(math.radians(scan_right), math.radians(scan_left), n).reshape((1, -1))
17+
angles_cos = np.cos(angles)
18+
angles_sin = np.sin(angles)
19+
is_valid = scan != NO_MEASUREMENT
20+
valid_scan = scan[is_valid]
21+
is_valid = is_valid.reshape((1, -1))
22+
acoss = angles_cos[is_valid]
23+
asins = angles_sin[is_valid]
24+
x = acoss * valid_scan
25+
y = asins * valid_scan
26+
far_map = valid_scan > max_obstacle_distance
27+
28+
height_px = 768
29+
width_px = 1024
30+
img = np.zeros((height_px, width_px, 3), dtype=np.uint8)
31+
32+
scale = 50
33+
for ix, iy, is_far in zip(x, y, far_map):
34+
point = (width_px//2 - int(iy*scale), height_px//2 - int(ix*scale))
35+
color = (0, 255, 0) if not is_far else (120, 120, 120)
36+
cv2.circle(img, point, radius=3, color=color, thickness=-1)
37+
38+
point = (width_px//2, height_px//2)
39+
point2 = (width_px//2, height_px//2-20)
40+
cv2.drawMarker(img, point, color=(0, 0, 255), markerType=cv2.MARKER_DIAMOND, thickness=3, markerSize=10)
41+
cv2.line(img, point, point2, thickness=3, color=(0, 0, 255))
42+
return img

subt/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ def go_safely(self, desired_direction):
257257
safety, safe_direction = 1.0, desired_direction
258258
else:
259259
safety, safe_direction = self.local_planner.recommend(desired_direction)
260-
#print(self.time,"safety:%f desired:%f safe_direction:%f"%(safety, desired_direction, safe_direction))
260+
if self.verbose:
261+
heading = math.degrees(quaternion.heading(self.orientation))
262+
desired_deg = math.degrees(desired_direction)
263+
safe_deg = math.degrees(safe_direction)
264+
print(self.time, self.sim_time_sec, f"safety:{safety:.2f} desired:{desired_deg:.2f}° safe_direction: {safe_deg:.2f}°")
261265
#desired_angular_speed = 1.2 * safe_direction
262266
desired_angular_speed = 0.9 * safe_direction
263267
size = len(self.scan)
@@ -326,7 +330,7 @@ def follow_wall(self, radius, right_wall=False, timeout=timedelta(hours=3), dist
326330
if self.use_center:
327331
desired_direction = 0
328332
else:
329-
desired_direction = follow_wall_angle(self.scan, radius=radius, right_wall=right_wall)
333+
desired_direction = follow_wall_angle(self.scan, radius=radius, right_wall=right_wall, debug=self.verbose)
330334
self.go_safely(desired_direction)
331335
if dist_limit is not None:
332336
if dist_limit < abs(self.traveled_dist - start_dist): # robot can return backward -> abs()

0 commit comments

Comments
 (0)