Skip to content

Commit cf296d7

Browse files
committed
Initial commit for Unity driven URDF Importer changes.
1 parent f0feb2f commit cf296d7

File tree

105 files changed

+3098
-1520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+3098
-1520
lines changed

UnityEditorScripts/Extensions.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityEditorScripts/Extensions/HeaderExtensions.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

UnityEditorScripts/Extensions/TransformExtensions.cs

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
/*
2-
© Siemens AG, 2017-2018
3-
Author: Dr. Martin Bischoff (martin.bischoff@siemens.com)
4-
5-
Licensed under the Apache License, Version 2.0 (the "License");
6-
you may not use this file except in compliance with the License.
7-
You may obtain a copy of the License at
8-
<http://www.apache.org/licenses/LICENSE-2.0>.
9-
Unless required by applicable law or agreed to in writing, software
10-
distributed under the License is distributed on an "AS IS" BASIS,
11-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
See the License for the specific language governing permissions and
13-
limitations under the License.
14-
*/
1+

152

163
using System;
174
using System.IO;
@@ -83,6 +70,15 @@ public static void MoveChildTransformToParent(this Transform parent, bool transf
8370
childTransform.localScale = Vector3.one;
8471
}
8572

73+
public static double[] ToRosRPY(this Vector3 transform)
74+
{
75+
Vector3 rpyVector = new Vector3(
76+
-transform.z * Mathf.Deg2Rad,
77+
transform.x * Mathf.Deg2Rad,
78+
-transform.y * Mathf.Deg2Rad);
79+
return rpyVector == Vector3.zero ? null : rpyVector.ToRoundedDoubleArray();
80+
}
81+
8682
public static Vector3 Ros2Unity(this Vector3 vector3)
8783
{
8884
return new Vector3(-vector3.y, vector3.z, vector3.x);
@@ -131,5 +127,103 @@ public static string SetSeparatorChar(this string path)
131127
{
132128
return path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
133129
}
130+
131+
/// <summary>
132+
/// This function is written to compare two double arrays.
133+
/// It returns true if each number respective number in the array is equal to within a delta amount to each other.
134+
/// Vector3 == Vector3 only allows for a difference of 1e-5
135+
/// </summary>
136+
/// <param name="array">First double array</param>
137+
/// <param name="array2">Second Double array</param>
138+
/// <param name="delta">Allowed delta between numbers allowed</param>
139+
/// <returns></returns>
140+
public static bool DoubleDeltaCompare(this double[] array, double[] array2, double delta)
141+
{
142+
if (array.Length != array2.Length)
143+
return false;
144+
for (int i = 0; i < array.Length ; i++)
145+
{
146+
if ((array[i] >= array2[i] - delta) && (array[i] <= array2[i] + delta))
147+
continue;
148+
else
149+
return false;
150+
}
151+
return true;
152+
}
153+
154+
/// <summary>
155+
/// This function is written to compare two Vector3.
156+
/// It returns true if each number respective number in the array is equal to within a delta amount to each other.
157+
/// </summary>
158+
/// <param name="array">First Vector3</param>
159+
/// <param name="array2">Second Vector3</param>
160+
/// <param name="delta">Allowed delta between numbers allowed</param>
161+
/// <returns></returns>
162+
public static bool VectorEqualDelta(this Vector3 source, Vector3 exported, double delta)
163+
{
164+
165+
return Vector3.SqrMagnitude(source - exported) < delta;
166+
}
167+
168+
public static bool EqualsDelta(this double first, double second, double delta)
169+
{
170+
if (Math.Abs(first - second) <= Math.Abs(delta * first))
171+
return true;
172+
else
173+
return false;
174+
}
175+
176+
/// <summary>
177+
/// Implments element-wise subtraction between two matrices
178+
/// </summary>
179+
/// <param name="first"></param>
180+
/// <param name="second"></param>
181+
/// <returns></returns>
182+
public static Matrix4x4 Subtract(this Matrix4x4 first, Matrix4x4 second)
183+
{
184+
Matrix4x4 result = new Matrix4x4();
185+
for(int i = 0; i < 4; i++)
186+
{
187+
for(int j = 0; j < 4; j++)
188+
{
189+
result[i, j] = first[i, j] - second[i, j];
190+
}
191+
}
192+
return result;
193+
}
194+
195+
/// <summary>
196+
/// Divides each float number of a Matrix4x4 type with a float scalar.
197+
/// The function is added as it does not support element-wise division by float
198+
/// https://docs.unity3d.com/ScriptReference/Matrix4x4.html
199+
/// </summary>
200+
/// <param name="first">Matrix divisor</param>
201+
/// <param name="second">Float dividend</param>
202+
/// <returns></returns>
203+
public static Matrix4x4 FloatDivide(this Matrix4x4 first, float second)
204+
{
205+
Matrix4x4 result = new Matrix4x4();
206+
for (int i = 0; i < 4; i++)
207+
{
208+
for (int j = 0; j < 4; j++)
209+
{
210+
result[i, j] = first[i, j] / second;
211+
}
212+
}
213+
return result;
214+
}
215+
216+
/// <summary>
217+
/// Function overload of ToString method of Matrix3x3 to achieve unrounded print of the matrix
218+
/// </summary>
219+
/// <param name="first">Matrix to be printed</param>
220+
/// <returns></returns>
221+
public static string ToString(this Matrix4x4 first)
222+
{
223+
return ("Matrix : " + first[0, 0] + " " + first[0, 1] + " " + first[0, 2] + " " + first[0, 3] + " " +
224+
first[1, 0] + " " + first[1, 1] + " " + first[1, 2] + " " + first[1, 3] + " " +
225+
first[2, 0] + " " + first[2, 1] + " " + first[2, 2] + " " + first[2, 3] + " " +
226+
first[3, 0] + " " + first[3, 1] + " " + first[3, 2] + " " + first[3, 3]);
227+
}
134228
}
135229
}

UnityEditorScripts/Urdf.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityEditorScripts/Urdf/Controller.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace RosSharp.Control
6+
{
7+
public enum RotationDirection { None = 0, Positive = 1, Negative = -1 };
8+
public enum ControlType { PositionControl};
9+
10+
public class Controller : MonoBehaviour
11+
{
12+
13+
private ArticulationBody[] articulationChain;
14+
private Color[] prevColor;
15+
private int previousIndex;
16+
17+
public ControlType control = ControlType.PositionControl;
18+
public int selectedIndex;
19+
public string jointName;
20+
public float stiffness;
21+
public float damping;
22+
public float R, G, B, Alpha;
23+
public float speed = 5f; // Units: degree/s
24+
public float torque = 100f; // Units: Nm or N
25+
public float acceleration = 5f;// Units: m/s^2 / degree/s^2
26+
27+
void Start()
28+
{
29+
previousIndex = selectedIndex = 1;
30+
this.gameObject.AddComponent<FKRobot>();
31+
articulationChain = this.GetComponentsInChildren<ArticulationBody>();
32+
33+
foreach (ArticulationBody joint in articulationChain)
34+
{
35+
joint.gameObject.AddComponent<JointControl>();
36+
joint.jointFriction = 10;
37+
joint.angularDamping = 10;
38+
}
39+
jointName = articulationChain[selectedIndex].name;
40+
StoreColors(selectedIndex);
41+
B = G = 0;
42+
Alpha = R = 1;
43+
}
44+
45+
void Update()
46+
{
47+
bool SelectionInput1 = Input.GetKeyDown("right");
48+
bool SelectionInput2 = Input.GetKeyDown("left");
49+
50+
UpdateDirection(selectedIndex);
51+
52+
if (SelectionInput2)
53+
{
54+
if (selectedIndex == 1)
55+
{
56+
selectedIndex = articulationChain.Length - 1;
57+
}
58+
else
59+
{
60+
selectedIndex = selectedIndex - 1;
61+
}
62+
Highlight(selectedIndex);
63+
}
64+
else if (SelectionInput1)
65+
{
66+
if (selectedIndex == articulationChain.Length - 1)
67+
{
68+
selectedIndex = 1;
69+
}
70+
else
71+
{
72+
selectedIndex = selectedIndex + 1;
73+
}
74+
Highlight(selectedIndex);
75+
}
76+
77+
UpdateDirection(selectedIndex);
78+
}
79+
80+
/// <summary>
81+
/// Highlights the color of the robot by changing the color of the part to a color set by the user in the inspector window
82+
/// </summary>
83+
/// <param name="selectedIndex">Index of the link selected in the Articulation Chain</param>
84+
private void Highlight(int selectedIndex)
85+
{
86+
if(selectedIndex == previousIndex)
87+
{
88+
return;
89+
}
90+
91+
Renderer[] previousMaterialList = articulationChain[previousIndex].transform.GetChild(0).GetComponentsInChildren<Renderer>();
92+
93+
for (int counter = 0; counter < previousMaterialList.Length; counter++)
94+
{
95+
previousMaterialList[counter].material.color = prevColor[counter];
96+
}
97+
jointName = articulationChain[selectedIndex].name;
98+
Renderer[] materialList = articulationChain[selectedIndex].transform.GetChild(0).GetComponentsInChildren<Renderer>();
99+
100+
StoreColors(selectedIndex);
101+
102+
foreach (var mesh in materialList)
103+
{
104+
Color tempColor = new Color(R, G, B, Alpha);
105+
mesh.material.color = tempColor;
106+
}
107+
108+
}
109+
110+
/// <summary>
111+
/// Sets the direction of movement of the joint on every update
112+
/// </summary>
113+
/// <param name="jointIndex">Index of the link selected in the Articulation Chain</param>
114+
private void UpdateDirection(int jointIndex)
115+
{
116+
float moveDirection = Input.GetAxis("Vertical");
117+
JointControl current = articulationChain[jointIndex].GetComponent<JointControl>();
118+
if (previousIndex != jointIndex)
119+
{
120+
JointControl previous = articulationChain[previousIndex].GetComponent<JointControl>();
121+
previous.direction = RotationDirection.None;
122+
previousIndex = jointIndex;
123+
}
124+
125+
if (current.controltype != control)
126+
UpdateControlType(current);
127+
128+
if (moveDirection > 0)
129+
{
130+
current.direction = RotationDirection.Positive;
131+
}
132+
else if (moveDirection < 0)
133+
{
134+
current.direction = RotationDirection.Negative;
135+
}
136+
else
137+
{
138+
current.direction = RotationDirection.None;
139+
}
140+
141+
142+
}
143+
144+
/// <summary>
145+
/// Stores original color of the part being highlighted
146+
/// </summary>
147+
/// <param name="index">Index of the part in the Articulation chain</param>
148+
private void StoreColors(int index)
149+
{
150+
Renderer[] materialLists = articulationChain[index].transform.GetChild(0).GetComponentsInChildren<Renderer>();
151+
prevColor = new Color[materialLists.Length];
152+
for (int counter = 0; counter < materialLists.Length; counter++)
153+
{
154+
prevColor[counter] = materialLists[counter].sharedMaterial.GetColor("_Color");
155+
}
156+
}
157+
158+
public void UpdateControlType(JointControl joint)
159+
{
160+
joint.controltype = control;
161+
if(control == ControlType.PositionControl)
162+
{
163+
ArticulationDrive drive = joint.joint.xDrive;
164+
drive.stiffness = stiffness;
165+
drive.damping = 0;
166+
joint.joint.xDrive = drive;
167+
}
168+
}
169+
}
170+
}

UnityEditorScripts/Extensions/HeaderExtensions.cs.meta renamed to UnityEditorScripts/Urdf/Controller/Controller.cs.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)