@@ -1017,7 +1017,7 @@ private static void DeserializeGeometricPrimitive(GameObject go, JObject primiti
10171017
10181018 public XElement XML { get ; }
10191019
1020- protected void BuildURDFVisuals ( Transform visualTransform , XElement link , XElement joint ) {
1020+ protected void BuildURDFVisuals ( Transform visualTransform , XElement link ) {
10211021 // build 3d primitive if exists
10221022 MeshFilter meshFilter = visualTransform . GetComponent < MeshFilter > ( ) ;
10231023 if ( meshFilter ) {
@@ -1091,7 +1091,7 @@ protected void BuildURDFVisuals(Transform visualTransform, XElement link, XEleme
10911091 XElement origin = new XElement ( "origin" ) ;
10921092 origin . SetAttributeValue ( "xyz" , xyz . ToXMLString ( ) ) ;
10931093 origin . SetAttributeValue ( "rpy" , rpy . ToXMLString ( ) ) ;
1094-
1094+
10951095 visual . Add ( origin ) ;
10961096
10971097 visual . Add ( geometry ) ;
@@ -1126,7 +1126,7 @@ protected void BuildURDFVisuals(Transform visualTransform, XElement link, XEleme
11261126 /// <summary>
11271127 /// URDF has a "flattened" hierarchy where the hierarchy is build by URDF joints.
11281128 /// </summary>
1129- public void BuildURDFLink ( ZOSimDocumentRoot documentRoot , XElement robot , XElement joint , Transform jointTransform , ZOSimOccurrence parent = null ) {
1129+ public void BuildURDFLink ( XElement robot ) {
11301130
11311131 XElement link = new XElement ( "link" ) ;
11321132 link . SetAttributeValue ( "name" , Name ) ;
@@ -1143,16 +1143,61 @@ public void BuildURDFLink(ZOSimDocumentRoot documentRoot, XElement robot, XEleme
11431143 // go through the children of the visuals and get all the models
11441144 foreach ( Transform visualsChild in child ) {
11451145 // check if it is a primitive type (cube, sphere, cylinder, etc)
1146- BuildURDFVisuals ( visualsChild , link , joint ) ;
1146+ BuildURDFVisuals ( visualsChild , link ) ;
11471147 }
11481148 }
11491149
11501150 }
11511151
11521152 }
11531153
1154- public void BuildURDFJoints ( ZOSimDocumentRoot documentRoot , XElement robot , ZOSimOccurrence parent , Matrix4x4 worldJointMatrix ) {
1155-
1154+ protected readonly struct URDFJoint {
1155+ public URDFJoint ( ZOSimOccurrence child , ZOSimOccurrence parent , Vector3 anchor , Vector3 connectedAnchor ) {
1156+ Child = child ;
1157+ Parent = parent ;
1158+ Anchor = anchor ;
1159+ ConnectedAnchor = connectedAnchor ;
1160+ }
1161+
1162+
1163+ public ZOSimOccurrence Child {
1164+ get ;
1165+ }
1166+
1167+ public ZOSimOccurrence Parent {
1168+ get ;
1169+ }
1170+
1171+ public Vector3 Anchor {
1172+ get ;
1173+ }
1174+
1175+ public Vector3 ConnectedAnchor {
1176+ get ;
1177+ }
1178+ }
1179+
1180+ public static void BuildURDF ( XElement robot , ZOSimOccurrence simOccurrence , Matrix4x4 baseTransform ) {
1181+ List < URDFJoint > joints = new List < URDFJoint > ( ) ;
1182+
1183+ simOccurrence . BuildURDFJoints ( robot , null , baseTransform , ref joints ) ;
1184+
1185+ // build links
1186+ HashSet < ZOSimOccurrence > links = new HashSet < ZOSimOccurrence > ( ) ;
1187+ foreach ( URDFJoint joint in joints ) {
1188+ if ( links . Contains < ZOSimOccurrence > ( joint . Parent ) == false ) {
1189+ joint . Parent . BuildURDFLink ( robot ) ;
1190+ links . Add ( joint . Parent ) ;
1191+ }
1192+ if ( links . Contains < ZOSimOccurrence > ( joint . Child ) == false ) {
1193+ joint . Child . BuildURDFLink ( robot ) ;
1194+ links . Add ( joint . Child ) ;
1195+ }
1196+ }
1197+ }
1198+
1199+ protected void BuildURDFJoints ( XElement robot , ZOSimOccurrence parent , Matrix4x4 worldJointMatrix , ref List < URDFJoint > joints ) {
1200+
11561201 // if we have a parent build a joint
11571202 if ( parent ) {
11581203 XElement jointX = new XElement ( "joint" ) ;
@@ -1162,8 +1207,8 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
11621207
11631208 ZOHingeJoint hingeJoint = parent . GetComponent < ZOHingeJoint > ( ) ;
11641209 if ( hingeJoint != null ) {
1165- jointX . SetAttributeValue ( "type" , "revolute" ) ;
1166-
1210+ jointX . SetAttributeValue ( "type" , "revolute" ) ;
1211+
11671212 // create axis
11681213 Vector3 axis = hingeJoint . UnityHingeJoint . axis . Unity2Ros ( ) ;
11691214 XElement axisX = new XElement ( "axis" ) ;
@@ -1178,11 +1223,11 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
11781223 jointX . Add ( limitX ) ;
11791224
11801225 // Add the anchor position
1181- jointMatrix = parent . transform . WorldTranslationRotationMatrix ( ) ;
1182- jointMatrix = jointMatrix . AddTranslation ( hingeJoint . Anchor ) ;
1226+ jointMatrix = parent . transform . WorldTranslationRotationMatrix ( ) ;
1227+ jointMatrix = jointMatrix . AddTranslation ( hingeJoint . Anchor ) ;
11831228
11841229 // save this off as the new world joint matrix
1185- Matrix4x4 newWorldJointMatrix = jointMatrix ;
1230+ Matrix4x4 newWorldJointMatrix = jointMatrix ;
11861231
11871232 // subtract out the parent root
11881233 jointMatrix = jointMatrix * worldJointMatrix . inverse ;
@@ -1196,9 +1241,13 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
11961241 origin . SetAttributeValue ( "rpy" , rpy . ToXMLString ( ) ) ;
11971242 jointX . Add ( origin ) ;
11981243
1244+ URDFJoint joint = new URDFJoint ( this , parent , hingeJoint . Anchor , hingeJoint . ConnectedAnchor ) ;
1245+ joints . Add ( joint ) ;
1246+
11991247
12001248 } else { // children of the parent even without an explicit joint are "fixed" joints
1201- jointX . SetAttributeValue ( "type" , "fixed" ) ;
1249+
1250+ jointX . SetAttributeValue ( "type" , "fixed" ) ;
12021251 jointMatrix = this . transform . WorldTranslationRotationMatrix ( ) * parent . transform . WorldTranslationRotationMatrix ( ) . inverse ;
12031252
12041253 Vector3 xyz = jointMatrix . Position ( ) . Unity2Ros ( ) ;
@@ -1209,11 +1258,15 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
12091258 origin . SetAttributeValue ( "rpy" , rpy . ToXMLString ( ) ) ;
12101259 jointX . Add ( origin ) ;
12111260
1261+ URDFJoint joint = new URDFJoint ( this , parent , jointMatrix . Position ( ) , jointMatrix . Position ( ) ) ;
1262+ joints . Add ( joint ) ;
1263+
1264+
12121265 }
12131266
1214-
1267+
12151268 // build origin
1216-
1269+
12171270 // Vector3 xyz = jointTransform.localPosition.Unity2Ros();
12181271 // Vector3 rpy = new Vector3(-jointTransform.localEulerAngles.z * Mathf.Deg2Rad,
12191272 // jointTransform.localEulerAngles.x * Mathf.Deg2Rad,
@@ -1241,17 +1294,15 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
12411294 jointX . Add ( childX ) ;
12421295
12431296
1244-
12451297 // build the links
1246- parent . BuildURDFLink ( documentRoot , robot , jointX , null , parent ) ;
1247- this . BuildURDFLink ( documentRoot , robot , jointX , null , parent ) ;
1298+ // parent.BuildURDFLink(documentRoot, robot, parent);
1299+ // this.BuildURDFLink(documentRoot, robot, parent);
12481300 }
1249-
12501301 // recursively go through the children
12511302 foreach ( Transform child in transform ) {
12521303 ZOSimOccurrence simOccurrence = child . GetComponent < ZOSimOccurrence > ( ) ;
12531304 if ( simOccurrence ) {
1254- simOccurrence . BuildURDFJoints ( documentRoot , robot , this , worldJointMatrix ) ;
1305+ simOccurrence . BuildURDFJoints ( robot , this , worldJointMatrix , ref joints ) ;
12551306 }
12561307 }
12571308
0 commit comments