-
Notifications
You must be signed in to change notification settings - Fork 70
/
UrdfJointContinuous.cs
149 lines (130 loc) · 5.18 KB
/
UrdfJointContinuous.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
© Siemens AG, 2018-2019
Author: Suzannah Smith (suzannah.smith@siemens.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<http://www.apache.org/licenses/LICENSE-2.0>.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using UnityEngine;
namespace Unity.Robotics.UrdfImporter
{
public class UrdfJointContinuous : UrdfJoint
{
public override JointTypes JointType => JointTypes.Continuous;
public static UrdfJoint Create(GameObject linkObject)
{
UrdfJointContinuous urdfJoint = linkObject.AddComponent<UrdfJointContinuous>();
#if UNITY_2020_1_OR_NEWER
urdfJoint.unityJoint = linkObject.GetComponent<ArticulationBody>();
urdfJoint.unityJoint.jointType = ArticulationJointType.RevoluteJoint;
#else
urdfJoint.unityJoint = linkObject.AddComponent<HingeJoint>();
urdfJoint.unityJoint.autoConfigureConnectedAnchor = true;
#endif
return urdfJoint;
}
#region Runtime
/// <summary>
/// Returns the current position of the joint in radians
/// </summary>
/// <returns>floating point number for joint position in radians</returns>
public override float GetPosition() // Check Units
{
#if UNITY_2020_1_OR_NEWER
return unityJoint.jointPosition[xAxis];
#else
return -((HingeJoint)unityJoint).angle;
#endif
}
/// <summary>
/// Returns the current velocity of joint in radians per second in the reduced coordinates
/// </summary>
/// <returns>floating point for joint velocity in radians per second</returns>
public override float GetVelocity()
{
#if UNITY_2020_1_OR_NEWER
return unityJoint.jointVelocity[xAxis];
#else
return -((HingeJoint)unityJoint).velocity;
#endif
}
/// <summary>
/// Returns current joint torque in Nm
/// </summary>
/// <returns>floating point in Nm</returns>
public override float GetEffort()
{
#if UNITY_2020_1_OR_NEWER
return unityJoint.jointForce[xAxis];
#else
return -((HingeJoint)unityJoint).motor.force;
#endif
}
/// <summary>
/// Rotates the joint by deltaState radians
/// </summary>
/// <param name="deltaState">amount in radians by which joint needs to be rotated</param>
protected override void OnUpdateJointState(float deltaState)
{
#if UNITY_2020_1_OR_NEWER
ArticulationDrive drive = unityJoint.xDrive;
drive.target += deltaState;
unityJoint.xDrive = drive;
#else
Quaternion rot = Quaternion.AngleAxis(-deltaState * Mathf.Rad2Deg, unityJoint.axis);
transform.rotation = transform.rotation * rot;
#endif
}
#endregion
protected override void ImportJointData(Joint joint)
{
AdjustMovement(joint);
SetDynamics(joint.dynamics);
}
protected override Joint ExportSpecificJointData(Joint joint)
{
#if UNITY_2020_1_OR_NEWER
joint.axis = GetAxisData(axisofMotion);
joint.dynamics = new Joint.Dynamics(unityJoint.angularDamping, unityJoint.jointFriction);
joint.limit = ExportLimitData();
#else
joint.axis = GetAxisData(unityJoint.axis);
joint.dynamics = new Joint.Dynamics(
((HingeJoint)unityJoint).spring.damper,
((HingeJoint)unityJoint).spring.spring);
#endif
return joint;
}
/// <summary>
/// Reads axis joint information and rotation to the articulation body to produce the required motion
/// </summary>
/// <param name="joint">Structure containing joint information</param>
protected override void AdjustMovement(Joint joint)
{
axisofMotion = joint.axis.xyz.ToVector3();
unityJoint.linearLockX = ArticulationDofLock.LockedMotion;
unityJoint.linearLockY = ArticulationDofLock.LockedMotion;
unityJoint.linearLockZ = ArticulationDofLock.LockedMotion;
unityJoint.twistLock = ArticulationDofLock.FreeMotion;
Vector3 axisofMotionUnity = axisofMotion.Ros2Unity();
Quaternion motion = new Quaternion();
motion.SetFromToRotation(new Vector3(1, 0, 0), -1 * axisofMotionUnity);
unityJoint.anchorRotation = motion;
if (joint.limit != null)
{
ArticulationDrive drive = unityJoint.xDrive;
drive.forceLimit = (float)(joint.limit.effort);
unityJoint.maxAngularVelocity = (float)(joint.limit.velocity);
drive.damping = unityJoint.xDrive.damping;
drive.stiffness = unityJoint.xDrive.stiffness;
unityJoint.xDrive = drive;
}
}
}
}