LCOV - code coverage report
Current view: top level - SITL - SIM_RF_LightWare_GRF.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 3 3 100.0 %
Date: 2026-09-09 11:02:08 Functions: 2 2 100.0 %

          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             : /*
      17             :    Simulator for the LightWare GRF rangefinder
      18             : 
      19             :    ./Tools/autotest/sim_vehicle.py -v ArduCopter \
      20             :        -A --serial5=sim:lightware_grf \
      21             :        --console
      22             : 
      23             :    Then in ArduPilot:
      24             :      param set SERIAL5_PROTOCOL 9   # Rangefinder
      25             :      param set RNGFND1_TYPE   45
      26             :      reboot
      27             : */
      28             : 
      29             : #pragma once
      30             : 
      31             : #include "SIM_config.h"
      32             : 
      33             : #if AP_SIM_RF_LIGHTWAR_GRF250_ENABLED
      34             : 
      35             : #include "SIM_SerialRangeFinder.h"
      36             : #include <AP_Math/crc.h>
      37             : #include <stdint.h>
      38             : 
      39             : namespace SITL {
      40             : 
      41             : class RF_LightWareGRF : public SerialRangeFinder {
      42             : public:
      43           3 :     static SerialRangeFinder *create() { return NEW_NOTHROW RF_LightWareGRF(); }
      44             : 
      45             :     uint32_t packet_for_alt(float alt_m, uint8_t *buffer, uint8_t buflen) override;
      46             : 
      47      180200 :     uint16_t reading_interval_ms() const override {
      48      180200 :         return update_period_ms;
      49             :     }
      50             : 
      51             : private:
      52             :     /*
      53             :         GRF binary message format:
      54             : 
      55             :            [0]   PREAMBLE 0xAA
      56             :            [1-2] FLAGS  (payload length encoded in upper bits)
      57             :            [3]   MSGID
      58             :            [4..] PAYLOAD
      59             :            [end-2..end-1] CRC-XMODEM
      60             :     */
      61             : 
      62             :     enum class MsgID : uint8_t {
      63             :         PRODUCT_NAME     = 0,
      64             :         UPDATE_RATE      = 74,
      65             :         DISTANCE_OUTPUT  = 27,
      66             :         STREAM           = 30,
      67             :         DISTANCE_DATA_CM = 44
      68             :     };
      69             : 
      70             :     uint32_t update_period_ms;
      71             :     bool stream_enabled;
      72             : 
      73             :     // internal parser buffer
      74             :     uint8_t rxbuf[128];
      75             :     uint8_t rxlen;
      76             : 
      77             :     void process_input();
      78             :     void try_parse_message();
      79             : 
      80             :     uint16_t compute_crc(const uint8_t *buf, uint8_t len);
      81             : 
      82             :     void send_product_name();
      83             :     void send_ack_u32(MsgID id, uint32_t value);
      84             :     void send_ack_u8(MsgID id, uint8_t value);
      85             : 
      86             :     void build_distance_packet(float alt_m, uint8_t *out, uint32_t &outlen);
      87             : };
      88             : 
      89             : }
      90             : 
      91             : #endif // AP_SIM_RF_LIGHTWAR_GRF250_ENABLED

Generated by: LCOV version 1.14