SyncManager.cpp 9.3 KB

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