#ifndef _COMMON_H
#define _COMMON_H

#include <time.h>
#include <stdio.h>
#include "scl_shm.h"
#include "PID_AutoTune.h"

#define WEB_DIR "/var/www/web"

//#define STOP_SAMPLE_COOL
//#define YSP_DEBUG

/*
#ifdef YSP_DEBUG
#define ysp_log((x)) printf(x)
#else
#define ysp_log(x)   
#endif */ 

#define CTRL_SERIAL_DEV    "/dev/ttyO4"
//#define CTRL_SERIAL_DEV    "/dev/ttyUSB0"
#define CTRL_SERIAL_SPEED    115200

#define SAMPLE_SERIAL_DEV    "/dev/ttyO2"
#define SAMPLE_SERIAL_SPEED    115200


#define CTRL_TX_MQ             "/ctrl_tx_mq"
#define CTRL_TX_MAX_MESSAGE    16
#define CTRL_TX_MESSAGE_SIZE   1024
#define CTRL_MQ_PRIO           31

#define OFF 0
#define ON  1

#define INDEX_H2   0
#define INDEX_CO   1
#define INDEX_CH4  2
#define INDEX_C2H4 3
#define INDEX_C2H6 4
#define INDEX_C2H2 5

#define CO2_SAMPLE_LEN 50
#define CO2_BACK_NUM   150

//继电器定义
#define RLY_OIL    14  //油泵 
#define RLY_YV4    3 //油缸退(I5反馈)
#define RLY_YV5    4 //油缸进(I4反馈)
#define RLY_YV6    5//气缸退(I7反馈)
#define RLY_YV7    6 //气缸进(I6反馈)
#define RLY_YV8    7
#define RLY_YV9    8
#define RLY_YV10   19 //清洗阀(进载气清洗)
#define RLY_YV11   10 //气缸进油阀(脱气)
#define RLY_YV12   11 //进气到集气缸阀
#define RLY_YV13   12 //出油阀
#define RLY_YV14   13 //清洗排空阀

//注意新RLY_A板V2版本将YV15-YV16移动到7,8了 
//#define RLY_YV15   14 //样气进气缸阀
//#define RLY_YV16   15//进油阀
#define RLY_YV15   7 //样气进气缸阀
#define RLY_YV16   8//进油阀

#define RLY_YV17   16//样气压力平衡气阀
#define RLY_YV18   17//吹扫阀
#define RLY_YV19   18//定量阀(6通阀)
//#define RLY_YV20   19//传感器平衡阀
#define RLY_YV21   20//CO2传感器检测阀

#define RLY_KA1    14  //抽油电机
//#define RLY_KA2  22 //传感器加热开关
#define RLY_KA3    23 //柜子风扇


#define RLY_KA4    24 //采样单元风扇
#define RLY_KA5    25 //采样电源控制信号
#define RLY_KA6    26 //冷井风扇输出
#define RLY_KA7    27  //冷井电源控制信号
#define RLY_SENSOR 29  //传感器加热控制 

#define RLY_FAN    30  //采样单元排热风扇 
#define RLY_PUMP   31  //气泵启停控制(220v,550w) 

#define RLY_KA8    21  //排水阀 


#define STA_I4     0//油缸进到位指示
#define STA_I5     1//油缸退到位指示
#define STA_I6     2//气缸进到位指示
#define STA_I7     3//气缸退到位指示

//注意新RLY_A板V2版本将I8-I9移动到6,7了  
//#define STA_I8     4//油位液位开关(定量传感器)
//#define STA_I9     5//集气缸进油检测开关

#define STA_I8     6//油位液位开关(定量传感器)
#define STA_I9     7//集气缸进油检测开关



#define PRES_PA1      0  //载气压力检测通道
#define PRES_GAS_PUMP 1  //气缸压力 

#define START_PRES   0.25 //Mpa
#define STOP_PRES    0.51 //Mpa

#define TEMP_SPZ     0  //色谱柱温度通道 
#define TEMP_LJ      2  //冷井温度通道
#define TEMP_SAM     3  //采样单元温度通道
#define TEMP_ENV     1  //环境温度(柜内温度) 
#define TEMP_OUTSIDE 4  //室外温度 


#define PID_SPZ    0  //色谱柱PID通道 
#define PID_LJ     2  //冷井PID通道,输出对应A3 
#define PID_SAMPLE 3  //采样单元PID通道号,输出对应A2 


#define PID_EN     1
#define PID_DIS    0


#define I7_WAIT_CNT 12
#define I7_WAIT_MAX 24
#define I6_WAIT_CNT 12
#define I6_WAIT_MAX 24

#define I4_WAIT_CNT 8
#define I4_WAIT_MAX 24
#define I5_WAIT_CNT 8
#define I5_WAIT_MAX 24


#define GAS_CLEAN_MAX_STEP 17         //气路清洗最大步骤 
#define GAS_CLEAN_MAX_TIME        600 //气路清洗最长时间 
#define CLEAN_AIR_REPEAT_CNT      3   //空气清洗次数 
#define CLEAN_CARRIER_REPEAT_CNT  6   //载气清洗次数 
#define GAS_MAKE_REPEAT_CNT       2    //制气重复次数  

#define YV10_ON_TIME 12            //进载气时间6秒 
#define MOTOR_RUN_TIME (3*60*2)    //抽油电机运行3分钟 
#define GAS_ADMISSION_TIME 8      //进油气时间4秒 
#define GAS_MAKE_TIME     (4*60*2) //油气制作时间4分钟


//状态定义 
#define MODE_IDLE     		0
#define SIX_SAMPLE    		1  //6组分采样 
#define DEMARCATE     		2  //标定 
#define REMOVE_GAS        	3  //脱气 
#define GAS_FAULT_PROCESS 	4 //脱气故障处理 
#define PUMP_OIL_PROCESS    5

//命令定义 
#define ENTER_IDLE          0
#define START_SIX_SAMPLE    1
#define START_DEMARCATE     2
#define START_RM_GAS        3
#define START_FAULT_PROCESS 4
#define START_OIL_PROCESS   5
#define STOP_SAMPLE         6


#define NORMAL_PRES    0.103  //Mpa
#define DELT_PRES      0.005
#define SPZ_TEMP_DELT  0.01



//低字节序品质定义,由于是低字节序,mmslite会将高低字节倒过来
#define GOOD         0x0000 
#define INVALID      0x0040
#define QUESTIONABLE 0x00C0

#define OVERFLOWED   0x0020
#define OUTOFRANGE   0x0010
#define BADREFERENCE 0x0008
#define OSCILLATORY  0x0004
#define FAILURED     0x0002
#define OLDDATA      0x0001
#define INCONSISTENT 0x8000
#define INACCURATE   0x4000

#define SUBSTITUTED  0x2000
#define TEST  		 0x1000

typedef struct{
int    auto_turn;
float tune_input;
unsigned short tune_output;	
}AUTO_TUNE_RECORD;

typedef struct{
int step;
int dly_cnt;
int repeat_cnt;
}CLEAN_STAT;

typedef struct{
int cmd;//命令 
int src;//发送者	
}ACTION_MSG_TYPE;//动作消息定义 

typedef struct{
unsigned int   t;
unsigned int   stNum;
unsigned int   sqNum;
unsigned char  dataSet;
unsigned char  numDatEntries;
unsigned short next_time;
}COM_HEADER;



typedef struct{
float Kp;
float Ki;
float Kd;
}PID_TYPE_DEF;

typedef struct{
unsigned int  relay_ctrl;    //继电器控制 
unsigned char pid_en[4];     //PID使能0-1加热,2-3制冷,0关闭,非零开启 
float         temperature[4];//0-1加热,2-3制冷
float         volt_output[2];//0-5V输出控制	
PID_TYPE_DEF  pid_para[3];   //PID参数,通道0色谱柱,通道1冷井,输出到A3,通道2是采样室,输出到A2 
unsigned short man_output[4];//ch0=色谱柱 ch1=保留 ch2=冷井 ch3=采样单元 
}OUTPUT_TYPE_DEF;//输出数据结构定义 


typedef struct{
PID_TYPE_DEF heat_pid[2];
PID_TYPE_DEF cool_pid[2]; 
}OUTPUT_PARA_DEF;//PID参数定义 


typedef struct{
//input
float    PresVolt[5];   //pres
float    temperature[5];//  
unsigned int io_input;  //io输入 

//output
unsigned int   relay_state;	//继电器输出 
unsigned short pwm_out[2];  //pwm输出 
unsigned short vi_out[4];   //模拟量输出 
unsigned char  pid_en[4];   //pid控制通道工作状态 
unsigned int   work_state;  //工作状态 
float  		   temp;//油温 
float          humidity;//湿度
float          water;//微水
unsigned short com_flg; //微水传感器通讯状态,0=OK,1=FAILED 
}INPUT_TYPE_DEF;            //输入数据结构 



typedef struct{
unsigned int data_flg;     
int ref_out;      
int sen_in;       
int amp_in;       
float temperature;  
float humidity; 
unsigned int co2_ppm; 
unsigned int co2_len;   
}SAMPLE_INPUT_TYPE;


typedef struct{
float a;
float b;
}PRES_FAC_TYPE;


typedef struct
{
char version[16];
char station[64];
char device[64];
unsigned int  start_hour;    //采样开始时刻 
unsigned int  start_minute;  //采样开始分钟 
unsigned int  start_interval;//采样间隔 
PRES_FAC_TYPE pres_fac[8];   //8组压力计算系数 
float         volt_output[2];//0-5V输出控制
float         temperature[4];//0-1加热,2-3制冷
unsigned char pid_en[4];     //4路PID使能信号	
PID_TYPE_DEF  pid_type_def[4];//4路pid参数
unsigned int  sample_len;     //采集长度1-5000
unsigned int  sample_rate;    //采样率,单位为毫秒,取值范围为1-1000 
unsigned int  sample_offs;    //采样偏置设置0-4095,对应0-3.3v 
unsigned int  sample_cool_en; //
unsigned int  co2_pos;
unsigned int  h2o_en;
unsigned int  auto_gas_en;//自动造气体 
unsigned int  run_mode;  //运行模式 
unsigned int  upload_time;  //上传时间间隔 
}MACHINE_RUN_PARA;//机器参数定义 



typedef struct{
int start;//起始位置 
int peak;//峰顶位置 
int end;//结束位置 
int width;	//峰宽 
}KEY_POS_TYPE;//查找到的位置定义 

typedef struct
{
 KEY_POS_TYPE position[6];
}FIND_POS_DATA;


typedef struct{
COM_HEADER header;
}COM_FRAME;

typedef struct{
int start;//起始位置 
int peak;//峰顶位置 
int end;//结束位置 
int width;	//峰宽 
}POSSIBLE_POS_TYPE;//可能的位置定义 

typedef struct{
float lYmin;//左梯度 
float lXmax;
float rYmin;
float rXmax;	
}GRADIENT_TYPE_DEF;//梯度定义

typedef struct
{
float fac[6]; //依次为K值,柱修正系数,脱气率修正系数,最小面积,最大面积,离线/在线计算偏差基值	
}FACTOR_TYPE; 

typedef struct
{
FACTOR_TYPE k[12];	//12级修正系数 
}CORRECTION_FACTOR_TYPE; 


typedef struct{
POSSIBLE_POS_TYPE      position; //可能的位置定义 
GRADIENT_TYPE_DEF      gradient; //梯度定义 
CORRECTION_FACTOR_TYPE correct_fac;//修正系数定义	
}GAS_CAL_PARA;//气体浓度计算参数 



typedef struct{
MACHINE_RUN_PARA mach_run_para; //机器运行参数 
GAS_CAL_PARA gas_cal_fac[6]; //气体浓度计算系数,依次为H2,CO,CH4,C2H4,C2H6,C2H2  
GAS_CAL_PARA co2_cal_fac;//CO2计算参数 
}YSP_PARA;//油色谱参数定义 




/*
typedef struct
{
 float v;
 int   q;
 unsigned int t;	
} MV;

typedef struct
{
 int v;
 int q;
 unsigned int t;	
} MV_I;

typedef struct
{
 char v;
 int  q;
 unsigned int t;		
}SPS;

typedef union{
MV   f;
MV_I i;
SPS  stVal;	
}SCL_VAL;*/

typedef struct
{
unsigned int secs;     // Number of seconds since January 1, 1970	
unsigned int fraction; // Fraction of a second				
unsigned int qflags;   // Quality flags, 8 least-significant bits only		
}Timestamp;


typedef struct
{
float H2ppm;
float COppm;
float CH4ppm;
float C2H2ppm;
float C2H4ppm;
float C2H6ppm;
float TotHyd;
int   SmpTm; //采样时间
float CO2ppm;//	
float Mst;   //微水 
float temp;  //油温 
}YSP_PRI_DATA;//油色谱原始数据


typedef struct
{
int start;
int peak;
}POS_INFO;

typedef struct
{
YSP_PRI_DATA result;
int find_mask;
POS_INFO pos_info[6];//找到的位置 
float area[6];//计算出的面积
float spzTemp1;//采样前色谱柱温度 
float spzTemp2;//采样后色谱柱温度 
float samTemp1;//采样前采样室温度 
float samTemp2;//采样后采样室温度
float pres;//采样后载气压力	
}ANALY_RESULT;


typedef struct
{
USR_MV H2ppm;     //氢气含量0 
USR_MV COppm;     //一氧化碳含量1 
USR_MV CH4ppm;    //甲烷含量2 
USR_MV C2H4ppm;   //乙烯含量3 
USR_MV C2H6ppm;   //乙烷含量4 
USR_MV C2H2ppm;   //乙炔含量5 
USR_MV TotHyd;    //总烃含量6 
USR_MV SmpTm;     //采样时间(mv_i)7
USR_MV CO2ppm;  //二氧化碳含量8 
USR_MV Mst;     //微水含量9
USR_MV Tmp;     //绝缘液体温度10
USR_MV GasPres; //载气压力11
USR_MV GasBot;       //异常的气瓶号(ins)12 
USR_MV MoDevConf;    //IED与监测设备通讯异常(sps)13 
USR_MV SupDevRun;    //监测设备运行异常(sps)14
USR_MV GasUnPresAlm; //载气欠压告警(sps)15 
USR_MV GasLowPresAlm;//载气低压告警(sps)16 
USR_MV ActCyGasSta;  //实际气瓶供气状态异常(sps)17
}YSP_IEC_DATA;       //油色谱IEC 61850数据 

extern ANALY_RESULT    ana_result;
extern float last_co2_ppm;
extern int co2_is_ok;
extern OUTPUT_TYPE_DEF my_output_data;
extern INPUT_TYPE_DEF  my_input_data;
extern SAMPLE_INPUT_TYPE my_sample_data;
extern YSP_PARA   my_para_data;
extern unsigned int last_sample_time;
extern int comunication_flg;
extern PID_ATune HeatAutoTuner;
extern AUTO_TUNE_RECORD HeatTuneRecord;
extern unsigned int curr_state;
extern YSP_IEC_DATA ysp_glb_data;
extern void mutex_init(void);
extern void lock_input_data(void);
extern void unlock_input_data(void);
extern void lock_output_data(void);
extern void unlock_output_data(void);

extern unsigned char cal_sum(unsigned char *buf,unsigned int len);
extern int chk_sum(unsigned char *buf,unsigned int len,unsigned char sum);

#endif

 
