JoyPublisher.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. © Siemens AG, 2017-2018
  3. Author: Dr. Martin Bischoff (martin.bischoff@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. namespace RosSharp.RosBridgeClient
  15. {
  16. public class JoyPublisher : UnityPublisher<MessageTypes.Sensor.Joy>
  17. {
  18. private JoyAxisReader[] JoyAxisReaders;
  19. private JoyButtonReader[] JoyButtonReaders;
  20. public string FrameId = "Unity";
  21. private MessageTypes.Sensor.Joy message;
  22. protected override void Start()
  23. {
  24. base.Start();
  25. InitializeGameObject();
  26. InitializeMessage();
  27. }
  28. private void Update()
  29. {
  30. UpdateMessage();
  31. }
  32. private void InitializeGameObject()
  33. {
  34. JoyAxisReaders = GetComponents<JoyAxisReader>();
  35. JoyButtonReaders = GetComponents<JoyButtonReader>();
  36. }
  37. private void InitializeMessage()
  38. {
  39. message = new MessageTypes.Sensor.Joy();
  40. message.header.frame_id = FrameId;
  41. message.axes = new float[JoyAxisReaders.Length];
  42. message.buttons = new int[JoyButtonReaders.Length];
  43. }
  44. private void UpdateMessage()
  45. {
  46. message.header.Update();
  47. for (int i = 0; i < JoyAxisReaders.Length; i++)
  48. message.axes[i] = JoyAxisReaders[i].Read();
  49. for (int i = 0; i < JoyButtonReaders.Length; i++)
  50. message.buttons[i] = (JoyButtonReaders[i].Read() ? 1 : 0);
  51. Publish(message);
  52. }
  53. }
  54. }