Skip to content

Commit 152d409

Browse files
committed
Publish URDF to robot_description.
1 parent c48d894 commit 152d409

File tree

7 files changed

+1153
-33
lines changed

7 files changed

+1153
-33
lines changed

Editor/CustomEditors/ZOHingeJointEditor.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@ public class ZOHingeJointEditor : UnityEditor.Editor {
1313
public override void OnInspectorGUI() {
1414
DrawPropertiesExcluding(serializedObject, _dontIncludeMe);
1515
serializedObject.ApplyModifiedProperties();
16-
17-
// DrawDefaultInspector();
18-
19-
// ZO.Physics.ZOHingeJoint hingeJoint = (ZO.Physics.ZOHingeJoint)target;
20-
21-
// UnityEngine.HingeJoint unityHinge = hingeJoint.UnityHinge;
22-
23-
// serializedObject.Update();
24-
// SerializedProperty serializedProperty = serializedObject.FindProperty("_hinge");
25-
// EditorGUILayout.PropertyField(serializedProperty);
26-
// serializedObject.ApplyModifiedProperties();
2716
}
2817
}
2918

Runtime/Scripts/Document/ZOSimDocumentRoot.cs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
using System.Collections.Generic;
55
using UnityEngine;
66
using Newtonsoft.Json.Linq;
7-
using Newtonsoft.Json;
8-
using ZO.ROS.Publisher;
9-
using ZO.ROS.Controllers;
10-
using ZO.ROS.Unity.Service;
11-
using ZO.Math;
7+
using ZO.ROS;
8+
using ZO.ROS.Unity;
129
using ZO.ImportExport;
1310
namespace ZO.Document {
1411

@@ -26,25 +23,21 @@ namespace ZO.Document {
2623
/// ZOSimOccurrence:
2724
/// ```
2825
///
29-
/// It is primarily responsible for serialization of the Unity structure to ZoSim JSON document and
30-
/// deserialization from ZoSim JSON document to a Unity structure.
3126
/// </summary>
3227
[ExecuteAlways]
33-
public class ZOSimDocumentRoot : MonoBehaviour {
28+
public class ZOSimDocumentRoot : ZOROSUnityGameObjectBase {
3429

3530

31+
public bool _publishRobotDescription = false;
32+
3633
/// <summary>
37-
/// The ZOSim JSON document path.
34+
/// Publish URDF robot description to ROS parameter server /robot_description
3835
/// </summary>
3936
/// <value></value>
40-
private string _zoSimDocumentFilePath;
41-
public string ZOSimDocumentFilePath {
42-
get { return _zoSimDocumentFilePath; }
43-
set { _zoSimDocumentFilePath = value; }
37+
public bool PublishRobotDescription {
38+
get => _publishRobotDescription;
39+
set => _publishRobotDescription = value;
4440
}
45-
46-
47-
4841

4942

5043
/// <summary>
@@ -55,8 +48,16 @@ public string ZOSimDocumentFilePath {
5548
/// <value></value>
5649
public string Name {
5750

58-
get => gameObject.name;
59-
set => gameObject.name = value;
51+
get {
52+
if (string.IsNullOrEmpty(_name)) {
53+
_name = gameObject.name;
54+
}
55+
return _name;
56+
}
57+
set {
58+
gameObject.name = value;
59+
_name = value;
60+
}
6061
}
6162

6263

@@ -69,14 +70,34 @@ public string Name {
6970
/// <returns>ZOSimOccurrence</returns>
7071
public ZOSimOccurrence GetOccurrence(string occurrenceName) {
7172
ZOSimOccurrence[] children = this.transform.GetComponentsInChildren<ZOSimOccurrence>(true);
72-
foreach(ZOSimOccurrence child in children) {
73+
foreach (ZOSimOccurrence child in children) {
7374
if (child.Name == occurrenceName) {
7475
return child;
7576
}
7677
}
7778
return null;
7879
}
7980

81+
public override void OnROSBridgeConnected(ZOROSUnityManager rosUnityManager) {
82+
Debug.Log("INFO: ZOSimDocumentRoot::OnROSBridgeConnected");
83+
84+
if (PublishRobotDescription == true) {
85+
ExecuteInUpdate = () => {
86+
Debug.Log("INFO: ZOSimDocumentRoot publishing robot description.");
87+
ZOExportURDF exportURDF = new ZOExportURDF();
88+
XDocument urdfXML = exportURDF.BuildURDF(this);
89+
string urdfString = "<?xml version='1.0' encoding='utf-8'?>\n" + urdfXML.ToString();
90+
ZOROSAPI.SetParam("robot_description", urdfString, Name);
91+
92+
};
93+
94+
}
95+
96+
}
97+
98+
public override void OnROSBridgeDisconnected(ZOROSUnityManager rosUnityManager) {
99+
Debug.Log("INFO: ZOSimDocumentRoot::OnROSBridgeDisconnected");
100+
}
80101

81102

82103

Runtime/Scripts/ROS/Unity/ZOROSUnityGameObjectBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,6 @@ protected override void ZOOnDestroy() {
133133
#endregion // ZOROSUnityInterface
134134

135135

136+
136137
}
137138
}

Runtime/Scripts/Util/GameObject/ZOGameObjectBase.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public float UpdateTimeSeconds {
3737
if (UpdateRateHz == 0.0f) {
3838
return 0.0f; // avoid divide by zero
3939
}
40-
40+
4141
return 1.0f / UpdateRateHz;
4242
}
4343
}
@@ -67,6 +67,23 @@ public double NextFixedUpdateTime {
6767

6868
private static float _nextUpdateTimeOffset = 0.0f;
6969

70+
#region Threading
71+
// See: https://stackoverflow.com/questions/41330771/use-unity-api-from-another-thread-or-call-a-function-in-the-main-thread
72+
73+
private List<System.Action> _actionQueuesUpdateFunc = new List<System.Action>();
74+
private List<System.Action> _actionCopiedQueueUpdateFunc = new List<System.Action>();
75+
76+
77+
public System.Action ExecuteInUpdate {
78+
set {
79+
_actionQueuesUpdateFunc.Add(value);
80+
}
81+
}
82+
83+
84+
#endregion // Threading
85+
86+
7087
#if UNITY_EDITOR
7188

7289
private ZOFrequencyCounter _updateHzCounter = new ZOFrequencyCounter(ZOFrequencyCounter.TimeSourceType.RENDER_UPDATE);
@@ -105,7 +122,7 @@ protected virtual void ZOOnEnable() { }
105122
/// <summary>
106123
/// OnValidate function for Zero Sim Obects
107124
/// </summary>
108-
protected virtual void ZOOnValidate() {}
125+
protected virtual void ZOOnValidate() { }
109126

110127

111128
/// <summary>
@@ -195,6 +212,18 @@ private void OnGUI() {
195212
/// IMPORTANT: do not redefine this method in child classes, use ZOUpdate() instead.
196213
/// </summary>
197214
void Update() {
215+
216+
// run queued actions
217+
_actionCopiedQueueUpdateFunc.Clear();
218+
lock (_actionQueuesUpdateFunc) {
219+
_actionCopiedQueueUpdateFunc.AddRange(_actionQueuesUpdateFunc);
220+
_actionQueuesUpdateFunc.Clear();
221+
}
222+
223+
foreach (System.Action action in _actionCopiedQueueUpdateFunc) {
224+
action.Invoke();
225+
}
226+
198227
ZOUpdate();
199228

200229
if (Time.time >= _nextUpdateTime) {

Samples~/ZeroSimSamples/Scenes/URDF_test.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3497,7 +3497,7 @@ Transform:
34973497
m_GameObject: {fileID: 1357432953}
34983498
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
34993499
m_LocalPosition: {x: 0, y: 0, z: 0}
3500-
m_LocalScale: {x: 0.01, y: 0.01, z: 0.01}
3500+
m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
35013501
m_Children: []
35023502
m_Father: {fileID: 2050636604}
35033503
m_RootOrder: 0

0 commit comments

Comments
 (0)