Skip to content

Commit 03b766f

Browse files
authored
Merge pull request #14 from leggedrobotics/fix/improve_shifting
Fix/improve shifting
2 parents 85e113a + aae3980 commit 03b766f

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

elevation_mapping_cupy/script/elevation_mapping.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@
2727
)
2828

2929
import cupy as cp
30-
import cupyx.scipy as csp
31-
import cupyx.scipy.ndimage
3230

3331
xp = cp
34-
sp = csp
3532
pool = cp.cuda.MemoryPool(cp.cuda.malloc_managed)
3633
cp.cuda.set_allocator(pool.malloc)
3734

@@ -131,20 +128,34 @@ def move_to(self, position):
131128
self.shift_map_xy(-delta_pixel)
132129
self.shift_map_z(-delta[2])
133130

131+
def pad_value(self, x, shift_value, idx=None, value=0.0):
132+
if idx is None:
133+
if shift_value[0] > 0:
134+
x[:, : shift_value[0], :] = value
135+
elif shift_value[0] < 0:
136+
x[:, shift_value[0] :, :] = value
137+
if shift_value[1] > 0:
138+
x[:, :, : shift_value[1]] = value
139+
elif shift_value[1] < 0:
140+
x[:, :, shift_value[1] :] = value
141+
else:
142+
if shift_value[0] > 0:
143+
x[idx, : shift_value[0], :] = value
144+
elif shift_value[0] < 0:
145+
x[idx, shift_value[0] :, :] = value
146+
if shift_value[1] > 0:
147+
x[idx, :, : shift_value[1]] = value
148+
elif shift_value[1] < 0:
149+
x[idx, :, shift_value[1] :] = value
150+
134151
def shift_map_xy(self, delta_pixel):
135-
shift_value = delta_pixel
136-
shift_fn = sp.ndimage.interpolation.shift
152+
shift_value = delta_pixel.astype(cp.int)
153+
if cp.abs(shift_value).sum() == 0:
154+
return
137155
with self.map_lock:
138-
# elevation
139-
self.elevation_map[0] = shift_fn(self.elevation_map[0], shift_value, cval=0.0)
140-
# variance
141-
self.elevation_map[1] = shift_fn(self.elevation_map[1], shift_value, cval=self.initial_variance)
142-
# is valid (1 is valid 0 is not valid)
143-
self.elevation_map[2] = shift_fn(self.elevation_map[2], shift_value, cval=0)
144-
# upper bound
145-
self.elevation_map[5] = shift_fn(self.elevation_map[5], shift_value, cval=0)
146-
# is upper bound
147-
self.elevation_map[6] = shift_fn(self.elevation_map[6], shift_value, cval=0)
156+
self.elevation_map = cp.roll(self.elevation_map, shift_value, axis=(1, 2))
157+
self.pad_value(self.elevation_map, shift_value, value=0.0)
158+
self.pad_value(self.elevation_map, shift_value, idx=1, value=self.initial_variance)
148159

149160
def shift_map_z(self, delta_z):
150161
with self.map_lock:
@@ -513,14 +524,17 @@ def initialize_map(self, points, method="cubic"):
513524
R = xp.random.rand(3, 3)
514525
t = xp.random.rand(3)
515526
print(R, t)
516-
param = Parameter(use_chainer=False)
517-
param.load_weights("../config/weights.dat")
527+
param = Parameter(
528+
use_chainer=False, weight_file="../config/weights.dat", plugin_config_file="../config/plugin_config.yaml"
529+
)
518530
elevation = ElevationMap(param)
519531
layers = ["elevation", "variance", "traversability", "min_filter", "smooth", "inpaint"]
520532
data = np.zeros((elevation.cell_n - 2, elevation.cell_n - 2), dtype=np.float32)
521533
for i in range(500):
522534
elevation.input(points, R, t, 0, 0)
523535
elevation.update_normal(elevation.elevation_map[0])
536+
pos = np.array([i * 0.01, i * 0.02, i * 0.01])
537+
elevation.move_to(pos)
524538
for layer in layers:
525539
elevation.get_map_with_name_ref(layer, data)
526540
print(i)

0 commit comments

Comments
 (0)