@@ -28,6 +28,27 @@ public ZOIMU IMUSensor {
2828 set => _imuSensor = value ;
2929 }
3030
31+ public enum CoordinateSystemEnum {
32+ RightHanded_XBackward_YLeft_ZUp ,
33+ RightHanded_XRight_YDown_ZForward , // RealSense D435i
34+ Unity_LeftHanded_XRight_YUp_ZForward , // Unity Standard
35+ ROS_RightHanded_XForward_YLeft_ZUp , // ROS Standard
36+ } ;
37+
38+ public CoordinateSystemEnum _coordinateSystem = CoordinateSystemEnum . ROS_RightHanded_XForward_YLeft_ZUp ;
39+ /// <summary>
40+ /// The coordinate system for the published IMU data.
41+ /// RightHanded_XBackward_YLeft_ZUp: ? Some IMU's want this reference frame
42+ /// RightHanded_XRight_YDown_ZForward: RealSense D435i IMU prefers this reference frame.
43+ /// Unity_LeftHanded_XRight_YUp_ZForward: Unity reference frame
44+ /// ROS_RightHanded_XForward_YLeft_ZUp: ROS standard reference frame.
45+ /// </summary>
46+ /// <value></value>
47+ public CoordinateSystemEnum CoordinateSystem {
48+ get { return _coordinateSystem ; }
49+ set { _coordinateSystem = value ; }
50+ }
51+
3152
3253 private ZOROSTransformPublisher _transformPublisher = null ;
3354 public ZOROSTransformPublisher TransformPublisher {
@@ -80,18 +101,56 @@ public override void OnROSBridgeDisconnected(ZOROSUnityManager rosUnityManager)
80101 private Task OnPublishImuDelegate ( ZOIMU lidar , string name , Vector3 linearAccel , Vector3 angularVelocity , Quaternion orientation ) {
81102 _imuMessage . header . Update ( ) ;
82103 _imuMessage . header . frame_id = TransformPublisher . ChildFrameID ;
83- _imuMessage . linear_acceleration . UnityVector3 = linearAccel ;
84- _imuMessage . angular_velocity . UnityVector3 = angularVelocity ;
85- _imuMessage . orientation . UnityQuaternion = orientation ;
86-
87- // if (_referenceFrame == ReferenceFrame.RightHanded_XBackward_YLeft_ZUp) {
88- // // (x, y, z, w) -> (-x, -z, y, -w).
89- // publishedOrientation = new Quaternion(-transform.rotation.z, -transform.rotation.x, transform.rotation.y, -transform.rotation.w);
90- // // if (m_zReverse)
91- // // rot = new Quaternion (-rot.x, rot.y, -rot.z, rot.w);
92- // publishedAcceleration = new Vector3(-_acceleration.z, -_acceleration.x, _acceleration.y);
93- // publishedAngularVelocity = new Vector3(-_angularVelocity.z, -_angularVelocity.x, _angularVelocity.y);
94- // }
104+
105+ if ( _coordinateSystem == CoordinateSystemEnum . ROS_RightHanded_XForward_YLeft_ZUp ) {
106+ _imuMessage . linear_acceleration . UnityVector3 = linearAccel ;
107+ _imuMessage . angular_velocity . UnityVector3 = angularVelocity ;
108+ _imuMessage . orientation . UnityQuaternion = orientation ;
109+
110+ } else if ( CoordinateSystem == CoordinateSystemEnum . RightHanded_XBackward_YLeft_ZUp ) {
111+ // (x, y, z, w) -> (-x, -z, y, -w).
112+ _imuMessage . orientation . x = - orientation . z ;
113+ _imuMessage . orientation . y = - orientation . x ;
114+ _imuMessage . orientation . z = orientation . y ;
115+ _imuMessage . orientation . w = - orientation . w ;
116+
117+ _imuMessage . linear_acceleration . x = - linearAccel . z ;
118+ _imuMessage . linear_acceleration . y = - linearAccel . x ;
119+ _imuMessage . linear_acceleration . z = linearAccel . y ;
120+
121+ _imuMessage . angular_velocity . x = - angularVelocity . z ;
122+ _imuMessage . angular_velocity . y = - angularVelocity . x ;
123+ _imuMessage . angular_velocity . z = angularVelocity . y ;
124+ } else if ( CoordinateSystem == CoordinateSystemEnum . RightHanded_XRight_YDown_ZForward ) {
125+ Quaternion flippedYOrientation = Quaternion . Euler ( Vector3 . Scale ( orientation . eulerAngles , Vector3 . up * - 1 ) ) ;
126+ _imuMessage . orientation . x = flippedYOrientation . x ;
127+ _imuMessage . orientation . y = flippedYOrientation . y ;
128+ _imuMessage . orientation . z = flippedYOrientation . z ;
129+ _imuMessage . orientation . w = - flippedYOrientation . w ; // negate left to right handed coordinate system
130+
131+ _imuMessage . linear_acceleration . x = linearAccel . x ;
132+ _imuMessage . linear_acceleration . y = - linearAccel . y ;
133+ _imuMessage . linear_acceleration . z = linearAccel . z ;
134+
135+ _imuMessage . angular_velocity . x = angularVelocity . x ;
136+ _imuMessage . angular_velocity . y = - angularVelocity . y ;
137+ _imuMessage . angular_velocity . z = angularVelocity . z ;
138+
139+ } else if ( CoordinateSystem == CoordinateSystemEnum . Unity_LeftHanded_XRight_YUp_ZForward ) {
140+ _imuMessage . orientation . x = orientation . x ;
141+ _imuMessage . orientation . y = orientation . y ;
142+ _imuMessage . orientation . z = orientation . z ;
143+ _imuMessage . orientation . w = orientation . w ;
144+
145+ _imuMessage . linear_acceleration . x = linearAccel . x ;
146+ _imuMessage . linear_acceleration . y = linearAccel . y ;
147+ _imuMessage . linear_acceleration . z = linearAccel . z ;
148+
149+ _imuMessage . angular_velocity . x = angularVelocity . x ;
150+ _imuMessage . angular_velocity . y = angularVelocity . y ;
151+ _imuMessage . angular_velocity . z = angularVelocity . z ;
152+
153+ }
95154
96155
97156 ROSBridgeConnection . Publish ( _imuMessage , ROSTopic , Name ) ;
0 commit comments