uart_Lidian2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. ******************************************************************************
  3. * @file uartSpecial.c
  4. * @author
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief uartSpecial function.
  8. ******************************************************************************
  9. *
  10. * COPYRIGHT(c) 2015 STMicroelectronics
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "uart_Lidian2.h"
  38. #include "string.h"
  39. #include "usart.h"
  40. #include "canAppl.h"
  41. #include "syspar.h"
  42. #include "bikebrake.h"
  43. #include "bikethrottle.h"
  44. /******************************
  45. *
  46. * Parameter
  47. *
  48. ******************************/
  49. Lidian2_OBC2MCInfo_Struct_t Lidian2_stOBC2MCInfo;
  50. Lidian2_MC2OBCInfo_Struct_t Lidian2_stMC2OBCInfo;
  51. UBYTE ucWalk_cnt;
  52. /******************************
  53. *
  54. * Functions
  55. *
  56. ******************************/
  57. /***************************************************************
  58. Function: SP_OBCSetPara;
  59. Description: OBC set MC parameter(轮径 电流阈值/功率阈值 低压阈值 限速值)
  60. Call by:
  61. Input Variables: N/A
  62. Output/Return Variables: N/A
  63. Subroutine Call: N/A;
  64. Reference: N/A
  65. ****************************************************************/
  66. void Lidian2_OBCSetPara(void)
  67. {
  68. /*参数设定*/
  69. /*轮径*/
  70. #if 0
  71. UWORD tmpdata;
  72. tmpdata = Lidian2_stOBC2MCInfo.uwWheelDiameter*8/10;
  73. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != tmpdata)
  74. {
  75. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = tmpdata;
  76. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  77. cp_stFlg.ParaSaveEEFlg = TRUE;
  78. cp_stFlg.ParaUpdateFlg = TRUE;
  79. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  80. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  81. }
  82. /*功率限幅阈值更新*/
  83. // #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
  84. // UWORD uwVolt = 480;
  85. // #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
  86. // UWORD uwVolt = 360;
  87. // #else
  88. // UWORD uwVolt = 360;
  89. // #endif
  90. UWORD uwVolt= cp_stMotorPara.swRUdcV;
  91. if(MC_UpcInfo.stTestParaInfo.uwPwrLimit != ((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500))
  92. {
  93. MC_UpcInfo.stTestParaInfo.uwPwrLimit = (UWORD)((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500);
  94. MC_UpcInfo.stTestParaInfo.uwSaveFlg = 1;
  95. cp_stFlg.ParaSaveEEFlg = TRUE;
  96. cp_stFlg.ParaUpdateFlg = TRUE;
  97. cp_stFlg.TestParaInfoUpdateFlg = TRUE;
  98. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  99. }
  100. /*低压保护阈值更新*/
  101. if(MC_UpcInfo.stMContorlInfo.uwAlamUVolV != Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation)
  102. {
  103. MC_UpcInfo.stMContorlInfo.uwAlamUVolV = Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation;
  104. MC_UpcInfo.stMContorlInfo.uwSaveFlg = 1;
  105. cp_stFlg.ParaSaveEEFlg = TRUE;
  106. cp_stFlg.ParaUpdateFlg = TRUE;
  107. cp_stFlg.ParaMCInfoUpdateFlg = TRUE;
  108. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  109. }
  110. #endif
  111. /*限速阈值更新*/
  112. if(Lidian2_stOBC2MCInfo.ucSpeedLimiation != (MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart + MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit))//有变化时更新
  113. {
  114. SWORD deltaSpeed = 0;
  115. deltaSpeed = (Lidian2_stOBC2MCInfo.ucSpeedLimiation - MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart);
  116. if(deltaSpeed > 0)
  117. {
  118. deltaSpeed = 0;
  119. }
  120. if(deltaSpeed != MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit)
  121. {
  122. MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit = deltaSpeed;
  123. ass_ParaCong.swDeltaBikeSpeedLimit = deltaSpeed;
  124. MC_UpcInfo.stBikeInfo2.uwSaveFlg = TRUE;
  125. cp_stFlg.ParaSaveEEFlg = TRUE;
  126. cp_stFlg.ParaUpdateFlg = TRUE;
  127. cp_stFlg.ParaBikeInfo2UpdateFlg = TRUE;
  128. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  129. }
  130. }
  131. Lidian2_XinSiWeiLimitThrottleSpd(Lidian2_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ThrottleGearEnable,Lidian2_stOBC2MCInfo.uwThrottlePWM);
  132. }
  133. /***************************************************************
  134. Function: SP_USART_DataProcess;
  135. Description: Get OBC GearSt and LightControl information
  136. Call by:
  137. Input Variables: N/A
  138. Output/Return Variables: N/A
  139. Subroutine Call: N/A;
  140. Reference: N/A
  141. ****************************************************************/
  142. void Lidian2_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data)
  143. {
  144. /*数据获取*/
  145. Lidian2_stOBC2MCInfo.ucPowerStyle = Data[3];
  146. Lidian2_stOBC2MCInfo.ucGearSt = Data[4];
  147. memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo1, &Data[5], 1);
  148. Lidian2_stOBC2MCInfo.ucSpeedMagnetSteel = Data[6];
  149. Lidian2_stOBC2MCInfo.uwWheelDiameter = (UWORD)((Data[7]<<8)+Data[8]);//0.1inch
  150. Lidian2_stOBC2MCInfo.ucAssistSensitivity = Data[9];
  151. Lidian2_stOBC2MCInfo.ucAssistPowerMode = Data[10];
  152. Lidian2_stOBC2MCInfo.ucInnerSpeedMagnetSteel = Data[11];
  153. Lidian2_stOBC2MCInfo.ucSpeedLimiation = Data[12];//km/h
  154. Lidian2_stOBC2MCInfo.ucCurrentLimiation = Data[13];
  155. Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation = (UWORD)((Data[14]<<8)+Data[15]);
  156. Lidian2_stOBC2MCInfo.uwThrottlePWM = (UWORD)((Data[16]<<8)+Data[17]);
  157. memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo2, &Data[18], 1);
  158. #if 0
  159. /*数据更新*/
  160. /*WALK挡位逻辑为:WALK优先级最高,仪表长按"-"进入WALK档或退出WALK档,每次长按发送10帧bit1为1*/
  161. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
  162. {
  163. ucWalk_cnt++;
  164. }
  165. else
  166. {
  167. ucWalk_cnt = 0;
  168. }
  169. if(ucWalk_cnt >= 8)
  170. {
  171. ucWalk_cnt = 0;
  172. if(MC_ControlCode.GearSt == MC_GearSt_WALK)
  173. {
  174. MC_ControlCode.GearSt = MC_GearSt_OFF;
  175. }
  176. else
  177. {
  178. MC_ControlCode.GearSt = MC_GearSt_WALK;
  179. }
  180. }
  181. #else
  182. //长按-键进入助推,松开退出
  183. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
  184. {
  185. MC_ControlCode.GearSt = MC_GearSt_WALK;
  186. }
  187. else
  188. {
  189. MC_ControlCode.GearSt = MC_GearSt_OFF;
  190. }
  191. #endif
  192. //0-OFF, 1<=GearSt<6:ECO, 6<=GearSt<9:NORM, 9<=GearSt<12:SPORT, 12<=GearSt<15:TURBO, 15==GearSt:SMART
  193. if(MC_ControlCode.GearSt != MC_GearSt_WALK)
  194. {
  195. if(Lidian2_stOBC2MCInfo.ucGearSt == 15)
  196. {
  197. MC_ControlCode.GearSt = MC_GearSt_SMART;
  198. }
  199. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 12)
  200. {
  201. MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
  202. }
  203. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 9)
  204. {
  205. MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
  206. }
  207. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 6)
  208. {
  209. MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
  210. }
  211. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 1)
  212. {
  213. MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
  214. }
  215. else
  216. {
  217. MC_ControlCode.GearSt = MC_GearSt_OFF;
  218. }
  219. }
  220. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Light == 1) //大灯
  221. {
  222. MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
  223. }
  224. else
  225. {
  226. MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
  227. }
  228. MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
  229. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
  230. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  231. Lidian2_OBCSetPara();//参数更新
  232. Lidian2_SendData_OBC(ptUartTx);//数据回传
  233. }
  234. /***************************************************************
  235. Function: SP_SendData_OBC;
  236. Description: Send Data to OBC
  237. Call by:
  238. Input Variables: N/A
  239. Output/Return Variables: N/A
  240. Subroutine Call: N/A;
  241. Reference: N/A
  242. ****************************************************************/
  243. void Lidian2_SendData_OBC(USART_Buf_TypeDef* ptUartTx)
  244. {
  245. UBYTE i = 0;
  246. UBYTE sendlen = 14;
  247. UBYTE databuf[14] = {0};
  248. if(MC_ControlCode.GearSt == MC_GearSt_WALK)
  249. {
  250. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 1;
  251. }
  252. else
  253. {
  254. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 0;
  255. }
  256. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.HallErr = MC_ErrorCode.ERROR_Bit.Fault_HallSensor;
  257. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ThrottleErr = MC_ErrorCode.ERROR_Bit.Fault_Throttle;
  258. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ControllerErr = MC_ErrorCode.ERROR_Bit.Fault_MCU;
  259. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.UnderVoltageErr = MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage;
  260. if(bikeCruise.CruiseMode == 1)
  261. {
  262. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 1;
  263. }
  264. else
  265. {
  266. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 0;
  267. }
  268. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.BrakeErr = 0;//暂未判断刹把故障
  269. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.RotorLock = MC_ErrorCode.ERROR_Bit.Fault_PhaseLine;
  270. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Horizontal = 1;//不实现
  271. if(cp_stFlg.RunModelSelect == CadAssist)
  272. {
  273. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.CadenceSen;
  274. }
  275. else if(cp_stFlg.RunModelSelect == TorqAssist)
  276. {
  277. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.TorqSen;
  278. }
  279. else
  280. {
  281. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = 0;
  282. }
  283. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Brake = BikeBrake_blGetstate();
  284. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.CommErr = 0;//暂不判断通信故障
  285. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Charging = 0;//MC无法判断充电
  286. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.SpeedLimited = 0;
  287. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.GearSt = Lidian2_stOBC2MCInfo.ucGearSt;
  288. Lidian2_stMC2OBCInfo.uwCurrent = (UWORD)abs(MC_RunInfo.BusCurrent/1000);
  289. Lidian2_stMC2OBCInfo.ucCurrentRatio = 0;//未知用途
  290. if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
  291. {
  292. Lidian2_stMC2OBCInfo.uwSpeed = 0;
  293. }
  294. else
  295. {
  296. Lidian2_stMC2OBCInfo.uwSpeed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
  297. }
  298. Lidian2_stMC2OBCInfo.SOC = MC_RunInfo.SOC;//由BMS实现
  299. Lidian2_stMC2OBCInfo.uwRemainDistance = 0;//由BMS实现
  300. databuf[0] = 0x02;
  301. databuf[1] = sendlen;
  302. databuf[2] = 0x01;
  303. memcpy(&databuf[3],&Lidian2_stMC2OBCInfo.stMC_StatusInfo1,4);
  304. databuf[4]=0;
  305. databuf[5] =(UBYTE)(Lidian2_stMC2OBCInfo.uwCurrent>>8);
  306. databuf[6] =(UBYTE)(Lidian2_stMC2OBCInfo.uwCurrent&0xff);
  307. databuf[7] = (UBYTE)(Lidian2_stMC2OBCInfo.ucCurrentRatio);
  308. databuf[8] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed>>8);
  309. databuf[9] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed&0xFF);
  310. databuf[10] = Lidian2_stMC2OBCInfo.SOC;
  311. databuf[11] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance>>8);
  312. databuf[12] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance&0xFF);
  313. databuf[sendlen-1] = 0;
  314. for(i=0; i<(sendlen-1); i++)
  315. {
  316. databuf[sendlen-1] ^= databuf[i];
  317. }
  318. for(i=0; i<sendlen; i++)
  319. {
  320. UART_PutChar(ptUartTx, databuf[i]);
  321. }
  322. }
  323. /***************************************************************
  324. Function: SP_USART_RxData_Process;
  325. Description: Get Uart Data following the protocol
  326. Call by:
  327. Input Variables: N/A
  328. Output/Return Variables: N/A
  329. Subroutine Call: N/A;
  330. Reference: N/A
  331. ****************************************************************/
  332. void Lidian2_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
  333. {
  334. static UBYTE Data[255];
  335. //UWORD Cmd, ID;
  336. UWORD i;
  337. UWORD CheckxorResult=0;
  338. if(dataCount >= 20)
  339. {
  340. if(buf[0] == 0x01)//地址
  341. {
  342. if(buf[1] == 20)//帧长
  343. {
  344. if(buf[2] == 0x01)//命令号
  345. {
  346. if(buf[1] == dataCount)
  347. {
  348. for(i=0; i<dataCount; i++)//数据
  349. {
  350. Data[i] = buf[i];
  351. CheckxorResult ^= Data[i];
  352. }
  353. if(CheckxorResult == 0)// 校验和正常
  354. {
  355. Lidian2_USART_DataProcess(ptUartTx, Data);
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. void Lidian2_XinSiWeiLimitThrottleSpd(UBYTE throttleGear, UWORD speedPWM)
  364. {
  365. #if(HEYBIKEXINSIWEI_ENABLE == 1)
  366. speedPWM = (speedPWM & 0xFF);//只取低字节
  367. //助力始终分档限速
  368. ass_CurLimCoef.uwBikeSpdThresHold1 = ((SQWORD)1000 << 20) * (ass_ParaSet.uwAssistLimitBikeSpdStart + ass_ParaCong.swDeltaBikeSpeedLimit) *speedPWM / 255 /
  369. ((SQWORD)36 * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * FBASE); // Q20 3216 = Q10(3.1415926)
  370. ass_CurLimCoef.uwBikeSpdThresHold2 = ((SQWORD)1000 << 20) * (ass_ParaSet.uwAssistLimitBikeSpdStop + ass_ParaCong.swDeltaBikeSpeedLimit) *speedPWM / 255 /
  371. ((SQWORD)36 * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * FBASE); // Q20 3216 = Q10(3.1415926)
  372. ass_CurLimCoef.ulBikeSpdDeltInv = (1 << 20) / (ass_CurLimCoef.uwBikeSpdThresHold2 - ass_CurLimCoef.uwBikeSpdThresHold1);
  373. //转把有分档和不分档
  374. if(throttleGear == 0)
  375. {
  376. speedPWM = 255;
  377. }
  378. ass_stCadAssCoef.uwThrottleMaxBikeSpeed = (((ass_ParaCong.uwThrottleMaxSpdKmH+ass_ParaCong.swDeltaBikeSpeedLimit)<<4) + 8)*speedPWM/255;
  379. ass_stCadAssCoef.swThrottleMaxspd = ass_stCadAssCoef.swKmhToMSpdPu*((SLONG) ass_stCadAssCoef.uwThrottleMaxBikeSpeed + ASS_SPD_LIMIT_ERR) >> 4;
  380. #endif
  381. }
  382. /************************ (C) END OF FILE *********************************/