Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_GPS/AP_GPS_SITL.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415#include "AP_GPS_SITL.h"1617#if HAL_SIM_GPS_ENABLED1819#include <ctype.h>20#include <stdint.h>21#include <stdlib.h>22#include <stdio.h>23#include <sys/time.h>2425extern const AP_HAL::HAL& hal;2627/*28return GPS time of week in milliseconds29*/30/*31get timeval using simulation time32*/33static void simulation_timeval(struct timeval *tv)34{35uint64_t now = AP_HAL::micros64();36static uint64_t first_usec;37static struct timeval first_tv;38if (first_usec == 0) {39first_usec = now;40first_tv.tv_sec = AP::sitl()->start_time_UTC;41}42*tv = first_tv;43tv->tv_sec += now / 1000000ULL;44uint64_t new_usec = tv->tv_usec + (now % 1000000ULL);45tv->tv_sec += new_usec / 1000000ULL;46tv->tv_usec = new_usec % 1000000ULL;47}48static void gps_time(uint16_t *time_week, uint32_t *time_week_ms)49{50struct timeval tv;51simulation_timeval(&tv);52const uint32_t epoch = 86400*(10*365 + (1980-1969)/4 + 1 + 6 - 2) - (GPS_LEAPSECONDS_MILLIS / 1000ULL);53uint32_t epoch_seconds = tv.tv_sec - epoch;54*time_week = epoch_seconds / AP_SEC_PER_WEEK;55uint32_t t_ms = tv.tv_usec / 1000;56// round time to nearest 200ms57*time_week_ms = (epoch_seconds % AP_SEC_PER_WEEK) * AP_MSEC_PER_SEC + ((t_ms/200) * 200);58}5960bool AP_GPS_SITL::read(void)61{62const uint32_t now = AP_HAL::millis();63if (now - last_update_ms < 200) {64return false;65}66last_update_ms = now;6768auto *sitl = AP::sitl();6970double latitude =sitl->state.latitude;71double longitude = sitl->state.longitude;72float altitude = sitl->state.altitude;73const double speedN = sitl->state.speedN;74const double speedE = sitl->state.speedE;75const double speedD = sitl->state.speedD;76// const double yaw = sitl->state.yawDeg;7778uint16_t time_week;79uint32_t time_week_ms;8081gps_time(&time_week, &time_week_ms);8283state.time_week = time_week;84state.time_week_ms = time_week_ms;85state.status = AP_GPS::GPS_OK_FIX_3D;86state.num_sats = 15;8788state.location = Location{89int32_t(latitude*1e7),90int32_t(longitude*1e7),91int32_t(altitude*100),92Location::AltFrame::ABSOLUTE93};9495state.hdop = 100;96state.vdop = 100;9798state.have_vertical_velocity = true;99state.velocity.x = speedN;100state.velocity.y = speedE;101state.velocity.z = speedD;102103velocity_to_speed_course(state);104105state.have_speed_accuracy = true;106state.have_horizontal_accuracy = true;107state.have_vertical_accuracy = true;108state.have_vertical_velocity = true;109110// state.horizontal_accuracy = pkt.horizontal_pos_accuracy;111// state.vertical_accuracy = pkt.vertical_pos_accuracy;112// state.speed_accuracy = pkt.horizontal_vel_accuracy;113114state.last_gps_time_ms = now;115116return true;117}118119#endif // HAL_SIM_GPS_ENABLED120121122