LCOV - code coverage report
Current view: top level - AP_RangeFinder - AP_RangeFinder_MaxsonarSerialLV.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 24 26 92.3 %
Date: 2026-09-09 11:02:08 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2016  Intel Corporation. All rights reserved.
       3             :  *
       4             :  * This file is free software: you can redistribute it and/or modify it
       5             :  * under the terms of the GNU General Public License as published by the
       6             :  * Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This file is distributed in the hope that it will be useful, but
      10             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      12             :  * See the GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License along
      15             :  * with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "AP_RangeFinder_MaxsonarSerialLV.h"
      19             : 
      20             : #if AP_RANGEFINDER_MAXBOTIX_SERIAL_ENABLED
      21             : 
      22             : #include <AP_HAL/AP_HAL.h>
      23             : #include <ctype.h>
      24             : 
      25             : #define MAXSONAR_SERIAL_LV_BAUD_RATE 9600
      26             : 
      27             : extern const AP_HAL::HAL& hal;
      28             : 
      29           1 : AP_RangeFinder_MaxsonarSerialLV::AP_RangeFinder_MaxsonarSerialLV(
      30             :     RangeFinder::RangeFinder_State &_state,
      31           1 :     AP_RangeFinder_Params &_params):
      32           1 :     AP_RangeFinder_Backend_Serial(_state, _params)
      33             : {
      34           1 :     params.scaling.set_default(0.0254f);
      35           1 : }
      36             : 
      37             : // read - return last value measured by sensor
      38        1348 : bool AP_RangeFinder_MaxsonarSerialLV::get_reading(float &reading_m)
      39             : {
      40        1348 :     if (uart == nullptr) {
      41           0 :         return false;
      42             :     }
      43             : 
      44        1348 :     int32_t sum = 0;
      45        1348 :     uint16_t count = 0;
      46             : 
      47        2191 :     for (auto i=0; i<8192; i++) {
      48             :         uint8_t c;
      49        2191 :         if (!uart->read(c)) {
      50        1348 :             break;
      51             :         }
      52         843 :         if (c == '\r') {
      53         346 :             linebuf[linebuf_len] = 0;
      54         346 :             sum += (int)atoi(linebuf);
      55         346 :             count++;
      56         346 :             linebuf_len = 0;
      57         497 :         } else if (isdigit(c)) {
      58         497 :             linebuf[linebuf_len++] = c;
      59         497 :             if (linebuf_len == sizeof(linebuf)) {
      60             :                 // too long, discard the line
      61           0 :                 linebuf_len = 0;
      62             :             }
      63             :         }
      64             :     }
      65             : 
      66        1348 :     if (count == 0) {
      67        1009 :         return false;
      68             :     }
      69             : 
      70             :     // This sonar gives the metrics in inches, so we have to transform this to meters
      71         339 :     reading_m = params.scaling * (float(sum) / count);
      72             : 
      73         339 :     return true;
      74             : }
      75             : 
      76             : #endif  // AP_RANGEFINDER_MAXBOTIX_SERIAL_ENABLED

Generated by: LCOV version 1.14