gpsMng.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. local gpsMng = {}
  2. local gps_uart_id = 2
  3. libgnss.clear() -- 清空数据,兼初始化
  4. uart.setup(gps_uart_id, 115200)
  5. function exec_agnss()
  6. if http then
  7. -- AGNSS 已调通
  8. while 1 do
  9. local code, headers, body = http.request("GET", "http://download.openluat.com/9501-xingli/HXXT_GPS_BDS_AGNSS_DATA.dat").wait()
  10. -- local code, headers, body = http.request("GET", "http://nutzam.com/6228.bin").wait()
  11. log.info("gnss", "AGNSS", code, body and #body or 0)
  12. if code == 200 and body and #body > 1024 then
  13. -- uart.write(gps_uart_id, "$reset,0,h01\r\n")
  14. -- sys.wait(200)
  15. -- uart.write(gps_uart_id, body)
  16. for offset=1,#body,512 do
  17. log.info("gnss", "AGNSS", "write >>>", #body:sub(offset, offset + 511))
  18. uart.write(gps_uart_id, body:sub(offset, offset + 511))
  19. sys.wait(100) -- 等100ms反而更成功
  20. end
  21. io.writeFile("/6228.bin", body)
  22. break
  23. end
  24. sys.wait(60*1000)
  25. end
  26. end
  27. sys.wait(20)
  28. -- 读取之前的位置信息
  29. local gnssloc = io.readFile("/gnssloc")
  30. if gnssloc then
  31. str = "$AIDPOS," .. gnssloc
  32. log.info("POS", str)
  33. uart.write(gps_uart_id, str .. "\r\n")
  34. str = nil
  35. gnssloc = nil
  36. else
  37. -- TODO 发起基站定位
  38. uart.write(gps_uart_id, "$AIDPOS,3432.70,N,10885.25,E,1.0\r\n")
  39. end
  40. end
  41. sys.taskInit(function()
  42. -- Air780EG工程样品的GPS的默认波特率是9600, 量产版是115200,以下是临时代码
  43. log.info("GPS", "start")
  44. pm.power(pm.GPS, true)
  45. -- 绑定uart,底层自动处理GNSS数据
  46. -- 第二个参数是转发到虚拟UART, 方便上位机分析
  47. libgnss.bind(gps_uart_id, uart.VUART_0)
  48. -- libgnss.on("raw", function(data)
  49. -- -- 默认不上报, 需要的话自行打开
  50. -- data = data:split("\r\n")
  51. -- if data == nil then
  52. -- return
  53. -- end
  54. -- end)
  55. sys.wait(200) -- GPNSS芯片启动需要时间
  56. -- 调试日志,可选
  57. -- libgnss.debug(true)
  58. -- 显示串口配置
  59. -- uart.write(gps_uart_id, "$CFGPRT,1\r\n")
  60. -- sys.wait(20)
  61. -- 增加显示的语句
  62. -- uart.write(gps_uart_id, "$CFGMSG,0,1,1\r\n") -- GLL
  63. -- sys.wait(20)
  64. -- uart.write(gps_uart_id, "$CFGMSG,0,5,1\r\n") -- VTG
  65. -- sys.wait(20)
  66. -- uart.write(gps_uart_id, "$CFGMSG,0,6,1\r\n") -- ZDA
  67. -- sys.wait(20)
  68. exec_agnss()
  69. end)
  70. sys.taskInit(function ()
  71. while true do
  72. log.info("nmea", "isFix", libgnss.isFix())
  73. sys.wait(10000)
  74. end
  75. end)
  76. --获取时间
  77. function gpsMng.getTime()
  78. local tTime = libgnss.getZda()
  79. return (string.format("%02d%02d%02d%02d%02d%02d",tTime.year-2000,tTime.month,tTime.day,tTime.hour+8,tTime.min,tTime.sec)):fromHex()
  80. end
  81. return gpsMng