统一跨平台时间函数接口。

This commit is contained in:
xinyang
2019-07-31 14:51:01 +08:00
parent 01882b97c6
commit 98bc5ef95b
23 changed files with 87 additions and 95 deletions

View File

@@ -6,13 +6,13 @@
#define _ARMOR_FINDER_H_ #define _ARMOR_FINDER_H_
#include <map> #include <map>
#include <sys/time.h> #include <systime.h>
#include <constants.h> #include <constants.h>
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#include <opencv2/tracking.hpp> #include <opencv2/tracking.hpp>
#include <serial/serial.h> #include <serial.h>
#include <armor_finder/classifier/classifier.h> #include <armor_finder/classifier/classifier.h>
#include <additions/additions.h> #include <additions.h>
#define BLOB_RED ENEMY_RED #define BLOB_RED ENEMY_RED
#define BLOB_BLUE ENEMY_BLUE #define BLOB_BLUE ENEMY_BLUE
@@ -111,7 +111,7 @@ private:
INCREASE, DECREASE, NOCHANGE INCREASE, DECREASE, NOCHANGE
} BoxRatioChangeType; } BoxRatioChangeType;
timeval frame_time; // 当前帧对应时间; systime frame_time; // 当前帧对应时间;
const uint8_t &enemy_color; // 敌方颜色,引用外部变量,自动变化 const uint8_t &enemy_color; // 敌方颜色,引用外部变量,自动变化
State state; // 自瞄状态对象实例 State state; // 自瞄状态对象实例
ArmorBox armor_box; // 当前目标装甲板 ArmorBox armor_box; // 当前目标装甲板
@@ -123,7 +123,7 @@ private:
const uint8_t &use_classifier; // 标记是否启用CNN分类器引用外部变量自动变化 const uint8_t &use_classifier; // 标记是否启用CNN分类器引用外部变量自动变化
RoundQueue<double, 4> top_periodms; RoundQueue<double, 4> top_periodms;
RoundQueue<double, 5> box_ratioes; RoundQueue<double, 5> box_ratioes;
timeval last_front_time; // 上一次发生装甲板方向切换的时间 systime last_front_time; // 上一次发生装甲板方向切换的时间
BoxRatioChangeType last_ratio_type; BoxRatioChangeType last_ratio_type;
int anti_top_cnt; // 满足条件的装甲板方向切换持续次数,用于反陀螺 int anti_top_cnt; // 满足条件的装甲板方向切换持续次数,用于反陀螺
AntiTopState anti_top_state; // 当前是否识别到陀螺 AntiTopState anti_top_state; // 当前是否识别到陀螺

View File

@@ -3,7 +3,7 @@
// //
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <additions/additions.h> #include <additions.h>
#include <log.h> #include <log.h>
static double boxDistance(const cv::Rect2d &a, const cv::Rect2d &b) { static double boxDistance(const cv::Rect2d &a, const cv::Rect2d &b) {
@@ -53,8 +53,8 @@ void ArmorFinder::antiTop() {
if (anti_top_state == ANTI_TOP) { if (anti_top_state == ANTI_TOP) {
top_periodms.push(interval); top_periodms.push(interval);
LOGM(STR_CTR(WORD_LIGHT_GREEN, "top period: %.1lf ms"), interval); LOGM(STR_CTR(WORD_LIGHT_GREEN, "top period: %.1lf ms"), interval);
timeval curr_time; systime curr_time;
gettimeofday(&curr_time, nullptr); getsystime(curr_time);
auto calculate_time = getTimeIntervalms(curr_time, frame_time); auto calculate_time = getTimeIntervalms(curr_time, frame_time);
shoot_delay = mean(top_periodms) - calculate_time; shoot_delay = mean(top_periodms) - calculate_time;
} else if (anti_top_state == NORMAL) { } else if (anti_top_state == NORMAL) {

View File

@@ -17,11 +17,10 @@
#define LOG_LEVEL LOG_NONE #define LOG_LEVEL LOG_NONE
#include <log.h> #include <log.h>
#include <options/options.h> #include <options.h>
#include <show_images/show_images.h> #include <show_images/show_images.h>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <sys/time.h>
std::map<int, string> id2name = { //装甲板id到名称的map std::map<int, string> id2name = { //装甲板id到名称的map
{-1, "OO"},{ 0, "NO"}, {-1, "OO"},{ 0, "NO"},
@@ -58,7 +57,7 @@ ArmorFinder::ArmorFinder(uint8_t &color, Serial &u, const string &paras_folder,
} }
void ArmorFinder::run(cv::Mat &src) { void ArmorFinder::run(cv::Mat &src) {
gettimeofday(&frame_time, nullptr); getsystime(frame_time);
// stateSearchingTarget(src); // for debug // stateSearchingTarget(src); // for debug
// goto end; // goto end;
switch (state) { switch (state) {

View File

@@ -4,7 +4,7 @@
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <show_images/show_images.h> #include <show_images/show_images.h>
#include <options/options.h> #include <options.h>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#define DO_NOT_CNT_TIME #define DO_NOT_CNT_TIME
#include <log.h> #include <log.h>

View File

@@ -3,7 +3,7 @@
// //
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <options/options.h> #include <options.h>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
static double lw_rate(const cv::RotatedRect &rect) { static double lw_rate(const cv::RotatedRect &rect) {

View File

@@ -4,7 +4,7 @@
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <show_images/show_images.h> #include <show_images/show_images.h>
#include <options/options.h> #include <options.h>
#include <log.h> #include <log.h>
bool ArmorFinder::stateSearchingTarget(cv::Mat &src) { bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {

View File

@@ -2,7 +2,7 @@
// Created by xinyang on 19-3-27. // Created by xinyang on 19-3-27.
// //
#include <log.h> #include <log.h>
#include <options/options.h> #include <options.h>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <show_images/show_images.h> #include <show_images/show_images.h>

View File

@@ -1,6 +1,6 @@
#include <show_images/show_images.h> #include <show_images/show_images.h>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include <options/options.h> #include <options.h>
#include <log.h> #include <log.h>
using namespace cv; using namespace cv;

View File

@@ -4,7 +4,7 @@
#ifndef CONSTANT_H #ifndef CONSTANT_H
#define CONSTANT_H #define CONSTANT_H
#include "additions/additions.h" #include "additions.h"
const int BIG = 1; const int BIG = 1;
const int SMALL = 0; const int SMALL = 0;
const int SRC_WIDTH = 320; const int SRC_WIDTH = 320;

View File

@@ -13,12 +13,12 @@
#include <constants.h> #include <constants.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <sys/timeb.h> #include <systime.h>
#include "energy/constant.h" #include "energy/constant.h"
#include "energy/param_struct_define.h" #include "energy/param_struct_define.h"
#include "serial/serial.h" #include "serial.h"
#include "additions/additions.h" #include "additions.h"
#include "options/options.h" #include "options.h"
using std::vector; using std::vector;
@@ -87,7 +87,7 @@ private:
float shoot;//若为2则要求主控板发弹 float shoot;//若为2则要求主控板发弹
float sum_yaw, sum_pitch;//yaw和pitch的累计误差即PID中积分项 float sum_yaw, sum_pitch;//yaw和pitch的累计误差即PID中积分项
timeval time_start_guess; systime time_start_guess;
cv::RotatedRect centerR;//风车中心字母R的可能候选区 cv::RotatedRect centerR;//风车中心字母R的可能候选区
cv::RotatedRect flow_strip;//图像中所有流动条(理论上只有一个) cv::RotatedRect flow_strip;//图像中所有流动条(理论上只有一个)

View File

@@ -19,7 +19,7 @@ void Energy::judgeShootInWorld() {
// is_predicting = false; // is_predicting = false;
// is_guessing = true; // is_guessing = true;
// start_guess = true; // start_guess = true;
// gettimeofday(&time_start_guess, NULL); // getsystime(time_start_guess);
LOGM(STR_CTR(WORD_LIGHT_RED, "Start Guessing!")); LOGM(STR_CTR(WORD_LIGHT_RED, "Start Guessing!"));
} else } else
shoot = 2; shoot = 2;
@@ -35,7 +35,7 @@ void Energy::judgeShootInGimbal() {
// is_predicting = false; // is_predicting = false;
// is_guessing = true; // is_guessing = true;
// start_guess = true; // start_guess = true;
// gettimeofday(&time_start_guess, NULL); // getsystime(time_start_guess);
// LOGM(STR_CTR(WORD_LIGHT_RED, "Start Guessing!")); // LOGM(STR_CTR(WORD_LIGHT_RED, "Start Guessing!"));
} else } else
shoot = 2; shoot = 2;

View File

@@ -14,8 +14,7 @@ using namespace cv;
// 此函数用于判断guess模式是否超时 // 此函数用于判断guess模式是否超时
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
bool Energy::isGuessingTimeout() { bool Energy::isGuessingTimeout() {
timeval cur_time; systime cur_time;
gettimeofday(&cur_time, NULL); getsystime(cur_time);
return (cur_time.tv_sec - time_start_guess.tv_sec) * 1000.0 + return getTimeIntervalms(cur_time, time_start_guess) > 1000;
(cur_time.tv_usec - time_start_guess.tv_usec) / 1000.0 > 1000;
}; };

View File

@@ -4,8 +4,7 @@
#include "energy/energy.h" #include "energy/energy.h"
#include "log.h" #include "log.h"
#include "config/setconfig.h" #include "config/setconfig.h"
#include "options/options.h" #include "options.h"
#include <sys/time.h>
using namespace std; using namespace std;
using namespace cv; using namespace cv;

View File

@@ -10,14 +10,14 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <serial/serial.h> #include <serial.h>
#include <camera/camera_wrapper.h> #include <camera/camera_wrapper.h>
#include <camera/video_wrapper.h> #include <camera/video_wrapper.h>
#include <camera/wrapper_head.h> #include <camera/wrapper_head.h>
#include <energy/energy.h> #include <energy/energy.h>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <options/options.h> #include <options.h>
#include <additions/additions.h> #include <additions.h>
#include <config/setconfig.h> #include <config/setconfig.h>
#define DO_NOT_CNT_TIME #define DO_NOT_CNT_TIME

View File

@@ -6,8 +6,8 @@
#define _ADDITIONS_H_ #define _ADDITIONS_H_
#include <stdint.h> #include <stdint.h>
#include <sys/time.h> #include <systime.h>
#include <serial/serial.h> #include <serial.h>
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
struct mcu_data { struct mcu_data {
@@ -41,7 +41,7 @@ void extract(cv::Mat &gimbal_src, cv::Mat &chassis_src);
void extract(cv::Mat &gimbal_src); void extract(cv::Mat &gimbal_src);
double getTimeIntervalms(const timeval &now, const timeval &last); float getTimeIntervalms(const systime &now, const systime &last);
template<class type, int length> template<class type, int length>
class RoundQueue { class RoundQueue {

View File

@@ -7,7 +7,7 @@
#ifndef VIDEO_TEST1_CAMERA_WRAPPER_H #ifndef VIDEO_TEST1_CAMERA_WRAPPER_H
#define VIDEO_TEST1_CAMERA_WRAPPER_H #define VIDEO_TEST1_CAMERA_WRAPPER_H
#include <additions/additions.h> #include <additions.h>
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <camera/wrapper_head.h> #include <camera/wrapper_head.h>

23
others/include/systime.h Normal file
View File

@@ -0,0 +1,23 @@
//
// Created by xinyang on 19-7-31.
//
#ifndef _PLATFORM_H_
#define _PLATFORM_H_
typedef struct{
int second;
int millisecond;
} systime;
void getsystime(systime &t);
#if defined(Linux)
#include <sys/time.h>
#elif defined(Windows)
#include <Windows.h>
#else
#error "nonsupport platform."
#endif
#endif /* _PLATFORM_H_ */

View File

@@ -9,7 +9,7 @@
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio/videoio_c.h> #include <opencv2/videoio/videoio_c.h>
#include <additions/additions.h> #include <additions.h>
#include <camera/camera_wrapper.h> #include <camera/camera_wrapper.h>
#include <energy/energy.h> #include <energy/energy.h>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
@@ -170,6 +170,6 @@ void extract(cv::Mat &gimbal_src) {//图像预处理将视频切成640×480
} }
} }
double getTimeIntervalms(const timeval& now, const timeval &last){ float getTimeIntervalms(const systime &now, const systime &last){
return (now.tv_sec-last.tv_sec)*1000.0 + (now.tv_usec-last.tv_usec)/1000.0; return (now.second-last.second)*1000.0 + (now.millisecond-last.millisecond);
} }

View File

@@ -4,8 +4,8 @@
#include <iostream> #include <iostream>
#include <camera/camera_wrapper.h> #include <camera/camera_wrapper.h>
#include <log.h> #include <log.h>
#include <additions/additions.h> #include <additions.h>
#include <options/options.h> #include <options.h>
#include <config/setconfig.h> #include <config/setconfig.h>
using namespace std; using namespace std;
@@ -86,35 +86,7 @@ bool CameraWrapper::init() {
CameraLoadParameter(h_camera, PARAMETER_TEAM_A); CameraLoadParameter(h_camera, PARAMETER_TEAM_A);
CameraSetAeState(h_camera, false); CameraSetAeState(h_camera, false);
CameraSetExposureTime(h_camera, CAMERA_EXPOSURE * 1000); CameraSetExposureTime(h_camera, CAMERA_EXPOSURE * 1000);
#ifndef WITH_TIME_BASED_CAMERA_GAIN
CameraSetAnalogGain(h_camera, gain); CameraSetAnalogGain(h_camera, gain);
#else
#include <sys/time.h>
timeval tv;
int gain;
gettimeofday(&tv, nullptr);
float hour = (tv.tv_sec % (3600 * 24)) / 3600.0;
if (6 <= hour && hour < 10) {
gain = 20;
LOGM("Set gain to morning 20");
} else if (10 <= hour && hour < 16) {
gain = 10;
LOGM("Set gain to day 10");
} else if (16 <= hour && hour < 17) {
gain = 20;
LOGM("Set gain to early evening 20");
} else if (17 <= hour && hour < 18) {
gain = 40;
LOGM("Set gain to evening 40");
} else {
gain = 50;
LOGM("Set gain to early night 50");
}
CameraSetAnalogGain(h_camera, gain);
#endif
#endif #endif
double t; double t;
int g; int g;
@@ -204,10 +176,10 @@ bool CameraWrapper::readProcessed(cv::Mat &src) {
} }
bool CameraWrapper::readCallback(cv::Mat &src) { bool CameraWrapper::readCallback(cv::Mat &src) {
timeval ts, te; systime ts, te;
gettimeofday(&ts, NULL); getsystime(ts);
while(src_queue.empty()){ while(src_queue.empty()){
gettimeofday(&te, NULL); getsystime(te);
if(getTimeIntervalms(te, ts) > 500){ if(getTimeIntervalms(te, ts) > 500){
return false; return false;
} }

View File

@@ -2,7 +2,7 @@
// Created by xinyang on 19-3-27. // Created by xinyang on 19-3-27.
// //
#include <options/options.h> #include <options.h>
#include <log.h> #include <log.h>
#include <cstring> #include <cstring>

View File

@@ -1,5 +1,5 @@
#include <serial/serial.h> #include <serial.h>
#include <options/options.h> #include <options.h>
#include <iostream> #include <iostream>
//#define LOG_LEVEL LOG_NONE //#define LOG_LEVEL LOG_NONE