Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-return for snow simulation #348

Merged
merged 9 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 8
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 11
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 6
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 5
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 9
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 10
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 20
modelPreset: 7
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 2
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 3
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
AutomaticCaptureHz: 10
modelPreset: 4
returnType: 0
returnMode: 16777220
applyDistanceGaussianNoise: 1
applyAngularGaussianNoise: 1
applyVelocityDistortion: 0
simulateBeamDivergence: 0
doValidateConfigurationOnStartup: 1
configuration:
id: 0
Expand Down
Binary file not shown.
26 changes: 15 additions & 11 deletions Assets/RGLUnityPlugin/Scripts/LidarSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class LidarSensor : MonoBehaviour
[Tooltip("Allows to select one of built-in LiDAR models")]
public LidarModel modelPreset = LidarModel.RangeMeter;

[Tooltip("Allows to select between not divergent beams and different multi-return modes")]
public RGLReturnType returnType = RGLReturnType.RGL_RETURN_TYPE_NOT_DIVERGENT;
[Tooltip("Allows to select between LiDAR return modes")]
public RGLReturnMode returnMode = RGLReturnMode.SingleReturnFirst;

[Tooltip("Allows to quickly enable/disable distance gaussian noise")]
public bool applyDistanceGaussianNoise = true;
Expand All @@ -58,6 +58,9 @@ public class LidarSensor : MonoBehaviour
[Tooltip("Allows to quickly enable/disable velocity distortion")]
public bool applyVelocityDistortion = false;

[Tooltip("If disable, both beam divergence values are set to 0. Otherwise, they are set based on LiDAR configuration.")]
public bool simulateBeamDivergence = true;
PawelLiberadzki marked this conversation as resolved.
Show resolved Hide resolved
PawelLiberadzki marked this conversation as resolved.
Show resolved Hide resolved

[Tooltip(
"If enabled, validates whether the configuration is the same as the manual for the selected model (only on startup)")]
public bool doValidateConfigurationOnStartup = true;
Expand Down Expand Up @@ -85,7 +88,6 @@ public class LidarSensor : MonoBehaviour
private const string lidarPoseNodeId = "LIDAR_POSE";
private const string noiseLidarRayNodeId = "NOISE_LIDAR_RAY";
private const string lidarRaytraceNodeId = "LIDAR_RAYTRACE";
private const string lidarMRNodeId = "LIDAR_MR";
private const string noiseHitpointNodeId = "NOISE_HITPOINT";
private const string noiseDistanceNodeId = "NOISE_DISTANCE";
private const string pointsCompactNodeId = "POINTS_COMPACT";
Expand Down Expand Up @@ -113,7 +115,6 @@ public void Awake()
.AddNodeRaysTransform(lidarPoseNodeId, Matrix4x4.identity)
.AddNodeGaussianNoiseAngularRay(noiseLidarRayNodeId, 0, 0)
.AddNodeRaytrace(lidarRaytraceNodeId)
.AddNodeMultiReturnSwitch(lidarMRNodeId, RGLReturnType.RGL_RETURN_TYPE_NOT_DIVERGENT)
.AddNodeGaussianNoiseAngularHitpoint(noiseHitpointNodeId, 0, 0)
.AddNodeGaussianNoiseDistance(noiseDistanceNodeId, 0, 0, 0);

Expand Down Expand Up @@ -144,8 +145,7 @@ public void Start()
if (LidarSnowManager.Instance != null)
{
// Add deactivated node with some initial values. To be activated and updated when validating.
rglGraphLidar.AddNodePointsSimulateSnow(snowNodeId, 0.0f, 1.0f, 0.0001f, 0.0001f, 0.2f, 0.01f, 1, 0.01f,
false, 0.0f);
rglGraphLidar.AddNodePointsSimulateSnow(snowNodeId, 0.0f, 1.0f, 0.0001f, 0.0001f, 0.2f, 0.01f, 1, 0.01f, 0.0f);
rglGraphLidar.SetActive(snowNodeId, false);
LidarSnowManager.Instance.OnNewConfig += OnValidate;
}
Expand Down Expand Up @@ -199,10 +199,9 @@ private void ApplyConfiguration(BaseLidarConfiguration newConfig)
newConfig.noiseParams.angularNoiseMean * Mathf.Deg2Rad,
newConfig.noiseParams.angularNoiseStDev * Mathf.Deg2Rad)
.UpdateNodeGaussianNoiseDistance(noiseDistanceNodeId, newConfig.noiseParams.distanceNoiseMean,
newConfig.noiseParams.distanceNoiseStDevBase, newConfig.noiseParams.distanceNoiseStDevRisePerMeter)
.UpdateMultiReturnSwitch(lidarMRNodeId, returnType);
newConfig.noiseParams.distanceNoiseStDevBase, newConfig.noiseParams.distanceNoiseStDevRisePerMeter);

if (returnType != RGLReturnType.RGL_RETURN_TYPE_NOT_DIVERGENT)
if (simulateBeamDivergence)
{
rglGraphLidar.ConfigureNodeRaytraceBeamDivergence(lidarRaytraceNodeId,
Mathf.Deg2Rad * newConfig.horizontalBeamDivergence,
Expand All @@ -212,6 +211,8 @@ private void ApplyConfiguration(BaseLidarConfiguration newConfig)
{
rglGraphLidar.ConfigureNodeRaytraceBeamDivergence(lidarRaytraceNodeId, 0.0f, 0.0f);
}

rglGraphLidar.ConfigureNodeRaytraceReturnMode(lidarRaytraceNodeId, returnMode);

rglGraphLidar.SetActive(noiseDistanceNodeId, applyDistanceGaussianNoise);
var angularNoiseType = newConfig.noiseParams.angularNoiseType;
Expand All @@ -235,8 +236,11 @@ private void ApplyConfiguration(BaseLidarConfiguration newConfig)
LidarSnowManager.Instance.Density,
newConfig.laserArray.GetLaserRingIds().Length,
newConfig.horizontalBeamDivergence * Mathf.Deg2Rad,
LidarSnowManager.Instance.DoSimulateEnergyLoss,
LidarSnowManager.Instance.SnowflakeOccupancyThreshold);
LidarSnowManager.Instance.OccupancyThreshold);
rglGraphLidar.UpdateNodePointsSnowDefaults(snowNodeId,
LidarSnowManager.Instance.SnowflakesId,
LidarSnowManager.Instance.FullBeamIntensity,
0.0f); // Default, because it is not supported in AWSIM.
}

rglGraphLidar.SetActive(snowNodeId, LidarSnowManager.Instance.IsSnowEnabled);
Expand Down
20 changes: 17 additions & 3 deletions Assets/RGLUnityPlugin/Scripts/LidarSnowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class LidarSnowManager : MonoBehaviour
public delegate void OnNewConfigDelegate();
public OnNewConfigDelegate OnNewConfig;

[field: Header("Base Settings")]

[field: SerializeField]
public bool IsSnowEnabled { get; private set; } = false;

// Snow model properties
[field: SerializeField]
[field: Tooltip("The precipitation rate for snow is expressed in rate of equivalent water depth in mm per hour")]
Expand All @@ -55,11 +60,20 @@ public class LidarSnowManager : MonoBehaviour
public bool DoSimulateEnergyLoss { get; private set; } = true;
PawelLiberadzki marked this conversation as resolved.
Show resolved Hide resolved

[field: SerializeField]
[field: Tooltip("Minimal snowflake occupancy (in fraction of ray beam angle) included in energy loss calculation")]
[field: Tooltip("Minimal beam aperture occupancy (ratio) that means a hit, both for snowflakes and for original hit")]
[field: Range(0.0f, 1.0f)]
public float SnowflakeOccupancyThreshold { get; private set; } = 0.0f;
public float OccupancyThreshold { get; private set; } = 0.0f;

public bool IsSnowEnabled { get; private set; } = false;
[field: Header("Defaults")]

[field: SerializeField]
[field: Tooltip("Entity ID that is assigned to cloud points resulting from snowflake hits")]
public int SnowflakesId { get; private set; } = 268435455; // Default RGL entity ID.

[field: SerializeField]
[field: Tooltip("Initial intensity of each LiDAR laser beam, used to evaluate energy loss based on beam aperture occupancy")]
[field: Min(0.0f)]
public float FullBeamIntensity { get; private set; } = 1.0f;

private void Awake()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ public enum RGLNodeType
GAUSSIAN_NOISE_ANGULAR_HITPOINT,
GAUSSIAN_NOISE_DISTANCE,
PUBLISH_ROS2_RADARSCAN,
MULTI_RETURN_SWITCH,
};
}
33 changes: 21 additions & 12 deletions Assets/RGLUnityPlugin/Scripts/LowLevelWrappers/RGLNativeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public static class RGLNativeAPI
[DllImport("RobotecGPULidar")]
public static extern int rgl_node_raytrace_configure_beam_divergence(IntPtr node, float horizontal_divergence, float vertical_divergence);

[DllImport("RobotecGPULidar")]
public static extern int rgl_node_raytrace_configure_return_mode(IntPtr node, RGLReturnMode return_mode);

[DllImport("RobotecGPULidar")]
public static extern int rgl_node_points_format(ref IntPtr node, IntPtr fields, int field_count);

Expand Down Expand Up @@ -149,10 +152,6 @@ public static extern int rgl_node_points_udp_publish(
[DllImport("RobotecGPULidar")]
public static extern int rgl_node_gaussian_noise_distance(ref IntPtr node, float mean, float st_dev, float st_dev_rise_per_meter);


[DllImport("RobotecGPULidar")]
public static extern int rgl_node_multi_return_switch(ref IntPtr node, RGLReturnType return_type);

[DllImport("RobotecGPULidar")]
public static extern int rgl_node_points_filter_ground(ref IntPtr node, IntPtr sensor_up_vector, float ground_angle_threshold);

Expand All @@ -177,7 +176,11 @@ public static extern int rgl_node_publish_udp_objectlist(ref IntPtr node, [Marsh
[DllImport("RobotecGPULidar")]
public static extern int rgl_node_points_simulate_snow(ref IntPtr node, float min_range, float max_range, float rain_rate,
float mean_snowflake_diameter, float terminal_velocity, float density, Int32 num_channels, float beam_divergence,
bool simulate_energy_loss, float snowflake_occupancy_threshold);
float occupancy_threshold);

[DllImport("RobotecGPULidar")]
public static extern int rgl_node_points_simulate_snow_configure_defaults(IntPtr node, int snowflakes_id, float full_beam_intensity,
float snowflakes_laser_retro);

[DllImport("RobotecGPULidar")]
public static extern int rgl_graph_run(IntPtr node);
Expand Down Expand Up @@ -485,6 +488,11 @@ public static void NodeRaytraceConfigureBeamDivergence(IntPtr node, float horizo
CheckErr(rgl_node_raytrace_configure_beam_divergence(node, horizontalDivergence, verticalDivergence));
}

public static void NodeRaytraceConfigureReturnMode(IntPtr node, RGLReturnMode returnMode)
{
CheckErr(rgl_node_raytrace_configure_return_mode(node, returnMode));
}

public static void NodePointsFormat(ref IntPtr node, RGLField[] fields)
{
unsafe
Expand Down Expand Up @@ -562,11 +570,6 @@ public static void NodeGaussianNoiseDistance(ref IntPtr node, float mean, float
CheckErr(rgl_node_gaussian_noise_distance(ref node, mean, stDev, stDevRisePerMeter));
}

public static void NodeMultiReturnSwitch(ref IntPtr node, RGLReturnType return_type)
{
CheckErr(rgl_node_multi_return_switch(ref node, return_type));
}

public static void NodePointsFilterGround(ref IntPtr node, float groundAngleThreshold)
{
var upVector = IntoVec3f(new Vector3(0, 1, 0));
Expand Down Expand Up @@ -637,10 +640,16 @@ public static void NodePublishUdpObjectList(ref IntPtr node, string deviceIp, st

public static void NodePointsSimulateSnow(ref IntPtr node, float minRange, float maxRange, float rainRate,
float meanSnowflakeDiameter, float terminalVelocity, float density, Int32 numChannels, float beamDivergence,
bool doSimulateEnergyLoss, float snowflakeOccupancyThreshold)
float occupancyThreshold)
{
CheckErr(rgl_node_points_simulate_snow(ref node, minRange, maxRange, rainRate,
meanSnowflakeDiameter, terminalVelocity, density, numChannels, beamDivergence, doSimulateEnergyLoss, snowflakeOccupancyThreshold));
meanSnowflakeDiameter, terminalVelocity, density, numChannels, beamDivergence, occupancyThreshold));
}

public static void NodePointsSimulateSnowConfigureDefaults(IntPtr node, int snowflakesId, float fullBeamIntensity,
float snowflakesLaserRetro)
{
CheckErr(rgl_node_points_simulate_snow_configure_defaults(node, snowflakesId, fullBeamIntensity, snowflakesLaserRetro));
}

public static void GraphRun(IntPtr node)
Expand Down
42 changes: 25 additions & 17 deletions Assets/RGLUnityPlugin/Scripts/LowLevelWrappers/RGLNativeTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,33 @@ public enum RGLLidarModel : Int32
RGL_HESAI_PANDAR_XT32 = 8,
};

public enum RGLReturnType : Int32
{
ReturnStrongest = 1,
ReturnLast = 2,
ReturnSecond = 3,
ReturnFirst = 4,
ReturnSecondStrongest = 5,
}

public enum RGLReturnCount : Int32
{
SingleReturn = 1 << 24,
DualReturn = 2 << 24,
}

// Items have been renamed to be displayed in Unity nicer.
public enum RGLReturnMode : UInt32
public enum RGLReturnMode : Int32
{
/* RGL_RETURN_FIRST */ SingleReturnFirst = 1 << 29, // Three of the most significant bits encode the number of returns
/* RGL_RETURN_SECOND */ SingleReturnSecond,
/* RGL_RETURN_LAST */ SingleReturnLast,
/* RGL_RETURN_STRONGEST */ SingleReturnStrongest,
/* RGL_RETURN_LAST_STRONGEST */ DualReturnLastStrongest = 2 << 29,
/* RGL_RETURN_FIRST_LAST */ DualReturnFirstLast,
/* RGL_RETURN_FIRST_STRONGEST */ DualReturnFirstStrongest,
/* RGL_RETURN_STRONGEST_SECOND_STRONGEST */ DualReturnStrongestSecondStrongest,
/* RGL_RETURN_FIRST_SECOND */ DualReturnFirstSecond,
/* RGL_RETURN_FIRST */ SingleReturnFirst = RGLReturnCount.SingleReturn | RGLReturnType.ReturnFirst,
/* RGL_RETURN_SECOND */ SingleReturnSecond = RGLReturnCount.SingleReturn | RGLReturnType.ReturnSecond,
/* RGL_RETURN_LAST */ SingleReturnLast = RGLReturnCount.SingleReturn | RGLReturnType.ReturnLast,
/* RGL_RETURN_STRONGEST */ SingleReturnStrongest = RGLReturnCount.SingleReturn | RGLReturnType.ReturnStrongest,
/* RGL_RETURN_LAST_STRONGEST */ DualReturnLastStrongest = RGLReturnCount.DualReturn | RGLReturnType.ReturnLast | (RGLReturnType.ReturnStrongest << 8),
/* RGL_RETURN_FIRST_LAST */ DualReturnFirstLast = RGLReturnCount.DualReturn | RGLReturnType.ReturnFirst | (RGLReturnType.ReturnLast << 8),
/* RGL_RETURN_FIRST_STRONGEST */ DualReturnFirstStrongest = RGLReturnCount.DualReturn | RGLReturnType.ReturnFirst | (RGLReturnType.ReturnStrongest << 8),
/* RGL_RETURN_STRONGEST_SECOND_STRONGEST */ DualReturnStrongestSecondStrongest = RGLReturnCount.DualReturn | RGLReturnType.ReturnStrongest | (RGLReturnType.ReturnSecondStrongest << 8),
/* RGL_RETURN_FIRST_SECOND */ DualReturnFirstSecond = RGLReturnCount.DualReturn | RGLReturnType.ReturnFirst | (RGLReturnType.ReturnSecond << 8),
};

// Items have been renamed to be displayed in Unity nicer.
Expand Down Expand Up @@ -163,11 +178,4 @@ public enum RGLExtension : Int32
RGL_EXTENSION_SNOW = 3,
RGL_EXTENSION_COUNT
};

public enum RGLReturnType : Int32
{
RGL_RETURN_TYPE_NOT_DIVERGENT = 0,
RGL_RETURN_TYPE_FIRST = 1,
RGL_RETURN_TYPE_LAST = 2,
}
}
Loading
Loading