Skip to content

Commit 69a3853

Browse files
Moved the snapping extractor out of the RapidHeightMapManager and to a higher level closer to where its used (#796)
* Split the autonomy height map thread and the continuous hiking height map thread * Close kernel before the rest of the objeccts * Change things to use the EnvironmentHandler rather then setting the HeightMapData and the TerrainMapData individually * Moved the snapping extractor out of the RapidHeightMapManager and to a higher level closer to where its used * Renaming the methods didn't catch everything * Fix snapping test * Remove duplicate variable * Uncomment bounds check * Need to increase window height to go down stairs
1 parent 8abee2f commit 69a3853

File tree

16 files changed

+96
-153
lines changed

16 files changed

+96
-153
lines changed

ihmc-high-level-behaviors/src/main/java/us/ihmc/behaviors/activeMapping/ContinuousHikingProcess.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import us.ihmc.footstepPlanning.graphSearch.EnvironmentHandler;
88
import us.ihmc.humanoidRobotics.communication.ControllerFootstepQueueMonitor;
99
import us.ihmc.perception.StandAloneRealsenseProcess;
10+
import us.ihmc.perception.gpuHeightMap.SnappingTerrainExtractor;
1011
import us.ihmc.robotics.physics.RobotCollisionModel;
1112
import us.ihmc.ros2.ROS2Node;
1213
import us.ihmc.ros2.ROS2NodeBuilder;
@@ -26,6 +27,8 @@ public class ContinuousHikingProcess
2627
private final ContinuousPlannerSchedulingTask continuousPlannerSchedulingTask;
2728
private final StandAloneRealsenseProcess standAloneRealsenseProcess;
2829

30+
private final SnappingTerrainExtractor snappingTerrainExtractor;
31+
2932
public ContinuousHikingProcess(DRCRobotModel robotModel, RobotCollisionModel robotCollisionModel)
3033
{
3134
ROS2Node ros2Node = new ROS2NodeBuilder().build("nadia_terrain_perception_node");
@@ -41,8 +44,9 @@ public ContinuousHikingProcess(DRCRobotModel robotModel, RobotCollisionModel rob
4144

4245
ControllerFootstepQueueMonitor controllerFootstepQueueMonitor = new ControllerFootstepQueueMonitor(ros2Node, robotModel.getSimpleRobotName());
4346

44-
environmentHandler = new EnvironmentHandler();
4547
activeMappingParameterToolBox = new ActiveMappingParameterToolBox(ros2Node, robotModel, "ForContinuousWalking");
48+
environmentHandler = new EnvironmentHandler();
49+
snappingTerrainExtractor = new SnappingTerrainExtractor(activeMappingParameterToolBox.getHeightMapParameters());
4650
standAloneRealsenseProcess = new StandAloneRealsenseProcess(ros2Node,
4751
ros2Helper,
4852
syncedRobot,
@@ -69,17 +73,21 @@ public void update()
6973
activeMappingParameterToolBox.update();
7074

7175
if (standAloneRealsenseProcess.getLatestHeightMapData() != null)
76+
{
7277
environmentHandler.setHeightMapData(standAloneRealsenseProcess.getLatestHeightMapData());
78+
snappingTerrainExtractor.update(environmentHandler.getHeightMapData());
79+
}
7380

74-
if (standAloneRealsenseProcess.getLatestTerrainMapData() != null)
75-
environmentHandler.setTerrainMapData(standAloneRealsenseProcess.getLatestTerrainMapData());
81+
if (snappingTerrainExtractor.getTerrainMapData() != null)
82+
environmentHandler.setTerrainMapData(snappingTerrainExtractor.getTerrainMapData());
7683

7784
continuousPlannerSchedulingTask.setLatestEnvironmentHandler(environmentHandler);
7885
}
7986

8087
public void destroy()
8188
{
8289
continuousPlannerSchedulingTask.destroy();
90+
snappingTerrainExtractor.close();
8391
standAloneRealsenseProcess.destroy();
8492
}
8593
}

ihmc-high-level-behaviors/src/main/java/us/ihmc/perception/RapidHeightMapAutonomyUpdateThread.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,6 @@ public HeightMapData getLatestHeightMapData()
8787
}
8888
}
8989

90-
public TerrainMapData getLatestTerrainMapData()
91-
{
92-
synchronized (heightMapLock)
93-
{
94-
return heightMapManager.getTerrainMapData();
95-
}
96-
}
97-
9890
@Override
9991
public void kill()
10092
{

ihmc-high-level-behaviors/src/main/java/us/ihmc/perception/RapidHeightMapThread.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,6 @@ public HeightMapData getLatestHeightMapData()
9494
}
9595
}
9696

97-
public TerrainMapData getLatestTerrainMapData()
98-
{
99-
synchronized (heightMapLock)
100-
{
101-
return heightMapManager.getTerrainMapData();
102-
}
103-
}
104-
10597
public void destroy()
10698
{
10799
scheduler.shutdown();

ihmc-high-level-behaviors/src/main/java/us/ihmc/perception/StandAloneRealsenseProcess.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import us.ihmc.communication.ros2.ROS2Helper;
99
import us.ihmc.communication.ros2.ROS2TunedRigidBodyTransform;
1010
import us.ihmc.humanoidRobotics.communication.ControllerFootstepQueueMonitor;
11-
import us.ihmc.perception.heightMap.TerrainMapData;
1211
import us.ihmc.robotics.physics.RobotCollisionModel;
1312
import us.ihmc.ros2.ROS2Node;
1413
import us.ihmc.sensorProcessing.heightMap.HeightMapData;
@@ -96,11 +95,6 @@ public HeightMapData getLatestHeightMapData()
9695
return rapidHeightMapThread.getLatestHeightMapData();
9796
}
9897

99-
public TerrainMapData getLatestTerrainMapData()
100-
{
101-
return rapidHeightMapThread.getLatestTerrainMapData();
102-
}
103-
10498
public void destroy()
10599
{
106100
rapidHeightMapThread.destroy();

ihmc-perception/src/main/java/us/ihmc/perception/gpuHeightMap/RapidHeightMapExtractorCUDA.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public class RapidHeightMapExtractorCUDA
3535
private final GpuMat globalHeightMapImage;
3636
private final GpuMat terrainCostImage;
3737
private final GpuMat contactMapImage;
38-
private final GpuMat sensorCroppedHeightMapImage;
39-
private final GpuMat terrainHeightMapImage;
38+
private final GpuMat croppedHeightMapImage;
4039
private final GpuMat emptyGlobalHeightMapImage;
4140
private final CUDAProgram heightMapCUDAProgram;
4241

@@ -65,11 +64,9 @@ public class RapidHeightMapExtractorCUDA
6564
private float gridOffsetX;
6665
private int centerIndex;
6766
private int cellsPerAxisLocal;
68-
private int cellsPerAxisTerrain;
6967
private int globalCenterIndex;
7068
private int croppedCenterIndex;
7169
private int cellsPerAxisCropped;
72-
private int terrainCenterIndex;
7370
private int globalCellsPerAxis;
7471
private dim3 blockSize;
7572
private dim3 updateKernelGridDim;
@@ -117,8 +114,7 @@ public RapidHeightMapExtractorCUDA(int mode, HeightMapParameters heightMapParame
117114
globalHeightMapImage = new GpuMat(globalCellsPerAxis, globalCellsPerAxis, opencv_core.CV_16UC1);
118115
terrainCostImage = new GpuMat(globalCellsPerAxis, globalCellsPerAxis, opencv_core.CV_8UC1);
119116
contactMapImage = new GpuMat(globalCellsPerAxis, globalCellsPerAxis, opencv_core.CV_8UC1);
120-
sensorCroppedHeightMapImage = new GpuMat(cellsPerAxisCropped, cellsPerAxisCropped, opencv_core.CV_16UC1);
121-
terrainHeightMapImage = new GpuMat(cellsPerAxisTerrain, cellsPerAxisTerrain, opencv_core.CV_16UC1);
117+
croppedHeightMapImage = new GpuMat(cellsPerAxisCropped, cellsPerAxisCropped, opencv_core.CV_16UC1);
122118

123119
emptyGlobalHeightMapImage = new GpuMat(globalCellsPerAxis, globalCellsPerAxis, opencv_core.CV_16UC1);
124120

@@ -149,9 +145,6 @@ private void recomputeDerivedParameters()
149145
globalCenterIndex = HeightMapTools.computeCenterIndex(heightMapParameters.getInternalGlobalWidthInMeters(), heightMapParameters.getCellSizeInMeters());
150146
globalCellsPerAxis = 2 * globalCenterIndex + 1;
151147

152-
terrainCenterIndex = HeightMapTools.computeCenterIndex(heightMapParameters.getTerrainWidthInMeters(), heightMapParameters.getCellSizeInMeters());
153-
cellsPerAxisTerrain = 2 * terrainCenterIndex + 1;
154-
155148
croppedCenterIndex = HeightMapTools.computeCenterIndex(heightMapParameters.getCroppedWidthInMeters(), heightMapParameters.getCellSizeInMeters());
156149
cellsPerAxisCropped = 2 * croppedCenterIndex + 1;
157150

@@ -269,11 +262,9 @@ public void update(GpuMat latestDepthImageGPU,
269262

270263
// Run the cropping kernel
271264
croppingKernel.withPointer(globalHeightMapImage.data()).withLong(globalHeightMapImage.step());
272-
croppingKernel.withPointer(sensorCroppedHeightMapImage.data()).withLong(sensorCroppedHeightMapImage.step());
273-
croppingKernel.withPointer(terrainHeightMapImage.data()).withLong(terrainHeightMapImage.step());
265+
croppingKernel.withPointer(croppedHeightMapImage.data()).withLong(croppedHeightMapImage.step());
274266
croppingKernel.withPointer(parametersDevicePointer);
275267
croppingKernel.withInt(cellsPerAxisCropped);
276-
croppingKernel.withInt(terrainHeightMapImage.rows());
277268
error = cudaStreamSynchronize(stream);
278269
CUDATools.checkCUDAError(error);
279270

@@ -377,7 +368,6 @@ public float[] populateParameterArray(HeightMapParameters parameters,
377368
(float) parameters.getSearchWindowHeight(),
378369
(float) parameters.getSearchWindowWidth(),
379370
(float) croppedCenterIndex,
380-
(float) terrainCenterIndex,
381371
(float) parameters.getMinClampHeight(),
382372
(float) parameters.getMaxClampHeight(),
383373
(float) parameters.getHeightOffset(),
@@ -418,8 +408,7 @@ public void destroy()
418408
globalHeightMapImage.close();
419409
terrainCostImage.close();
420410
contactMapImage.close();
421-
sensorCroppedHeightMapImage.close();
422-
terrainHeightMapImage.close();
411+
croppedHeightMapImage.close();
423412

424413
filteredRapidHeightMapExtractor.destroy();
425414
verticalSurfacesExtractor.destroy();
@@ -449,13 +438,8 @@ public int getSequenceNumber()
449438
return sequenceNumber;
450439
}
451440

452-
public GpuMat getTerrainHeightMapImage()
453-
{
454-
return terrainHeightMapImage;
455-
}
456-
457-
public GpuMat getVisualizedHeightMap()
441+
public GpuMat getCroppedHeightMap()
458442
{
459-
return sensorCroppedHeightMapImage.clone();
443+
return croppedHeightMapImage.clone();
460444
}
461445
}

ihmc-perception/src/main/java/us/ihmc/perception/gpuHeightMap/RapidHeightMapManager.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import us.ihmc.perception.camera.CameraIntrinsics;
1919
import us.ihmc.perception.cuda.CUDABodyCollisionFilter;
2020
import us.ihmc.perception.filters.CUDAFlyingPointsFilter;
21-
import us.ihmc.perception.heightMap.TerrainMapData;
2221
import us.ihmc.perception.opencv.OpenCVTools;
2322
import us.ihmc.perception.tools.PerceptionMessageTools;
2423
import us.ihmc.robotModels.FullHumanoidRobotModel;
@@ -28,6 +27,7 @@
2827
import us.ihmc.scs2.simulation.collision.Collidable;
2928
import us.ihmc.sensorProcessing.heightMap.HeightMapData;
3029
import us.ihmc.sensorProcessing.heightMap.HeightMapParameters;
30+
import us.ihmc.sensorProcessing.heightMap.HeightMapTools;
3131

3232
import java.time.Instant;
3333
import java.util.ArrayList;
@@ -40,7 +40,6 @@ public class RapidHeightMapManager
4040
{
4141
private final HeightMapParameters heightMapParameters;
4242
private final RapidHeightMapExtractorCUDA rapidHeightMapExtractor;
43-
private final SnappingTerrainExtractor snappedFootstepsExtractor;
4443

4544
private final Point3D sensorOrigin = new Point3D();
4645
private final FramePose3D cameraPose = new FramePose3D();
@@ -56,6 +55,7 @@ public class RapidHeightMapManager
5655
private final ROS2Publisher<ImageMessage> heightMapPublisher;
5756
private final ImageMessage croppedHeightMapImageMessage = new ImageMessage();
5857
private final BytePointer compressedCroppedHeightMapPointer = new BytePointer();
58+
private final GpuMat deviceCroppedHeightMap;
5959

6060
public RapidHeightMapManager(ROS2Node ros2Node,
6161
RobotCollisionModel robotCollisionModel,
@@ -76,8 +76,10 @@ public RapidHeightMapManager(ROS2Node ros2Node,
7676
rapidHeightMapDriftOffset = new RapidHeightMapDriftOffset(controllerFootstepQueueMonitor);
7777
flyingPointsFilter = new CUDAFlyingPointsFilter();
7878

79+
int croppedCenterIndex = HeightMapTools.computeCenterIndex(heightMapParameters.getCroppedWidthInMeters(), heightMapParameters.getCellSizeInMeters());
80+
int cellsPerAxisCropped = 2 * croppedCenterIndex + 1;
81+
deviceCroppedHeightMap = new GpuMat(cellsPerAxisCropped, cellsPerAxisCropped, opencv_core.CV_16UC1);
7982
rapidHeightMapExtractor = new RapidHeightMapExtractorCUDA(1, heightMapParameters);
80-
snappedFootstepsExtractor = new SnappingTerrainExtractor(heightMapParameters);
8183

8284
// We use a notification to only call resetting the height map in one place
8385
heightMapPublisher = ros2Node.createPublisher(PerceptionAPI.HEIGHT_MAP_CROPPED);
@@ -96,15 +98,14 @@ public RapidHeightMapManager(ROS2Node ros2Node,
9698
}
9799
}
98100

99-
public void updateAndPublishHeightMap(RawImage depthImage, ReferenceFrame cameraFrame, ReferenceFrame cameraZUpFrame) throws Exception
101+
public void updateAndPublishHeightMap(RawImage depthImage, ReferenceFrame cameraFrame, ReferenceFrame cameraZUpFrame)
100102
{
101103
RawImage depthImageCopy = depthImage.get();
102104
Mat latestDepthImage = depthImageCopy.getCpuImageMat();
103105
Instant acquisitionTime = depthImageCopy.getAcquisitionTime();
104106
CameraIntrinsics depthIntrinsicsCopy = depthImageCopy.getIntrinsicsCopy();
105107

106108
// -------- Update the Height Map with the latest depth image from the sensor --------------
107-
GpuMat deviceCroppedHeightMap = new GpuMat();
108109
updateInternal(latestDepthImage, deviceCroppedHeightMap, depthIntrinsicsCopy, cameraFrame, cameraZUpFrame);
109110

110111
// Publish the height map to anyone who is subscribing
@@ -121,16 +122,18 @@ public void updateAndPublishHeightMap(RawImage depthImage, ReferenceFrame camera
121122
hostCroppedHeightMap.cols(),
122123
(float) heightMapParameters.getHeightScaleFactor());
123124

124-
deviceCroppedHeightMap.close();
125125
hostCroppedHeightMap.close();
126126
depthImageCopy.release();
127127
}
128128

129+
/**
130+
* Update the Height Map with the latest depth image from the sensor
131+
*/
129132
private void updateInternal(Mat latestDepthImage,
130133
GpuMat deviceHeightMapToPack,
131134
CameraIntrinsics depthIntrinsicsCopy,
132135
ReferenceFrame cameraFrame,
133-
ReferenceFrame cameraZUpFrame) throws Exception
136+
ReferenceFrame cameraZUpFrame)
134137
{
135138
// Option that gets triggered from a message sent from the user
136139
if (lowerHeightMapBackdropRequested.poll())
@@ -194,7 +197,7 @@ private void updateInternal(Mat latestDepthImage,
194197
sensorOrigin,
195198
computeFootHeight());
196199

197-
GpuMat deviceCroppedHeightMap = rapidHeightMapExtractor.getVisualizedHeightMap();
200+
GpuMat deviceCroppedHeightMap = rapidHeightMapExtractor.getCroppedHeightMap();
198201
// We have used the depth image without the robot, close this to avoid creating a memory leak
199202
depthImageWithoutRobot.close();
200203

@@ -209,30 +212,24 @@ private void updateInternal(Mat latestDepthImage,
209212

210213
// Don't close this mat as its being used in the extractor till that finish's
211214
deviceCroppedHeightMap.convertTo(deviceHeightMapToPack, deviceCroppedHeightMap.type());
212-
213-
// Now extract the maps to be used in the footstep planning algorithm
214-
snappedFootstepsExtractor.update(rapidHeightMapExtractor.getTerrainHeightMapImage(), sensorOrigin);
215-
}
216-
217-
public TerrainMapData getTerrainMapData()
218-
{
219-
return snappedFootstepsExtractor.getTerrainMapData();
220215
}
221216

222217
public HeightMapData getLatestHeightMapData()
223218
{
224219
HeightMapData latestHeightMapData = new HeightMapData((float) heightMapParameters.getCellSizeInMeters(),
225-
(float) heightMapParameters.getTerrainWidthInMeters(),
220+
(float) heightMapParameters.getCroppedWidthInMeters(),
226221
sensorOrigin.getX(),
227222
sensorOrigin.getY());
228223

229-
Mat heightMapMat = getTerrainMapData().getHeightMap();
230-
PerceptionMessageTools.convertToHeightMapData(heightMapMat,
224+
Mat heightMap = new Mat();
225+
deviceCroppedHeightMap.download(heightMap);
226+
PerceptionMessageTools.convertToHeightMapData(heightMap,
231227
latestHeightMapData,
232228
sensorOrigin,
233-
(float) heightMapParameters.getTerrainWidthInMeters(),
229+
(float) heightMapParameters.getCroppedWidthInMeters(),
234230
(float) heightMapParameters.getCellSizeInMeters(),
235231
heightMapParameters);
232+
heightMap.close();
236233

237234
return latestHeightMapData;
238235
}
@@ -257,8 +254,8 @@ private double computeFootHeight()
257254
public void destroy()
258255
{
259256
rapidHeightMapExtractor.destroy();
260-
snappedFootstepsExtractor.close();
261257
flyingPointsFilter.destroy();
258+
deviceCroppedHeightMap.close();
262259
compressedCroppedHeightMapPointer.close();
263260
}
264261
}

0 commit comments

Comments
 (0)