yy1717
2021-01-12 683b1595260e638d1d3c6cc0d6543a72f6d6f925
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//
// Created by YY on 2019/12/23.
//
 
#include <cstring>
#include <cstdio>
#include <pthread.h>
#include <cmath>
#include <cstdlib>
#include <cctype>
#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"
 
#define DEBUG(fmt, args...)     LOGD("<rtk> <%s>: " fmt, __func__, ##args)
 
#define RTK_MODULE_UART         UART_0
 
#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 int (*WriteRtk)(int id, const void *buf, int len);
 
static rtk_info CurrRTKInfo;
static bool needSetPjk = false;
static int lostCnt;
 
static void CheckPjkParam(void);
static void CheckPjkParamTimeout(union sigval sig);
static int WriteBluetooth(int id, const void *buf, int len);
static void GetModuleVersion(void);
 
static void VersionTimeout(union sigval sig);
static void GpsDataTimeout(union sigval sig);
 
static void *UartThread(void *p);
 
void RtkCommModeSel(int mode)
{
    if (mode == 0) {
        WriteRtk = WriteSerialPort;
    } else {
        WriteRtk = WriteBluetooth;
    }
}
 
void ConfigRTKModule(bool ayDevice)
{
    // TODO
    DEBUG("ConfigRTKModule");
 
    memset(&CurrRTKInfo, 0, sizeof(CurrRTKInfo));
    CurrRTKInfo.hh = -1;
 
    memset(&gpsStatus, 0, sizeof(gpsStatus));
    gpsStatus.hh = -1;
 
    if (ayDevice) {
        RtkCommModeSel(0);
 
        static struct serial_config serialConfig;
 
        strcpy(serialConfig.name, "/dev/ttyHSL0");
        serialConfig.baud = 115200;
        serialConfig.data_bit = 8;
        serialConfig.verify_bit = 'N';
        serialConfig.stop_bit = 1;
        serialConfig.flow_ctrl = 0;
 
        pthread_t pid;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//detached
        pthread_create(&pid, &attr, UartThread, &serialConfig);
    } else {
        RtkCommModeSel(1);
        // 等待蓝牙连接
    }
}
 
// 蓝牙连接后
void ConfigRTKModuleLater(void)
{
    AppTimer_delete(GpsDataTimeout);
    AppTimer_delete(VersionTimeout);
    AppTimer_delete(CheckPjkParamTimeout);
    AppTimer_add(CheckPjkParamTimeout, D_SEC(1));
}
 
void FactorySettings(void)
{
    WriteRtk(RTK_MODULE_UART, FACTORY, strlen(FACTORY));
}
 
void RebootModule(void)
{
    WriteRtk(RTK_MODULE_UART, 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)
{
    WriteRtk(RTK_MODULE_UART, UNLOGALL, strlen(UNLOGALL));
    WriteRtk(RTK_MODULE_UART, 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);
        WriteRtk(RTK_MODULE_UART, 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);
        WriteRtk(RTK_MODULE_UART, 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);
 
    WriteRtk(RTK_MODULE_UART, buff, strlen(buff));
 
    DEBUG("%s", buff);
}
 
void GetGpsStatus(gpsStatus_t &data)
{
    data = gpsStatus;
}
 
static void *UartThread(void *p) {
    struct serial_config *cfg = (struct serial_config *) p;
 
    int res = InitSerialPort(RTK_MODULE_UART, cfg->baud, cfg->data_bit, cfg->verify_bit, cfg->stop_bit, cfg->flow_ctrl);
    DEBUG("Serial %s open %d", cfg->name, res);
    uint8_t RxBuf[PARSE_BUFF_SIZE];
    int RxBufLen = 0;
 
    if (res == 0) {
//        CheckPjkParam();
        GetModuleVersion();
    }
 
    while (res == 0) {
        int ul = ReadSerialPort(RTK_MODULE_UART, (uint8_t *)RxBuf + RxBufLen, sizeof(RxBuf) - RxBufLen);
        RxBufLen += ul;
 
        /*{
            static char buffd[16384];
 
            buffd[0] = 0;
            int i = 0;
            for (i = 0; i < ul; i++) {
                if ((i % 32) == 0) {
                    sprintf(buffd + strlen(buffd), "\n");
                }
                sprintf(buffd + strlen(buffd), "%02X ", RxBuf[i]);
                if (strlen(buffd) > 800) {
                    DEBUG("%s <- %s...", "UART", buffd);
                    buffd[0] = 0;
                }
            }
            if (strlen(buffd) > 0)
                DEBUG("%s <- %s", "UART", buffd);
        }*/
 
        if (RxBufLen > 0) {
#if 1
            if (VirtualIsConnected()) {     //PC模拟用时
                static bool first = false;
 
                if (!first) {
                    first = true;
 
                    uint8_t buff[33] = "emulator";
                    buff[32] = 1;
                    PlatformStatusChanged(RTK_STATUS_EVT, buff, 33);
                }
                RxBufLen = 0;
            } else {
                const uint8_t *ptr = parseGPS(RxBuf, RxBuf + RxBufLen);
                if (ptr != RxBuf) {
                    memcpy(RxBuf, ptr, RxBufLen - (ptr - RxBuf));
                    RxBufLen -= ptr - RxBuf;
                } else if (RxBufLen == PARSE_BUFF_SIZE) {        //填满了,且没有一个\r,都抛弃
                    DEBUG("Parse GPS error");
                    RxBufLen = 0;
                }
            }
#else
            RxBufLen = 0;           //PC模拟用时
#endif
        }
    }
    if (res == 0) {
        UninitSerialPort(RTK_MODULE_UART);
    }
    pthread_exit(NULL);
}
 
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();
        }
    }
}
 
void handleGPGGA(const struct nmea *s)
{
    static uint32_t qfCnt = 0;
 
//    DEBUG("handleGPGGA num = %d", s->nmea_num);
    if (s->nmea_num >= 10) {
        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);
 
    static double sbx = -12;
    static double sby = 27;
 
//    CurrRTKInfo.x = (-2.8984 - 4.913)/2;
//    CurrRTKInfo.y = (31.6962 + 29.4974)/2;
 
    sbx += 0.01;
    sby += 0.01;
 
//    const double by1 = 28.013;
//    const double bx1 = -11.9669;
//
//    const double by2 = 29.3232;
//    const double bx2 = -9.5057;
//
//    static double xx = -10.9669, yy = 28.013;
//
//    CurrRTKInfo.y = yy;
//    CurrRTKInfo.x = xx;
//
//    if (forwardx) {
//        xx += 0.02;
//        yy += 0.02 * (by2 - by1) / (bx2 - bx1);
//    } else {
//        xx -= 0.02;
//        yy -= 0.02 * (by2 - by1) / (bx2 - bx1);
//    }
 
    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);
 
//    CurrRTKInfo.heading = 60;
 
    static double deg = 0;
//    CurrRTKInfo.heading = deg;
    deg += 2.0;
    if (deg >= 360) deg = 0;
 
    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)
{
    WriteRtk(RTK_MODULE_UART, INQ_PJK_PARAM, strlen(INQ_PJK_PARAM));
    DEBUG("获取PJK参数...");
    AppTimer_delete(CheckPjkParamTimeout);
    AppTimer_add(CheckPjkParamTimeout, D_SEC(3));
}
 
static void CheckPjkParamTimeout(union sigval sig) {
    AppTimer_delete(CheckPjkParamTimeout);
 
    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));
    WriteRtk(RTK_MODULE_UART, CMD_VERSION, strlen(CMD_VERSION));
 
    DEBUG("获取版本...");
}
 
static void VersionTimeout(union sigval sig)
{
    AppTimer_delete(VersionTimeout);
    DEBUG("版本获取超时");
    GetModuleVersion();
 
    uint8_t buff[33];
    memcpy(buff, rtkModel, 32);
    buff[32] = 0;
    PlatformStatusChanged(RTK_STATUS_EVT, buff, 33);
}
 
static void GpsDataTimeout(union sigval sig)
{
    AppTimer_delete(GpsDataTimeout);
 
    if (++lostCnt >= 3) {
        DEBUG("RTK模块收不到GPS数据");
        GetModuleVersion();
    } else {
        AppTimer_add(GpsDataTimeout, D_SEC(5));
    }
}