yy1717
2022-12-08 f7a18ec4494b9c5c9ef3fd440bbf68ffc6425e18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//
// Created by YY on 2019/12/23.
//
 
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <thread>
#include "rtk.h"
#include "parse_gps.h"
#include "../common/serial_port.h"
#include "../jni_log.h"
#include "../driver_test.h"
#include "../utils/num.h"
#include "../defs.h"
#include "../common/apptimer.h"
#include "../rtk_platform/platform.h"
#include "../native-lib.h"
#include "virtual_rtk.h"
#include "../mcu/mcu_if.h"
#include "../mcu/ahp.h"
 
#define DEBUG(fmt, args...)     LOGD("<rtk> <%s>: " fmt, __func__, ##args)
 
#define PARSE_BUFF_SIZE         4096
 
const static char FACTORY[] = "FRESET\r\n";
const static char REBOOT[] = "RESET\r\n";
const static char INQ_PJK_PARAM[] = "LOG PJKPARA\r\n";
const static char AY_PJKPARAM[] = "set pjkpara 6378137 298.257223563 29.51245330924 106.4553361945 0 0\r\n";
const static char UNLOGALL[] = "unlogall\r\n";
const static char IFCOM2[] = "interfacemode com2 auto auto on\r\n";
const static char SAVECONFIG[] = "saveconfig\r\n";
const static char *PJKITEMS[] = {"gptra", "ptnlpjk"};
const static char *GPSITEMS[] = {"gpgga", "gprmc", "gpvtg"};
const char CMD_VERSION[] = "log version\r\n";
 
static gpsStatus_t gpsStatus;
static char rtkModel[32] = {0};
 
static rtk_info CurrRTKInfo;
static bool needSetPjk = false;
static int lostCnt;
 
static void CheckPjkParam(void);
static void CheckPjkParamTimeout(apptimer_var_t val);
static int WriteBluetooth(int id, const void *buf, int len);
static void GetModuleVersion(void);
 
static void VersionTimeout(apptimer_var_t val);
static void GpsDataTimeout(apptimer_var_t val);
 
// 蓝牙连接后
void ConfigRTKModuleLater(void)
{
    AppTimer_delete(GpsDataTimeout);
    AppTimer_delete(VersionTimeout);
    AppTimer_delete(CheckPjkParamTimeout);
    AppTimer_add(CheckPjkParamTimeout, D_SEC(1));
}
 
void FactorySettings(void)
{
    WriteRtkCommand(FACTORY, strlen(FACTORY));
}
 
void RebootModule(void)
{
    WriteRtkCommand(REBOOT, strlen(REBOOT));
}
 
void handleRTKRebootComp(const struct nmea *s)
{
    DEBUG("RTK Reboot complete!!");
//    CheckPjkParam();
    GetModuleVersion();
}
 
void handlePJKParam(const struct nmea *s) {
    DEBUG("handlePJKParam");
 
    AppTimer_delete(CheckPjkParamTimeout);
 
    SetAYFactoryParam(5);
    needSetPjk = true;
 
    lostCnt = 0;
    AppTimer_delete(GpsDataTimeout);
    AppTimer_add(GpsDataTimeout, D_SEC(5));
 
    uint8_t buff[33];
 
    memcpy(buff, rtkModel, 32);
    buff[32] = 1;
    PlatformStatusChanged(RTK_STATUS_EVT, buff, 33);
}
 
void SetAYFactoryParam(int freq)
{
    WriteRtkCommand(UNLOGALL, strlen(UNLOGALL));
    WriteRtkCommand(IFCOM2, strlen(IFCOM2));
 
    if (freq == 0)
        freq = 5;
 
    for (int i = 0; i < sizeof(PJKITEMS)/ sizeof(PJKITEMS[0]); ++i) {
        char cmd[64];
        sprintf(cmd, "log com1 %s ontime %0.1f\r\n", PJKITEMS[i], 1.0/(double)freq);
        WriteRtkCommand(cmd, strlen(cmd));
    }
 
    for (int i = 0; i <  sizeof(GPSITEMS)/ sizeof(GPSITEMS[0]); ++i) {
        char cmd[64];
        sprintf(cmd, "log com1 %s ontime %0.1f\r\n", GPSITEMS[i], 1.0/(double)freq);
        WriteRtkCommand(cmd, strlen(cmd));
    }
 
//    WriteSerialPort(RTK_MODULE_UART, AY_PJKPARAM, strlen(AY_PJKPARAM));
//    WriteSerialPort(RTK_MODULE_UART, SAVECONFIG, strlen(SAVECONFIG));
}
 
void SetPjkPara(int centLon)
{
    char buff[64];
 
    sprintf(buff, "set pjkpara 6378137 298.257223563 0 %d 0 500000\r\n", centLon);
 
    WriteRtkCommand(buff, strlen(buff));
 
    DEBUG("%s", buff);
}
 
void GetGpsStatus(gpsStatus_t &data)
{
    data = gpsStatus;
}
 
void handleUnrecognisedNMEA(const uint8_t *data, uint16_t length) {
    char buff[4096] = {0};
    memcpy(buff, data, MIN(length, 4000));
    DEBUG("handleUnrecognisedNMEA: %s", buff);
 
    if (length >= 100) {
        string cs(buff);
 
        if (cs.find("K708") != string::npos) {
            // 最初的标准基站模块
            DEBUG("K708 模块");
            strcpy(rtkModel, "K708");
 
            AppTimer_delete(VersionTimeout);
            CheckPjkParam();
        } else if (cs.find("K726") != string::npos) {
            // 移动站模块,也可以用做基站功能
            DEBUG("K726 模块");
            strcpy(rtkModel, "K726");
 
            AppTimer_delete(VersionTimeout);
            CheckPjkParam();
        }
    }
}
 
static bool bbs = false;
 
void handleGPGGA(const struct nmea *s)
{
    static uint32_t qfCnt = 0;
 
//    DEBUG("handleGPGGA num = %d", s->nmea_num);
    if (s->nmea_num >= 10) {
 
        if (!bbs) {
            bbs = true;
            RebootModule();
        }
 
        lostCnt = 0;
        AppTimer_delete(GpsDataTimeout);
        AppTimer_add(GpsDataTimeout, D_SEC(5));
 
        gpsStatus.gps_status = str2int(s->nmea_value[5].data, s->nmea_value[5].length);
 
        int hh = str2int(s->nmea_value[0].data, 2);
        int mm = str2int(s->nmea_value[0].data + 2, 2);
        int ss = str2int(s->nmea_value[0].data + 4, 2);
        int mss = str2int(s->nmea_value[0].data + 7, 2);
 
        double lat1, lat2, lon1, lon2, alt;
        str2float(&lat1, s->nmea_value[1].data, 2);
        str2float(&lat2, s->nmea_value[1].data+2, s->nmea_value[1].length-2);
 
        str2float(&lon1, s->nmea_value[3].data, 3);
        str2float(&lon2, s->nmea_value[3].data+3, s->nmea_value[3].length-3);
 
        lat1 = lat1 + lat2/60.0;
        lon1 = lon1 + lon2/60.0;
 
        str2float(&alt, s->nmea_value[8].data, s->nmea_value[8].length);
 
        gpsStatus.latitude = lat1;
        gpsStatus.longitude = lon1;
        gpsStatus.altitude = alt;
        gpsStatus.satNum = str2int(s->nmea_value[6].data, s->nmea_value[6].length);
 
        if (hh == gpsStatus.hh && mm == gpsStatus.mm && ss == gpsStatus.ss && mss == gpsStatus.mss) {
            // 同步的RMC消息也收集了
            PlatformStatusChanged(GPS_UPDATE_EVT, (uint8_t *)&gpsStatus, sizeof(gpsStatus));
        }
        gpsStatus.hh = hh;
        gpsStatus.mm = mm;
        gpsStatus.ss = ss;
        gpsStatus.mss = mss;
 
        // 计算中央子午线
        int qf = str2int(s->nmea_value[5].data, s->nmea_value[5].length);
 
        if (qf > 0) {
            qfCnt++;
            if (needSetPjk && qfCnt >= 3) {
                needSetPjk = false;
 
                DEBUG("set pjkpara 原始经度 %f", lon1);
                SetPjkPara(((int) (lon1 / 3.0 + 0.5)) * 3);
            }
        } else {
            qfCnt = 0;
        }
    }
}
 
void handleGPGSA(const struct nmea *s) {
}
 
void handleGPGSV(const struct nmea *s) {
}
 
void handleGPRMC(const struct nmea *s)
{
//    DEBUG("handleGPRMC num = %d", s->nmea_num);
 
    if (s->nmea_num >= 9) {
        int hh = str2int(s->nmea_value[0].data, 2);
        int mm = str2int(s->nmea_value[0].data + 2, 2);
        int ss = str2int(s->nmea_value[0].data + 4, 2);
        int mss = str2int(s->nmea_value[0].data + 7, 2);
 
        gpsStatus.DD = str2int(s->nmea_value[8].data, 2);
        gpsStatus.MM = str2int(s->nmea_value[8].data + 2, 2);
        gpsStatus.YY = str2int(s->nmea_value[8].data + 4, 2);
 
        double speed;
 
        str2float(&speed, s->nmea_value[6].data, s->nmea_value[6].length);
 
        gpsStatus.speed = speed * 1.85184;          // 节 -> 公里
 
        double trackTure;
        str2float(&trackTure, s->nmea_value[7].data, s->nmea_value[7].length);
        gpsStatus.trackTure = trackTure;
 
        if (hh == gpsStatus.hh && mm == gpsStatus.mm && ss == gpsStatus.ss && mss == gpsStatus.mss) {
            // 同步的GGA消息也收集了
            PlatformStatusChanged(GPS_UPDATE_EVT, (uint8_t *)&gpsStatus, sizeof(gpsStatus));
        }
        gpsStatus.hh = hh;
        gpsStatus.mm = mm;
        gpsStatus.ss = ss;
        gpsStatus.mss = mss;
    }
}
 
const char *GPS_SOL_OK = "SOL_COMPUTED";
const char *GPS_SOL_OBS = "INSUFFICIENT_OBS";
const char *GPS_SOL_CS = "COLD_START";
 
const char *GPS_POS_NONE = "NONE";
const char *GPS_POS_FIX = "FIXEDPOS";
const char *GPS_POS_SIG = "SINGLE";
const char *GPS_POS_PDIFF = "PSRDIFF";
const char *GPS_POS_NARF = "NARROW_FLOAT";
const char *GPS_POS_WIDI = "WIDE_INT";
const char *GPS_POS_NARI = "NARROW_INT";
const char *GPS_POS_SWIDL = "SUPER WIDE-LANE";
 
void handleBESTPOSA(const struct nmea *s) {
    DEBUG("handleBESTPOSA num = %d", s->nmea_num);
    double lat, lon;
 
    if (memcmp(s->nmea_value[0].data, GPS_SOL_OK, s->nmea_value[0].length)) {
        str2float(&lat, s->nmea_value[2].data, s->nmea_value[2].length);
        str2float(&lon, s->nmea_value[3].data, s->nmea_value[3].length);
    }
}
 
void handleGPVTG(const struct nmea *s) {
}
 
void handleGPGLL(const struct nmea *s) {
}
 
void handleGPZDA(const struct nmea *s) {
}
 
void handlePJK(const struct nmea *s) {
//    DEBUG("handlePJK num = %d", s->nmea_num);
 
    int hh = str2int(s->nmea_value[0].data, 2);
    int mm = str2int(s->nmea_value[0].data + 2, 2);
    int ss = str2int(s->nmea_value[0].data + 4, 2);
    int dss = str2int(s->nmea_value[0].data + 7, 2);
 
    CurrRTKInfo.MM = str2int(s->nmea_value[1].data, 2);
    CurrRTKInfo.DD = str2int(s->nmea_value[1].data + 2, 2);
    CurrRTKInfo.YY = str2int(s->nmea_value[1].data + 4, 2);
 
    CurrRTKInfo.qf = str2int(s->nmea_value[6].data, s->nmea_value[6].length);
 
    // NOTE: RTK模块是以南北向为X轴,西东向为Y轴,我们交换下,以符合一般逻辑
    str2float(&CurrRTKInfo.y, s->nmea_value[2].data, s->nmea_value[2].length);
    str2float(&CurrRTKInfo.x, s->nmea_value[4].data, s->nmea_value[4].length);
 
    if (CurrRTKInfo.hh == hh && CurrRTKInfo.mm == mm && CurrRTKInfo.ss == ss && CurrRTKInfo.dss == dss) {
        PlatformStatusChanged(RTK_UPDATE_EVT, (uint8_t *)&CurrRTKInfo, sizeof(CurrRTKInfo));
//        UpdateRTKInfo(&CurrRTKInfo);
//        up_num++;
        /*if ((up_num % 5) == 0)*/ {
//            NewMgrEvent(DRIVER_UPDATE_EVT);
        }
    }
    CurrRTKInfo.hh = hh;
    CurrRTKInfo.mm = mm;
    CurrRTKInfo.ss = ss;
    CurrRTKInfo.dss = dss;
}
 
void handleGPTRA(const struct nmea *s) {
//    DEBUG("handleGPTRA num = %d", s->nmea_num);
 
    int hh = str2int(s->nmea_value[0].data, 2);
    int mm = str2int(s->nmea_value[0].data + 2, 2);
    int ss = str2int(s->nmea_value[0].data + 4, 2);
    int dss = str2int(s->nmea_value[0].data + 7, 2);
 
    str2float(&CurrRTKInfo.heading, s->nmea_value[1].data, s->nmea_value[1].length);
    str2float(&CurrRTKInfo.pitch, s->nmea_value[2].data, s->nmea_value[2].length);
    str2float(&CurrRTKInfo.roll, s->nmea_value[3].data, s->nmea_value[3].length);
 
    if (CurrRTKInfo.hh == hh && CurrRTKInfo.mm == mm && CurrRTKInfo.ss == ss && CurrRTKInfo.dss == dss) {
        PlatformStatusChanged(RTK_UPDATE_EVT, (uint8_t *)&CurrRTKInfo, sizeof(CurrRTKInfo));
//        UpdateRTKInfo(&CurrRTKInfo);
//        up_num++;
        /*if ((up_num % 5) == 0)*/ {
//            NewMgrEvent(DRIVER_UPDATE_EVT);
        }
    }
    CurrRTKInfo.hh = hh;
    CurrRTKInfo.mm = mm;
    CurrRTKInfo.ss = ss;
    CurrRTKInfo.dss = dss;
}
 
static void CheckPjkParam(void)
{
    WriteRtkCommand(INQ_PJK_PARAM, strlen(INQ_PJK_PARAM));
    DEBUG("获取PJK参数...");
    AppTimer_delete(CheckPjkParamTimeout);
    AppTimer_add(CheckPjkParamTimeout, D_SEC(3));
}
 
static void CheckPjkParamTimeout(apptimer_var_t val) {
    DEBUG("获取PJK参数超时");
 
    uint8_t buff[33];
    memcpy(buff, rtkModel, 32);
    buff[32] = 0;
    PlatformStatusChanged(RTK_STATUS_EVT, buff, 33);
 
    GetModuleVersion();
}
 
static int WriteBluetooth(int id, const void *buf, int len)
{
//    SendToBluetooth((uint8_t *)buf, len);
//    SendMcuCommand(0x000B, (uint8_t *)buf, len);
    return len;
}
 
static void GetModuleVersion(void)
{
    AppTimer_delete(VersionTimeout);
    AppTimer_add(VersionTimeout, D_SEC(3));
    WriteRtkCommand(CMD_VERSION, strlen(CMD_VERSION));
 
    DEBUG("获取版本...");
}
 
static void VersionTimeout(apptimer_var_t val)
{
    DEBUG("版本获取超时");
    GetModuleVersion();
 
    uint8_t buff[33];
    memcpy(buff, rtkModel, 32);
    buff[32] = 0;
    PlatformStatusChanged(RTK_STATUS_EVT, buff, 33);
}
 
static void GpsDataTimeout(apptimer_var_t val)
{
    if (++lostCnt >= 3) {
        DEBUG("RTK模块收不到GPS数据");
        GetModuleVersion();
    } else {
        AppTimer_add(GpsDataTimeout, D_SEC(5));
    }
}