Skip to content

Commit 983d064

Browse files
committed
urdf joint position calculations are now correct
1 parent 46eb194 commit 983d064

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

Runtime/Scripts/Document/ZOSimDocumentRoot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using ZO.ROS.Publisher;
99
using ZO.ROS.Controllers;
1010
using ZO.ROS.Unity.Service;
11-
11+
using ZO.Math;
1212
namespace ZO.Document {
1313

1414
/// <summary>
@@ -261,7 +261,7 @@ public void ExportURDF(string exportDirectory) {
261261
foreach (Transform child in transform) {
262262
ZOSimOccurrence simOccurence = child.GetComponent<ZOSimOccurrence>();
263263
if (simOccurence) {
264-
simOccurence.BuildURDFJoints(this, robot);
264+
simOccurence.BuildURDFJoints(this, robot, null, this.transform.WorldTranslationRotationMatrix());
265265

266266
}
267267
}

Runtime/Scripts/Document/ZOSimOccurrence.cs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,14 +1151,14 @@ public void BuildURDFLink(ZOSimDocumentRoot documentRoot, XElement robot, XEleme
11511151

11521152
}
11531153

1154-
public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSimOccurrence parent = null) {
1154+
public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSimOccurrence parent, Matrix4x4 worldJointMatrix) {
11551155

11561156
// if we have a parent build a joint
11571157
if (parent) {
11581158
XElement jointX = new XElement("joint");
11591159
jointX.SetAttributeValue("name", $"{parent.Name}_to_{this.Name}");
11601160
// Transform jointTransform = this.transform;
1161-
Matrix4x4 jointMatrix;
1161+
Matrix4x4 jointMatrix = Matrix4x4.identity;
11621162

11631163
ZOHingeJoint hingeJoint = parent.GetComponent<ZOHingeJoint>();
11641164
if (hingeJoint != null) {
@@ -1177,11 +1177,38 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
11771177
limitX.SetAttributeValue("velocity", 3.14f); // HACK
11781178
jointX.Add(limitX);
11791179

1180-
jointMatrix = this.transform.WorldTranslationRotationMatrix() * parent.transform.WorldTranslationRotationMatrix().inverse;
1180+
// Add the anchor position
1181+
jointMatrix = parent.transform.WorldTranslationRotationMatrix();
1182+
jointMatrix = jointMatrix.AddTranslation(hingeJoint.Anchor);
1183+
1184+
// save this off as the new world joint matrix
1185+
Matrix4x4 newWorldJointMatrix = jointMatrix;
1186+
1187+
// subtract out the parent root
1188+
jointMatrix = jointMatrix * worldJointMatrix.inverse;
1189+
worldJointMatrix = newWorldJointMatrix;
1190+
1191+
Vector3 xyz = jointMatrix.Position().Unity2Ros();
1192+
Vector3 rpy = jointMatrix.rotation.Unity2RosRollPitchYaw();
1193+
1194+
XElement origin = new XElement("origin");
1195+
origin.SetAttributeValue("xyz", xyz.ToXMLString());
1196+
origin.SetAttributeValue("rpy", rpy.ToXMLString());
1197+
jointX.Add(origin);
1198+
11811199

11821200
} else { // children of the parent even without an explicit joint are "fixed" joints
11831201
jointX.SetAttributeValue("type", "fixed");
11841202
jointMatrix = this.transform.WorldTranslationRotationMatrix() * parent.transform.WorldTranslationRotationMatrix().inverse;
1203+
1204+
Vector3 xyz = jointMatrix.Position().Unity2Ros();
1205+
Vector3 rpy = jointMatrix.rotation.Unity2RosRollPitchYaw();
1206+
1207+
XElement origin = new XElement("origin");
1208+
origin.SetAttributeValue("xyz", xyz.ToXMLString());
1209+
origin.SetAttributeValue("rpy", rpy.ToXMLString());
1210+
jointX.Add(origin);
1211+
11851212
}
11861213

11871214

@@ -1193,15 +1220,15 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
11931220
// -jointTransform.localEulerAngles.y * Mathf.Deg2Rad);
11941221

11951222
// remember everything is relative to parent.
1196-
// TODO: THOUGH MAYBE IT IS RELATIVE TO PARENT JOINT WHICH MAY NOT BE THE SAME!!!
1197-
1198-
Vector3 xyz = jointMatrix.Position().Unity2Ros();
1199-
Vector3 rpy = jointMatrix.rotation.Unity2RosRollPitchYaw();
1223+
// TODO: THOUGH MAYBE IT IS RELATIVE TO PARENT JOINT WHICH MAY NOT BE THE SAME!!!
1224+
1225+
// Vector3 xyz = jointMatrix.Position().Unity2Ros();
1226+
// Vector3 rpy = jointMatrix.rotation.Unity2RosRollPitchYaw();
12001227

1201-
XElement origin = new XElement("origin");
1202-
origin.SetAttributeValue("xyz", xyz.ToXMLString());
1203-
origin.SetAttributeValue("rpy", rpy.ToXMLString());
1204-
jointX.Add(origin);
1228+
// XElement origin = new XElement("origin");
1229+
// origin.SetAttributeValue("xyz", xyz.ToXMLString());
1230+
// origin.SetAttributeValue("rpy", rpy.ToXMLString());
1231+
// jointX.Add(origin);
12051232

12061233
robot.Add(jointX);
12071234

@@ -1224,7 +1251,7 @@ public void BuildURDFJoints(ZOSimDocumentRoot documentRoot, XElement robot, ZOSi
12241251
foreach (Transform child in transform) {
12251252
ZOSimOccurrence simOccurrence = child.GetComponent<ZOSimOccurrence>();
12261253
if (simOccurrence) {
1227-
simOccurrence.BuildURDFJoints(documentRoot, robot, this);
1254+
simOccurrence.BuildURDFJoints(documentRoot, robot, this, worldJointMatrix);
12281255
}
12291256
}
12301257

0 commit comments

Comments
 (0)