Data_AirPressure.c 12.6 KB
Newer Older
hu's avatar
hu committed
1 2 3 4 5 6
#include "Data_AirPressure.h"
#include "Gauges.h"
#include "GaugesInterface.h"
#include "CAN_Communication_Matrix.h"
#include "RTE_ADC.h"

hu's avatar
hu committed
7 8
#include "Common_Interface.h"

hu's avatar
hu committed
9
//断码显示
hu's avatar
hu committed
10 11 12 13 14
//-------------------------------------------
//344 - 3.1.2.FRONT/REAR空气计显示
//342 - 3.3.  FRONT/REAR气压指针
//-------------------------------------------

hu's avatar
hu committed
15 16
#define     PRI_ID      0
#define     SEC_ID      1
hu's avatar
hu committed
17

hu's avatar
hu committed
18
#define     TableLenMax 6u
hu's avatar
hu committed
19

hu's avatar
hu committed
20
#define     EnTable     0  // 0 - 计算  1 - 查表
hu's avatar
hu committed
21

hu's avatar
hu committed
22
#define     VALID_TIME  100 // 2S
hu's avatar
hu committed
23 24 25

static struct
{
hu's avatar
hu committed
26
    uint8_t SumCnt ;
hu's avatar
hu committed
27 28 29
    uint32_t Voltage ;
    uint16_t Result ;
} AirPressureFilter[2u] ;
hu's avatar
hu committed
30 31 32

static struct
{
hu's avatar
hu committed
33 34 35 36 37 38 39 40 41 42 43 44
    uint16_t VoltageTable[TableLenMax] ;
    uint16_t kPaTable[TableLenMax] ;
    uint16_t slopeTable[TableLenMax - 1 ] ;// 100倍的
    uint16_t ResultkPa[2u] ; // 100倍的
    uint16_t AvrVoltage[2u] ;
    uint8_t Valid[2u] ;
    uint8_t CurSeg[2u] ;
    uint8_t ValidTimer[2u]  ;
    uint8_t InvalidTimer[2u]  ;
} AirPressureVariable ;

static void Calc_AirPressure_Table(uint16_t Voltage, uint16_t * kPa);
hu's avatar
hu committed
45
static uint16_t Get_AirPressure_AdC_Filter(uint8_t ID);
hu's avatar
hu committed
46
static uint8_t CalAirPressure_CurSeg(uint8_t ID, uint8_t Valid, uint16_t Voltage);
hu's avatar
hu committed
47 48
/*-------------------------------------------------------------------------
 * Function Name  : AirPressure_KL30_Init
hu's avatar
hu committed
49
 * Description    :
hu's avatar
hu committed
50 51 52 53 54 55 56
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : Table固化 斜率同时固化
 --------------------------------------------------------------------------*/
void AirPressure_KL30_Init(void)
{
hu's avatar
hu committed
57 58
    uint8_t i = 0 ;
    uint32_t u32slope = 0 ;
hu's avatar
hu committed
59
    AirPressureVariable.VoltageTable[ 0 ] = 500 ;
hu's avatar
hu committed
60 61 62
    AirPressureVariable.kPaTable[ 0 ] = 0 ;


hu's avatar
hu committed
63
    AirPressureVariable.VoltageTable[ 1 ] = 1750    ;
hu's avatar
hu committed
64 65
    AirPressureVariable.kPaTable[ 1 ] = 500 ;

hu's avatar
hu committed
66
    AirPressureVariable.VoltageTable[ 2 ] = 1960    ;
hu's avatar
hu committed
67 68
    AirPressureVariable.kPaTable[ 2 ] = 583 ;

hu's avatar
hu committed
69
    AirPressureVariable.VoltageTable[ 3 ] = 2000    ;
hu's avatar
hu committed
70 71
    AirPressureVariable.kPaTable[ 3 ] = 600 ;

hu's avatar
hu committed
72
    AirPressureVariable.VoltageTable[ 4 ] = 3000    ;
hu's avatar
hu committed
73 74
    AirPressureVariable.kPaTable[ 4 ] = 1000 ;

hu's avatar
hu committed
75
    AirPressureVariable.VoltageTable[ 5 ] = 4250    ;
hu's avatar
hu committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    AirPressureVariable.kPaTable[ 5 ] = 1500 ;

    for (i = 0; i < (TableLenMax - 1); i ++)
    {
        /*100倍 斜率*/
        u32slope = AirPressureVariable.kPaTable[ i + 1] - AirPressureVariable.kPaTable[ i ];
        u32slope *= 100 ;
        u32slope /= (AirPressureVariable.VoltageTable[ i + 1 ] - AirPressureVariable.VoltageTable[ i ]);
        AirPressureVariable.slopeTable[i] = u32slope ;
    }

    for (i = 0; i < 2; i ++)
    {
        AirPressureFilter[ i ].SumCnt = 0 ;
        AirPressureFilter[ i ].Voltage = 0 ;
        AirPressureFilter[ i ].Result = 0 ;

        AirPressureVariable.ResultkPa[ i ] = 0 ;
        AirPressureVariable.Valid[ i ] = 0 ;
        AirPressureVariable.ValidTimer[ i ] = 0 ;
        AirPressureVariable.InvalidTimer[ i ] = 0 ;
    }



hu's avatar
hu committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}
void AirPressure_KL15_ON_Init(void)
{

}
void AirPressure_KL15_OFF_Init(void)
{

}
void AirPressure_Wakeup_Init(void)
{

}
void AirPressure_Sleep_Init(void)
{

}
/*-------------------------------------------------------------------------
 * Function Name  : AirPressure_Processing_Service
 * Description    : 主函数周期调用
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
void AirPressure_Processing_Service(void)
{
hu's avatar
hu committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    uint8_t i = 0 ;

    AirPressure_AdcData_Filter();

    for (i = 0; i < 2; i ++)
    {
        AirPressureVariable.AvrVoltage[ i ] = Get_AirPressure_AdC_Filter(i) ;
        if ((AirPressureVariable.AvrVoltage[ i ] < 500) || (AirPressureVariable.AvrVoltage[ i ] > 4250) )
        {
            if (AirPressureVariable.InvalidTimer[ i ] < VALID_TIME)
            {
                //2s
                AirPressureVariable.InvalidTimer[ i ] ++ ;
            }
            else
            {
                AirPressureVariable.ValidTimer[ i ] = 0 ;
                AirPressureVariable.Valid[i] = 0 ;
            }

        }
        else
        {
            if (AirPressureVariable.ValidTimer[ i ] < VALID_TIME)
            {
                AirPressureVariable.ValidTimer[ i ] ++ ;
            }
            else
            {
                AirPressureVariable.InvalidTimer[ i ] = 0 ;
                AirPressureVariable.Valid[i] = 1 ;
            }

        }

        Calc_AirPressure_Table(AirPressureVariable.AvrVoltage[i], (uint16_t *)&AirPressureVariable.ResultkPa[i]);

        AirPressureVariable.CurSeg[ i ] = CalAirPressure_CurSeg(i, AirPressureVariable.Valid[i], AirPressureVariable.AvrVoltage[ i ]);
    }
hu's avatar
hu committed
167 168 169 170 171 172 173 174 175
}
/*-------------------------------------------------------------------------
 * Function Name  : Calc_AirPressure_Table
 * Description    : 电压 转为 压力
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
hu's avatar
hu committed
176
static void Calc_AirPressure_Table(uint16_t Voltage, uint16_t * kPa)
hu's avatar
hu committed
177
{
hu's avatar
hu committed
178 179 180
    uint8_t i = 0 ;
    uint8_t Index = 0 ;
    uint32_t u32Result = 0 ;
hu's avatar
hu committed
181 182

#if EnTable
hu's avatar
hu committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    if (Voltage <= AirPressureVariable.VoltageTable[ 0 ])
    {
        u32Result = AirPressureVariable.kPaTable[ 0 ] ;
    }
    else if (Voltage >= AirPressureVariable.VoltageTable[ TableLenMax - 1 ])
    {
        u32Result = AirPressureVariable.kPaTable[ TableLenMax - 1 ] ;
    }
    else
    {
        for (i = 0; i < TableLenMax; i ++ )
        {
            if (Voltage < AirPressureVariable.VoltageTable[ i ])
            {
                Index = i ;
                break ;
            }
        }
        if ((Index < 1) || (Index > (TableLenMax - 1)) )
        {
            //err
            while (1)
            {
                ;
            }
        }

        //y=kx + b
        Voltage -= AirPressureVariable.VoltageTable[Index - 1] ;
        u32Result = AirPressureVariable.slopeTable[Index - 1] * Voltage  ;
        u32Result += 50 ;
        u32Result /= 100 ;
        u32Result += AirPressureVariable.kPaTable[ Index - 1 ]  ;

    }
hu's avatar
hu committed
218
#else
hu's avatar
hu committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    if (Voltage <= AirPressureVariable.VoltageTable[ 0 ])
    {
        u32Result = AirPressureVariable.kPaTable[ 0 ] ;
    }
    else if (Voltage >= AirPressureVariable.VoltageTable[ TableLenMax - 1 ])
    {
        u32Result = AirPressureVariable.kPaTable[ TableLenMax - 1 ] ;
    }
    else
    {
        //指示刻度 = (输入电压– 0.5) / 0.0025
        if (Voltage >= 500)
        {
            Voltage -= 500 ;
        }

        u32Result = Voltage ;
        u32Result *= 10 ;
        u32Result /= 25 ;
    }
hu's avatar
hu committed
239
#endif
hu's avatar
hu committed
240 241

    *kPa = (uint16_t)u32Result ;
hu's avatar
hu committed
242 243 244 245 246 247 248 249 250 251 252
}
/*-------------------------------------------------------------------------
 * Function Name  : AirPressure_AdcData_Filter
 * Description    : 主负气压输入数据滤波 - 主函数周期调用
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
void AirPressure_AdcData_Filter(void)
{
hu's avatar
hu committed
253 254 255 256 257
    uint8_t u8PriValid = 0 ;
    uint8_t u8SecValid = 0 ;
    uint16_t PriVoltage = 0 ;
    uint16_t SecVoltage = 0 ;

hu's avatar
hu committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    if ((K_Line_Set.K_Line_LID42 == 0x00) || (K_Line_Set.K_Line_LID42 == 0x02))
    {
        u8PriValid = RTE_Read_PRIMARY_AIR_Valid();
        u8SecValid = RTE_Read_SECONDARY_AIR_Valid();
    }
    else if ((K_Line_Set.K_Line_LID42 == 0x01) || (K_Line_Set.K_Line_LID42 == 0x03))
    {
        u8PriValid = RTE_Read_SECONDARY_AIR_Valid();
        u8SecValid = RTE_Read_PRIMARY_AIR_Valid();
    }
    else /*配置无效时*/
    {
        u8PriValid = RTE_Read_PRIMARY_AIR_Valid();
        u8SecValid = RTE_Read_SECONDARY_AIR_Valid();
    }

hu's avatar
hu committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    if (u8PriValid)
    {
        if (AirPressureFilter[PRI_ID].SumCnt < 10)
        {
            AirPressureFilter[PRI_ID].SumCnt ++ ;
            PriVoltage = RTE_Read_PRIMARY_AIR_Voltage() ;
            AirPressureFilter[PRI_ID].Voltage += PriVoltage ;
            PriVoltage = (uint16_t)(AirPressureFilter[PRI_ID].Voltage / AirPressureFilter[PRI_ID].SumCnt) ;
        }
        else
        {
            AirPressureFilter[PRI_ID].SumCnt = 0 ;
            AirPressureFilter[PRI_ID].Voltage = 0 ;
            PriVoltage = RTE_Read_PRIMARY_AIR_Voltage() ;
            AirPressureFilter[PRI_ID].SumCnt ++ ;
            AirPressureFilter[PRI_ID].Voltage += PriVoltage ;
            PriVoltage = (uint16_t)(AirPressureFilter[PRI_ID].Voltage / AirPressureFilter[PRI_ID].SumCnt) ;
        }

        AirPressureFilter[PRI_ID].Result = PriVoltage ;
    }
    else
    {
        AirPressureFilter[PRI_ID].SumCnt = 0 ;
        AirPressureFilter[PRI_ID].Voltage = 0 ;
        PriVoltage = 0 ;
    }

    if (u8SecValid)
    {
        if (AirPressureFilter[SEC_ID].SumCnt < 10)
        {
            AirPressureFilter[SEC_ID].SumCnt ++ ;
            SecVoltage = RTE_Read_SECONDARY_AIR_Voltage() ;
            AirPressureFilter[SEC_ID].Voltage += SecVoltage ;
            SecVoltage = (uint16_t)(AirPressureFilter[SEC_ID].Voltage / AirPressureFilter[SEC_ID].SumCnt) ;
        }
        else
        {
            AirPressureFilter[SEC_ID].SumCnt = 0 ;
            AirPressureFilter[SEC_ID].Voltage = 0 ;
            SecVoltage = RTE_Read_SECONDARY_AIR_Voltage() ;
            AirPressureFilter[SEC_ID].SumCnt ++ ;
            AirPressureFilter[SEC_ID].Voltage += SecVoltage ;
            SecVoltage = (uint16_t)(AirPressureFilter[SEC_ID].Voltage / AirPressureFilter[SEC_ID].SumCnt) ;
        }
        AirPressureFilter[SEC_ID].Result = SecVoltage ;
    }
    else
    {
        AirPressureFilter[SEC_ID].SumCnt = 0 ;
        AirPressureFilter[SEC_ID].Voltage = 0 ;
        SecVoltage = 0 ;
    }
hu's avatar
hu committed
328 329 330 331 332 333 334 335 336
}
/*-------------------------------------------------------------------------
 * Function Name  : CalAirPressure_CurSeg
 * Description    : 计算主负气压显示段
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
hu's avatar
hu committed
337
static uint8_t CalAirPressure_CurSeg(uint8_t ID, uint8_t Valid, uint16_t Voltage)
hu's avatar
hu committed
338
{
hu's avatar
hu committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    uint8_t u8Result = 0 ;
    if (Valid)
    {

    }
    else
    {

    }

    if (ID == PRI_ID)
    {
        SetGaugesPara(PRIGasGauges, Voltage);
        u8Result = GetGaugesCurrentPos(PRIGasGauges) ;
    }
    else
    {
        SetGaugesPara(SECGasGauges, Voltage);
        u8Result = GetGaugesCurrentPos(SECGasGauges) ;
    }

    return u8Result ;
hu's avatar
hu committed
361 362 363 364 365 366 367 368 369 370 371
}
/*-------------------------------------------------------------------------
 * Function Name  : Get_AirPressure_AdC_Filter
 * Description    : 主负气压滤波后结果
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
static uint16_t Get_AirPressure_AdC_Filter(uint8_t ID)
{
hu's avatar
hu committed
372 373 374
    uint16_t u16Result ;
    u16Result = AirPressureFilter[ID].Result ;
    return u16Result ;
hu's avatar
hu committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
}
/*-------------------------------------------------------------------------
 * Function Name  : AirPressure_Get_kPa
 * Description    : 压力值 kPa
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
uint16_t AirPressure_Get_kPa(uint8_t ID)
{
    uint16_t u16Result = 0u;
    u16Result = AirPressureVariable.ResultkPa[ID];
    return u16Result;
}
/*-------------------------------------------------------------------------
 * Function Name  : AirPressure_Get_Valid
hu's avatar
hu committed
392
 * Description    :
hu's avatar
hu committed
393 394 395 396 397 398 399
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
uint8_t AirPressure_Get_Valid(uint8_t ID)
{
hu's avatar
hu committed
400 401 402
    uint8_t u8Result = 0u;
    u8Result = AirPressureVariable.Valid[ID];
    return u8Result ;
hu's avatar
hu committed
403 404 405 406 407 408 409 410 411 412 413
}
/*-------------------------------------------------------------------------
 * Function Name  : AirPressure_Get_CurSeg
 * Description    : 当前显示段
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
uint8_t AirPressure_Get_CurSeg(uint8_t ID)
{
hu's avatar
hu committed
414
    return AirPressureVariable.CurSeg[ ID ] ;
hu's avatar
hu committed
415 416 417 418
}