Rtk.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include "../common/comm.h"
  4. #include "../common/iobuffer.h"
  5. #include "../common/sensor_socket.h"
  6. #include "Rtk.h"
  7. #include "message_queue.h"
  8. #include "MqttClient.h"
  9. #include "imu_sensor.h"
  10. #include <string.h>
  11. #include <sys/time.h>
  12. CRtkSensor::CRtkSensor(CMessageQueue* q) :_message(q)
  13. {
  14. F_SendDate = false;
  15. _run = false;
  16. }
  17. gtime_t CRtkSensor::timeadd(gtime_t t, double sec)
  18. {
  19. double tt;
  20. t.sec+=sec; tt=floor(t.sec); t.time+=(int)tt; t.sec-=tt;
  21. return t;
  22. }
  23. double CRtkSensor::timediff(gtime_t t1, gtime_t t2)
  24. {
  25. return difftime(t1.time,t2.time)+t1.sec-t2.sec;
  26. }
  27. gtime_t CRtkSensor::epoch2time(const double *ep)
  28. {
  29. const int doy[]={1,32,60,91,121,152,182,213,244,274,305,335};
  30. gtime_t time={0};
  31. int days,sec,year=(int)ep[0],mon=(int)ep[1],day=(int)ep[2];
  32. if (year<1970||2099<year||mon<1||12<mon) return time;
  33. /* leap year if year%4==0 in 1901-2099 */
  34. days=(year-1970)*365+(year-1969)/4+doy[mon-1]+day-2+(year%4==0&&mon>=3?1:0);
  35. sec=(int)floor(ep[5]);
  36. time.time=(time_t)days*86400+(int)ep[3]*3600+(int)ep[4]*60+sec;
  37. time.sec=ep[5]-sec;
  38. return time;
  39. }
  40. //gps 转utc
  41. gtime_t CRtkSensor::gpst2utc(gtime_t t)
  42. {
  43. gtime_t tu;
  44. int i;
  45. for (i=0;leaps[i][0]>0;i++) {
  46. tu=timeadd(t,leaps[i][6]);
  47. if (timediff(tu,epoch2time(leaps[i]))>=0.0) return tu;
  48. }
  49. return t;
  50. }
  51. void CRtkSensor::Notify(int8_t* buffer, int32_t size)
  52. {
  53. //$GPCHC,2319,183643.68,119.85, 1.67,2.53,0.02,-0.13,-0.06,-0.0450,0.0282,1.0009,21.06074840,110.50436901,-1.54,-0.002,0.007,0.008,50,46,42,1,2*40
  54. //$GPCHC,1980,4632.64,289.19,-0.42,0.21,-0.23,0.07,-0.06,
  55. // p p1 p p1 p p1 p p1 p
  56. //0.0009,0.0048,-1.0037,38.8594969,121.5150073,121.51,-0.023,0.011,0.011,1.500,14,6,42,0,2*67
  57. // p1 p p1 p
  58. if(_run)
  59. {
  60. int Temp = ',';
  61. unsigned char* p = NULL, * p1 = NULL;
  62. p = (unsigned char*)strchr((const char*)buffer, Temp); //header
  63. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //Gpsweek
  64. float gpsweek = strtof((const char*)p1 + 1, NULL);
  65. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //gpsTime
  66. float gpsTime = strtof((const char*)p + 1, NULL);
  67. float headingAngle = strtof((const char*)p + 1, NULL); //heading
  68. //std::cout << "angle" <<headingAngle << std::endl;
  69. //����IMU
  70. ImuData m_imu;
  71. p1 = (unsigned char*)strchr((const char*)p + 1, Temp);//pithch
  72. // m_imu.rx = strtof((const char*)p1 + 1, NULL);
  73. p = (unsigned char*)strchr((const char*)p1 + 1, Temp);//roll
  74. // m_imu.ry = strtof((const char*)p + 1, NULL);
  75. // _count++;
  76. // if (_count > 20)
  77. // {
  78. // _message->WriteIMUData(&m_imu);
  79. // //std::cout << "Send Date" << std::endl;
  80. // _count = 0;
  81. // }
  82. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //gyro x
  83. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //gyro y
  84. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //gyro z
  85. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //acc x
  86. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //acc y
  87. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //acc z
  88. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //lat
  89. float lat = strtof((const char*)p1 + 1, NULL);
  90. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //lon
  91. float lon = strtof((const char*)p + 1, NULL);
  92. //增加
  93. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //alti
  94. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //Ve
  95. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //Vn
  96. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //Vu
  97. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //V
  98. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //NSV1
  99. p1 = (unsigned char*)strchr((const char*)p + 1, Temp); //NSV2
  100. p = (unsigned char*)strchr((const char*)p1 + 1, Temp); //STATUS
  101. float loc_status = strtof((const char*)p + 1, NULL); //
  102. //std::cout << "loc status" << loc_status << std::endl;
  103. m_imu.rx = strtof((const char*)p + 1, NULL); //
  104. if(m_imu.rx == 42)
  105. m_imu.ry =1;
  106. else
  107. m_imu.ry = 0;
  108. // m_imu.ry = strtof((const char*)p + 1, NULL); //
  109. _message ->WriteIMUData((&m_imu));
  110. /*_count++;
  111. if (_count > 10)
  112. {
  113. if(CMqttSensor::_run)
  114. Send_ZGJ_Rtk(headingAngle, lat, lon);
  115. _count = 0;
  116. }*/
  117. if (!F_SendDate)
  118. {
  119. _curTick = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  120. F_SendDate = true;
  121. }
  122. else
  123. {
  124. long long tick = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  125. if (_curTick != 0 && tick - _curTick > 300)
  126. {
  127. if (CMqttSensor::_run && (CMqttSensor::m_mqttClient != NULL))
  128. // Send_ZGJ_Rtk(headingAngle, lat, lon,loc_status);
  129. Send_ZGJ_Rtk(gpsweek,gpsTime,headingAngle, lat, lon,loc_status);
  130. F_SendDate = false;
  131. }
  132. }
  133. }
  134. }
  135. void CRtkSensor::Start()
  136. {
  137. _run = true;
  138. }
  139. void CRtkSensor::Run()
  140. {
  141. }
  142. void CRtkSensor::SetSensorSocket(SensorSocket<CRtkSensor>* udp)
  143. {
  144. _socket = udp;
  145. }
  146. void CRtkSensor::Stop()
  147. {
  148. F_SendDate = false;
  149. _run = false;
  150. }
  151. #include "../thirdparty/jsoncpp/include/json/json.h"
  152. void CRtkSensor::Send_ZGJ_Rtk(float gpsweek,float gpsTime,float headingAngle,float lat,float lon,float loc_status)//ץ�ֻ�λ����Ϣ�ϱ�
  153. {
  154. // gtime_t CRtkSensor::gpst2utc(gtime_t t)
  155. struct timeval tv;
  156. gettimeofday(&tv, NULL);
  157. char m_Send[16],Dst[16];
  158. memset(Dst, 0, 16);
  159. // sprintf((char*)Dst, "%ld", tv.tv_sec * 1000 + tv.tv_usec / 1000);
  160. sprintf((char*)Dst, "%f", rtk_time.time * 1000 + rtk_time.sec);
  161. // gtime_t CRtkSensor::gpst2utc(gtime_t t);
  162. Json::Value root;
  163. Json::Value Source;
  164. Json::FastWriter writer;
  165. std::string SendTime;
  166. std::string m_headingAngle;
  167. std::string m_lon;
  168. std::string m_lat;
  169. std::string m_loc_status;
  170. Source["deviceEsn"] = "";
  171. Source["cloudEsn"] = CMqttSensor::_Esn;
  172. Source["sendTime"] = SendTime.assign(Dst,strlen(Dst));
  173. Source["ack"] = false;
  174. Source["seqNum"] = CMqttSensor::_Esn + SendTime.assign(Dst,strlen(Dst));
  175. Json::Value GraspGPS;
  176. GraspGPS["graspEsn"] = CMqttSensor::_Esn;
  177. memset(m_Send, 0, 16);
  178. sprintf((char*)m_Send, "%.7f", lon);
  179. GraspGPS["lon"] = m_lon.assign(m_Send);
  180. memset(m_Send, 0, 16);
  181. sprintf((char*)m_Send, "%.7f", lat);
  182. GraspGPS["lat"] = m_lat.assign(m_Send);
  183. memset(m_Send, 0, 16);
  184. sprintf((char*)m_Send, "%.2f", headingAngle);
  185. GraspGPS["headingAngle"] = m_headingAngle.assign(m_Send);
  186. GraspGPS["timeStamp"] = SendTime.assign(Dst,strlen(Dst));
  187. memset(m_Send, 0, 16);
  188. sprintf((char*)m_Send, "%.0f", loc_status);
  189. GraspGPS["loc_status"] = m_loc_status.assign(m_Send);
  190. Source["graspInfoList"].append(GraspGPS);
  191. Json::StyledWriter sw;
  192. //std::cout << sw.write(Source) << std::endl << std::endl;
  193. //CMqttSensor::sendMessage((char*)sw.write(Source).c_str(), 1, (char*)*(CMqttSensor::subTopic));
  194. char Topic[64];
  195. memset(Topic, 0, 64);
  196. sprintf(Topic, "v2x/v1/grasp/%s/info/up", CMqttSensor::_Esn.c_str());
  197. CMqttSensor::sendMessage((char*)sw.write(Source).c_str(), 0, (char*)Topic);
  198. }
  199. // void CRtkSensor::Send_ZGJ_status(float gpsweek, ,int Status)//ץ�ֻ�״̬��Ϣ�ϱ�
  200. // void CRtkSensor::Send_ZGJ_status(float gpsweek,float gpsTime,int Status)
  201. void CRtkSensor::Send_ZGJ_status(int Status)
  202. {
  203. if (CMqttSensor::_run)
  204. {
  205. struct timeval tv;
  206. gettimeofday(&tv, NULL);
  207. rtk_time.time = gpsweek * 604800 + int(gpsTime);
  208. rtk_time.sec = gpsTime - int(gpsTime);
  209. char Dst[16];
  210. memset(Dst, 0, 16);
  211. // sprintf((char*)Dst, "%ld", tv.tv_sec * 1000 + tv.tv_usec / 1000);
  212. sprintf((char*)Dst, "%f", rtk_time.time*1000 + rtk_time.sec*1000);
  213. Json::Value root;
  214. Json::Value Source;
  215. Json::FastWriter writer;
  216. std::string SendTime;
  217. Source["deviceEsn"] = "";
  218. Source["cloudEsn"] = CMqttSensor::_Esn;
  219. Source["sendTime"] = SendTime.assign(Dst,strlen(Dst));
  220. Source["ack"] = true;
  221. Source["seqNum"] = CMqttSensor::_Esn + SendTime.assign(Dst,strlen(Dst));
  222. Json::Value GraspStatus;
  223. GraspStatus["graspEsn"] = CMqttSensor::_Esn;
  224. GraspStatus["status"] = Status;
  225. GraspStatus["timeStamp"] = SendTime.assign(Dst,strlen(Dst));
  226. Source["graspStatusList"].append(GraspStatus);
  227. Json::StyledWriter sw;
  228. //std::cout << sw.write(Source) << std::endl << std::endl;
  229. char Topic[64];
  230. memset(Topic, 0, 64);
  231. sprintf(Topic, "v2x/v1/grasp/%s/status/up", CMqttSensor::_Esn.c_str());
  232. CMqttSensor::sendMessage((char*)sw.write(Source).c_str(), 0, (char*)Topic);
  233. }
  234. }