SyncManager.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include"SyncManager.h"
  2. #include <deque>
  3. //#include <Eigen/Dense>
  4. #include "../global.h"
  5. HostServer::SyncManager::SyncManager()
  6. {
  7. init();
  8. }
  9. void HostServer::SyncManager::init()
  10. {
  11. _anchors.swap(unordered_map<unsigned long long, Position>());
  12. _syncTimeMsgs.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTimeMsg>>());
  13. _historySync.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTime>>());
  14. _distance.swap(unordered_map<unsigned long long,unordered_map<unsigned long long, double>>());
  15. }
  16. void HostServer::SyncManager::analyzeSyncMsg(SyncTimeMsg &msg)
  17. {
  18. // 如果时间同步消息的版本数量超过最大限制,则删除最早添加的消息
  19. if(_syncTimeMsgs.size() >= MAX_SYNCTIME_NUM && _syncTimeMsgs.count(msg.SyncNum()) == 0)
  20. {
  21. if(_historySync.begin()->first == _syncTimeMsgs.begin()->first){
  22. _historySync.erase(_historySync.begin());
  23. }
  24. _syncTimeMsgs.erase(_syncTimeMsgs.begin());
  25. }
  26. // 将msg添加到_historySyncTimeMsg
  27. _syncTimeMsgs[msg.SyncNum()][msg.LocalIdCode()] = msg;
  28. // 更新时间同步并计算
  29. for(auto it :_syncTimeMsgs[msg.SyncNum()])
  30. {
  31. updateSync(msg.SyncNum(), it.first);
  32. }
  33. }
  34. bool HostServer::SyncManager::updateSync(unsigned short SyncNum, unsigned long long localIdCode)
  35. {
  36. // 如果当前版本的时间同步消息未收到则返回false
  37. if(!_syncTimeMsgs.count(SyncNum) || !_syncTimeMsgs[SyncNum].count(localIdCode))
  38. {
  39. return false;
  40. }
  41. SyncTimeMsg &msg = _syncTimeMsgs[SyncNum][localIdCode];
  42. // 如果当前的为root,则返回true
  43. if(msg.SyncLevel() == 0)
  44. {
  45. return true;
  46. }
  47. // 如果时间同步已经计算过,则返回true
  48. if(_historySync.count(SyncNum) && _historySync[SyncNum].count(localIdCode) && _historySync[SyncNum][localIdCode].TimeDelay())
  49. {
  50. return true;
  51. }
  52. // 如果已经收到了上一级的同步消息
  53. if(_syncTimeMsgs[SyncNum].count(msg.UpperIdCode()))
  54. {
  55. if(!updateSync(SyncNum, msg.UpperIdCode()))
  56. {
  57. return false;
  58. }
  59. SyncTimeMsg &upperMsg = _syncTimeMsgs[SyncNum][msg.UpperIdCode()];
  60. SyncTime* s = NULL;
  61. for(auto it(_historySync.rbegin()); it != _historySync.rend(); ++it)
  62. {
  63. if(it->first != msg.SyncNum() && it->second.count(localIdCode))
  64. {
  65. s = &(it->second.find(localIdCode)->second);
  66. break;
  67. }
  68. }
  69. _historySync[SyncNum][localIdCode] = SyncTime(msg, upperMsg, s);
  70. // 计算时间同步
  71. long long upperTimeDelay = 0;
  72. if(_syncTimeMsgs[SyncNum][msg.UpperIdCode()].SyncLevel() != 0)
  73. {
  74. upperTimeDelay = _historySync[SyncNum][msg.UpperIdCode()].TimeDelay();
  75. }
  76. long long sendTime = _historySync[SyncNum][localIdCode].SendTime();
  77. long long receiveTime = _historySync[SyncNum][localIdCode].ReceiveTime();
  78. long long timeDelay = receiveTime - sendTime - _distance[localIdCode][msg.UpperIdCode()];
  79. timeDelay += upperTimeDelay;
  80. _historySync[SyncNum][localIdCode].TimeDelay(timeDelay);
  81. return true;
  82. }
  83. return false;
  84. }
  85. HostServer::Position HostServer::SyncManager::analyzeTagMsg(TagMsg &tagMsg, unsigned long long tagId, unsigned short tagNum)
  86. {
  87. _tagRecords[tagId][tagNum].push_back(tagMsg);
  88. // 获取预估的时间差
  89. unordered_map<unsigned long long, long long> times;
  90. if(!_tagRecords.size() || !_tagRecords[tagId].size() || _tagRecords[tagId][tagNum].size() != 4)
  91. {
  92. return Position(DBL_MAX, DBL_MAX, DBL_MAX);
  93. }
  94. for(auto tag : _tagRecords[tagId][tagNum])
  95. {
  96. auto res = calTimeByLinar(tag);
  97. if(res == LLONG_MAX) return Position(DBL_MAX, DBL_MAX, DBL_MAX);
  98. times[tag.StationIdCode] = res;
  99. }
  100. // 初始化矩阵
  101. auto begin = times.begin();
  102. auto it = begin;
  103. it++;
  104. //Eigen::VectorXd x(3);
  105. //Eigen::MatrixXd data(3,4), pos(1,3);
  106. //pos(0,0) = _anchors[begin->first].x;
  107. //pos(0,1) = _anchors[begin->first].y;
  108. //pos(0,2) = _anchors[begin->first].z;
  109. //// 设置tag的初始坐标
  110. // x << 500,500,100;
  111. //cout << begin->first << " " << pos(0,0) << ", "<< pos(0,1) << ", "<< pos(0,2) << endl;
  112. //ofstream fout("test/linar_" + to_string(tagId) + ".txt",ios::app);
  113. //for (int i = 0; it != times.end(); it++, i++)
  114. //{
  115. // data(i,0) = _anchors[it->first].x;
  116. // data(i,1) = _anchors[it->first].y;
  117. // data(i,2) = _anchors[it->first].z;
  118. // data(i,3) = (it->second - begin->second) * 15.65 * 2.99702547 * 0.01;
  119. // cout << it->first << " " << data(i,0) << ", " << data(i,1) << ", " << data(i,2) << ", "<< data(i,3) <<";" << endl;
  120. // fout << hex << it->first << ' ' << begin->first << ' ' << data(i,3) << endl;
  121. //}
  122. //TDOAFunctor functor(data, pos);
  123. //Eigen::NumericalDiff<TDOAFunctor> numDiff(functor);
  124. //Eigen::LevenbergMarquardt<Eigen::NumericalDiff<TDOAFunctor>,double> lm(numDiff);
  125. //lm.parameters.maxfev = 2000;
  126. //lm.parameters.xtol = 1.0e-10;
  127. //// 计算后,如果计算出结果则返回该结果
  128. //int ret = lm.minimize(x);
  129. //if(ret > 0)
  130. //{
  131. // Position p(x[0],x[1],x[2]);
  132. // cout << p.x * 17 / 15<< " " << p.y * 17 / 15<< " " << p.z * 17 / 15<< endl;
  133. // fout << p.x * 17 / 15<< " " << p.y * 17 / 15<< " " << p.z * 17 / 15<< endl;
  134. // fout << endl;
  135. // fout.close();
  136. // return p;
  137. //}
  138. return Position(DBL_MAX, DBL_MAX, DBL_MAX);
  139. }
  140. unsigned long long HostServer::SyncManager::calTimeByLinar(TagMsg &tag)
  141. {
  142. // 获取历史记录中与此tag最近的两条
  143. deque<SyncTime> hisSync;
  144. //ofstream fout("test/linar_513.txt",ios::app);
  145. int i = 0;
  146. //for(int j = 0; j < MAX_ROOT_DELAY_NUM; j++){ // 增加判断次数
  147. if(_syncTimeMsgs[tag.SyncNum][tag.StationIdCode].SyncLevel() == 0)
  148. {
  149. //fout << hex << tag.SyncNum << ' ' << tag.StationIdCode << ' ' << tag.ReceiveTime<< endl;
  150. return tag.ReceiveTime;
  151. }
  152. //}
  153. while(hisSync.size() < 2 && i < MAX_SYNCTIME_NUM)
  154. {
  155. auto syncNum = tag.SyncNum - i;
  156. if(_historySync.count(syncNum) && _historySync[syncNum].count(tag.StationIdCode))
  157. {
  158. // return (tag.ReceiveTime + (_historySync[syncNum][tag.StationIdCode].IsRTimeOverflow() ? TIME_MAX : 0) - _historySync[syncNum][tag.StationIdCode].TimeDelay()) & TIME_MAX;
  159. hisSync.push_front(_historySync[syncNum][tag.StationIdCode]);
  160. }
  161. i++;
  162. }
  163. // 如果满足条件的历史记录不足两个则返回
  164. if(hisSync.size() < 2) return LLONG_MAX;
  165. // 计算预估值
  166. auto y1(hisSync.at(0).ReceiveTime() - hisSync.at(0).TimeDelay()),y2(hisSync.at(1).ReceiveTime() - hisSync.at(1).TimeDelay());
  167. auto x1(hisSync.at(0).RealReceiveTime()), x2(hisSync.at(1).RealReceiveTime()), x3(tag.ReceiveTime);
  168. unsigned long long res;
  169. if(x1 > x2)
  170. {
  171. x2 += TIME_MAX;
  172. }
  173. if(x2 > x3)
  174. {
  175. x3 += TIME_MAX;
  176. }
  177. if(y1 > y2)
  178. {
  179. y2 += TIME_MAX;
  180. }
  181. Eigen::Matrix3d a;
  182. a << x1 ,1 , 0,
  183. x2 ,1 , 0,
  184. x3, 1, -1;
  185. Eigen::Vector3d b(y1, y2, 0);
  186. Eigen::Vector3d X = a.colPivHouseholderQr().solve(b);
  187. res = X(2);
  188. res &= TIME_MAX;
  189. //fout << hex << tag.SyncNum << ' ' << tag.StationIdCode << endl
  190. // << "RecieveTime " << hisSync.at(0).RealReceiveTime() << ' ' << hisSync.at(1).RealReceiveTime() << ' ' << tag.ReceiveTime << endl
  191. // << "RootTime " << hisSync.at(0).RealSendTime() << ' ' << hisSync.at(1).RealSendTime() << ' ' << res << endl
  192. // << "TimeDelay " << hisSync.at(0).TimeDelay() << ' ' << hisSync.at(1).TimeDelay() << " " << endl;
  193. return res;
  194. }
  195. void HostServer::SyncManager::updateDistance(unsigned int localId, unsigned char localAntNum, unsigned int upperId, unsigned char uppderAntNum, double d)
  196. {
  197. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  198. unsigned long long uId = SyncHelper::parseId(upperId, uppderAntNum);
  199. if(_anchors.count(lId) == 0)
  200. {
  201. _anchors[lId] = Position();
  202. }
  203. if(_anchors.count(uId) == 0)
  204. {
  205. _anchors[uId] = Position();
  206. }
  207. _distance[lId][uId] = d;
  208. _distance[uId][lId] = d;
  209. // 删除所有版本的消息记录
  210. _historySync.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTime>>());
  211. _syncTimeMsgs.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTimeMsg>>());
  212. }
  213. void HostServer::SyncManager::updateAnchor(unsigned int localId, unsigned char localAntNum, double x, double y, double z)
  214. {
  215. deleteAnchor(localId, localAntNum);
  216. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  217. _anchors[lId] = Position( x, y, z);
  218. }
  219. void HostServer::SyncManager::deleteAnchor(unsigned int localId, unsigned char localAntNum)
  220. {
  221. unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
  222. auto it = _anchors.find(lId);
  223. if(it == _anchors.end()) return;
  224. // 删除anchor
  225. _anchors.erase(it);
  226. // 删除与此anchor相关的距离信息
  227. _distance.erase(_distance.find(lId));
  228. for(auto it(_distance.begin()); it != _distance.end(); it++)
  229. {
  230. it->second.erase(it->second.find(lId));
  231. if(it->second.size() == 0)
  232. {
  233. it = _distance.erase(it);
  234. }
  235. }
  236. // 删除所有版本的消息记录
  237. _historySync.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTime>>());
  238. _syncTimeMsgs.swap(unordered_map<unsigned short, unordered_map<unsigned long long, SyncTimeMsg>>());
  239. }