소스 검색

sync time

zhuyf 4 년 전
부모
커밋
be95db33da
9개의 변경된 파일979개의 추가작업 그리고 0개의 파일을 삭제
  1. 17 0
      sync_time/common.h
  2. 25 0
      sync_time/position.h
  3. 68 0
      sync_time/sync_helper.cpp
  4. 47 0
      sync_time/sync_helper.h
  5. 400 0
      sync_time/sync_manager.cpp
  6. 144 0
      sync_time/sync_manager.h
  7. 142 0
      sync_time/sync_time.h
  8. 115 0
      sync_time/sync_time_message.h
  9. 21 0
      sync_time/tag_message.h

+ 17 - 0
sync_time/common.h

@@ -0,0 +1,17 @@
+#ifndef sync_time_common_h
+#define sync_time_common_h
+
+#define MAX_SYNCTIME_NUM	300		// 队列保存的最大时间同步数
+#define MAX_CALCLINER_NUM	25		//
+#define MAX_SYNCTIME_LOST_NUM  10   // 接收标志卡数据的分站,时间同步序号与root节点分站同步序号差值
+
+#define MAX_SYNCTIME_DIFF_NUM 10    // 参与定位计算接收标识卡数据的多分站间,时间同步序号差值
+#define MAX_SYNCTIME_DIFF_NUM_CROSS_PERIOD  65525 // 65535 - MAX_SYNCTIME_DIFF 跨周期
+#define MAX_SYNCTIME_DELAY_NUM 5    // 新参与计算的标识卡序号与上次定位计算的标识卡序号差值
+
+#define TIME_MAX			0x0FFFFFFFFFF // 0x10000000000 线性插值后的tt最大周期值
+#define TIME_MAX_ADD        1099511627776 // 1099511627775 + 1, 0x10000000000
+
+#define LONGLONG_MAX ((long long)(~0ULL>>1))
+
+#endif

+ 25 - 0
sync_time/position.h

@@ -0,0 +1,25 @@
+#ifndef sync_time_position_h
+#define sync_time_position_h
+
+namespace host_server{
+	/*
+	 * 保存定位结果的点坐标
+	*/
+    class position
+    {
+    public:
+        double x;
+        double y;
+        double z;
+        position():x(0), y(0), z(0){}
+        position(double x, double y, double z):x(x), y(y), z(z){}
+        void operator = (const position &p)
+        {
+            this->x = p.x;
+            this->y = p.y;
+            this->z = p.z;
+        }
+    };
+}
+
+#endif

+ 68 - 0
sync_time/sync_helper.cpp

@@ -0,0 +1,68 @@
+#include "sync_helper.h"
+
+/*
+ * Description: 将char[5]解析成long long
+ * Method:      parse_time
+ * Returns:     long long 
+ * Parameter:   char * time msg中携带的时间,长度为5的char数组
+*/
+long long host_server::sync_helper::parse_time(const char * c)
+{
+	long long ttt0 = (((long long)(c[0]))<<32)  & (0x000000FF00000000);
+	long long ttt1 = (((long long)(c[1]))<<24) & (0x00000000FF000000);
+	long long ttt2 = (((long long)(c[2]))<<16) & (0x0000000000FF0000);
+	long long ttt3 = (((long long)(c[3]))<<8)  & (0x000000000000FF00);
+	long long  all = ttt0 + ttt1 + ttt2 + ttt3 + ((long long)(c[4]&(0xFF)));
+	return all;
+}
+
+/*
+ * Description: 将基站号与天线号拼接成一个编码
+ * Method:      parse_id
+ * Returns:     unsigned long long 拼接后的编码
+ * Parameter:   unsigned int id 基站号
+ * Parameter:   unsigned char ant_num 天线号
+*/
+unsigned long long host_server::sync_helper::parse_id(unsigned int id, unsigned char antNum)
+{
+	return (id << 8) + antNum;
+}
+
+/*
+ * Description: 从文件中分析数据
+ * Method:      parse_from_fstream
+ * Returns:     unsigned long long
+ * Parameter:   ifstream & f
+ * Parameter:   int size
+*/
+unsigned long long host_server::sync_helper::parse_from_fstream(ifstream &f, int size)
+{
+	unsigned long long x(0);
+	for(int i(0); i < size; i++)
+	{
+		int t;
+		f >> hex >> t;
+		x = (x << 8) + t;
+	}
+	return x;
+}
+
+/*
+ * Description: 从文件中分析时间
+ * Method:      parse_time_from_fstream
+ * Returns:     unsigned long long
+ * Parameter:   ifstream & f
+*/
+unsigned long long host_server::sync_helper::parse_time_from_fstream(ifstream &f)
+{
+	char* t = new char[5];
+	for (int i = 0; i < 5; i++)
+	{
+		int tmp;
+		f >> hex >> tmp;
+		t[i] = tmp;
+	}
+	auto time = sync_helper::parse_time(t);
+	delete[] t;
+	return time;
+}

+ 47 - 0
sync_time/sync_helper.h

@@ -0,0 +1,47 @@
+#ifndef sync_time_helper_h
+#define sync_time_helper_h
+
+#include <fstream>
+using namespace std;
+
+namespace host_server{
+	class sync_helper
+	{
+	public:
+		//************************************
+		// Description: 将char[5]解析成long long
+		// Method:      parseTime
+		// Returns:     long long 
+		// Parameter:   char * time msg中携带的时间长度为5的char数组
+		//************************************
+		static long long parse_time(const char *time);
+
+		//************************************
+		// Description: 将基站号与天线号拼接成一个编码
+		// Method:      parseId
+		// Returns:     unsigned long long 拼接后的编码
+		// Parameter:   unsigned int id 基站号
+		// Parameter:   unsigned char antNum 天线号
+		//************************************
+		static unsigned long long parse_id(unsigned int id, unsigned char antNum);
+
+        //************************************
+        // Description: 从文件中分析数据
+        // Method:      parseFromFstream
+        // Returns:     unsigned long long
+        // Parameter:   ifstream & f
+        // Parameter:   int size
+        //************************************
+        static unsigned long long parse_from_fstream(ifstream &f, int size);
+
+        //************************************
+        // Description: 从文件中分析时间
+        // Method:      parseTimeFromFstream
+        // Returns:     unsigned long long
+        // Parameter:   ifstream & f
+        //************************************
+        static unsigned long long parse_time_from_fstream(ifstream &f);
+	};
+}
+
+#endif

+ 400 - 0
sync_time/sync_manager.cpp

@@ -0,0 +1,400 @@
+#include"sync_manager.h"
+#include <deque>
+#include <Eigen/Dense>
+#include <fstream>  
+#include <iostream>  
+#include <log.h>
+
+host_server::sync_manager::sync_manager()
+{
+    init();
+}
+
+host_server::sync_manager::sync_manager(bool status)
+{
+    init();
+    m_log_status = status;
+}
+
+void host_server::sync_manager::init()
+{
+    unordered_map<unsigned long long, position> a;
+    ump_anchors.swap(a);
+    unordered_map<unsigned long long, unordered_map<unsigned long long, double>> b;
+    ump_distance.swap(b);
+	m_log_status = false;
+}
+
+void host_server::sync_manager::analyze_sync_msg(sync_time_message& msg)
+{
+    std::lock_guard<std::recursive_mutex> lg(m_mu_sync_time);
+	// 查找root节点的时间同步序号
+	int idx = find_sync_time_msg(msg.get_root_id(), msg.get_sync_num());
+
+	if( -1 == idx){ 
+		// 没找到
+		// 如果时间同步消息的版本数量超过最大限制,则删除最早添加的消息
+		if(ump_sync_time_msg[msg.get_root_id()].size() >= MAX_SYNCTIME_NUM){ 
+			// 删除第一个,可能有泄漏
+			ump_sync_time_msg[msg.get_root_id()].pop_front();
+		}
+		// 构造此时间同步数据保存到队列中
+		sync_time_msg_item it;
+		it.sync_num = msg.get_sync_num();
+		it.ump_sync_time_msg[msg.get_local_id()] = msg;
+		ump_sync_time_msg[msg.get_root_id()].push_back(it);
+
+		idx = ump_sync_time_msg[msg.get_root_id()].size() - 1;
+	}else{
+		ump_sync_time_msg[msg.get_root_id()][idx].ump_sync_time_msg[msg.get_local_id()] = msg;
+	}
+
+	// 如果历史同步队列中的时间同步数据大于指定容量,清除数据直到满足指定容量
+	while(ump_history_sync[msg.get_root_id()].size() > MAX_SYNCTIME_NUM){
+		ump_history_sync[msg.get_root_id()].pop_front();
+	}
+
+    // 更新时间同步并计算
+	for(auto it : ump_sync_time_msg[msg.get_root_id()][idx].ump_sync_time_msg)
+	{
+		update_sync(msg.get_root_id(), idx, msg.get_sync_num(), it.first);
+	}
+}
+
+bool host_server::sync_manager::update_sync(unsigned long long root_id_code, int idx, unsigned short sync_num, unsigned long long local_id_code)
+{
+	if(-1 == idx){
+		return false;
+	}
+
+    // 如果当前版本的时间同步消息未收到则返回false
+	unordered_map<unsigned long long, sync_time_message>::iterator itSyncTime = ump_sync_time_msg[root_id_code][idx].ump_sync_time_msg.find(local_id_code);
+	if(itSyncTime == ump_sync_time_msg[root_id_code][idx].ump_sync_time_msg.end()){
+		return false;
+	}
+
+    sync_time_message& msg = ump_sync_time_msg[root_id_code][idx].ump_sync_time_msg[local_id_code];
+
+    // 如果当前节点为root,则返回true
+    if(msg.get_sync_level() == 0){
+        return true;
+    } 
+
+	int idx_synctime = find_his_sync_time(root_id_code, sync_num); 
+
+	// 如果时间同步已经计算过,则返回true
+	if(-1 != idx_synctime){
+		unordered_map<unsigned long long, sync_time>::iterator itHistSync = ump_history_sync[root_id_code][idx_synctime].hist_sync.find(local_id_code);
+		if(itHistSync != ump_history_sync[root_id_code][idx_synctime].hist_sync.end()){
+			if(itHistSync->second.get_delay_time())
+				return true;
+		}
+	}
+
+    // 如果已经收到了上一级的同步消息
+    if(ump_sync_time_msg[root_id_code][idx].ump_sync_time_msg.count(msg.get_upper_id())){
+        if(!update_sync(root_id_code, idx, sync_num, msg.get_upper_id())){
+            return false;
+        }
+
+        sync_time_message &upperMsg = ump_sync_time_msg[root_id_code][idx].ump_sync_time_msg[msg.get_upper_id()];
+        sync_time* s = nullptr;
+        for(auto it = ump_history_sync[root_id_code].rbegin(); it != ump_history_sync[root_id_code].rend(); ++it)
+        {
+            if(it->sync_num != msg.get_sync_num() && it->hist_sync.count(local_id_code))
+            {
+                s = &(it->hist_sync.find(local_id_code)->second);
+                break;
+            }
+        }
+
+		idx_synctime = find_his_sync_time(root_id_code, sync_num);
+
+		if(-1 == idx_synctime){
+			sync_time_item it;
+			it.sync_num = msg.get_sync_num();
+			it.hist_sync[local_id_code] = sync_time(msg, upperMsg, s);
+			ump_history_sync[root_id_code].push_back(it);
+
+			idx_synctime = ump_history_sync[root_id_code].size() - 1;
+		}else{
+			ump_history_sync[root_id_code][idx_synctime].hist_sync[local_id_code] = sync_time(msg, upperMsg, s);
+		}
+
+        // 计算时间同步
+        long long upperTimeDelay = 0;
+        if(ump_sync_time_msg[root_id_code][idx].ump_sync_time_msg[msg.get_upper_id()].get_sync_level() != 0)
+        {
+            upperTimeDelay = ump_history_sync[root_id_code][idx_synctime].hist_sync[msg.get_upper_id()].get_delay_time();
+        }
+        long long sendTime = ump_history_sync[root_id_code][idx_synctime].hist_sync[local_id_code].get_send_time();
+        long long receiveTime = ump_history_sync[root_id_code][idx_synctime].hist_sync[local_id_code].get_receive_time();
+        long long timeDelay = receiveTime - sendTime - ump_distance[local_id_code][msg.get_upper_id()];
+
+        timeDelay += upperTimeDelay;
+		
+		// 从不同的upper来的同步数据,跨周期判断可能不正确,将时间差控制在一个周期内
+		while(timeDelay > TIME_MAX){
+			timeDelay -= TIME_MAX;
+		}
+		while(timeDelay + TIME_MAX < 0 ){
+			timeDelay += TIME_MAX;
+		}
+        ump_history_sync[root_id_code][idx_synctime].hist_sync[local_id_code].set_delay_time(timeDelay);
+
+		if(m_log_status){
+        }
+
+        return true;
+    }
+
+    return false;
+}
+
+// 线性插值外插计算tt值
+unsigned long long host_server::sync_manager::cal_time_by_linear(tag_message& tag)
+{
+    std::lock_guard<std::recursive_mutex> lg(m_mu_sync_time);
+    log_info("[tdoa] tdoa_sync begin calc");
+    // 获取历史记录中与此tag最近的两条
+    deque<sync_time> hisSync;
+
+	unsigned long long rootIdCode = tag.m_sync_root_id;
+
+    int i = 0;
+	int idx = find_sync_time_msg(rootIdCode, tag.m_sync_num);
+	if(-1 != idx){
+		unordered_map<unsigned long long, sync_time_message>::iterator it = ump_sync_time_msg[rootIdCode][idx].ump_sync_time_msg.find(tag.m_local_id);
+		if(it != ump_sync_time_msg[rootIdCode][idx].ump_sync_time_msg.end()){
+			if(it->second.get_sync_level() == 0){
+				return tag.m_receive_time;
+			}
+		}
+	}
+	
+	int idx_sync = -1;
+    while(hisSync.size() < 2 && i < MAX_CALCLINER_NUM)
+    {
+        auto syncNum = tag.m_sync_num - i;
+		idx_sync = find_his_sync_time(rootIdCode, syncNum);
+		if(-1 != idx_sync){
+			if(ump_history_sync[rootIdCode][idx_sync].hist_sync.count(tag.m_local_id)){
+				hisSync.push_front(ump_history_sync[rootIdCode][idx_sync].hist_sync[tag.m_local_id]);
+			}
+		}
+        i++;
+    }
+    // 如果满足条件的历史记录不足两个则返回
+    if(hisSync.size() < 2){
+        log_info("[tdoa] tdoa_sync his_sync's size is less 2");
+		return LLONG_MAX;
+	}
+
+    // 计算预估值
+    long long y1(hisSync.at(0).get_receive_time() - hisSync.at(0).get_delay_time()),y2(hisSync.at(1).get_receive_time() - hisSync.at(1).get_delay_time());
+    long long x1(hisSync.at(0).get_real_receive_time()), x2(hisSync.at(1).get_real_receive_time()), x3(tag.m_receive_time);
+
+    unsigned long long res;
+    if(x1 > x2)
+    {
+        x2 += TIME_MAX;
+    }
+    if(x2 > x3)
+    {
+        x3 += TIME_MAX;
+    }
+
+	if(y1 < 0){ 
+        // 理论y值不能小于0
+		y1 += TIME_MAX;
+	}
+
+	if(y2 < 0 ){
+		y2 += TIME_MAX;
+	}
+
+	if(y1 > y2){
+		y2 += TIME_MAX;
+	}else if(y2-y1 > TIME_MAX){
+		y2 -=TIME_MAX;
+	}
+
+    Eigen::Matrix3d a;
+    a << x1 ,1 , 0,
+         x2 ,1 , 0,
+         x3, 1, -1;
+
+    Eigen::Vector3d b(y1, y2, 0);
+    Eigen::Vector3d X = a.colPivHouseholderQr().solve(b);
+    
+    res = X(2);
+    res &= TIME_MAX;
+
+    log_info("[tdoa] tdoa_sync_cal end, value=%lld", res);
+
+    return res;
+}
+
+/*
+ * 使用内插值计算tt值
+ *
+ * param
+ *      tag     卡的参数信息
+ *
+ * return
+ *      返回插值后的tt值
+ * */
+unsigned long long host_server::sync_manager::cal_time_by_inter_linear(tag_message& tag)
+{
+    std::lock_guard<std::recursive_mutex> lg(m_mu_sync_time);
+    deque<sync_time> hisSync;
+
+	unsigned long long rootIdCode = tag.m_sync_root_id;
+
+	int i = 0;
+	int idx = find_sync_time_msg(rootIdCode, tag.m_sync_num);
+	if(-1 != idx){
+		unordered_map<unsigned long long, sync_time_message>::iterator it = ump_sync_time_msg[rootIdCode][idx].ump_sync_time_msg.find(tag.m_local_id);
+		if(it != ump_sync_time_msg[rootIdCode][idx].ump_sync_time_msg.end()){
+			if(it->second.get_sync_level() == 0){
+				return sub(tag.m_receive_time, it->second.get_local_send_time());
+			}
+		}
+	}
+
+	int idx_sync = -1;
+
+	long long y[2] = {0};
+	long long x[2] = {0};
+	unsigned int r[2] = {0};
+
+	i = 0;
+	r[0] = tag.m_local_id;
+	while (i<2)
+	{
+		idx_sync = find_sync_time_msg(rootIdCode, tag.m_sync_num + i);
+		if(-1 != idx_sync){
+			if(ump_sync_time_msg[rootIdCode][idx_sync].ump_sync_time_msg.count(tag.m_local_id)){
+				x[i] = ump_sync_time_msg[rootIdCode][idx_sync].ump_sync_time_msg[tag.m_local_id].get_local_receive_time();
+				for (auto it = ump_sync_time_msg[rootIdCode][idx_sync].ump_sync_time_msg.begin();it != ump_sync_time_msg[rootIdCode][idx_sync].ump_sync_time_msg.end();++it)
+				{
+					if (it->second.get_sync_level() == 0)
+					{
+						y[i] = it->second.get_local_send_time();
+						r[1] = it->second.get_local_id();
+						break;
+					}
+				}
+			}
+		}
+		i++;
+	}
+
+	if (0 == x[0] || 0 == x[1] || 0 == y[0] || 0 == y[1])
+	{
+		return 0;
+	}
+
+	long long y1(y[0]),y2(y[1]);
+	long long x1(x[0]),x2(x[1]);
+	long long x3 = tag.m_receive_time;
+	long long Tf = ump_distance[r[0]][r[1]];
+
+	unsigned long long res = 0;
+    //long double k = long double (sub(y2,y1)) / long double(sub(x2,x1));
+	res = (long double)(sub(y2,y1)) / (long double)(sub(x2,x1)) * sub(x3,x1) + Tf;
+
+	return res;
+}
+
+void host_server::sync_manager::update_distance(unsigned int local_id, uint8_t local_ant_num, unsigned int upper_id, uint8_t upper_ant_num, double d)
+{
+    unsigned long long lId = sync_helper::parse_id(local_id, local_ant_num);
+    unsigned long long uId = sync_helper::parse_id(upper_id, upper_ant_num);
+    if(ump_anchors.count(lId) == 0)
+    {
+        ump_anchors[lId] = position();
+    }
+    if(ump_anchors.count(uId) == 0) 
+    {
+        ump_anchors[uId] = position();
+    }
+    ump_distance[lId][uId] = d;
+    ump_distance[uId][lId] = d;
+
+    // 删除所有版本的消息记录
+	ump_history_sync.clear();
+	ump_sync_time_msg.clear();
+}
+
+void host_server::sync_manager::update_anchor(unsigned int local_id, uint8_t local_ant_num, double x, double y, double z)
+{
+    delete_anchor(local_id, local_ant_num);
+
+    unsigned long long lId = sync_helper::parse_id(local_id, local_ant_num);
+    ump_anchors[lId] = position( x, y, z);
+}
+
+/*void host_server::sync_manager::update_anchor(unsigned int local_id, unsigned char local_ant_num, position p)
+{
+    delete_anchor(local_id, local_ant_num);
+
+    unsigned long long lid = sync_helper::parse_id(local_id, local_ant_num);
+    ump_anchors[lid] = p;
+}*/
+
+void host_server::sync_manager::delete_anchor(unsigned int local_id, uint8_t local_ant_num)
+{
+    unsigned long long lId = sync_helper::parse_id(local_id, local_ant_num);
+    auto it = ump_anchors.find(lId);
+    if(it == ump_anchors.end()) 
+        return;
+
+    // 删除anchor
+    ump_anchors.erase(it);
+
+    // 删除与此anchor相关的距离信息
+    ump_distance.erase(ump_distance.find(lId));
+    for(auto it = ump_distance.begin(); it != ump_distance.end(); ++it)
+    {
+        it->second.erase(it->second.find(lId));
+        if(it->second.size() == 0)
+        {
+            it = ump_distance.erase(it);
+        }
+    }
+
+    // 删除所有版本的消息记录
+	ump_history_sync.clear();
+	ump_sync_time_msg.clear();
+}
+
+host_server::sync_manager::~sync_manager()
+{
+}
+
+int host_server::sync_manager::find_sync_time_msg(unsigned long long root_id_code, unsigned short sync_num )
+{
+	int idx = -1;
+	for(int i = ump_sync_time_msg[root_id_code].size() - 1; i >= 0; --i){
+		if(ump_sync_time_msg[root_id_code][i].sync_num == sync_num ){
+			idx = i;
+            break;
+		}
+	}
+
+	return idx;
+}
+
+int host_server::sync_manager::find_his_sync_time(unsigned long long root_id_code, unsigned short sync_num )
+{
+	int idx = -1;
+	for(int i = ump_history_sync[root_id_code].size() - 1; i >= 0; --i){
+		if(ump_history_sync[root_id_code][i].sync_num == sync_num ){
+			idx = i;
+            break;
+		}
+	}
+
+	return idx;
+}

+ 144 - 0
sync_time/sync_manager.h

@@ -0,0 +1,144 @@
+#ifndef sync_time_manager_h
+#define sync_time_manager_h
+
+#include <boost/serialization/singleton.hpp>
+#include <unordered_map>
+#include <map>
+#include <deque>
+#include <iostream>
+#include <mutex>
+#include "sync_time_message.h"
+#include "position.h"
+#include "common.h"
+#include "sync_time.h"
+#include "sync_helper.h"
+#include "tag_message.h"
+
+using namespace std;
+
+namespace host_server{
+    class sync_manager
+    {
+		struct sync_time_msg_item
+		{
+			unsigned short sync_num;
+			unordered_map<unsigned long long, sync_time_message> ump_sync_time_msg;
+		};
+
+		struct sync_time_item
+		{
+			unsigned short sync_num;
+			unordered_map<unsigned long long, sync_time> hist_sync;
+		};
+
+	    std::recursive_mutex m_mu_sync_time;
+
+        // 是否输出日志标志
+		bool m_log_status;
+	public:
+        sync_manager();
+        sync_manager(bool status);
+		~sync_manager();
+
+    	// unsigned long long 代表anchor编码 ID+AntNum
+        unordered_map<unsigned long long, position> ump_anchors; 
+        // unsigned long long 代表anchor编码 ID+AntNum
+        unordered_map<unsigned long long, unordered_map<unsigned long long, double>> ump_distance; 
+
+		// with root
+        // 使用_syncTimeMsgs[SyncNum][LocalIdCode]访问对应的SyncTimeMsg,即同步时间消息
+		unordered_map<unsigned long long, deque<sync_time_msg_item>> ump_sync_time_msg;
+
+        // 使用_historySync[SyncNum][LocalIdCode]访问对应的SyncTime即计算后的时间同步
+		unordered_map<unsigned long long, deque<sync_time_item>> ump_history_sync;
+	public:
+        //************************************
+        // Description: 用于初始化所有成员
+        // Method:      init
+        // Returns:     void
+        //************************************
+        void init();
+
+        //************************************
+        // Description: 用于解析时间同步msg中的内容
+        // Method:      analyze_sync_msg
+        // Returns:     void
+        // Parameter:   SyncTimeMsg & msg 从socket获取的消息
+        //************************************
+        void analyze_sync_msg(sync_time_message& msg);
+
+        //************************************
+        // Description: 更新距离信息
+        // Method:      update_distance
+        // Returns:     void
+        // Parameter:   unsigned int local_id 当前分站号
+        // Parameter:   unsigned char local_ant_num 当前分站的天线号
+        // Parameter:   unsigned int upper_id 上级分站号
+        // Parameter:   unsigned char uppder_ant_num 上级分站的天线号
+        // Parameter:   int d 距离
+        //************************************
+        void update_distance(unsigned int local_id, uint8_t local_ant_num, unsigned int upper_id, uint8_t upper_ant_num, double d);
+
+        //************************************
+        // Description: 更新anchor信息
+        // Method:      update_anchor
+        // Returns:     void
+        // Parameter:   unsigned int local_id 当前分站号
+        // Parameter:   unsigned char local_ant_num 当前分站的天线号
+        // Parameter:   int x
+        // Parameter:   int y
+        // Parameter:   int z
+        //************************************
+        void update_anchor(unsigned int local_id, uint8_t local_ant_num, double x, double y, double z);
+        //void update_anchor(unsigned int local_id, unsigned char local_ant_num, const position& p);
+
+        //************************************
+        // Description: 删除anchor
+        // Method:      delete_anchor
+        // Returns:     void
+        // Parameter:   unsigned int local_id 当前分站号
+        // Parameter:   unsigned char local_ant_num 当前分站的天线号
+        //************************************
+        void delete_anchor(unsigned int local_id, uint8_t local_ant_num);
+
+	        //************************************
+        // Description: 根据Tag的记录和历史msg,利用线性插值算法计算Tag的预估时间
+        // Method:      cal_time_by_linar
+        // Returns:     unsigned long long 
+        // Parameter:   unsigned long long tagReceiveTime tag的接收时间
+        // Parameter:   unsigned short SyncNum  接收tag时的时间同步编号
+        // Parameter:   unsigned long long localIdCode 接收tag的anchorID编码
+        //************************************
+        unsigned long long cal_time_by_linear(tag_message& tag);
+		unsigned long long cal_time_by_inter_linear(tag_message& tag);
+		//************************************
+		// Method:    update_sync
+		// Returns:   bool
+		// Parameter: unsigned long long root_id_code
+		// Parameter: unsigned short sync_num
+		// Parameter: unsigned long long local_id_code
+		//************************************
+		bool update_sync(unsigned long long root_id_code, int idx, unsigned short sync_num, unsigned long long local_id_code);
+		int find_sync_time_msg(unsigned long long root_id_code, unsigned short sync_num);
+		int find_his_sync_time(unsigned long long root_id_code, unsigned short sync_num);
+
+		void set_log_status(bool status)
+        {	
+            this->m_log_status = status;
+        }
+
+		bool get_log_status() const 
+        {
+            return m_log_status;
+        }
+		
+        long long sub(unsigned long long t1, unsigned long long t2){
+			return ((t1 >= t2)? (t1 - t2):(t1 - t2 + TIME_MAX_ADD));
+		}
+    };
+};
+
+using singleton_sync_manager = boost::serialization::singleton<host_server::sync_manager>;
+#define ssync_manager singleton_sync_manager::get_mutable_instance()
+
+#endif

+ 142 - 0
sync_time/sync_time.h

@@ -0,0 +1,142 @@
+#ifndef sync_time_base_h
+#define sync_time_base_h
+#include "sync_time_message.h"
+
+namespace host_server{
+    class sync_time
+    {
+    private:
+        unsigned long long m_upper_id{0};       // 上级分站id
+        unsigned long long m_receive_time{0};   // 接收时间
+        unsigned long long m_send_time{0};		// 发送时间
+        unsigned short m_sync_level{0};		    // 分站时间同步层级
+        long long m_delay_time{0};			    // 延迟时间
+        bool m_rtime_over_status{false};		// 接收时间是否溢出,过周期
+        bool m_stime_over_status{false};		// 发送时间是否溢出,过周期
+   public:
+        sync_time():m_upper_id(0),
+                    m_receive_time(0),
+                    m_send_time(0),
+                    m_sync_level(0),
+                    m_delay_time(0),
+                    m_rtime_over_status(false),
+                    m_stime_over_status(false)
+        {}
+
+        sync_time(unsigned long long upperIdCode, long long receiveTime,bool isRTimeOverflow, long long sendTime, bool isSTimeOverflow, unsigned short syncLevel):m_upper_id(upperIdCode),
+        m_receive_time(receiveTime), 
+        m_send_time(sendTime), 
+        m_sync_level(syncLevel),
+        m_delay_time(0),
+        m_rtime_over_status(isRTimeOverflow), 
+        m_stime_over_status(isSTimeOverflow)
+        {}
+
+        sync_time(sync_time_message& msg, sync_time_message& upper_msg, sync_time* s = nullptr)
+            :m_upper_id(msg.get_upper_id()),
+            m_receive_time(msg.get_local_receive_time()), 
+            m_send_time(upper_msg.get_local_send_time()), 
+            m_sync_level(msg.get_sync_level()),
+            m_delay_time(0)
+        {
+            // 根据历史记录解决时间戳溢出的问题
+            if(s == nullptr)
+            {
+                m_rtime_over_status = false;
+                m_stime_over_status = false;
+            }
+            else
+            {
+                m_rtime_over_status = (m_receive_time < s->get_real_receive_time()) ? !s->get_rtime_status() : s->get_rtime_status();
+                m_stime_over_status = (m_send_time < s->get_real_send_time()) ? !s->get_stime_status() : s->get_stime_status();
+
+                // 解决同时溢出的问题
+                if(m_rtime_over_status && m_stime_over_status)
+                {
+                    m_rtime_over_status = false;
+                    m_stime_over_status = false;
+                }
+            }
+        }
+
+        unsigned long long get_upper_id() const 
+        { 
+            return m_upper_id; 
+        }
+
+        /*
+         * 获得接收时间
+         * */
+        unsigned long long get_receive_time() const 
+        { 
+            return m_receive_time + (m_rtime_over_status ? TIME_MAX : 0); 
+        }
+
+        unsigned long long get_real_receive_time() const 
+        {
+            return m_receive_time; 
+        }
+
+        void set_receive_time(unsigned long long& val) 
+        { 
+            m_receive_time = val; 
+        }
+
+        bool get_rtime_status() const 
+        { 
+            return m_rtime_over_status; 
+        }
+
+        void set_rtime_status(bool val) 
+        { 
+            m_rtime_over_status = val; 
+        }
+
+        unsigned long long get_send_time() const 
+        { 
+            return m_send_time + (m_stime_over_status ? TIME_MAX : 0); 
+        }
+
+        unsigned long long get_real_send_time() const 
+        { 
+            return m_send_time; 
+        }
+
+        void set_send_time(const unsigned long long& val) 
+        { 
+            m_send_time = val; 
+        }
+
+        bool get_stime_status() const 
+        { 
+            return m_stime_over_status; 
+        }
+
+        void set_stime_status(bool val) 
+        { 
+            m_stime_over_status = val; 
+        }
+
+        unsigned short get_sync_level() const 
+        { 
+            return m_sync_level; 
+        }
+        
+        void set_sync_level(unsigned short val) 
+        { 
+            m_sync_level = val; 
+        }
+
+        long long get_delay_time() const 
+        { 
+            return m_delay_time; 
+        }
+
+        void set_delay_time(const long long& val) 
+        { 
+            m_delay_time = val; 
+        }
+    };   
+}
+
+#endif

+ 115 - 0
sync_time/sync_time_message.h

@@ -0,0 +1,115 @@
+#ifndef sync_time_message_h
+#define sync_time_message_h
+
+#include <iostream>
+#include <fstream>
+
+using namespace std;
+
+namespace host_server{
+    class sync_time_message
+    {
+    private:
+        unsigned long long  m_local_id;				// 本节点的id码
+        unsigned long long  m_upper_id;				// 上一级同步节点的id码
+		unsigned long long  m_root_id;				// 根节点的id码
+        unsigned short      m_sync_num;				// 时间同步号
+        unsigned short      m_sync_level;			// 同步层级
+        unsigned long long  m_local_send_time;		// 本分站的发送时间
+        unsigned long long  m_local_receive_time;	// 本分站的接收时间
+    public:
+        sync_time_message():m_local_id(0),
+                            m_upper_id(0),
+                            m_root_id(0),
+                            m_sync_num(0),
+                            m_sync_level(0),
+                            m_local_send_time(0),
+                            m_local_receive_time(0)
+        {}
+
+        sync_time_message(unsigned long long local_id, unsigned long long upper_id, unsigned long long root_id, unsigned short sync_num, unsigned short sync_level, unsigned long long send_time, unsigned long long rec_time):
+            m_local_id(local_id),
+            m_upper_id(upper_id),
+            m_root_id(root_id),
+            m_sync_num(sync_num),
+            m_sync_level(sync_level),
+            m_local_send_time(send_time),
+            m_local_receive_time(rec_time)
+        {}
+
+        unsigned long long get_local_id() const 
+        { 
+            return m_local_id; 
+        }
+
+        void set_local_id(unsigned long long val) 
+		{
+			if(val == 14757395258967641292){
+				val = 0;
+			}
+			m_local_id = val; 
+		}
+
+        unsigned long long get_upper_id() const 
+        { 
+            return m_upper_id; 
+        }
+
+        void set_upper_id(const unsigned long long& val) 
+        { 
+            m_upper_id = val; 
+        }
+
+		unsigned long long get_root_id() const 
+        { 
+            return m_root_id; 
+        }
+
+		void set_root_id(const unsigned long long& val) 
+        { 
+            m_root_id = val; 
+        }
+
+        unsigned short get_sync_num() const 
+        { 
+            return m_sync_num; 
+        }
+        
+        void set_sync_num(const unsigned short& val) 
+        { 
+            m_sync_num = val; 
+        }
+        
+        unsigned short get_sync_level() const 
+        { 
+            return m_sync_level; 
+        }
+        
+        void set_sync_level(const unsigned short& val) 
+        { 
+            m_sync_level = val; 
+        }
+
+        unsigned long long get_local_send_time() const 
+        { 
+            return m_local_send_time; 
+        }
+
+        void set_local_send_time(const unsigned long long& val) 
+        { 
+            m_local_send_time = val; 
+        }
+        
+        unsigned long long get_local_receive_time() const 
+        { 
+            return m_local_receive_time; 
+        }
+        
+        void set_local_receive_time(const unsigned long long& val) 
+        { 
+            m_local_receive_time = val; 
+        }
+    };
+}
+
+#endif

+ 21 - 0
sync_time/tag_message.h

@@ -0,0 +1,21 @@
+#ifndef sync_time_tag_message_h
+#define sync_time_tag_message_h
+
+namespace host_server{
+	/*
+	 * 用于保存时间同步节点的数据
+	*/
+    struct tag_message
+    {
+        unsigned long long  m_local_id;		    // 本节点的分站号
+		unsigned long long  m_sync_root_id;		// 同步根节点的分站号
+        unsigned short      m_sync_num;			// 当前时间同步号
+        unsigned long long  m_receive_time;		// 接收时间
+
+        tag_message():m_local_id(0), m_sync_root_id(0), m_sync_num(0), m_receive_time(0){}
+
+        tag_message(unsigned long long local_id, unsigned long long sync_root_id, unsigned short sync_num, unsigned long long receive_time): m_local_id(local_id), m_sync_root_id(sync_root_id), m_sync_num(sync_num), m_receive_time(receive_time){}
+    };
+}
+
+#endif