Imu_heading源码阅读(二)——GPS_time部分
GPS部分:主要進(jìn)行時(shí)間格式上的轉(zhuǎn)換,將utc時(shí)間轉(zhuǎn)換為unix時(shí)間(以秒為單位,具有時(shí)間戳)
GPS_time.h:
#pragma once一般由編譯器提供保證:同一個(gè)文件不會被包含多次。這里所說的”同一個(gè)文件”是指物理上的一個(gè)文件,而不是指內(nèi)容相同的兩個(gè)文件。無法對一個(gè)頭文件中的一段代碼作#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;//頭文件中對一些封裝函數(shù)進(jìn)行申明,具體實(shí)現(xiàn)在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:主要用于數(shù)據(jù)類型之間的強(qiáng)制轉(zhuǎn)換
C/C++中的數(shù)據(jù)類型轉(zhuǎn)換()/static_cast/dynamic_cast/const_cast/reinterpret_cast_AlbertS的博客-CSDN博客C/C++屬于靜態(tài)語言,也就是在編譯時(shí)變量的數(shù)據(jù)類型即可確定的強(qiáng)類型語言。當(dāng)不同的數(shù)據(jù)類型在一起運(yùn)算或者相互賦值的時(shí)候,就需要進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換。不同數(shù)據(jù)類型占用的內(nèi)存空間不同,而各種數(shù)據(jù)類型的轉(zhuǎn)換時(shí)有規(guī)則的,一種通用的規(guī)則就是“小轉(zhuǎn)大”自動(dòng)進(jìn)行,“大轉(zhuǎn)小”需要強(qiáng)制執(zhí)行。這里的“大”和“小”指的是數(shù)據(jù)范圍...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時(shí)間與GMT時(shí)間:
可以認(rèn)為格林威治時(shí)間就是時(shí)間協(xié)調(diào)時(shí)間(GMT=UTC),格林威治時(shí)間和UTC時(shí)間均用秒數(shù)來計(jì)算的。
UTC時(shí)間格式:2016-08-9T10:01:54.123Z
字母T代表后面跟的時(shí)間,最末尾的Z表示UTC統(tǒng)一時(shí)間。
UTC時(shí)間與CST世界(本地世界,就是本地時(shí)間):UTC + 時(shí)區(qū)差 = 本地時(shí)間;時(shí)區(qū)差東為正,西為負(fù)。在此,把東八區(qū)時(shí)區(qū)差記為 +0800,UTC + (+0800) = 本地(北京)時(shí)間 (1);那么,UTC = 本地時(shí)間(北京時(shí)間))- 0800 (2)。
UTC時(shí)間與Unix時(shí)間戳:
在計(jì)算機(jī)中看到的UTC時(shí)間都是從(1970年01月01日 0:00:00)開始計(jì)算秒數(shù)的。所看到的UTC時(shí)間那就是從1970年這個(gè)時(shí)間點(diǎn)起到具體時(shí)間共有多少秒。 這個(gè)秒數(shù)就是Unix時(shí)間戳。
sprintf()函數(shù):按照規(guī)定的格式進(jìn)行數(shù)據(jù)的輸出
sprintf 函數(shù)詳解_菁華如風(fēng)的博客-CSDN博客_sprintfprintf函數(shù)大家都熟悉,但是printf一般打印到標(biāo)準(zhǔn)輸出,在需要整理、格式化字符串時(shí),sprintf就大顯身手了。例如,在處理傳感器數(shù)據(jù)時(shí),為了將得到的數(shù)據(jù)整合成特定的格式通過網(wǎng)絡(luò)發(fā)送出去,char buffer[100] = { 0 };sprintf(buffer, "temperature: %f; humidity:%f\r\n", tempData, humiData);send(clientSocket, buffer, strlen(buffer));又例如,在進(jìn)行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()函數(shù):拷貝指定的字符串,并返回值
c_str()方法解析_一葉知秋@qqy的博客-CSDN博客_c_str偶然之間看見這樣一條語句,源自師傅給的一個(gè)以前C++項(xiàng)目中,str.c_str(),經(jīng)過百度和多位大佬的博客得知,c_str()方法是返回一個(gè)C語言字符串的指針常量(即可讀不可改變),內(nèi)容與調(diào)用此方法的原字符串相同。即通過c_str()方法,補(bǔ)充C中沒有string類型的問題,,通過STRING類對象的成員函數(shù)c_str()把string對象轉(zhuǎn)換為c中字符串的樣式。其函數(shù)原型為:const char *c_str();通過觀察不難發(fā)現(xiàn),c_str()是一個(gè)指針(實(shí)際上是一個(gè)臨時(shí)指針),指向一個(gè)字符.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()函數(shù):將n進(jìn)制的字符串轉(zhuǎn)化為十進(jìn)制的數(shù)
stoi函數(shù)_shx6666的博客-CSDN博客_stoi作用:將n進(jìn)制的字符串轉(zhuǎn)化為十進(jìn)制的整數(shù)頭文件#include <string>用法stoi(字符串, 起始位置, n進(jìn)制) //將n進(jìn)制字符串轉(zhuǎn)化為十進(jìn)制整數(shù)//將一串二進(jìn)制字符串從第0位開始轉(zhuǎn)化為十進(jìn)制數(shù)string str = 0101010101;stoi(str, 0, 2);//十進(jìn)制數(shù)可以直接轉(zhuǎn)化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()函數(shù):用來將參數(shù) tm 所指的tm結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成從公元1970年1月1日0時(shí)0分0 秒算起至今的UTC時(shí)間所經(jīng)過的秒數(shù)。返回值:返回經(jīng)過的秒數(shù)
【mktime】mktime函數(shù)使用_小石頭有大內(nèi)涵的博客-CSDN博客_mktime函數(shù)原型time_t mktime(struct tm *)其中的 tm 結(jié)構(gòu)體定義如下:struct tm {int tm_sec; /* 秒 – 取值區(qū)間為[0,59] */int tm_min; /* 分 - 取值區(qū)間為[0,59] */int tm_hour; /* 時(shí) - 取值區(qū)間為[0,23] */int tm_mday; /* 一個(gè)月中的日期 - 取值區(qū)間為[1,31] */int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[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各種時(shí)間類型與時(shí)間函數(shù)提供技術(shù)文檔_dianboju4932的博客-CSDN博客簡介本文旨在為了解Linux各種時(shí)間類型與時(shí)間函數(shù)提供技術(shù)文檔。1、Linux下常用時(shí)間類型Linux下常用時(shí)間類型有四種:time_t、struct tm、struct timeval、struct timespec1.1 time_t時(shí)間類型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)); //調(diào)用common.h中的is_leap_year()函數(shù),判斷傳入的數(shù)據(jù)能否被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; }總結(jié)
以上是生活随笔為你收集整理的Imu_heading源码阅读(二)——GPS_time部分的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python数据聚合的方法
- 下一篇: 全球及中国硅基液晶空间光调制器(LCOS