2021-09-23 19:26:03 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
2024-02-03 23:43:51 -04:00
|
|
|
Simulate MicroStrain CX5 GNSS-INS devices
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
PARAMS:
|
|
|
|
param set AHRS_EKF_TYPE 11
|
|
|
|
param set EAHRS_TYPE 2
|
|
|
|
param set SERIAL3_PROTOCOL 36
|
|
|
|
param set SERIAL3_BAUD 115
|
|
|
|
sim_vehicle.py -v Plane -A "--serial3=sim:MicroStrain5" --console --map -DG
|
2021-09-23 19:26:03 -03:00
|
|
|
*/
|
2023-08-04 18:31:14 -03:00
|
|
|
#include "SIM_MicroStrain.h"
|
2021-09-23 19:26:03 -03:00
|
|
|
#include <stdio.h>
|
2024-08-12 19:25:51 -03:00
|
|
|
#include <sys/time.h>
|
2021-09-23 19:26:03 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
|
|
|
using namespace SITL;
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
MicroStrain::MicroStrain() :SerialDevice::SerialDevice()
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::simulation_timeval(struct timeval *tv)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
uint64_t now = AP_HAL::micros64();
|
|
|
|
static uint64_t first_usec;
|
|
|
|
static struct timeval first_tv;
|
|
|
|
if (first_usec == 0) {
|
|
|
|
first_usec = now;
|
|
|
|
first_tv.tv_sec = AP::sitl()->start_time_UTC;
|
|
|
|
}
|
|
|
|
*tv = first_tv;
|
|
|
|
tv->tv_sec += now / 1000000ULL;
|
|
|
|
uint64_t new_usec = tv->tv_usec + (now % 1000000ULL);
|
|
|
|
tv->tv_sec += new_usec / 1000000ULL;
|
|
|
|
tv->tv_usec = new_usec % 1000000ULL;
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::generate_checksum(MicroStrain_Packet& packet)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
uint8_t checksumByte1 = 0;
|
|
|
|
uint8_t checksumByte2 = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
checksumByte1 += packet.header[i];
|
|
|
|
checksumByte2 += checksumByte1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < packet.header[3]; i++) {
|
|
|
|
checksumByte1 += packet.payload[i];
|
|
|
|
checksumByte2 += checksumByte1;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.checksum[0] = checksumByte1;
|
|
|
|
packet.checksum[1] = checksumByte2;
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::send_packet(MicroStrain_Packet packet)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
generate_checksum(packet);
|
|
|
|
|
|
|
|
write_to_autopilot((char *)&packet.header, sizeof(packet.header));
|
|
|
|
write_to_autopilot((char *)&packet.payload, packet.payload_size);
|
|
|
|
write_to_autopilot((char *)&packet.checksum, sizeof(packet.checksum));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::send_imu_packet(void)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
const auto &fdm = _sitl->state;
|
2023-08-04 18:31:14 -03:00
|
|
|
MicroStrain_Packet packet;
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
simulation_timeval(&tv);
|
|
|
|
|
|
|
|
if (start_us == 0) {
|
|
|
|
start_us = tv.tv_usec * 1000;
|
|
|
|
}
|
|
|
|
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.header[0] = 0x75; // Sync One
|
|
|
|
packet.header[1] = 0x65; // Sync Two
|
|
|
|
packet.header[2] = 0x80; // INS Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
// Add ambient pressure field
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x06; // Ambient Pressure Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x17; // Descriptor
|
2024-04-28 04:11:56 -03:00
|
|
|
|
|
|
|
float pressure_Pa = AP_Baro::get_pressure_for_alt_amsl(fdm.altitude);
|
|
|
|
put_float(packet, pressure_Pa*0.001 + rand_float() * 0.1);
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
// Add scaled magnetometer field
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x0E; // Scaled Magnetometer Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x06; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_float(packet, fdm.bodyMagField.x*0.001);
|
|
|
|
put_float(packet, fdm.bodyMagField.y*0.001);
|
|
|
|
put_float(packet, fdm.bodyMagField.z*0.001);
|
|
|
|
|
2021-09-30 05:35:51 -03:00
|
|
|
// Add scaled accelerometer field
|
|
|
|
packet.payload[packet.payload_size++] = 0x0E; // Scaled Accelerometer Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x04; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_float(packet, fdm.xAccel / GRAVITY_MSS);
|
|
|
|
put_float(packet, fdm.yAccel / GRAVITY_MSS);
|
|
|
|
put_float(packet, fdm.zAccel / GRAVITY_MSS);
|
|
|
|
|
|
|
|
// Add scaled gyro field
|
|
|
|
const float gyro_noise = 0.05;
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x0E; // Scaled Gyro Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x05; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_float(packet, radians(fdm.rollRate + rand_float() * gyro_noise));
|
|
|
|
put_float(packet, radians(fdm.pitchRate + rand_float() * gyro_noise));
|
|
|
|
put_float(packet, radians(fdm.yawRate + rand_float() * gyro_noise));
|
|
|
|
|
|
|
|
// Add CF Quaternion field
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x12; // CF Quaternion Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x0A; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_float(packet, fdm.quaternion.q1);
|
|
|
|
put_float(packet, fdm.quaternion.q2);
|
|
|
|
put_float(packet, fdm.quaternion.q3);
|
|
|
|
put_float(packet, fdm.quaternion.q4);
|
|
|
|
|
|
|
|
packet.header[3] = packet.payload_size;
|
|
|
|
|
|
|
|
send_packet(packet);
|
|
|
|
}
|
|
|
|
|
2023-08-27 23:24:17 -03:00
|
|
|
void MicroStrain5::send_gnss_packet(void)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
const auto &fdm = _sitl->state;
|
2023-08-04 18:31:14 -03:00
|
|
|
MicroStrain_Packet packet;
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
simulation_timeval(&tv);
|
|
|
|
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.header[0] = 0x75; // Sync One
|
|
|
|
packet.header[1] = 0x65; // Sync Two
|
|
|
|
packet.header[2] = 0x81; // GNSS Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
// Add GPS Timestamp
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x0E; // GPS Time Field Size
|
2023-11-20 22:17:20 -04:00
|
|
|
packet.payload[packet.payload_size++] = 0xD3; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_double(packet, (double) tv.tv_sec);
|
|
|
|
put_int(packet, tv.tv_usec / (AP_MSEC_PER_WEEK * 1000000ULL));
|
|
|
|
put_int(packet, 0);
|
|
|
|
|
|
|
|
// Add GNSS Fix Information
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x08; // GNSS Fix Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x0B; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x00; // Fix type
|
|
|
|
packet.payload[packet.payload_size++] = 19; // Sat count
|
|
|
|
put_int(packet, 0); // Fix flags
|
|
|
|
put_int(packet, 0); // Valid flags
|
|
|
|
|
|
|
|
// Add GNSS LLH position
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x2C; // GNSS LLH Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x03; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_double(packet, fdm.latitude);
|
|
|
|
put_double(packet, fdm.longitude);
|
|
|
|
put_double(packet, 0); // Height above ellipsoid - unused
|
|
|
|
put_double(packet, fdm.altitude);
|
|
|
|
put_float(packet, 0.5f); // Horizontal accuracy
|
|
|
|
put_float(packet, 0.5f); // Vertical accuracy
|
2021-09-30 05:35:51 -03:00
|
|
|
put_int(packet, 31); // Valid flags
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
// Add DOP Data
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x20; // DOP Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x07; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_float(packet, 0); // GDOP
|
|
|
|
put_float(packet, 0); // PDOP
|
|
|
|
put_float(packet, 0); // HDOP
|
|
|
|
put_float(packet, 0); // VDOP
|
|
|
|
put_float(packet, 0); // TDOP
|
|
|
|
put_float(packet, 0); // NDOP
|
|
|
|
put_float(packet, 0); // EDOP
|
|
|
|
put_int(packet, 127);
|
|
|
|
|
|
|
|
// Add GNSS NED velocity
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x24; // GNSS NED Velocity Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x05; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_float(packet, fdm.speedN);
|
|
|
|
put_float(packet, fdm.speedE);
|
|
|
|
put_float(packet, fdm.speedD);
|
2021-09-30 05:35:51 -03:00
|
|
|
put_float(packet, 0); //speed - unused
|
|
|
|
put_float(packet, 0); //ground speed - unused
|
|
|
|
put_float(packet, 0); //heading - unused
|
|
|
|
put_float(packet, 0.25f); //speed accuracy
|
|
|
|
put_float(packet, 0); //heading accuracy - unused
|
|
|
|
put_int(packet, 31); //valid flags
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
packet.header[3] = packet.payload_size;
|
|
|
|
|
|
|
|
|
|
|
|
send_packet(packet);
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
|
|
|
|
|
2023-08-27 23:24:17 -03:00
|
|
|
void MicroStrain5::send_filter_packet(void)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
const auto &fdm = _sitl->state;
|
2023-08-04 18:31:14 -03:00
|
|
|
MicroStrain_Packet packet;
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
simulation_timeval(&tv);
|
|
|
|
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.header[0] = 0x75; // Sync One
|
|
|
|
packet.header[1] = 0x65; // Sync Two
|
|
|
|
packet.header[2] = 0x82; // Filter Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
// Add GPS Timestamp Shared Data
|
|
|
|
packet.payload[packet.payload_size++] = 0x0E; // GPS Timestamp Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0xD3; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_double(packet, (double) tv.tv_usec / 1e6);
|
|
|
|
put_int(packet, tv.tv_usec / (AP_MSEC_PER_WEEK * 1000000ULL));
|
|
|
|
put_int(packet, 0x0001);
|
|
|
|
|
|
|
|
// Add GNSS Filter velocity
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x10; // GNSS Velocity Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x02; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_float(packet, fdm.speedN);
|
|
|
|
put_float(packet, fdm.speedE);
|
|
|
|
put_float(packet, fdm.speedD);
|
|
|
|
put_int(packet, 0x0001);
|
|
|
|
|
|
|
|
// Add Filter LLH position
|
2021-09-30 05:35:51 -03:00
|
|
|
packet.payload[packet.payload_size++] = 0x1C; // Filter LLH Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x01; // Descriptor
|
2021-09-23 19:26:03 -03:00
|
|
|
put_double(packet, fdm.latitude);
|
|
|
|
put_double(packet, fdm.longitude);
|
2021-09-30 05:35:51 -03:00
|
|
|
put_double(packet, 0); // Height above ellipsoid - unused
|
|
|
|
put_int(packet, 0x0001); // Valid flags
|
2021-09-23 19:26:03 -03:00
|
|
|
|
2021-09-30 05:35:51 -03:00
|
|
|
// Add Filter State
|
|
|
|
packet.payload[packet.payload_size++] = 0x08; // Filter State Field Size
|
|
|
|
packet.payload[packet.payload_size++] = 0x10; // Descriptor
|
|
|
|
put_int(packet, 0x02); // Filter state (Running, Solution Valid)
|
|
|
|
put_int(packet, 0x03); // Dynamics mode (Airborne)
|
|
|
|
put_int(packet, 0); // Filter flags (None, no warnings)
|
2021-09-23 19:26:03 -03:00
|
|
|
|
|
|
|
packet.header[3] = packet.payload_size;
|
|
|
|
|
|
|
|
|
|
|
|
send_packet(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-08-04 18:31:14 -03:00
|
|
|
send MicroStrain data
|
2021-09-23 19:26:03 -03:00
|
|
|
*/
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::update(void)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
if (!init_sitl_pointer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
uint32_t ms_between_imu_packets = 40;
|
|
|
|
uint32_t ms_between_gnss_packets = 500;
|
|
|
|
uint32_t ms_between_filter_packets = 40;
|
2021-09-23 19:26:03 -03:00
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - last_imu_pkt_ms >= ms_between_imu_packets) {
|
|
|
|
last_imu_pkt_ms = now;
|
2021-09-23 19:26:03 -03:00
|
|
|
send_imu_packet();
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
if (now - last_gnss_pkt_ms >= ms_between_gnss_packets) {
|
|
|
|
last_gnss_pkt_ms = now;
|
2021-09-23 19:26:03 -03:00
|
|
|
send_gnss_packet();
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
if (now - last_filter_pkt_ms >= ms_between_filter_packets) {
|
|
|
|
last_filter_pkt_ms = now;
|
2021-09-23 19:26:03 -03:00
|
|
|
send_filter_packet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::put_float(MicroStrain_Packet &packet, float f)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
uint32_t fbits = 0;
|
|
|
|
memcpy(&fbits, &f, sizeof(fbits));
|
|
|
|
put_be32_ptr(&packet.payload[packet.payload_size], fbits);
|
|
|
|
packet.payload_size += sizeof(float);
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::put_double(MicroStrain_Packet &packet, double d)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
uint64_t dbits = 0;
|
|
|
|
memcpy(&dbits, &d, sizeof(dbits));
|
|
|
|
put_be64_ptr(&packet.payload[packet.payload_size], dbits);
|
|
|
|
packet.payload_size += sizeof(double);
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:17:20 -04:00
|
|
|
void MicroStrain::put_int(MicroStrain_Packet &packet, uint16_t t)
|
2021-09-23 19:26:03 -03:00
|
|
|
{
|
|
|
|
put_be16_ptr(&packet.payload[packet.payload_size], t);
|
|
|
|
packet.payload_size += sizeof(uint16_t);
|
2021-09-30 05:35:51 -03:00
|
|
|
}
|
|
|
|
|