@@ -23,62 +23,102 @@ namespace RosSharp.Urdf
23
23
[ RequireComponent ( typeof ( Rigidbody ) ) ]
24
24
public class UrdfInertial : MonoBehaviour
25
25
{
26
+ [ SerializeField ] private Rigidbody _rigidbody ;
26
27
public bool DisplayInertiaGizmo ;
27
28
28
- public bool UseUrdfData ;
29
+ public enum RigidbodyDataSource { Urdf , Unity , Manual } ;
30
+ public RigidbodyDataSource rigidbodyDataSource ;
31
+
32
+ public float Mass ;
29
33
public Vector3 CenterOfMass ;
30
34
public Vector3 InertiaTensor ;
31
35
public Quaternion InertiaTensorRotation ;
32
36
37
+ public float UrdfMass ;
38
+ public Vector3 UrdfCenterOfMass ;
39
+ public Vector3 UrdfInertiaTensor ;
40
+ public Quaternion UrdfInertiaTensorRotation ;
41
+
33
42
private const int RoundDigits = 10 ;
34
43
private const float MinInertia = 1e-6f ;
35
44
45
+ private bool isCreated ;
46
+
36
47
public static void Create ( GameObject linkObject , Link . Inertial inertial = null )
37
48
{
38
49
UrdfInertial urdfInertial = linkObject . AddComponent < UrdfInertial > ( ) ;
39
- Rigidbody _rigidbody = urdfInertial . GetComponent < Rigidbody > ( ) ;
40
-
41
- if ( inertial != null )
42
- {
43
- _rigidbody . mass = ( float ) inertial . mass ;
50
+ urdfInertial . UrdfMass = ( float ) inertial . mass ;
44
51
45
- if ( inertial . origin != null )
46
- _rigidbody . centerOfMass = UrdfOrigin . GetPositionFromUrdf ( inertial . origin ) ;
52
+ if ( inertial . origin != null )
53
+ urdfInertial . UrdfCenterOfMass = UrdfOrigin . GetPositionFromUrdf ( inertial . origin ) ;
47
54
48
- urdfInertial . ImportInertiaData ( inertial . inertia ) ;
55
+ urdfInertial . ImportInertiaData ( inertial . inertia ) ;
56
+ urdfInertial . Initialize ( ) ;
57
+ urdfInertial . isCreated = true ;
58
+ }
49
59
50
- urdfInertial . UseUrdfData = true ;
51
- }
60
+ private void Initialize ( )
61
+ {
62
+ rigidbodyDataSource = RigidbodyDataSource . Urdf ;
63
+
64
+ Mass = UrdfMass ;
65
+ CenterOfMass = UrdfCenterOfMass ;
66
+ InertiaTensor = UrdfInertiaTensor ;
67
+ InertiaTensorRotation = UrdfInertiaTensorRotation ;
52
68
53
- urdfInertial . DisplayInertiaGizmo = false ;
69
+ DisplayInertiaGizmo = false ;
54
70
55
- //Save original rigidbody data from URDF
56
- urdfInertial . CenterOfMass = _rigidbody . centerOfMass ;
57
- urdfInertial . InertiaTensor = _rigidbody . inertiaTensor ;
58
- urdfInertial . InertiaTensorRotation = _rigidbody . inertiaTensorRotation ;
71
+ UpdateRigidBodyData ( ) ;
59
72
}
60
73
61
74
#region Runtime
75
+ private void Reset ( )
76
+ {
77
+ if ( isCreated )
78
+ Initialize ( ) ;
79
+ }
62
80
63
- private void Start ( )
81
+ private void OnValidate ( )
64
82
{
65
- UpdateRigidBodyData ( ) ;
83
+ if ( isCreated )
84
+ UpdateRigidBodyData ( ) ;
66
85
}
67
86
68
87
public void UpdateRigidBodyData ( )
69
88
{
70
- Rigidbody _rigidbody = GetComponent < Rigidbody > ( ) ;
89
+ _rigidbody = GetComponent < Rigidbody > ( ) ;
71
90
72
- if ( UseUrdfData )
73
- {
74
- _rigidbody . centerOfMass = CenterOfMass ;
75
- _rigidbody . inertiaTensor = InertiaTensor ;
76
- _rigidbody . inertiaTensorRotation = InertiaTensorRotation ;
77
- }
78
- else
91
+ switch ( rigidbodyDataSource )
79
92
{
80
- _rigidbody . ResetCenterOfMass ( ) ;
81
- _rigidbody . ResetInertiaTensor ( ) ;
93
+ case RigidbodyDataSource . Urdf :
94
+ {
95
+ _rigidbody . mass = UrdfMass ;
96
+ _rigidbody . centerOfMass = UrdfCenterOfMass ;
97
+ _rigidbody . inertiaTensor = UrdfInertiaTensor ;
98
+ _rigidbody . inertiaTensorRotation = UrdfInertiaTensorRotation ;
99
+ return ;
100
+ }
101
+ case RigidbodyDataSource . Unity :
102
+ {
103
+ _rigidbody . mass = Mass ;
104
+ bool isKinematic = _rigidbody . isKinematic ;
105
+ _rigidbody . isKinematic = false ;
106
+ _rigidbody . ResetCenterOfMass ( ) ;
107
+ _rigidbody . ResetInertiaTensor ( ) ;
108
+ _rigidbody . isKinematic = isKinematic ;
109
+ CenterOfMass = _rigidbody . centerOfMass ;
110
+ InertiaTensor = _rigidbody . inertiaTensor ;
111
+ InertiaTensorRotation = _rigidbody . inertiaTensorRotation ;
112
+ return ;
113
+ }
114
+ case RigidbodyDataSource . Manual :
115
+ {
116
+ _rigidbody . mass = Mass ;
117
+ _rigidbody . centerOfMass = CenterOfMass ;
118
+ _rigidbody . inertiaTensor = InertiaTensor ;
119
+ _rigidbody . inertiaTensorRotation = InertiaTensorRotation ;
120
+ return ;
121
+ }
82
122
}
83
123
}
84
124
@@ -106,10 +146,10 @@ private void ImportInertiaData(Link.Inertial.Inertia inertia)
106
146
Matrix3x3 rotationMatrix = ToMatrix3x3 ( inertia ) ;
107
147
rotationMatrix . DiagonalizeRealSymmetric ( out eigenvalues , out eigenvectors ) ;
108
148
109
- Rigidbody _rigidbody = GetComponent < Rigidbody > ( ) ;
110
-
111
- _rigidbody . inertiaTensor = ToUnityInertiaTensor ( FixMinInertia ( eigenvalues ) ) ;
112
- _rigidbody . inertiaTensorRotation = ToQuaternion ( eigenvectors [ 0 ] , eigenvectors [ 1 ] , eigenvectors [ 2 ] ) . Ros2Unity ( ) ;
149
+ UrdfInertiaTensor = ToUnityInertiaTensor ( FixMinInertia ( eigenvalues ) ) ;
150
+ Debug . Log ( UrdfInertiaTensor ) ;
151
+ UrdfInertiaTensorRotation = ToQuaternion ( eigenvectors [ 0 ] , eigenvectors [ 1 ] , eigenvectors [ 2 ] ) . Ros2Unity ( ) ;
152
+ Debug . Log ( UrdfInertiaTensorRotation ) ;
113
153
}
114
154
115
155
private static Vector3 ToUnityInertiaTensor ( Vector3 vector3 )
@@ -180,12 +220,7 @@ private static Quaternion ToQuaternion(Vector3 eigenvector0, Vector3 eigenvector
180
220
#region Export
181
221
public Link . Inertial ExportInertialData ( )
182
222
{
183
- Rigidbody _rigidbody = GetComponent < Rigidbody > ( ) ;
184
-
185
- if ( _rigidbody == null )
186
- return null ;
187
-
188
- UpdateRigidBodyData ( ) ;
223
+ // should we read the data from this clas instead of _rigidbody?
189
224
Origin inertialOrigin = new Origin ( _rigidbody . centerOfMass . Unity2Ros ( ) . ToRoundedDoubleArray ( ) , new double [ ] { 0 , 0 , 0 } ) ;
190
225
Link . Inertial . Inertia inertia = ExportInertiaData ( _rigidbody ) ;
191
226
0 commit comments