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 : #pragma once 17 : 18 : #include "AP_RangeFinder_config.h" 19 : 20 : #if AP_RANGEFINDER_NMEA_ENABLED 21 : 22 : #include "AP_RangeFinder.h" 23 : #include "AP_RangeFinder_Backend_Serial.h" 24 : 25 : class AP_RangeFinder_NMEA : public AP_RangeFinder_Backend_Serial 26 : { 27 : 28 : public: 29 : 30 3 : static AP_RangeFinder_Backend_Serial *create( 31 : RangeFinder::RangeFinder_State &_state, 32 : AP_RangeFinder_Params &_params) { 33 3 : return NEW_NOTHROW AP_RangeFinder_NMEA(_state, _params); 34 : } 35 : 36 : protected: 37 : 38 2698 : MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override { 39 2698 : return MAV_DISTANCE_SENSOR_ULTRASOUND; 40 : } 41 : 42 : private: 43 : 44 : using AP_RangeFinder_Backend_Serial::AP_RangeFinder_Backend_Serial; 45 : 46 : /// enum for handled messages 47 : enum sentence_types : uint8_t { 48 : SONAR_UNKNOWN = 0, 49 : SONAR_DBT, 50 : SONAR_DPT, 51 : SONAR_MTW, // mean water temperature 52 : SONAR_HDED, // hondex custom sonar message 53 : }; 54 : 55 : // get a distance reading 56 : bool get_reading(float &reading_m) override; 57 : 58 : // get temperature reading in C. returns true on success and populates temp argument 59 : bool _get_temp(float &temp) const override; 60 : 61 22782 : uint16_t read_timeout_ms() const override { return 3000; } 62 : 63 : // add a single character to the buffer and attempt to decode 64 : // returns true if a distance was successfully decoded 65 : // distance should be pulled directly from _distance_m member 66 : bool decode(char c); 67 : 68 : // decode the just-completed term 69 : // returns true if new sentence has just passed checksum test and is validated 70 : bool decode_latest_term(); 71 : 72 : // message decoding related members 73 : char _term[15]; // buffer for the current term within the current sentence 74 : uint8_t _term_offset; // offset within the _term buffer where the next character should be placed 75 : uint8_t _term_number; // term index within the current sentence 76 : float _distance_m = -1.0f; // distance in meters parsed from a term, -1 if no distance 77 : float _temp_unvalidated; // unvalidated temperature in C (may have failed checksum) 78 : float _temp; // temperature in C (validated) 79 : uint32_t _temp_readtime_ms; // system time we last read a validated temperature, 0 if never read 80 : uint8_t _checksum; // checksum accumulator 81 : bool _term_is_checksum; // current term is the checksum 82 : sentence_types _sentence_type; // the sentence type currently being processed 83 : bool _sentence_done; // true if this sentence has already been decoded 84 : }; 85 : 86 : #endif // AP_RANGEFINDER_NMEA_ENABLED