LaserScanPublisher.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. © Siemens AG, 2018
  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 LaserScanPublisher : UnityPublisher<MessageTypes.Sensor.LaserScan>
  18. {
  19. public LaserScanReader laserScanReader;
  20. public string FrameId = "Unity";
  21. private MessageTypes.Sensor.LaserScan message;
  22. private float scanPeriod;
  23. private float previousScanTime = 0;
  24. protected override void Start()
  25. {
  26. base.Start();
  27. InitializeMessage();
  28. }
  29. private void FixedUpdate()
  30. {
  31. if (Time.realtimeSinceStartup >= previousScanTime + scanPeriod)
  32. {
  33. UpdateMessage();
  34. previousScanTime = Time.realtimeSinceStartup;
  35. }
  36. }
  37. private void InitializeMessage()
  38. {
  39. scanPeriod = (float)laserScanReader.samples / (float)laserScanReader.update_rate;
  40. message = new MessageTypes.Sensor.LaserScan
  41. {
  42. header = new MessageTypes.Std.Header { frame_id = FrameId },
  43. angle_min = laserScanReader.angle_min,
  44. angle_max = laserScanReader.angle_max,
  45. angle_increment = laserScanReader.angle_increment,
  46. time_increment = laserScanReader.time_increment,
  47. range_min = laserScanReader.range_min,
  48. range_max = laserScanReader.range_max,
  49. ranges = laserScanReader.ranges,
  50. intensities = laserScanReader.intensities
  51. };
  52. }
  53. private void UpdateMessage()
  54. {
  55. message.header.Update();
  56. message.ranges = laserScanReader.Scan();
  57. Publish(message);
  58. }
  59. }
  60. }