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 Ainstein LR-D1 Serial rangefinder - with v19.0.0 firmware 17 : */ 18 : 19 : #include "SIM_RF_Ainstein_LR_D1_v19.h" 20 : 21 : using namespace SITL; 22 : 23 1460 : uint32_t RF_Ainstein_LR_D1_v19::packet_for_alt(float alt_m, uint8_t *buffer, uint8_t buflen) 24 : { 25 1460 : const uint8_t PACKET_LEN = 32; 26 : 27 1460 : if (buflen < PACKET_LEN) { 28 0 : return 0; 29 : } 30 : 31 1460 : uint16_t malfunction_alert = 0; 32 : 33 1460 : uint8_t snr = 100; 34 1460 : uint8_t altitude_valid = 1; 35 1460 : if (alt_m > 525) { 36 : // overheats at 525 metres: 37 320 : malfunction_alert |= 1U << 0; 38 : } 39 1460 : if (alt_m > 500) { 40 : // out of range @500m 41 368 : snr = 0; 42 368 : altitude_valid = 0; 43 : } 44 1460 : if (alt_m*100 > 65535) { 45 110 : malfunction_alert |= 1U << 10; // AltitudeReading alert 46 : } 47 : 48 1460 : const uint16_t alt_cm = alt_m * 100; // this really does wrap on real hardware 49 : 50 1460 : buffer[0] = 0xEB; // packet header msb 51 1460 : buffer[1] = 0x90; // packet header lsb 52 1460 : buffer[2] = 0; // device_id 53 1460 : buffer[3] = 28; // length 54 1460 : buffer[4] = HIGHBYTE(malfunction_alert); 55 1460 : buffer[5] = LOWBYTE(malfunction_alert); // object count 56 1460 : buffer[6] = HIGHBYTE(alt_cm); 57 1460 : buffer[7] = LOWBYTE(alt_cm); 58 1460 : buffer[8] = snr; 59 1460 : buffer[9] = 0; // speed high 60 1460 : buffer[10] = 0; // speed low 61 1460 : memset(&buffer[11], 0xff, 20); // unused 62 1460 : buffer[11] = altitude_valid; 63 1460 : buffer[31] = crc_sum_of_bytes(&buffer[3], PACKET_LEN-4); // minus headerMSB, headerLSB, device_id and checksum 64 : 65 1460 : return PACKET_LEN; 66 : }