JointStatePublisher.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. © Siemens AG, 2017-2019
  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. using System.Collections.Generic;
  15. namespace RosSharp.RosBridgeClient
  16. {
  17. public class JointStatePublisher : UnityPublisher<MessageTypes.Sensor.JointState>
  18. {
  19. public List<JointStateReader> JointStateReaders;
  20. public string FrameId = "Unity";
  21. private MessageTypes.Sensor.JointState message;
  22. protected override void Start()
  23. {
  24. base.Start();
  25. InitializeMessage();
  26. }
  27. private void FixedUpdate()
  28. {
  29. UpdateMessage();
  30. }
  31. private void InitializeMessage()
  32. {
  33. int jointStateLength = JointStateReaders.Count;
  34. message = new MessageTypes.Sensor.JointState
  35. {
  36. header = new MessageTypes.Std.Header { frame_id = FrameId },
  37. name = new string[jointStateLength],
  38. position = new double[jointStateLength],
  39. velocity = new double[jointStateLength],
  40. effort = new double[jointStateLength]
  41. };
  42. }
  43. private void UpdateMessage()
  44. {
  45. message.header.Update();
  46. for (int i = 0; i < JointStateReaders.Count; i++)
  47. UpdateJointState(i);
  48. Publish(message);
  49. }
  50. private void UpdateJointState(int i)
  51. {
  52. JointStateReaders[i].Read(
  53. out message.name[i],
  54. out float position,
  55. out float velocity,
  56. out float effort);
  57. message.position[i] = position;
  58. message.velocity[i] = velocity;
  59. message.effort[i] = effort;
  60. }
  61. }
  62. }