Radar_R3.c 7.01 KB
Newer Older
崔立宝's avatar
崔立宝 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 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 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

#include "Radar_R3.h"


RadarR3DataBufferUnion        RadarR3Data;
RadarR3ReceivingCtrlStruct    RadarR3ReceivingCtrl;

void Radar_R3_Init(void)
{
  RadarR3ReceivingCtrl.Enable   = 0;
  RadarR3ReceivingCtrl.SelfTest = 0;
  RadarR3ReceivingCtrl.Ptr      = 0;
  RadarR3ReceivingCtrl.Len      = 0;
  RadarR3ReceivingCtrl.Complete = 0;
  RadarR3ReceivingCtrl.Counter  = 0;
  
  RADAR_VALID            = 0;
  RADAR_SELF_TEST_PASSED = 0;
  RADAR_SYS_FAULT        = 0;
  RADAR_SOUND            = RADAR_SND_MUTE;
  
  RADAR_SENSOR_FL  = 0;
  RADAR_SENSOR_FLM = 0;
  RADAR_SENSOR_FRM = 0;
  RADAR_SENSOR_FR  = 0;

  RADAR_SENSOR_RL  = 0;
  RADAR_SENSOR_RLM = 0;
  RADAR_SENSOR_RRM = 0;
  RADAR_SENSOR_RR  = 0;
}

//10ms
void Radar_R3_Receiving_Service(void)
{
#if 0
  uint16_t Counter;
  uint8_t  SelfTestResult;
  
  if (RadarR3ReceivingCtrl.Enable)
  {
    if (LINE_IN_GEAR_R_SIGNAL)
    {
      RADAR_VALID = (RADAR_REAR_VALID_FLAG | RADAR_FRONT_VALID_FLAG) ;
    
      if (RadarR3ReceivingCtrl.Timer < 0xFFFF)
        RadarR3ReceivingCtrl.Timer++;
      
      if (RadarR3ReceivingCtrl.Timer >= (RADAR_R3_MSG_LOST_THRESHOLD * 100))
        RADAR_SYS_FAULT |=  RADAR_MSG_LOST_BIT;
      else
        RADAR_SYS_FAULT &= ~RADAR_MSG_LOST_BIT;
      
      if (RadarR3ReceivingCtrl.Complete)
      {
        RadarR3ReceivingCtrl.Complete = 0;
        RadarR3ReceivingCtrl.Timer    = 0;
        
        if (RadarR3Data.Frame.Type == RADAR_R3_MESSAGE_FRAME)
        {
          if (RadarR3ReceivingCtrl.SelfTest == 1)     //雷达结束首次自检
          {
            RadarR3ReceivingCtrl.SelfTest = 2;
            SelfTestResult                = 0;
            
            SelfTestResult |= RADAR_SENSOR_FL;  
            SelfTestResult |= RADAR_SENSOR_FLM; 
            SelfTestResult |= RADAR_SENSOR_FRM; 
            SelfTestResult |= RADAR_SENSOR_FR;  
            
            SelfTestResult |= RADAR_SENSOR_RL;  
            SelfTestResult |= RADAR_SENSOR_RLM; 
            SelfTestResult |= RADAR_SENSOR_RRM; 
            SelfTestResult |= RADAR_SENSOR_RR;
            
            if (SelfTestResult == 0)
              RADAR_SELF_TEST_PASSED = 1;
          }
          RADAR_SOUND      = RadarR3Data.Msg.Snd;
          
          RADAR_SENSOR_FL  = RadarR3Data.Msg.FL;
          RADAR_SENSOR_FLM = RadarR3Data.Msg.FLM;
          RADAR_SENSOR_FRM = RadarR3Data.Msg.FRM;
          RADAR_SENSOR_FR  = RadarR3Data.Msg.FR;

          RADAR_SENSOR_RL  = RadarR3Data.Msg.RL;
          RADAR_SENSOR_RLM = RadarR3Data.Msg.RLM;
          RADAR_SENSOR_RRM = RadarR3Data.Msg.RRM;
          RADAR_SENSOR_RR  = RadarR3Data.Msg.RR;

        }
        else if (RadarR3Data.Frame.Type == RADAR_R3_SELF_TEST_FRAME)
        {
          if (RadarR3ReceivingCtrl.SelfTest == 0)     //雷达刚启动,未进行自检
            RadarR3ReceivingCtrl.SelfTest = 1;
          
          if (RadarR3Data.SelfTest.FLFault)
            RADAR_SENSOR_FL = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_FL = 0;
          
          if (RadarR3Data.SelfTest.FLMFault)
            RADAR_SENSOR_FLM = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_FLM = 0;
          
          if (RadarR3Data.SelfTest.FRMFault)
            RADAR_SENSOR_FRM = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_FRM = 0;
          
          if (RadarR3Data.SelfTest.FRFault)
            RADAR_SENSOR_FR = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_FR = 0;
          
          if (RadarR3Data.SelfTest.RLFault)
            RADAR_SENSOR_RL = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_RL  = 0;
          
          if (RadarR3Data.SelfTest.RLMFault)
            RADAR_SENSOR_RLM = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_RLM = 0;
          
          if (RadarR3Data.SelfTest.RRMFault)
            RADAR_SENSOR_RRM = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_RRM = 0;
          
          if (RadarR3Data.SelfTest.RRFault)
            RADAR_SENSOR_RR = RADAR_SENSOR_FAULT;
          else
            RADAR_SENSOR_RR  = 0;
        }
      }
      else
      {
        Counter = API_ROLLING_COUNTER;
        if (Counter - RadarR3ReceivingCtrl.Counter >= (10000 / API_INT_CYCLE))
          RadarR3ReceivingCtrl.Ptr = 0;
      }
    }
    else
    {
      if (RADAR_VALID)
      {
        RadarR3ReceivingCtrl.SelfTest = 0;
        RadarR3ReceivingCtrl.Ptr      = 0;
        RadarR3ReceivingCtrl.Complete = 0;
        RadarR3ReceivingCtrl.Timer    = 0;
        
        RADAR_VALID            = 0;
        RADAR_SELF_TEST_PASSED = 0;
        RADAR_SYS_FAULT        = 0;
        RADAR_SOUND            = RADAR_SND_MUTE;
        
        RADAR_SENSOR_FL  = 0;
        RADAR_SENSOR_FLM = 0;
        RADAR_SENSOR_FRM = 0;
        RADAR_SENSOR_FR  = 0;

        RADAR_SENSOR_RL  = 0;
        RADAR_SENSOR_RLM = 0;
        RADAR_SENSOR_RRM = 0;
        RADAR_SENSOR_RR  = 0;
      }
    }
  }
#endif  
}

void Radar_R3_Receive_Enable(uint8_t En)
{
  if (En)
    En = 1;
  
  if (RadarR3ReceivingCtrl.Enable != En)
  {
    RadarR3ReceivingCtrl.SelfTest = 0;
    RadarR3ReceivingCtrl.Enable   = En;
    RadarR3ReceivingCtrl.Timer    = 0;
    
    RADAR_SELF_TEST_PASSED = 0;
    RADAR_SYS_FAULT        = 0;
    RADAR_SOUND            = RADAR_SND_MUTE;
    
    RADAR_SENSOR_FL  = 0;
    RADAR_SENSOR_FLM = 0;
    RADAR_SENSOR_FRM = 0;
    RADAR_SENSOR_FR  = 0;

    RADAR_SENSOR_RL  = 0;
    RADAR_SENSOR_RLM = 0;
    RADAR_SENSOR_RRM = 0;
    RADAR_SENSOR_RR  = 0;
    
    if (RadarR3ReceivingCtrl.Enable == 0)
      RADAR_VALID    = 0;
  }
}

void Radar_R3_Receive_Data(uint8_t Data, uint8_t Parity)
{
  uint16_t Counter;
  
  if ((RadarR3ReceivingCtrl.Enable) && (RADAR_VALID))
  {
    if (RadarR3ReceivingCtrl.Complete)
      return;
    
    Counter = API_ROLLING_COUNTER;
    RadarR3Data.Byte[RadarR3ReceivingCtrl.Ptr] = Data;
    
    if (RadarR3ReceivingCtrl.Ptr == 0)
    {
      if (RadarR3Data.Frame.Type == RADAR_R3_MESSAGE_FRAME)
      {
        RadarR3ReceivingCtrl.Ptr     = 1;
        RadarR3ReceivingCtrl.Len     = 5;
        RadarR3ReceivingCtrl.Counter = Counter;
      }
      else if (RadarR3Data.Frame.Type == RADAR_R3_SELF_TEST_FRAME)
      {
        RadarR3ReceivingCtrl.Ptr     = 1;
        RadarR3ReceivingCtrl.Len     = 3;
        RadarR3ReceivingCtrl.Counter = Counter;
      }
    }
    else
    {
      //如果 1、超时10ms(10,000 us) 2、校验错误 则重新接收
      if ((Counter - RadarR3ReceivingCtrl.Counter >= (10000 / API_INT_CYCLE)) || \
          (Parity != 0))
      {
        RadarR3ReceivingCtrl.Ptr = 0;
        return;       
      }
      
      RadarR3ReceivingCtrl.Counter = Counter;
      RadarR3ReceivingCtrl.Ptr++;
      if (RadarR3ReceivingCtrl.Ptr >= RadarR3ReceivingCtrl.Len)
      {
        if (((RadarR3Data.Frame.Type == RADAR_R3_MESSAGE_FRAME) && \
             (RadarR3Data.Msg.EOF == RADAR_R3_EOF_MESSAGE)) || \
            ((RadarR3Data.Frame.Type == RADAR_R3_SELF_TEST_FRAME) && \
             (RadarR3Data.SelfTest.EOF == RADAR_R3_EOF_SELF_TEST)))
        {             
          RadarR3ReceivingCtrl.Complete = 1;
        }

        RadarR3ReceivingCtrl.Ptr = 0;
      }
    }
  }
}