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_DAL/AP_DAL.h
Views: 1798
#pragma once12#include "AP_DAL_InertialSensor.h"3#include "AP_DAL_Baro.h"4#include "AP_DAL_GPS.h"5#include "AP_DAL_RangeFinder.h"6#include "AP_DAL_Compass.h"7#include "AP_DAL_Airspeed.h"8#include "AP_DAL_Beacon.h"9#include "AP_DAL_VisualOdom.h"1011#include "LogStructure.h"1213#include <stdio.h>14#include <stdint.h>15#include <cstddef>161718#define DAL_CORE(c) AP::dal().logging_core(c)1920class NavEKF2;21class NavEKF3;2223class AP_DAL {24public:2526enum class FrameType : uint8_t {27InitialiseFilterEKF2 = 1U<<0,28UpdateFilterEKF2 = 1U<<1,29InitialiseFilterEKF3 = 1<<2,30UpdateFilterEKF3 = 1<<3,31LogWriteEKF2 = 1<<4,32LogWriteEKF3 = 1<<5,33};3435enum class Event : uint8_t {36resetGyroBias = 0,37resetHeightDatum = 1,38//setInhibitGPS = 2, // removed39//setTakeoffExpected = 3, // removed40//unsetTakeoffExpected = 4, // removed41//setTouchdownExpected = 5, // removed42//unsetTouchdownExpected = 6, // removed43//setInhibitGpsVertVelUse = 7, // removed44//unsetInhibitGpsVertVelUse = 8, // removed45setTerrainHgtStable = 9,46unsetTerrainHgtStable = 10,47requestYawReset = 11,48checkLaneSwitch = 12,49setSourceSet0 = 13,50setSourceSet1 = 14,51setSourceSet2 = 15,52};5354// must remain the same as AP_AHRS_VehicleClass numbers-wise55enum class VehicleClass : uint8_t {56UNKNOWN,57GROUND,58COPTER,59FIXED_WING,60SUBMARINE,61};6263AP_DAL() {}6465static AP_DAL *get_singleton() {66if (!_singleton) {67_singleton = NEW_NOTHROW AP_DAL();68}69return _singleton;70}7172void start_frame(FrameType frametype);73void end_frame(void);74uint64_t micros64() const { return _RFRH.time_us; }75uint32_t micros() const { return _micros; }76uint32_t millis() const { return _millis; }7778void log_event2(Event event);79void log_SetOriginLLH2(const Location &loc);80void log_writeDefaultAirSpeed2(const float aspeed, const float uncertainty);8182void log_event3(Event event);83void log_SetOriginLLH3(const Location &loc);84void log_SetLatLng(const Location &loc, float posAccuracy, uint32_t timestamp_ms);8586void log_writeDefaultAirSpeed3(const float aspeed, const float uncertainty);87void log_writeEulerYawAngle(float yawAngle, float yawAngleErr, uint32_t timeStamp_ms, uint8_t type);8889enum class StateMask {90ARMED = (1U<<0),91};9293// EKF ID for timing checks94enum class EKFType : uint8_t {95EKF2 = 0,96EKF3 = 1,97};9899// check if we are low on CPU for this core100bool ekf_low_time_remaining(EKFType etype, uint8_t core);101102// returns armed state for the current frame103bool get_armed() const { return _RFRN.armed; }104105// memory available at start of current frame. While this could106// potentially change as we go through the frame, the107// ramifications of being out of memory are that you don't start108// the EKF, so the simplicity of having one value for the entire109// frame is worthwhile.110#if CONFIG_HAL_BOARD == HAL_BOARD_SITL111uint32_t available_memory() const { return _RFRN.available_memory + 10240; }112#else113uint32_t available_memory() const { return _RFRN.available_memory; }114#endif115116int8_t get_ekf_type(void) const {117return _RFRN.ekf_type;118}119120int snprintf(char* str, size_t size, const char *format, ...) const;121122// copied in AP_HAL/Util.h123enum class MemoryType : uint8_t {124DMA_SAFE = 0,125FAST = 1,126};127void *malloc_type(size_t size, MemoryType mem_type) const;128void free_type(void *ptr, size_t size, MemoryType memtype) const;129130AP_DAL_InertialSensor &ins() { return _ins; }131AP_DAL_Baro &baro() { return _baro; }132AP_DAL_GPS &gps() { return _gps; }133134#if AP_RANGEFINDER_ENABLED135AP_DAL_RangeFinder *rangefinder() {136return _rangefinder;137}138#endif139140AP_DAL_Airspeed *airspeed() {141return _airspeed;142}143#if AP_BEACON_ENABLED144AP_DAL_Beacon *beacon() {145return _beacon;146}147#endif148#if HAL_VISUALODOM_ENABLED149AP_DAL_VisualOdom *visualodom() {150return _visualodom;151}152#endif153154AP_DAL_Compass &compass() { return _compass; }155156// random methods that AP_NavEKF3 wants to call on AHRS:157bool airspeed_sensor_enabled(void) const {158return _RFRN.ahrs_airspeed_sensor_enabled;159}160161// this replaces AP::ahrs()->EAS2TAS(), which should probably go162// away in favour of just using the Baro method.163// get apparent to true airspeed ratio164float get_EAS2TAS(void) const {165return _RFRN.EAS2TAS;166}167168VehicleClass get_vehicle_class(void) const {169return (VehicleClass)_RFRN.vehicle_class;170}171172bool get_fly_forward(void) const {173return _RFRN.fly_forward;174}175176bool get_takeoff_expected(void) const {177return _RFRN.takeoff_expected;178}179180bool get_touchdown_expected(void) const {181return _RFRN.touchdown_expected;182}183184// for EKF usage to enable takeoff expected to true185void set_takeoff_expected();186187// get ahrs trim188const Vector3f &get_trim() const {189return _RFRN.ahrs_trim;190}191192const Matrix3f &get_rotation_vehicle_body_to_autopilot_body(void) const {193return _rotation_vehicle_body_to_autopilot_body;194}195196// get the home location. This is const to prevent any changes to197// home without telling AHRS about the change198const class Location &get_home(void) const {199return _home;200}201202uint32_t get_time_flying_ms(void) const {203return _RFRH.time_flying_ms;204}205206bool opticalflow_enabled(void) const {207return _RFRN.opticalflow_enabled;208}209210bool wheelencoder_enabled(void) const {211return _RFRN.wheelencoder_enabled;212}213214// log optical flow data215void writeOptFlowMeas(const uint8_t rawFlowQuality, const Vector2f &rawFlowRates, const Vector2f &rawGyroRates, const uint32_t msecFlowMeas, const Vector3f &posOffset, float heightOverride);216217// log external nav data218void writeExtNavData(const Vector3f &pos, const Quaternion &quat, float posErr, float angErr, uint32_t timeStamp_ms, uint16_t delay_ms, uint32_t resetTime_ms);219void writeExtNavVelData(const Vector3f &vel, float err, uint32_t timeStamp_ms, uint16_t delay_ms);220221// log wheel odometry data222void writeWheelOdom(float delAng, float delTime, uint32_t timeStamp_ms, const Vector3f &posOffset, float radius);223void writeBodyFrameOdom(float quality, const Vector3f &delPos, const Vector3f &delAng, float delTime, uint32_t timeStamp_ms, uint16_t delay_ms, const Vector3f &posOffset);224225// Replay support:226void handle_message(const log_RFRH &msg) {227_RFRH = msg;228_micros = _RFRH.time_us;229_millis = _RFRH.time_us / 1000UL;230}231void handle_message(const log_RFRN &msg) {232_RFRN = msg;233_home.lat = msg.lat;234_home.lng = msg.lng;235_home.alt = msg.alt;236}237void handle_message(const log_RFRF &msg, NavEKF2 &ekf2, NavEKF3 &ekf3);238239void handle_message(const log_RISH &msg) {240_ins.handle_message(msg);241}242void handle_message(const log_RISI &msg) {243_ins.handle_message(msg);244}245246void handle_message(const log_RASH &msg) {247if (_airspeed == nullptr) {248_airspeed = NEW_NOTHROW AP_DAL_Airspeed;249}250_airspeed->handle_message(msg);251}252void handle_message(const log_RASI &msg) {253if (_airspeed == nullptr) {254_airspeed = NEW_NOTHROW AP_DAL_Airspeed;255}256_airspeed->handle_message(msg);257}258259void handle_message(const log_RBRH &msg) {260_baro.handle_message(msg);261}262void handle_message(const log_RBRI &msg) {263_baro.handle_message(msg);264}265266void handle_message(const log_RRNH &msg) {267#if AP_RANGEFINDER_ENABLED268if (_rangefinder == nullptr) {269_rangefinder = NEW_NOTHROW AP_DAL_RangeFinder;270}271_rangefinder->handle_message(msg);272#endif273}274void handle_message(const log_RRNI &msg) {275#if AP_RANGEFINDER_ENABLED276if (_rangefinder == nullptr) {277_rangefinder = NEW_NOTHROW AP_DAL_RangeFinder;278}279_rangefinder->handle_message(msg);280#endif281}282283void handle_message(const log_RGPH &msg) {284_gps.handle_message(msg);285}286void handle_message(const log_RGPI &msg) {287_gps.handle_message(msg);288}289void handle_message(const log_RGPJ &msg) {290_gps.handle_message(msg);291}292293void handle_message(const log_RMGH &msg) {294_compass.handle_message(msg);295}296void handle_message(const log_RMGI &msg) {297_compass.handle_message(msg);298}299300void handle_message(const log_RBCH &msg) {301#if AP_BEACON_ENABLED302if (_beacon == nullptr) {303_beacon = NEW_NOTHROW AP_DAL_Beacon;304}305_beacon->handle_message(msg);306#endif307}308void handle_message(const log_RBCI &msg) {309#if AP_BEACON_ENABLED310if (_beacon == nullptr) {311_beacon = NEW_NOTHROW AP_DAL_Beacon;312}313_beacon->handle_message(msg);314#endif315}316void handle_message(const log_RVOH &msg) {317#if HAL_VISUALODOM_ENABLED318if (_visualodom == nullptr) {319_visualodom = NEW_NOTHROW AP_DAL_VisualOdom;320}321_visualodom->handle_message(msg);322#endif323}324void handle_message(const log_ROFH &msg, NavEKF2 &ekf2, NavEKF3 &ekf3);325void handle_message(const log_REPH &msg, NavEKF2 &ekf2, NavEKF3 &ekf3);326void handle_message(const log_REVH &msg, NavEKF2 &ekf2, NavEKF3 &ekf3);327void handle_message(const log_RWOH &msg, NavEKF2 &ekf2, NavEKF3 &ekf3);328void handle_message(const log_RBOH &msg, NavEKF2 &ekf2, NavEKF3 &ekf3);329void handle_message(const log_RSLL &msg, NavEKF2 &ekf2, NavEKF3 &ekf3);330331// map core number for replay332uint8_t logging_core(uint8_t c) const;333334#if HAL_LOGGING_ENABLED335// write out a DAL log message. If old_msg is non-null, then336// only write if the content has changed337static void WriteLogMessage(enum LogMessages msg_type, void *msg, const void *old_msg, uint8_t msg_size);338#endif339340private:341342static AP_DAL *_singleton;343344// framing structures345struct log_RFRH _RFRH;346struct log_RFRF _RFRF;347struct log_RFRN _RFRN;348349// push-based sensor structures350struct log_ROFH _ROFH;351struct log_REPH _REPH;352struct log_REVH _REVH;353struct log_RWOH _RWOH;354struct log_RBOH _RBOH;355struct log_RSLL _RSLL;356357// cached variables for speed:358uint32_t _micros;359uint32_t _millis;360361Matrix3f _rotation_vehicle_body_to_autopilot_body;362Location _home;363uint32_t _last_imu_time_us;364365AP_DAL_InertialSensor _ins;366AP_DAL_Baro _baro;367AP_DAL_GPS _gps;368#if AP_RANGEFINDER_ENABLED369AP_DAL_RangeFinder *_rangefinder;370#endif371AP_DAL_Compass _compass;372AP_DAL_Airspeed *_airspeed;373#if AP_BEACON_ENABLED374AP_DAL_Beacon *_beacon;375#endif376#if HAL_VISUALODOM_ENABLED377AP_DAL_VisualOdom *_visualodom;378#endif379380static bool logging_started;381static bool force_write;382383bool ekf2_init_done;384bool ekf3_init_done;385386void init_sensors(void);387bool init_done;388};389390#if HAL_LOGGING_ENABLED391#define WRITE_REPLAY_BLOCK(sname,v) AP_DAL::WriteLogMessage(LOG_## sname ##_MSG, &v, nullptr, offsetof(log_ ##sname, _end))392#define WRITE_REPLAY_BLOCK_IFCHANGED(sname,v,old) do { static_assert(sizeof(v) == sizeof(old), "types must match"); \393AP_DAL::WriteLogMessage(LOG_## sname ##_MSG, &v, &old, offsetof(log_ ##sname, _end)); } \394while (0)395#else396#define WRITE_REPLAY_BLOCK(sname,v) do { (void)v; } while (false)397#define WRITE_REPLAY_BLOCK_IFCHANGED(sname,v,old) do { (void)old; } while (false)398#endif399400namespace AP {401AP_DAL &dal();402};403404// replay printf for debugging405void rprintf(const char *format, ...);406407408409