PX4 Firmware
PX4 Autopilot Software http://px4.io
mtk.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file mtk.h
36  *
37  * @author Thomas Gubler <thomasgubler@student.ethz.ch>
38  * @author Julian Oes <julian@oes.ch>
39  */
40 
41 #pragma once
42 
43 #include "gps_helper.h"
44 #include "../../definitions.h"
45 
46 #define MTK_SYNC1_V16 0xd0
47 #define MTK_SYNC1_V19 0xd1
48 #define MTK_SYNC2 0xdd
49 
50 #define MTK_OUTPUT_5HZ "$PMTK220,200*2C\r\n"
51 #define MTK_SET_BINARY "$PGCMD,16,0,0,0,0,0*6A\r\n"
52 #define MTK_SBAS_ON "$PMTK313,1*2E\r\n"
53 #define MTK_WAAS_ON "$PMTK301,2*2E\r\n"
54 #define MTK_NAVTHRES_OFF "$PMTK397,0*23\r\n"
55 
56 #define MTK_TIMEOUT_5HZ 400
57 #define MTK_BAUDRATE 38400
58 
59 typedef enum {
64 
65 /** the structures of the binary packets */
66 #pragma pack(push, 1)
67 
68 typedef struct {
69  uint8_t payload; ///< Number of payload bytes
70  int32_t latitude; ///< Latitude in degrees * 10^7
71  int32_t longitude; ///< Longitude in degrees * 10^7
72  uint32_t msl_altitude; ///< MSL altitude in meters * 10^2
73  uint32_t ground_speed; ///< velocity in m/s
74  int32_t heading; ///< heading in degrees * 10^2
75  uint8_t satellites; ///< number of satellites used
76  uint8_t fix_type; ///< fix type: XXX correct for that
77  uint32_t date;
78  uint32_t utc_time;
79  uint16_t hdop; ///< horizontal dilution of position (without unit)
80  uint8_t ck_a;
81  uint8_t ck_b;
83 
84 #pragma pack(pop)
85 
86 #define MTK_RECV_BUFFER_SIZE 40
87 
88 class GPSDriverMTK : public GPSHelper
89 {
90 public:
91  GPSDriverMTK(GPSCallbackPtr callback, void *callback_user, struct vehicle_gps_position_s *gps_position);
92  virtual ~GPSDriverMTK() = default;
93 
94  int receive(unsigned timeout) override;
95  int configure(unsigned &baudrate, OutputMode output_mode) override;
96 
97 private:
98  /**
99  * Parse the binary MTK packet
100  */
101  int parseChar(uint8_t b, gps_mtk_packet_t &packet);
102 
103  /**
104  * Handle the package once it has arrived
105  */
106  void handleMessage(gps_mtk_packet_t &packet);
107 
108  /**
109  * Reset the parse state machine for a fresh start
110  */
111  void decodeInit();
112 
113  /**
114  * While parsing add every byte (except the sync bytes) to the checksum
115  */
116  void addByteToChecksum(uint8_t);
117 
118  struct vehicle_gps_position_s *_gps_position {nullptr};
120  uint8_t _mtk_revision{0};
121  unsigned _rx_count{};
122  uint8_t _rx_ck_a{};
123  uint8_t _rx_ck_b{};
124 };
int32_t longitude
Longitude in degrees * 10^7.
Definition: mtk.h:71
uint32_t ground_speed
velocity in m/s
Definition: mtk.h:73
uint32_t utc_time
Definition: mtk.h:78
static enum ST24_DECODE_STATE _decode_state
Definition: st24.cpp:66
uint8_t ck_b
Definition: mtk.h:81
uint8_t ck_a
Definition: mtk.h:80
int32_t latitude
Latitude in degrees * 10^7.
Definition: mtk.h:70
int(* GPSCallbackPtr)(GPSCallbackType type, void *data1, int data2, void *user)
Callback function for platform-specific stuff.
Definition: gps_helper.h:132
uint32_t date
Definition: mtk.h:77
uint8_t fix_type
fix type: XXX correct for that
Definition: mtk.h:76
mtk_decode_state_t
Definition: mtk.h:59
uint8_t satellites
number of satellites used
Definition: mtk.h:75
uint8_t payload
Number of payload bytes.
Definition: mtk.h:69
int32_t heading
heading in degrees * 10^2
Definition: mtk.h:74
uint32_t msl_altitude
MSL altitude in meters * 10^2.
Definition: mtk.h:72
uint16_t hdop
horizontal dilution of position (without unit)
Definition: mtk.h:79
the structures of the binary packets
Definition: mtk.h:68