Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : 16 : #include "AP_VisualOdom_Backend.h" 17 : 18 : #if HAL_VISUALODOM_ENABLED 19 : 20 : #include <AP_AHRS/AP_AHRS.h> 21 : #include <GCS_MAVLink/GCS.h> 22 : 23 : /* 24 : base class constructor. 25 : This incorporates initialisation as well. 26 : */ 27 8 : AP_VisualOdom_Backend::AP_VisualOdom_Backend(AP_VisualOdom &frontend) : 28 8 : _frontend(frontend) 29 : { 30 8 : } 31 : 32 : // return true if sensor is basically healthy (we are receiving data) 33 286332 : bool AP_VisualOdom_Backend::healthy() const 34 : { 35 : // healthy if we have received sensor messages within the past 300ms 36 286332 : return ((AP_HAL::millis() - _last_update_ms) < AP_VISUALODOM_TIMEOUT_MS); 37 : } 38 : 39 : #if HAL_GCS_ENABLED 40 : // consume vision_position_delta mavlink messages 41 3633 : void AP_VisualOdom_Backend::handle_vision_position_delta_msg(const mavlink_message_t &msg) 42 : { 43 : // decode message 44 : mavlink_vision_position_delta_t packet; 45 3633 : mavlink_msg_vision_position_delta_decode(&msg, &packet); 46 : 47 : // apply rotation to angle and position delta 48 3633 : const enum Rotation rot = _frontend.get_orientation(); 49 3633 : Vector3f angle_delta = Vector3f(packet.angle_delta[0], packet.angle_delta[1], packet.angle_delta[2]); 50 3633 : angle_delta.rotate(rot); 51 3633 : Vector3f position_delta = Vector3f(packet.position_delta[0], packet.position_delta[1], packet.position_delta[2]); 52 3633 : position_delta.rotate(rot); 53 : 54 3633 : const uint32_t now_ms = AP_HAL::millis(); 55 3633 : _last_update_ms = now_ms; 56 : 57 : // send to EKF 58 : #if AP_AHRS_ENABLED || HAL_LOGGING_ENABLED 59 3633 : const float time_delta_sec = packet.time_delta_usec * 1.0E-6; 60 : #endif 61 : #if AP_AHRS_ENABLED 62 7266 : AP::ahrs().writeBodyFrameOdom(packet.confidence, 63 : position_delta, 64 : angle_delta, 65 : time_delta_sec, 66 : now_ms, 67 3633 : _frontend.get_delay_ms(), 68 3633 : _frontend.get_pos_offset()); 69 : #endif 70 : 71 : #if HAL_LOGGING_ENABLED 72 : // log sensor data 73 3633 : Write_VisualOdom(time_delta_sec, 74 : angle_delta, 75 : position_delta, 76 : packet.confidence); 77 : #endif 78 3633 : } 79 : #endif // HAL_GCS_ENABLED 80 : 81 : // returns the system time of the last reset if reset_counter has not changed 82 : // updates the reset timestamp to the current system time if the reset_counter has changed 83 10069 : uint32_t AP_VisualOdom_Backend::get_reset_timestamp_ms(uint8_t reset_counter) 84 : { 85 : // update reset counter and timestamp if reset_counter has changed 86 10069 : if (reset_counter != _last_reset_counter) { 87 0 : _last_reset_counter = reset_counter; 88 0 : _reset_timestamp_ms = AP_HAL::millis(); 89 : } 90 10069 : return _reset_timestamp_ms; 91 : } 92 : 93 : // align position with ahrs position by updating _pos_correction 94 : // sensor_pos should be the position directly from the sensor with only scaling applied (i.e. no yaw or position corrections) 95 0 : bool AP_VisualOdom_Backend::align_position_to_ahrs(const Vector3f &sensor_pos, bool align_xy, bool align_z) 96 : { 97 : // fail immediately if ahrs cannot provide position 98 0 : Vector3f ahrs_pos_ned; 99 0 : if (!AP::ahrs().get_relative_position_NED_origin_float(ahrs_pos_ned)) { 100 0 : return false; 101 : } 102 : 103 0 : align_position(sensor_pos, ahrs_pos_ned, align_xy, align_z); 104 0 : return true; 105 : } 106 : 107 : // apply rotation and correction to position 108 10075 : void AP_VisualOdom_Backend::rotate_and_correct_position(Vector3f &position) const 109 : { 110 10075 : if (_use_posvel_rotation) { 111 5524 : position = _posvel_rotation * position; 112 : } 113 10075 : position += _pos_correction; 114 10075 : } 115 : 116 : // align position with a new position by updating _pos_correction 117 : // sensor_pos should be the position directly from the sensor with only scaling applied (i.e. no yaw or position corrections) 118 : // new_pos should be a NED position offset from the EKF origin 119 0 : void AP_VisualOdom_Backend::align_position(const Vector3f &sensor_pos, const Vector3f &new_pos, bool align_xy, bool align_z) 120 : { 121 : // calculate position with current rotation and correction 122 0 : Vector3f pos_orig = sensor_pos; 123 0 : rotate_and_correct_position(pos_orig); 124 : 125 : // update position correction 126 0 : if (align_xy) { 127 0 : _pos_correction.x += (new_pos.x - pos_orig.x); 128 0 : _pos_correction.y += (new_pos.y - pos_orig.y); 129 : } 130 0 : if (align_z) { 131 0 : _pos_correction.z += (new_pos.z - pos_orig.z); 132 : } 133 0 : } 134 : #endif