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 : Simulator for the serial LightWare rangefinder 17 : */ 18 : 19 : #include "SIM_RF_LightWareSerial.h" 20 : 21 : #include <GCS_MAVLink/GCS.h> 22 : #include <stdio.h> 23 : 24 : 25 : using namespace SITL; 26 : 27 260426 : bool RF_LightWareSerial::check_synced() 28 : { 29 260426 : if (!synced) { 30 : // just try to slurp a buffer in one hit: 31 5982 : char buffer[12] {}; 32 5982 : ssize_t n = read_from_autopilot(buffer, ARRAY_SIZE(buffer) - 1); 33 5982 : if (n > 0) { 34 2 : if (!strncmp(buffer, "www\r\n", ARRAY_SIZE(buffer))) { 35 2 : GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Slurped a sync thing"); 36 2 : synced = true; 37 : } 38 : } 39 : } 40 260426 : return synced; 41 : } 42 : 43 260426 : void RF_LightWareSerial::update(float range) 44 : { 45 260426 : if (!check_synced()) { 46 5980 : return; 47 : } 48 254446 : return SerialRangeFinder::update(range); 49 : } 50 : 51 1061 : uint32_t RF_LightWareSerial::packet_for_alt(float alt_m, uint8_t *buffer, uint8_t buflen) 52 : { 53 : uint16_t alt_cm; 54 1061 : if (alt_m > 100) { 55 384 : alt_cm = 13000; // from datasheet 56 : } else { 57 677 : alt_cm = alt_m * 100; 58 : } 59 1061 : return snprintf((char*)buffer, buflen, "%0.2f\r", alt_cm * 0.01f); // note tragic lack of snprintf return checking 60 : }