123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #pragma once
- #include <AP_Common/AP_Common.h>
- #include <AP_HAL/AP_HAL.h>
- #include <AP_Common/Location.h>
- #include <AP_Filesystem/AP_Filesystem.h>
- #if HAVE_FILESYSTEM_SUPPORT && defined(HAL_BOARD_TERRAIN_DIRECTORY)
- #define AP_TERRAIN_AVAILABLE 1
- #else
- #define AP_TERRAIN_AVAILABLE 0
- #endif
- #if AP_TERRAIN_AVAILABLE
- #include <AP_Param/AP_Param.h>
- #include <AP_Mission/AP_Mission.h>
- #define TERRAIN_DEBUG 0
- #define TERRAIN_GRID_MAVLINK_SIZE 4
- #define TERRAIN_GRID_BLOCK_MUL_X 7
- #define TERRAIN_GRID_BLOCK_MUL_Y 8
- #define TERRAIN_GRID_BLOCK_SPACING_X ((TERRAIN_GRID_BLOCK_MUL_X-1)*TERRAIN_GRID_MAVLINK_SIZE)
- #define TERRAIN_GRID_BLOCK_SPACING_Y ((TERRAIN_GRID_BLOCK_MUL_Y-1)*TERRAIN_GRID_MAVLINK_SIZE)
- #define TERRAIN_GRID_BLOCK_SIZE_X (TERRAIN_GRID_MAVLINK_SIZE*TERRAIN_GRID_BLOCK_MUL_X)
- #define TERRAIN_GRID_BLOCK_SIZE_Y (TERRAIN_GRID_MAVLINK_SIZE*TERRAIN_GRID_BLOCK_MUL_Y)
- #define TERRAIN_GRID_BLOCK_CACHE_SIZE 12
- #define TERRAIN_GRID_FORMAT_VERSION 1
- #if TERRAIN_DEBUG
- #define ASSERT_RANGE(v,minv,maxv) assert((v)<=(maxv)&&(v)>=(minv))
- #else
- #define ASSERT_RANGE(v,minv,maxv)
- #endif
- class AP_Terrain {
- public:
- AP_Terrain(const AP_Mission &_mission);
-
- AP_Terrain(const AP_Terrain &other) = delete;
- AP_Terrain &operator=(const AP_Terrain&) = delete;
- static AP_Terrain *get_singleton(void) { return singleton; }
- enum TerrainStatus {
- TerrainStatusDisabled = 0,
- TerrainStatusUnhealthy = 1,
- TerrainStatusOK = 2
- };
- static const struct AP_Param::GroupInfo var_info[];
-
- void update(void);
- bool enabled() const { return enable; }
-
- enum TerrainStatus status(void) const { return system_status; }
-
- void send_request(mavlink_channel_t chan);
-
- void send_terrain_report(mavlink_channel_t chan, const Location &loc, bool extrapolate);
- void handle_data(mavlink_channel_t chan, const mavlink_message_t &msg);
- void handle_terrain_check(mavlink_channel_t chan, const mavlink_message_t &msg);
- void handle_terrain_data(const mavlink_message_t &msg);
-
- bool height_amsl(const Location &loc, float &height, bool corrected);
-
- bool height_terrain_difference_home(float &terrain_difference,
- bool extrapolate = false);
-
- bool height_relative_home_equivalent(float terrain_altitude,
- float &relative_altitude,
- bool extrapolate = false);
-
- bool height_above_terrain(float &terrain_altitude, bool extrapolate = false);
-
- float lookahead(float bearing, float distance, float climb_ratio);
-
- void log_terrain_data();
-
- void get_statistics(uint16_t &pending, uint16_t &loaded);
- private:
-
- bool allocate(void);
-
- struct PACKED grid_block {
-
- uint64_t bitmap;
-
- int32_t lat;
- int32_t lon;
-
- uint16_t crc;
-
- uint16_t version;
-
- uint16_t spacing;
-
- int16_t height[TERRAIN_GRID_BLOCK_SIZE_X][TERRAIN_GRID_BLOCK_SIZE_Y];
-
- uint16_t grid_idx_x;
- uint16_t grid_idx_y;
-
- int16_t lon_degrees;
- int8_t lat_degrees;
- };
-
- union grid_io_block {
- struct grid_block block;
- uint8_t buffer[2048];
- };
- enum GridCacheState {
- GRID_CACHE_INVALID=0,
- GRID_CACHE_DISKWAIT=1,
- GRID_CACHE_VALID=2,
- GRID_CACHE_DIRTY=3
-
- };
-
- struct grid_cache {
- struct grid_block grid;
- volatile enum GridCacheState state;
-
- uint32_t last_access_ms;
- };
-
- struct grid_info {
-
- int8_t lat_degrees;
- int16_t lon_degrees;
-
- int32_t grid_lat;
- int32_t grid_lon;
-
- uint16_t grid_idx_x;
- uint16_t grid_idx_y;
-
- uint8_t idx_x;
- uint8_t idx_y;
-
- float frac_x;
- float frac_y;
-
- uint32_t file_offset;
- };
-
- void calculate_grid_info(const Location &loc, struct grid_info &info) const;
-
- struct grid_cache &find_grid_cache(const struct grid_info &info);
-
- uint8_t grid_bitnum(uint8_t idx_x, uint8_t idx_y);
-
- bool check_bitmap(const struct grid_block &grid, uint8_t idx_x, uint8_t idx_y);
-
- bool request_missing(mavlink_channel_t chan, struct grid_cache &gcache);
- bool request_missing(mavlink_channel_t chan, const struct grid_info &info);
-
- void schedule_disk_io(void);
-
- uint8_t bitcount64(uint64_t b);
-
- int16_t find_io_idx(enum GridCacheState state);
- uint16_t get_block_crc(struct grid_block &block);
- void check_disk_read(void);
- void check_disk_write(void);
- void io_timer(void);
- void open_file(void);
- void seek_offset(void);
- void write_block(void);
- void read_block(void);
-
- void update_mission_data(void);
-
- void update_rally_data(void);
-
- AP_Int8 enable;
- AP_Int16 grid_spacing;
-
-
- const AP_Mission &mission;
-
- uint8_t cache_size = 0;
- struct grid_cache *cache = nullptr;
-
- enum DiskIoState {
- DiskIoIdle = 0,
- DiskIoWaitWrite = 1,
- DiskIoWaitRead = 2,
- DiskIoDoneRead = 3,
- DiskIoDoneWrite = 4
- };
- volatile enum DiskIoState disk_io_state;
- union grid_io_block disk_block;
-
- uint32_t last_request_time_ms[MAVLINK_COMM_NUM_BUFFERS];
- static const uint64_t bitmap_mask = (((uint64_t)1U)<<(TERRAIN_GRID_BLOCK_MUL_X*TERRAIN_GRID_BLOCK_MUL_Y)) - 1;
-
- int fd;
-
- bool timer_setup;
-
- int8_t file_lat_degrees;
- int16_t file_lon_degrees;
-
- volatile bool io_failure;
-
- bool directory_created;
-
- float home_height;
- Location home_loc;
-
-
-
- bool have_current_loc_height;
- float last_current_loc_height;
-
- uint16_t next_mission_index;
-
- uint8_t next_mission_pos;
-
- uint32_t last_mission_change_ms;
-
- uint16_t last_mission_spacing;
-
- uint16_t next_rally_index;
-
- uint32_t last_rally_change_ms;
-
- uint16_t last_rally_spacing;
- char *file_path = nullptr;
-
- enum TerrainStatus system_status = TerrainStatusDisabled;
- static AP_Terrain *singleton;
- };
- #endif
|