LaserScanWriter.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 LaserScanWriter : MonoBehaviour
  18. {
  19. private bool isReceived = false;
  20. private float range_max;
  21. private float range_min;
  22. private float[] ranges;
  23. private Vector3[] directions;
  24. private LaserScanVisualizer[] laserScanVisualizers;
  25. private void Update()
  26. {
  27. laserScanVisualizers = GetComponents<LaserScanVisualizer>();
  28. if (isReceived)
  29. if (laserScanVisualizers != null)
  30. foreach (LaserScanVisualizer laserScanVisualizer in laserScanVisualizers)
  31. laserScanVisualizer.SetSensorData(gameObject.transform, directions, ranges, range_min, range_max);
  32. isReceived = false;
  33. }
  34. public void Write(MessageTypes.Sensor.LaserScan laserScan)
  35. {
  36. ranges = new float[laserScan.ranges.Length];
  37. directions = new Vector3[laserScan.ranges.Length];
  38. range_max = laserScan.range_max;
  39. range_min = laserScan.range_min;
  40. for (int i = 0; i < laserScan.ranges.Length; i++)
  41. {
  42. ranges[i] = laserScan.ranges[i];
  43. directions[i] = new Vector3(Mathf.Cos(laserScan.angle_min + laserScan.angle_increment * i), Mathf.Sin(laserScan.angle_min + laserScan.angle_increment * i), 0).Ros2Unity();
  44. }
  45. isReceived = true;
  46. }
  47. }
  48. }