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 BLping rangefinder 17 : */ 18 : 19 : #include "SIM_RF_BLping.h" 20 : 21 : #include <GCS_MAVLink/GCS.h> 22 : #include <stdio.h> 23 : 24 : 25 : using namespace SITL; 26 : 27 353 : uint32_t RF_BLping::packet_for_alt(float alt_m, uint8_t *buffer, uint8_t buflen) 28 : { 29 : #define BLPING_MSGID_ACK 1 30 : #define BLPING_MSGID_NACK 2 31 : #define BLPING_MSGID_SET_PING_INTERVAL 1004 32 : #define BLPING_MSGID_GET_DEVICE_ID 1201 33 : #define BLDPIN_MSGID_DISTANCE_SIMPLE 1211 34 : #define BLPING_MSGID_CONTINUOUS_START 1400 35 : 36 353 : const uint32_t alt_mm = uint32_t(alt_m * 1000); 37 : const uint8_t payload[] = { 38 : uint8_t(alt_mm & 0xff), 39 353 : uint8_t((alt_mm >> 8) & 0xff), 40 353 : uint8_t((alt_mm >> 16) & 0xff), 41 353 : uint8_t((alt_mm >> 24) & 0xff), 42 353 : }; 43 353 : const uint16_t message_id = BLDPIN_MSGID_DISTANCE_SIMPLE; 44 353 : const uint8_t src_device_id = 1; 45 353 : const uint8_t dst_device_id = 0; 46 353 : uint16_t offs = 0; 47 353 : buffer[offs++] = 0x42; 48 353 : buffer[offs++] = 0x52; 49 353 : buffer[offs++] = ARRAY_SIZE(payload) & 0xff; 50 353 : buffer[offs++] = ARRAY_SIZE(payload) >> 8; 51 353 : buffer[offs++] = message_id & 0xff; 52 353 : buffer[offs++] = message_id >> 8; 53 353 : buffer[offs++] = src_device_id; 54 353 : buffer[offs++] = dst_device_id; 55 353 : memcpy(&buffer[offs], payload, ARRAY_SIZE(payload)); 56 353 : offs += ARRAY_SIZE(payload); 57 353 : uint16_t crc = 0; 58 4589 : for (uint8_t i=0; i<offs; i++) { 59 4236 : crc += buffer[i]; 60 : } 61 : 62 353 : buffer[offs++] = crc & 0xff; 63 353 : buffer[offs++] = crc >> 8; 64 : 65 353 : return offs; 66 : }