Data_VehicleSpeed.c 12.9 KB
Newer Older
hu's avatar
hu committed
1
#include "Data_VehicleSpeed.h"
hu's avatar
hu committed
2
#include "TimerB.h"
hu's avatar
hu committed
3 4
#include "Gauges.h"
#include "GaugesInterface.h"
hu's avatar
hu committed
5
#include "Common_Interface.h"
6
#include "GUI.h"
hu's avatar
hu committed
7
#include "kwp2000_service.h"
8
#include "GPIO.h"
hu's avatar
hu committed
9

hu's avatar
hu committed
10
static uint32_t SPEED_RADIO = 637u;
hu's avatar
hu committed
11 12 13

uint16_t Vehicle_ACT_Speed;
uint16_t Vehicle_DISP_Speed;
hu's avatar
hu committed
14 15 16 17 18
uint8_t  VehicleSpeedValid;
uint8_t  Vehicle_CurSeg;
uint8_t  DataOverSpeed;
uint8_t  DataOverSpeedK_Line;
uint32_t SPEED_PPK;
19 20 21 22 23

_ACCSUBSPEED   AccSubSpeed;
uint16_t       Speed_Old  ;
uint16_t       GslYblSpeedTime  ;

hu's avatar
hu committed
24
volatile uint32_t VehFreBuf[VehFreNum];
hu's avatar
hu committed
25

26 27 28
DriveInfoSpeedPlusConverterStruct  DriveInfoSpeed1PlusConverter;
DriveInfoSpeedPlusConverterStruct  DriveInfoSpeed2PlusConverter;

hu's avatar
hu committed
29 30
void Speed_KL30_Init(void)
{
hu's avatar
hu committed
31
    uint8_t i = 0u;
hu's avatar
hu committed
32 33 34
    Vehicle_ACT_Speed = 0u;
    Vehicle_DISP_Speed = 0u;
    VehicleSpeedValid = 0u;
hu's avatar
hu committed
35 36 37
    DataOverSpeedK_Line = 0u;

    for (i = 0; i < VehFreNum; i++)
hu's avatar
hu committed
38
    {
hu's avatar
hu committed
39
        VehFreBuf[i] = 0u;
hu's avatar
hu committed
40 41
    }
}
hu's avatar
hu committed
42

hu's avatar
hu committed
43 44
void Speed_KL15_ON_Init(void)
{
hu's avatar
hu committed
45
    uint8_t i = 0u;
hu's avatar
hu committed
46 47 48 49
    Vehicle_ACT_Speed = 0u;
    Vehicle_DISP_Speed = 0u;
    VehicleSpeedValid = 0u;
    Vehicle_CurSeg = 0u;
hu's avatar
hu committed
50 51 52 53 54 55
    DataOverSpeedK_Line = 0u;

    SPEED_PPK = 15925u;

    Veh_Fre_Init(SPEED_PPK);
    for (i = 0; i < VehFreNum; i++)
hu's avatar
hu committed
56
    {
hu's avatar
hu committed
57
        VehFreBuf[i] = 0u;
hu's avatar
hu committed
58
    }
59 60 61

    Speed_Old = 0u;
    GslYblSpeedTime = 0u;
62 63 64 65 66 67 68 69 70 71 72 73 74


    DriveInfoSpeed1PlusConverter.Enable    = 0;
    DriveInfoSpeed1PlusConverter.Level     = 1;
    DriveInfoSpeed1PlusConverter.Timer     = 0xFFFFFFFE;
    DriveInfoSpeed1PlusConverter.Period    = 0xFFFFFFFF;
    DriveInfoSpeed1PlusConverter.ActPeriod = 0xFFFFFFFF;

    DriveInfoSpeed2PlusConverter.Enable    = 0;
    DriveInfoSpeed2PlusConverter.Level     = 1;
    DriveInfoSpeed2PlusConverter.Timer     = 0xFFFFFFFE;
    DriveInfoSpeed2PlusConverter.Period    = 0xFFFFFFFF;
    DriveInfoSpeed2PlusConverter.ActPeriod = 0xFFFFFFFF;
hu's avatar
hu committed
75
}
hu's avatar
hu committed
76

hu's avatar
hu committed
77 78 79 80 81 82 83
void Speed_KL15_OFF_Init(void)
{
    Vehicle_ACT_Speed = 0u;
    Vehicle_DISP_Speed = 0u;
    VehicleSpeedValid = 0u;
    Vehicle_CurSeg = 0u;
}
hu's avatar
hu committed
84

hu's avatar
hu committed
85 86 87 88 89 90 91
void Speed_Wakeup_Init(void)
{
    Vehicle_ACT_Speed = 0u;
    Vehicle_DISP_Speed = 0u;
    VehicleSpeedValid = 0u;
    Vehicle_CurSeg = 0u;
}
hu's avatar
hu committed
92

hu's avatar
hu committed
93 94 95 96 97 98 99 100 101 102
void Speed_Sleep_Init(void)
{
    Vehicle_ACT_Speed = 0u;
    Vehicle_DISP_Speed = 0u;
    VehicleSpeedValid = 0u;
    Vehicle_CurSeg = 0u;
}

void Speed_Processing_Service(void)
{
hu's avatar
hu committed
103
    static uint8_t i = 0u;
104
    uint32_t VehValTmp = 0u;
hu's avatar
hu committed
105
    uint32_t VehValDisp = 0u;
hu's avatar
hu committed
106
    uint16_t VehValCup = 0u;
107
    uint32_t PPK = 0u;
hu's avatar
hu committed
108
    uint16_t  DataOverSpeedValue ;
hu's avatar
hu committed
109

hu's avatar
hu committed
110
    COMMON_PowerStatus_t IG_Sta;
hu's avatar
hu committed
111

hu's avatar
hu committed
112
    IG_Sta = Common_Get_IG_Sts_Valid();
hu's avatar
hu committed
113

hu's avatar
hu committed
114
    if ( IG_Sta == COMMON_POWER_ON)
hu's avatar
hu committed
115
    {
hu's avatar
hu committed
116 117 118
        /**/
        DataOverSpeedValue = K_Line_Set.K_Line_LID44;
        DataOverSpeedValue *= 10u;
hu's avatar
hu committed
119

hu's avatar
hu committed
120 121 122 123 124 125
        PPK = Get_Speed_PPK();

        VehValTmp = Cal_Veh_Frequency();
        VehValTmp *= 36000u;
        VehValTmp /= PPK;
        VehFreBuf[i] = VehValTmp;
hu's avatar
hu committed
126

hu's avatar
hu committed
127 128
        i++;
        if (i > VehFreNum)
hu's avatar
hu committed
129
        {
hu's avatar
hu committed
130 131
            Fre_SortShort(VehFreBuf, VehFreNum);
            i = 0u;
hu's avatar
hu committed
132
        }
hu's avatar
hu committed
133
        VehValCup = (uint16_t) VehFreBuf[1u];
hu's avatar
hu committed
134 135

        if (VehValCup > 2250)
hu's avatar
hu committed
136
        {
hu's avatar
hu committed
137 138
            VehicleSpeedValid = 0u;

hu's avatar
hu committed
139 140 141 142
            Vehicle_ACT_Speed = 0u;
        }
        else
        {
hu's avatar
hu committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156
            VehicleSpeedValid = 1u;

            if (VehValCup >= 1400u)
            {
                Vehicle_ACT_Speed = 1400u;
            }
            else  if (VehValCup <= 40u)
            {
                Vehicle_ACT_Speed = 0u;
            }
            else
            {
                Vehicle_ACT_Speed = VehValCup;
            }
hu's avatar
hu committed
157
        }
hu's avatar
hu committed
158

hu's avatar
hu committed
159 160 161
        //传入表头  计算放大系数
        VehValDisp = VehSpeedAmplification((uint16_t)Vehicle_ACT_Speed);
        SetGaugesPara(VehGauges, (uint16_t)VehValDisp);
hu's avatar
hu committed
162

hu's avatar
hu committed
163 164 165
        //获得显示格和显示车速
        Vehicle_CurSeg = GetGaugesCurrentPos(VehGauges);
        Vehicle_DISP_Speed = VehValDisp;//GetVehDisVal(Vehicle_CurSeg, &GaugesInfo_Init_Table[VehGauges]);
hu's avatar
hu committed
166

hu's avatar
hu committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180

        if (K_Line_Set.K_Line_LID44 != 0xFF)
        {
            if (DataOverSpeedK_Line)
            {
                if (Vehicle_DISP_Speed <= DataOverSpeedValue)
                    DataOverSpeedK_Line = 0;
            }
            else
            {
                if (Vehicle_DISP_Speed >= (DataOverSpeedValue + 20))
                    DataOverSpeedK_Line = 1;
            }
        }
hu's avatar
hu committed
181 182 183
    }
    else
    {
hu's avatar
hu committed
184
        i = 0u;
hu's avatar
hu committed
185 186 187 188 189 190
    }
}

/*-------------------------------------------------------------------------
 * Function Name  : Set_Speed_PPK
 * Description    : 设置PPK值,初始化 或需要改变时调用
hu's avatar
hu committed
191
 * Input          :
hu's avatar
hu committed
192 193 194 195 196 197
 * Output         : None
 * Return         : None
 * onther         : PPK = 频率 * 3600 / 车速
 --------------------------------------------------------------------------*/
void Set_Speed_PPK(uint32_t PPK)
{
hu's avatar
hu committed
198
    SPEED_PPK = PPK;
hu's avatar
hu committed
199 200 201 202 203 204 205 206 207 208 209
}
/*-------------------------------------------------------------------------
 * Function Name  : Set_Speed_PPK
 * Description    : 设置速比值,初始化 或需要改变时调用
 * Input          : 速比
 * Output         : None
 * Return         : None
 * onther         : 速比 = PPK / 脉冲
 --------------------------------------------------------------------------*/
void Set_Speed_Ratio(uint32_t Ratio)
{
hu's avatar
hu committed
210
    SPEED_RADIO = Ratio;
hu's avatar
hu committed
211 212 213
}
/*-------------------------------------------------------------------------
 * Function Name  : Get_Speed_PPK
hu's avatar
hu committed
214
 * Description    :
hu's avatar
hu committed
215 216 217
 * Input          : None
 * Output         : None
 * Return         : 10倍PPK值
hu's avatar
hu committed
218
 * onther         :
hu's avatar
hu committed
219 220 221
 --------------------------------------------------------------------------*/
uint32_t Get_Speed_PPK(void)
{
hu's avatar
hu committed
222
    uint32_t u32Result = 0u;
hu's avatar
hu committed
223 224
    u32Result = SPEED_PPK;
    return u32Result;
hu's avatar
hu committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}

uint16_t Speed_Get_ActualValue(void)
{
    uint16_t u16Result = 0u;
    u16Result = Vehicle_ACT_Speed;
    return u16Result;
}
uint16_t Speed_Get_Display_Value(void)
{
    uint16_t u16Result = 0u;
    u16Result = Vehicle_DISP_Speed;
    return u16Result;
}
/*-------------------------------------------------------------------------
 * Function Name  : Speed_Get_CurSeg
 * Description    : 车速当前格
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : 应用于断码
 --------------------------------------------------------------------------*/
uint8_t Speed_Get_CurSeg(void)
{
hu's avatar
hu committed
249
    return Vehicle_CurSeg;
hu's avatar
hu committed
250 251 252 253 254 255 256 257 258 259 260
}
/*-------------------------------------------------------------------------
 * Function Name  : Speed_Get_Valid
 * Description    : 车速有效?
 * Input          : None
 * Output         : None
 * Return         : None
 * onther         : None
 --------------------------------------------------------------------------*/
uint8_t Speed_Get_Valid(void)
{
hu's avatar
hu committed
261
    return VehicleSpeedValid;
hu's avatar
hu committed
262
}
hu's avatar
hu committed
263 264 265 266 267 268 269 270 271 272 273

/******************************************************************************
  函数名:Data_Acc_Speed_Processing
  功  能: 加速度计算
  参  数: 无
  返回值:无
 ******************************************************************************/
void Data_Acc_Speed_Processing(void)
{
    uint16_t Speed_New = 0;
    uint16_t AccSpeed  = 0;
274
    Speed_New = Common_Get_Act_V_Speed_Valid();
hu's avatar
hu committed
275

276 277
    AccSubSpeed.ACCSpeed_Time++;
    if (AccSubSpeed.ACCSpeed_Time >= 10)
hu's avatar
hu committed
278
    {
279
        AccSubSpeed.ACCSpeed_Time = 0;
hu's avatar
hu committed
280 281 282 283 284 285 286
        if (Speed_Old <= Speed_New)
        {
            AccSpeed      = Speed_New - Speed_Old;
            Speed_Old     = Speed_New ;

            if (AccSpeed > User_App.User_JADD)
            {
287
                AccSubSpeed.AccSpeed_Flag = 1 ;
hu's avatar
hu committed
288 289 290
            }
            else
            {
291
                AccSubSpeed.AccSpeed_Flag = 0 ;
hu's avatar
hu committed
292
            }
293
            AccSubSpeed.SubSpeed_Flag = 0 ;
hu's avatar
hu committed
294 295 296 297 298 299 300
        }
        else
        {
            AccSpeed      = Speed_Old - Speed_New;
            Speed_Old     = Speed_New ;
            if (AccSpeed > User_App.User_JSUB)
            {
301
                AccSubSpeed.SubSpeed_Flag = 1 ;
hu's avatar
hu committed
302 303 304
            }
            else
            {
305
                AccSubSpeed.SubSpeed_Flag = 0 ;
hu's avatar
hu committed
306
            }
307
            AccSubSpeed.AccSpeed_Flag = 0 ;
hu's avatar
hu committed
308 309 310 311 312 313 314 315 316 317 318 319 320
        }
    }
}
/******************************************************************************
  函数名:Data_GsLYbL_Speed_Processing
  功  能: 高速路, 一般路判断
  参  数: 无
  返回值:无
 ******************************************************************************/
void Data_GsLYbL_Speed_Processing(void)
{
    uint16_t User_Gsl = 0;
    uint16_t User_Ybl = 0;
321 322 323
    uint16_t Veh_Speed = 0u;

    Veh_Speed = Common_Get_Act_V_Speed_Valid();
hu's avatar
hu committed
324 325 326 327

    User_Gsl = (uint16_t)User_App.User_Gsl * 10 ;
    User_Ybl = (uint16_t)User_App.User_Ybl * 10 ;

328
    if ( Veh_Speed >= 600 )
hu's avatar
hu committed
329 330 331 332
    {
        if (GslYblSpeedTime <= 6000 )
            GslYblSpeedTime++;
    }
333
    else if ( Veh_Speed <= 590 )
hu's avatar
hu committed
334 335 336 337 338 339
    {
        GslYblSpeedTime = 0;
    }

    if (GslYblSpeedTime >= 6000)
    {
340
        AccSubSpeed.GslYbl_Flag = 1;
hu's avatar
hu committed
341 342 343
    }
    else
    {
344
        AccSubSpeed.GslYbl_Flag = 0;
hu's avatar
hu committed
345 346
    }

347
    if (AccSubSpeed.GslYbl_Flag == 1)
hu's avatar
hu committed
348
    {
349
        if (Veh_Speed > User_Gsl)
hu's avatar
hu committed
350
        {
351
            AccSubSpeed.GslSpeed_Flag = 1;
hu's avatar
hu committed
352
        }
353
        else if (Veh_Speed <= (User_Gsl - 10))
hu's avatar
hu committed
354
        {
355
            AccSubSpeed.GslSpeed_Flag = 0;
hu's avatar
hu committed
356 357 358 359
        }
    }
    else
    {
360
        if (Veh_Speed > User_Ybl)
hu's avatar
hu committed
361
        {
362
            AccSubSpeed.GslSpeed_Flag = 1;
hu's avatar
hu committed
363
        }
364
        else if (Veh_Speed <= (User_Ybl - 10))
hu's avatar
hu committed
365
        {
366
            AccSubSpeed.GslSpeed_Flag = 0;
hu's avatar
hu committed
367
        }
hu's avatar
hu committed
368

hu's avatar
hu committed
369
    }
hu's avatar
hu committed
370 371 372
}
//15925
//5096
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

void Drive_Info_Speed1_Plus_Conversion_ISR ( void )
{
    if ( DriveInfoSpeed1PlusConverter.Enable )
    {
        DriveInfoSpeed1PlusConverter.Timer++;

        if ( DriveInfoSpeed1PlusConverter.ActPeriod >= 10000u )
            DriveInfoSpeed1PlusConverter.ActPeriod = DriveInfoSpeed1PlusConverter.Period;

        if ( DriveInfoSpeed1PlusConverter.Timer >= DriveInfoSpeed1PlusConverter.ActPeriod )
        {
            DriveInfoSpeed1PlusConverter.Timer = 0;

            if ( DriveInfoSpeed1PlusConverter.Level )
            {
                DriveInfoSpeed1PlusConverter.Level = 0;
                DriveInfoSpeed1PlusConverter.ActPeriod = DriveInfoSpeed1PlusConverter.Period;
            }
            else
                DriveInfoSpeed1PlusConverter.Level = 1;
        }

        SPEED1_PWM_MCU_OUT = DriveInfoSpeed1PlusConverter.Level;
    }
}
void Drive_Info_Speed1_Plus_Conversion ( void )
{
    uint32_t Speed;

    if ( ( Speed_Get_Valid() == 1u ) && ( Common_Get_Disp_V_Speed() >= 0u ) )
    {
        Speed = ( uint32_t ) Common_Get_Disp_V_Speed();
        DriveInfoSpeed1PlusConverter.Period = ( uint32_t ) ( 360000000 / 15925) / Speed;
        DriveInfoSpeed1PlusConverter.Enable = 1;
    }
    else
    {
        if ( DriveInfoSpeed1PlusConverter.Enable )
        {
            DriveInfoSpeed1PlusConverter.Enable    = 0;
            DriveInfoSpeed1PlusConverter.Level     = 0;
            DriveInfoSpeed1PlusConverter.Timer     = 0;
            DriveInfoSpeed1PlusConverter.Period    = 0xFFFFFFFF;
            DriveInfoSpeed1PlusConverter.ActPeriod = 0xFFFFFFFF;
        }

        SPEED1_PWM_MCU_OUT = 1;
    }
}
void Drive_Info_Speed2_Plus_Conversion_ISR ( void )
hu's avatar
hu committed
424
{
425 426 427 428 429 430
    if ( DriveInfoSpeed2PlusConverter.Enable )
    {
        DriveInfoSpeed2PlusConverter.Timer++;

        if ( DriveInfoSpeed2PlusConverter.ActPeriod >= 10000u )
            DriveInfoSpeed2PlusConverter.ActPeriod = DriveInfoSpeed2PlusConverter.Period;
hu's avatar
hu committed
431

432 433 434
        if ( DriveInfoSpeed2PlusConverter.Timer >= DriveInfoSpeed2PlusConverter.ActPeriod )
        {
            DriveInfoSpeed2PlusConverter.Timer = 0;
hu's avatar
hu committed
435

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
            if ( DriveInfoSpeed2PlusConverter.Level )
            {
                DriveInfoSpeed2PlusConverter.Level = 0;
                DriveInfoSpeed2PlusConverter.ActPeriod = DriveInfoSpeed2PlusConverter.Period;
            }
            else
                DriveInfoSpeed2PlusConverter.Level = 1;
        }

        SPEED2_PWM_MCU_OUT = DriveInfoSpeed2PlusConverter.Level;
        SPEED3_PWM_MCU_OUT = DriveInfoSpeed2PlusConverter.Level;
    }
}
void Drive_Info_Speed2_Plus_Conversion ( void )
{
    uint32_t Speed;

    if ( ( Speed_Get_Valid() == 1u ) && ( Common_Get_Disp_V_Speed() >= 0u ) )
    {
        Speed = ( uint32_t ) Common_Get_Disp_V_Speed();
        DriveInfoSpeed2PlusConverter.Period = ( uint32_t ) ( 360000000 / 5096) / Speed;
        DriveInfoSpeed2PlusConverter.Enable = 1;
    }
    else
    {
        if ( DriveInfoSpeed2PlusConverter.Enable )
        {
            DriveInfoSpeed2PlusConverter.Enable    = 0;
            DriveInfoSpeed2PlusConverter.Level     = 0;
            DriveInfoSpeed2PlusConverter.Timer     = 0;
            DriveInfoSpeed2PlusConverter.Period    = 0xFFFFFFFF;
            DriveInfoSpeed2PlusConverter.ActPeriod = 0xFFFFFFFF;
        }

        SPEED2_PWM_MCU_OUT = 1;
        SPEED3_PWM_MCU_OUT = 1;
    }
}
//void Drive_Info_Speed2_Plus_Conversion(void)
//{
//    uint32_t Veh_Frequenc = 0u;
//
//    Veh_Frequenc = Cal_Veh_Frequency();
//    Veh_Frequenc *= 10u;
//    TimerB_PWM_Channel_Fre_Set(TIMERB_2_CH10, Veh_Frequenc, 500u);/*车速1 25*/
//
//    Veh_Frequenc = Veh_Frequenc * 5096 / 15925;
//    TimerB_PWM_Channel_Fre_Set(TIMERB_2_CH13, Veh_Frequenc, 500u);/*车速2 8*/
//
//    TimerB_PWM_Channel_Fre_Set(TIMERB_2_CH15, Veh_Frequenc, 500u);/*车速3 8*/
//}