JointStatePatcher.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. using UnityEngine;
  15. using System.Collections.Generic;
  16. using RosSharp.Urdf;
  17. namespace RosSharp.RosBridgeClient
  18. {
  19. public class JointStatePatcher : MonoBehaviour
  20. {
  21. public UrdfRobot UrdfRobot;
  22. public void SetPublishJointStates(bool publish)
  23. {
  24. if (publish)
  25. {
  26. JointStatePublisher jointStatePublisher = transform.AddComponentIfNotExists<JointStatePublisher>();
  27. jointStatePublisher.JointStateReaders = new List<JointStateReader>();
  28. foreach (UrdfJoint urdfJoint in UrdfRobot.GetComponentsInChildren<UrdfJoint>())
  29. {
  30. if(urdfJoint.JointType != UrdfJoint.JointTypes.Fixed)
  31. jointStatePublisher.JointStateReaders.Add(urdfJoint.transform.AddComponentIfNotExists<JointStateReader>());
  32. }
  33. }
  34. else
  35. {
  36. GetComponent<JointStatePublisher>()?.JointStateReaders.Clear();
  37. foreach (JointStateReader reader in UrdfRobot.GetComponentsInChildren<JointStateReader>())
  38. reader.transform.DestroyImmediateIfExists<JointStateReader>();
  39. }
  40. }
  41. public void SetSubscribeJointStates(bool subscribe)
  42. {
  43. if (subscribe)
  44. {
  45. JointStateSubscriber jointStateSubscriber = transform.AddComponentIfNotExists<JointStateSubscriber>();
  46. jointStateSubscriber.JointStateWriters = new List<JointStateWriter>();
  47. jointStateSubscriber.JointNames = new List<string>();
  48. foreach (UrdfJoint urdfJoint in UrdfRobot.GetComponentsInChildren<UrdfJoint>()) {
  49. if (urdfJoint.JointType != UrdfJoint.JointTypes.Fixed)
  50. {
  51. jointStateSubscriber.JointStateWriters.Add(urdfJoint.transform.AddComponentIfNotExists<JointStateWriter>());
  52. jointStateSubscriber.JointNames.Add(urdfJoint.JointName);
  53. }
  54. }
  55. }
  56. else
  57. {
  58. GetComponent<JointStateSubscriber>()?.JointStateWriters.Clear();
  59. GetComponent<JointStateSubscriber>()?.JointNames.Clear();
  60. foreach (JointStateWriter writer in UrdfRobot.GetComponentsInChildren<JointStateWriter>())
  61. writer.transform.DestroyImmediateIfExists<JointStateWriter>();
  62. }
  63. }
  64. }
  65. }