Imu_heading源码阅读(二)——GPS_time部分
GPS部分:主要進行時間格式上的轉換,將utc時間轉換為unix時間(以秒為單位,具有時間戳)
GPS_time.h:
#pragma once一般由編譯器提供保證:同一個文件不會被包含多次。這里所說的”同一個文件”是指物理上的一個文件,而不是指內容相同的兩個文件。無法對一個頭文件中的一段代碼作#pragma once聲明,而只能針對文件。
#pragma once//包含utils目錄下的common.h文件 #include "utils/common.h"// gps time start point constexpr uint32_t GPS_YEAR = 1980; constexpr uint32_t GPS_DAY_OFFSET = 5;//頭文件中對一些封裝函數進行申明,具體實現在cpp文件中 bool timeString2timecount(const std::string &time_string,double &time_count_us);bool timecount2timeString(double time_count_us, std::string &time_string);class GPSTime { public:GPSTime() = default;GPSTime(uint32_t gps_week, double gps_second);inline std::string GetUTCTime() { return utc_time_; }inline std::string GetUTCTimeUs() { return utc_time_us_; }inline double GetTotalSeconds() { return total_seconds_; }private:// accurate to ms for most of the datastd::string utc_time_;// accurate to us for raw imu datastd::string utc_time_us_;double total_seconds_; };GPS_time.cpp:
static_cast:主要用于數據類型之間的強制轉換
C/C++中的數據類型轉換()/static_cast/dynamic_cast/const_cast/reinterpret_cast_AlbertS的博客-CSDN博客C/C++屬于靜態語言,也就是在編譯時變量的數據類型即可確定的強類型語言。當不同的數據類型在一起運算或者相互賦值的時候,就需要進行數據類型轉換。不同數據類型占用的內存空間不同,而各種數據類型的轉換時有規則的,一種通用的規則就是“小轉大”自動進行,“大轉小”需要強制執行。這里的“大”和“小”指的是數據范圍...https://blog.csdn.net/albertsh/article/details/118663176?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165953794216782395351137%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165953794216782395351137&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-118663176-null-null.142%5Ev39%5Econtrol&utm_term=static_cast&spm=1018.2226.3001.4187
UTC時間與GMT時間:
可以認為格林威治時間就是時間協調時間(GMT=UTC),格林威治時間和UTC時間均用秒數來計算的。
UTC時間格式:2016-08-9T10:01:54.123Z
字母T代表后面跟的時間,最末尾的Z表示UTC統一時間。
UTC時間與CST世界(本地世界,就是本地時間):UTC + 時區差 = 本地時間;時區差東為正,西為負。在此,把東八區時區差記為 +0800,UTC + (+0800) = 本地(北京)時間 (1);那么,UTC = 本地時間(北京時間))- 0800 (2)。
UTC時間與Unix時間戳:
在計算機中看到的UTC時間都是從(1970年01月01日 0:00:00)開始計算秒數的。所看到的UTC時間那就是從1970年這個時間點起到具體時間共有多少秒。 這個秒數就是Unix時間戳。
sprintf()函數:按照規定的格式進行數據的輸出
sprintf 函數詳解_菁華如風的博客-CSDN博客_sprintfprintf函數大家都熟悉,但是printf一般打印到標準輸出,在需要整理、格式化字符串時,sprintf就大顯身手了。例如,在處理傳感器數據時,為了將得到的數據整合成特定的格式通過網絡發送出去,char buffer[100] = { 0 };sprintf(buffer, "temperature: %f; humidity:%f\r\n", tempData, humiData);send(clientSocket, buffer, strlen(buffer));又例如,在進行HThttps://blog.csdn.net/qq_37253168/article/details/120184412?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165957816016782184689425%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165957816016782184689425&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-120184412-null-null.142%5Ev39%5Econtrol&utm_term=sprintf&spm=1018.2226.3001.4187cstr()函數:拷貝指定的字符串,并返回值
c_str()方法解析_一葉知秋@qqy的博客-CSDN博客_c_str偶然之間看見這樣一條語句,源自師傅給的一個以前C++項目中,str.c_str(),經過百度和多位大佬的博客得知,c_str()方法是返回一個C語言字符串的指針常量(即可讀不可改變),內容與調用此方法的原字符串相同。即通過c_str()方法,補充C中沒有string類型的問題,,通過STRING類對象的成員函數c_str()把string對象轉換為c中字符串的樣式。其函數原型為:const char *c_str();通過觀察不難發現,c_str()是一個指針(實際上是一個臨時指針),指向一個字符.https://blog.csdn.net/qq_41004932/article/details/110962216?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165957878416781432935235%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165957878416781432935235&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-110962216-null-null.142%5Ev39%5Econtrol&utm_term=c_str&spm=1018.2226.3001.4187stoi()函數:將n進制的字符串轉化為十進制的數
stoi函數_shx6666的博客-CSDN博客_stoi作用:將n進制的字符串轉化為十進制的整數頭文件#include <string>用法stoi(字符串, 起始位置, n進制) //將n進制字符串轉化為十進制整數//將一串二進制字符串從第0位開始轉化為十進制數string str = 0101010101;stoi(str, 0, 2);//十進制數可以直接轉化string s = 12345;stoi(s);...https://blog.csdn.net/sunhongxuan666/article/details/120422143?ops_request_misc=&request_id=&biz_id=102&utm_term=stoi&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-120422143.142%5Ev39%5Econtrol&spm=1018.2226.3001.4187mktime()函數:用來將參數 tm 所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。返回值:返回經過的秒數
【mktime】mktime函數使用_小石頭有大內涵的博客-CSDN博客_mktime函數原型time_t mktime(struct tm *)其中的 tm 結構體定義如下:struct tm {int tm_sec; /* 秒 – 取值區間為[0,59] */int tm_min; /* 分 - 取值區間為[0,59] */int tm_hour; /* 時 - 取值區間為[0,23] */int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */inthttps://blog.csdn.net/shileiwu0505/article/details/123030559?ops_request_misc=&request_id=&biz_id=102&utm_term=mktime&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-123030559.142%5Ev39%5Econtrol&spm=1018.2226.3001.4187Linux各種時間類型與時間函數提供技術文檔_dianboju4932的博客-CSDN博客簡介本文旨在為了解Linux各種時間類型與時間函數提供技術文檔。1、Linux下常用時間類型Linux下常用時間類型有四種:time_t、struct tm、struct timeval、struct timespec1.1 time_t時間類型time_t類型在time.h中定義:[cpp] view plaincopyprint?'#ifndef __TIME_T'#...https://blog.csdn.net/dianboju4932/article/details/101572386?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165958012416781818775319%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165958012416781818775319&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-101572386-null-null.142%5Ev39%5Econtrol&utm_term=linux%E6%97%B6%E9%97%B4%E7%B1%BB%E5%9E%8B&spm=1018.2226.3001.4187
#include "time.h" #include <string>#include "utils/GPS_time.h"GPSTime::GPSTime(uint32_t gps_week, double gps_seconds) {double total_seconds = gps_week * SECOND_WEEK + gps_seconds - LEAP_SECOND +TIME_ZONE * SECOND_HOUR + GPS_DAY_OFFSET * SECOND_DAY;// reserve the total secondtotal_seconds_ = total_seconds;uint32_t utc_year = 0;uint32_t utc_month = 0;uint32_t utc_day = 0;uint32_t utc_hour = 0;uint32_t utc_minute = 0;uint32_t utc_second = 0;uint32_t utc_ms = 0;uint32_t utc_us = 0;uint32_t year_inc = 0;// compute yearuint32_t leap_year;while (true) {leap_year = static_cast<uint32_t>(is_leap_year(GPS_YEAR + year_inc)); //調用common.h中的is_leap_year()函數,判斷傳入的數據能否被4整除且不能被100整除||被400整除;若滿足,則返回真值。total_seconds -= MONTH_DAY[leap_year][0] * SECOND_DAY;if (total_seconds < 0) {utc_year = GPS_YEAR + year_inc;total_seconds += MONTH_DAY[leap_year][0] * SECOND_DAY;break;}year_inc += 1;}// compute monthuint32_t month_index = 1;leap_year = static_cast<uint32_t>(is_leap_year(utc_year));while (true) {total_seconds -= MONTH_DAY[leap_year][month_index] * SECOND_DAY;if (total_seconds < 0) {utc_month = month_index;total_seconds += MONTH_DAY[leap_year][month_index] * SECOND_DAY;break;}month_index += 1;}// compute dayutc_day = static_cast<uint32_t>(total_seconds / SECOND_DAY) + 1;total_seconds -= (utc_day - 1) * SECOND_DAY;// compute hourutc_hour = static_cast<uint32_t>(total_seconds / SECOND_HOUR);total_seconds -= utc_hour * SECOND_HOUR;// compute minuteutc_minute = static_cast<uint32_t>(total_seconds / 60);total_seconds -= utc_minute * 60;// compute secondutc_second = static_cast<uint32_t>(total_seconds);// compute msutc_ms = static_cast<uint32_t>((total_seconds - utc_second) * 1000);// compute usutc_us = static_cast<uint32_t>((total_seconds - utc_second) * 1000000);// format yyyy-mm-dd hh:mm:ss.mschar buff[1024];sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d.%03d", utc_year, utc_month,utc_day, utc_hour, utc_minute, utc_second, utc_ms);utc_time_ = std::string(buff);// format yyyy-mm-dd hh:mm:ss.ussprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d.%06d", utc_year, utc_month,utc_day, utc_hour, utc_minute, utc_second, utc_us);utc_time_us_ = std::string(buff);return; }/** Convert standart utc time string into time_count from unix time start* in: time string with utc format* out: time count from 1970-01-01 with us* Note: different from gps time start point 1980-01-06*/ bool timeString2timecount(const std::string &time_string,double &time_count_us) {if (time_string.size() < 23)return false;timeval tv;struct tm stm;if (!strptime(time_string.substr(0, 19).c_str(), "%Y-%m-%d %H:%M:%S", &stm)) {printf("Convert %s to tm struct failed, please check your time format!\n",time_string.substr(0, 19).c_str());return false;}std::string usStr = time_string.substr(20);int us;if (usStr.size() == 3) {us = stoi(usStr) * 1000; // ms to us} else if (usStr.size() == 6) {us = stoi(usStr);} else {printf("Please use millisecond or microsecond time format!\n");return false;}time_t sec = mktime(&stm);tv.tv_sec = sec;tv.tv_usec = us;time_count_us = static_cast<double>(tv.tv_sec * 1e6 + tv.tv_usec);return true; }/** Convert time count(us) to standart utc format* in: time count from 1970-01-01 with us* out: time string with utc format* Note: different from gps time start point 1980-01-06*/ bool timecount2timeString(double time_count_us, std::string &time_string) {timeval tv;tv.tv_sec = static_cast<__time_t>(time_count_us / 1e6);tv.tv_usec = static_cast<__suseconds_t>(time_count_us - tv.tv_sec * 1e6);unsigned int ms = tv.tv_usec / 1000;time_t time(tv.tv_sec);struct tm stm;localtime_r(&time, &stm);char buffer[128];strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%M-%S", &stm);char res[128];snprintf(res, sizeof(res), "%s-%03d", buffer, ms);time_string = std::string(res);return true; }總結
以上是生活随笔為你收集整理的Imu_heading源码阅读(二)——GPS_time部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python数据聚合的方法
- 下一篇: 全球及中国硅基液晶空间光调制器(LCOS