@@ -609,6 +609,29 @@ void GazeboMavlinkInterface::send_mavlink_message(const mavlink_message_t *messa
609
609
}
610
610
}
611
611
612
+ template <class T >
613
+ void GazeboMavlinkInterface::setSensorOrientation (ignition::math::Vector3d u_Xs, T sensor_msg) {
614
+ const ignition::math::Vector3d u_Xb = kForwardRotation ; // This is unit vector of X-axis `base_link`
615
+
616
+ // Current rotation types are described as https://mavlink.io/en/messages/common.html#MAV_SENSOR_ORIENTATION
617
+ if (u_Xs.Dot (kDownwardRotation ) > 0.99 )
618
+ sensor_msg.orientation = MAV_SENSOR_ORIENTATION::MAV_SENSOR_ROTATION_PITCH_270;
619
+ else if (u_Xs.Dot (kUpwardRotation ) > 0.99 )
620
+ sensor_msg.orientation = MAV_SENSOR_ORIENTATION::MAV_SENSOR_ROTATION_PITCH_90;
621
+ else if (u_Xs.Dot (kBackwardRotation ) > 0.99 )
622
+ sensor_msg.orientation = MAV_SENSOR_ORIENTATION::MAV_SENSOR_ROTATION_PITCH_180;
623
+ else if (u_Xs.Dot (kForwardRotation ) > 0.99 )
624
+ sensor_msg.orientation = MAV_SENSOR_ORIENTATION::MAV_SENSOR_ROTATION_NONE;
625
+ else if (u_Xs.Dot (kLeftRotation ) > 0.99 )
626
+ sensor_msg.orientation = MAV_SENSOR_ORIENTATION::MAV_SENSOR_ROTATION_YAW_270;
627
+ else if (u_Xs.Dot (kRightRotation ) > 0.99 )
628
+ sensor_msg.orientation = MAV_SENSOR_ORIENTATION::MAV_SENSOR_ROTATION_YAW_90;
629
+ else {
630
+ sensor_msg.orientation = MAV_SENSOR_ORIENTATION::MAV_SENSOR_ROTATION_CUSTOM;
631
+ }
632
+
633
+ }
634
+
612
635
void GazeboMavlinkInterface::forward_mavlink_message (const mavlink_message_t *message)
613
636
{
614
637
if (gotSigInt_) {
@@ -849,8 +872,24 @@ void GazeboMavlinkInterface::LidarCallback(LidarPtr& lidar_message) {
849
872
sensor_msg.current_distance = lidar_message->current_distance () * 100.0 ;
850
873
sensor_msg.type = 0 ;
851
874
sensor_msg.id = 0 ;
852
- sensor_msg.orientation = lidar_message->rotation ();
853
875
sensor_msg.covariance = 0 ;
876
+ sensor_msg.horizontal_fov = lidar_message->h_fov ();
877
+ sensor_msg.vertical_fov = lidar_message->v_fov ();
878
+ sensor_msg.quaternion [0 ] = lidar_message->orientation ().w ();
879
+ sensor_msg.quaternion [1 ] = lidar_message->orientation ().x ();
880
+ sensor_msg.quaternion [2 ] = lidar_message->orientation ().y ();
881
+ sensor_msg.quaternion [3 ] = lidar_message->orientation ().z ();
882
+
883
+ ignition::math::Quaterniond q_bs = ignition::math::Quaterniond (
884
+ lidar_message->orientation ().w (),
885
+ lidar_message->orientation ().x (),
886
+ lidar_message->orientation ().y (),
887
+ lidar_message->orientation ().z ());
888
+
889
+ const ignition::math::Vector3d u_Xb = kForwardRotation ; // This is unit vector of X-axis `base_link`
890
+ const ignition::math::Vector3d u_Xs = q_bs.RotateVectorReverse (u_Xb); // This is unit vector of X-axis sensor in `base_link` frame
891
+
892
+ setSensorOrientation (u_Xs, sensor_msg);
854
893
855
894
// distance needed for optical flow message
856
895
optflow_distance = lidar_message->current_distance (); // [m]
@@ -896,45 +935,27 @@ void GazeboMavlinkInterface::SonarCallback(SonarPtr& sonar_message) {
896
935
sensor_msg.min_distance = sonar_message->min_distance () * 100.0 ;
897
936
sensor_msg.max_distance = sonar_message->max_distance () * 100.0 ;
898
937
sensor_msg.current_distance = sonar_message->current_distance () * 100.0 ;
899
- ignition::math::Quaterniond q_gr = ignition::math::Quaterniond (
938
+
939
+ ignition::math::Quaterniond q_bs = ignition::math::Quaterniond (
900
940
sonar_message->orientation ().w (),
901
941
sonar_message->orientation ().x (),
902
942
sonar_message->orientation ().y (),
903
943
sonar_message->orientation ().z ());
904
944
905
- // rotation of the sensor with respect to the vehicle
906
- ignition::math::Vector3d euler = q_gr.Euler ();
907
- int roll = static_cast <int >(round (GetDegrees360 (euler.X ())));
908
- int pitch = static_cast <int >(round (GetDegrees360 (euler.Y ())));
909
- int yaw = static_cast <int >(round (GetDegrees360 (euler.Z ())));
910
-
911
-
912
- if (roll == 0 && pitch == 0 && yaw == 0 ) {
913
- sensor_msg.orientation = 25 ; // downward facing
914
- } else if (roll == 0 && pitch == 180 && yaw == 0 ) {
915
- sensor_msg.orientation = 24 ; // upward facing
916
- } else if (roll == 0 && pitch == 90 && yaw == 180 ) {
917
- sensor_msg.orientation = 12 ; // backward facing
918
- } else if (roll == 0 && pitch == 90 && yaw == 0 ) {
919
- sensor_msg.orientation = 0 ; // forward facing
920
- } else if (roll == 0 && pitch == 90 && yaw == 90 ) {
921
- sensor_msg.orientation = 6 ; // left facing
922
- } else if (roll == 0 && pitch == 90 && yaw == 270 ) {
923
- sensor_msg.orientation = 2 ; // right facing
924
- } else {
925
- sensor_msg.orientation = 100 ; // custom orientation
926
- sensor_msg.quaternion [0 ] = sonar_message->orientation ().w ();
927
- sensor_msg.quaternion [1 ] = sonar_message->orientation ().x ();
928
- sensor_msg.quaternion [2 ] = sonar_message->orientation ().y ();
929
- sensor_msg.quaternion [3 ] = sonar_message->orientation ().z ();
930
- }
945
+ const ignition::math::Vector3d u_Xb = kForwardRotation ; // This is unit vector of X-axis `base_link`
946
+ const ignition::math::Vector3d u_Xs = q_bs.RotateVectorReverse (u_Xb); // This is unit vector of X-axis sensor in `base_link` frame
947
+
948
+ setSensorOrientation (u_Xs, sensor_msg);
931
949
932
950
sensor_msg.type = 1 ;
933
951
sensor_msg.id = 1 ;
934
- sensor_msg.orientation = sonar_message->rotation ();
935
952
sensor_msg.covariance = 0 ;
936
953
sensor_msg.horizontal_fov = sonar_message->h_fov ();
937
954
sensor_msg.vertical_fov = sonar_message->v_fov ();
955
+ sensor_msg.quaternion [0 ] = sonar_message->orientation ().w ();
956
+ sensor_msg.quaternion [1 ] = sonar_message->orientation ().x ();
957
+ sensor_msg.quaternion [2 ] = sonar_message->orientation ().y ();
958
+ sensor_msg.quaternion [3 ] = sonar_message->orientation ().z ();
938
959
939
960
mavlink_message_t msg;
940
961
mavlink_msg_distance_sensor_encode_chan (1 , 200 , MAVLINK_COMM_0, &msg, &sensor_msg);
0 commit comments