LaserScanReader.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. © Siemens AG, 2018-2019
  3. Author: Berkay Alp Cakal (berkay_alp.cakal.ct@siemens.com)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. <http://www.apache.org/licenses/LICENSE-2.0>.
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. using UnityEngine;
  15. namespace RosSharp.RosBridgeClient
  16. {
  17. public class LaserScanReader : MonoBehaviour
  18. {
  19. private Ray[] rays;
  20. private RaycastHit[] raycastHits;
  21. private Vector3[] directions;
  22. private LaserScanVisualizer[] laserScanVisualizers;
  23. public int samples = 360;
  24. public int update_rate = 1800;
  25. public float angle_min = 0;
  26. public float angle_max = 6.28f;
  27. public float angle_increment = 0.0174533f;
  28. public float time_increment = 0;
  29. public float scan_time = 0;
  30. public float range_min = 0.12f;
  31. public float range_max = 3.5f;
  32. public float[] ranges;
  33. public float[] intensities;
  34. public void Start()
  35. {
  36. directions = new Vector3[samples];
  37. ranges = new float[samples];
  38. intensities = new float[samples];
  39. rays = new Ray[samples];
  40. raycastHits = new RaycastHit[samples];
  41. }
  42. public float[] Scan()
  43. {
  44. MeasureDistance();
  45. laserScanVisualizers = GetComponents<LaserScanVisualizer>();
  46. if (laserScanVisualizers != null)
  47. foreach (LaserScanVisualizer laserScanVisualizer in laserScanVisualizers)
  48. laserScanVisualizer.SetSensorData(gameObject.transform, directions, ranges, range_min, range_max);
  49. return ranges;
  50. }
  51. private void MeasureDistance()
  52. {
  53. rays = new Ray[samples];
  54. raycastHits = new RaycastHit[samples];
  55. ranges = new float[samples];
  56. for (int i = 0; i < samples; i++)
  57. {
  58. rays[i] = new Ray(transform.position, GetRayRotation(i) * transform.forward);
  59. directions[i] = Quaternion.Euler(-transform.rotation.eulerAngles) * rays[i].direction;
  60. raycastHits[i] = new RaycastHit();
  61. if (Physics.Raycast(rays[i], out raycastHits[i], range_max))
  62. if (raycastHits[i].distance >= range_min && raycastHits[i].distance <= range_max)
  63. ranges[i] = raycastHits[i].distance;
  64. }
  65. }
  66. private Quaternion GetRayRotation(int sample) {
  67. float eulerAngleInRadians = angle_min + (angle_increment * sample);
  68. float eulerAngleInDegrees = eulerAngleInRadians * 180 / Mathf.PI;
  69. return Quaternion.Euler(new Vector3(0, eulerAngleInDegrees, 0));
  70. }
  71. }
  72. }