uart_Lidian2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #include "Customization.h"
  45. /******************************
  46. *
  47. * Parameter
  48. *
  49. ******************************/
  50. Lidian2_OBC2MCInfo_Struct_t Lidian2_stOBC2MCInfo;
  51. Lidian2_MC2OBCInfo_Struct_t Lidian2_stMC2OBCInfo;
  52. UBYTE ucWalk_cnt;
  53. /******************************
  54. *
  55. * Functions
  56. *
  57. ******************************/
  58. /***************************************************************
  59. Function: SP_OBCSetPara;
  60. Description: OBC set MC parameter(轮径 电流阈值/功率阈值 低压阈值 限速值)
  61. Call by:
  62. Input Variables: N/A
  63. Output/Return Variables: N/A
  64. Subroutine Call: N/A;
  65. Reference: N/A
  66. ****************************************************************/
  67. void Lidian2_OBCSetPara(void)
  68. {
  69. /*参数设定*/
  70. /*轮径*/
  71. #if 0
  72. UWORD tmpdata;
  73. tmpdata = Lidian2_stOBC2MCInfo.uwWheelDiameter*8/10;
  74. if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != tmpdata)
  75. {
  76. MC_UpcInfo.stBikeInfo.uwWheelPerimeter = tmpdata;
  77. MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
  78. cp_stFlg.ParaSaveEEFlg = TRUE;
  79. cp_stFlg.ParaUpdateFlg = TRUE;
  80. cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
  81. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  82. }
  83. /*功率限幅阈值更新*/
  84. // #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
  85. // UWORD uwVolt = 480;
  86. // #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
  87. // UWORD uwVolt = 360;
  88. // #else
  89. // UWORD uwVolt = 360;
  90. // #endif
  91. UWORD uwVolt= cp_stMotorPara.swRUdcV;
  92. if(MC_UpcInfo.stTestParaInfo.uwPwrLimit != ((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500))
  93. {
  94. MC_UpcInfo.stTestParaInfo.uwPwrLimit = (UWORD)((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500);
  95. MC_UpcInfo.stTestParaInfo.uwSaveFlg = 1;
  96. cp_stFlg.ParaSaveEEFlg = TRUE;
  97. cp_stFlg.ParaUpdateFlg = TRUE;
  98. cp_stFlg.TestParaInfoUpdateFlg = TRUE;
  99. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  100. }
  101. /*低压保护阈值更新*/
  102. if(MC_UpcInfo.stMContorlInfo.uwAlamUVolV != Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation)
  103. {
  104. MC_UpcInfo.stMContorlInfo.uwAlamUVolV = Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation;
  105. MC_UpcInfo.stMContorlInfo.uwSaveFlg = 1;
  106. cp_stFlg.ParaSaveEEFlg = TRUE;
  107. cp_stFlg.ParaUpdateFlg = TRUE;
  108. cp_stFlg.ParaMCInfoUpdateFlg = TRUE;
  109. cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
  110. }
  111. #endif
  112. /*限速阈值更新*/
  113. if(Lidian2_stOBC2MCInfo.ucSpeedLimiation != (MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart + MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit))//有变化时更新
  114. {
  115. SWORD deltaSpeed = 0;
  116. deltaSpeed = (Lidian2_stOBC2MCInfo.ucSpeedLimiation - MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart);
  117. if(deltaSpeed > 0)
  118. {
  119. deltaSpeed = 0;
  120. }
  121. if(deltaSpeed != MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit)
  122. {
  123. MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit = deltaSpeed;
  124. ass_ParaCong.swDeltaBikeSpeedLimit = deltaSpeed;
  125. MC_UpcInfo.stBikeInfo2.uwSaveFlg = TRUE;
  126. cp_stFlg.ParaSaveEEFlg = TRUE;
  127. cp_stFlg.ParaUpdateFlg = TRUE;
  128. cp_stFlg.ParaBikeInfo2UpdateFlg = TRUE;
  129. cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
  130. }
  131. }
  132. Lidian2_XinSiWeiLimitThrottleSpd(Lidian2_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ThrottleGearEnable,Lidian2_stOBC2MCInfo.uwThrottlePWM);
  133. }
  134. /***************************************************************
  135. Function: SP_USART_DataProcess;
  136. Description: Get OBC GearSt and LightControl information
  137. Call by:
  138. Input Variables: N/A
  139. Output/Return Variables: N/A
  140. Subroutine Call: N/A;
  141. Reference: N/A
  142. ****************************************************************/
  143. void Lidian2_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data)
  144. {
  145. /*数据获取*/
  146. Lidian2_stOBC2MCInfo.ucPowerStyle = Data[3];
  147. Lidian2_stOBC2MCInfo.ucGearSt = Data[4];
  148. memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo1, &Data[5], 1);
  149. Lidian2_stOBC2MCInfo.ucSpeedMagnetSteel = Data[6];
  150. Lidian2_stOBC2MCInfo.uwWheelDiameter = (UWORD)((Data[7]<<8)+Data[8]);//0.1inch
  151. Lidian2_stOBC2MCInfo.ucAssistSensitivity = Data[9];
  152. Lidian2_stOBC2MCInfo.ucAssistPowerMode = Data[10];
  153. Lidian2_stOBC2MCInfo.ucInnerSpeedMagnetSteel = Data[11];
  154. Lidian2_stOBC2MCInfo.ucSpeedLimiation = Data[12];//km/h
  155. Lidian2_stOBC2MCInfo.ucCurrentLimiation = Data[13];
  156. Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation = (UWORD)((Data[14]<<8)+Data[15]);
  157. Lidian2_stOBC2MCInfo.uwThrottlePWM = (UWORD)((Data[16]<<8)+Data[17]);
  158. memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo2, &Data[18], 1);
  159. #if 0
  160. /*数据更新*/
  161. /*WALK挡位逻辑为:WALK优先级最高,仪表长按"-"进入WALK档或退出WALK档,每次长按发送10帧bit1为1*/
  162. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
  163. {
  164. ucWalk_cnt++;
  165. }
  166. else
  167. {
  168. ucWalk_cnt = 0;
  169. }
  170. if(ucWalk_cnt >= 8)
  171. {
  172. ucWalk_cnt = 0;
  173. if(MC_ControlCode.GearSt == MC_GearSt_WALK)
  174. {
  175. MC_ControlCode.GearSt = MC_GearSt_OFF;
  176. }
  177. else
  178. {
  179. MC_ControlCode.GearSt = MC_GearSt_WALK;
  180. }
  181. }
  182. #else
  183. //长按-键进入助推,松开退出
  184. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
  185. {
  186. MC_ControlCode.GearSt = MC_GearSt_WALK;
  187. }
  188. else
  189. {
  190. MC_ControlCode.GearSt = MC_GearSt_OFF;
  191. }
  192. #endif
  193. //0-OFF, 1<=GearSt<6:ECO, 6<=GearSt<9:NORM, 9<=GearSt<12:SPORT, 12<=GearSt<15:TURBO, 15==GearSt:SMART
  194. if(MC_ControlCode.GearSt != MC_GearSt_WALK)
  195. {
  196. if(Lidian2_stOBC2MCInfo.ucGearSt == 15)
  197. {
  198. MC_ControlCode.GearSt = MC_GearSt_SMART;
  199. }
  200. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 12)
  201. {
  202. MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
  203. }
  204. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 9)
  205. {
  206. MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
  207. }
  208. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 6)
  209. {
  210. MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
  211. }
  212. else if(Lidian2_stOBC2MCInfo.ucGearSt >= 1)
  213. {
  214. MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
  215. }
  216. else
  217. {
  218. MC_ControlCode.GearSt = MC_GearSt_OFF;
  219. }
  220. }
  221. if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Light == 1) //大灯
  222. {
  223. MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
  224. }
  225. else
  226. {
  227. MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
  228. }
  229. MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
  230. MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
  231. ulOBC_ComTimeOutCount = cp_ulSystickCnt;
  232. Lidian2_OBCSetPara();//参数更新
  233. Lidian2_SendData_OBC(ptUartTx);//数据回传
  234. }
  235. /***************************************************************
  236. Function: SP_SendData_OBC;
  237. Description: Send Data to OBC
  238. Call by:
  239. Input Variables: N/A
  240. Output/Return Variables: N/A
  241. Subroutine Call: N/A;
  242. Reference: N/A
  243. ****************************************************************/
  244. void Lidian2_SendData_OBC(USART_Buf_TypeDef* ptUartTx)
  245. {
  246. UBYTE i = 0;
  247. UBYTE sendlen = 14;
  248. UBYTE databuf[14] = {0};
  249. if(MC_ControlCode.GearSt == MC_GearSt_WALK)
  250. {
  251. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 1;
  252. }
  253. else
  254. {
  255. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 0;
  256. }
  257. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.HallErr = MC_ErrorCode.ERROR_Bit.Fault_HallSensor;
  258. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ThrottleErr = MC_ErrorCode.ERROR_Bit.Fault_Throttle;
  259. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ControllerErr = MC_ErrorCode.ERROR_Bit.Fault_MCU;
  260. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.UnderVoltageErr = MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage;
  261. if(bikeCruise.CruiseMode == 1)
  262. {
  263. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 1;
  264. }
  265. else
  266. {
  267. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 0;
  268. }
  269. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.BrakeErr = 0;//暂未判断刹把故障
  270. Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.RotorLock = MC_ErrorCode.ERROR_Bit.Fault_PhaseLine;
  271. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Horizontal = 1;//不实现
  272. if(cp_stFlg.RunModelSelect == CadAssist)
  273. {
  274. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.CadenceSen;
  275. }
  276. else if(cp_stFlg.RunModelSelect == TorqAssist)
  277. {
  278. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.TorqSen;
  279. }
  280. else
  281. {
  282. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = 0;
  283. }
  284. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Brake = BikeBrake_blGetstate();
  285. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.CommErr = 0;//暂不判断通信故障
  286. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Charging = 0;//MC无法判断充电
  287. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.SpeedLimited = 0;
  288. Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.GearSt = Lidian2_stOBC2MCInfo.ucGearSt;
  289. Lidian2_stMC2OBCInfo.uwCurrent = (UWORD)abs(MC_RunInfo.BusCurrent/1000);
  290. Lidian2_stMC2OBCInfo.ucCurrentRatio = 0;//未知用途
  291. if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
  292. {
  293. Lidian2_stMC2OBCInfo.uwSpeed = 0;
  294. }
  295. else
  296. {
  297. Lidian2_stMC2OBCInfo.uwSpeed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
  298. }
  299. Lidian2_stMC2OBCInfo.SOC = MC_RunInfo.SOC;//由BMS实现
  300. Lidian2_stMC2OBCInfo.uwRemainDistance = 0;//由BMS实现
  301. databuf[0] = 0x02;
  302. databuf[1] = sendlen;
  303. databuf[2] = 0x01;
  304. //memcpy(&databuf[3],&Lidian2_stMC2OBCInfo.stMC_StatusInfo1,4);
  305. databuf[3] = (UBYTE)(Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status);
  306. databuf[4] = (UBYTE)(Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status);
  307. databuf[5] = (UBYTE)(Lidian2_stMC2OBCInfo.uwCurrent>>8);
  308. databuf[6] = (UBYTE)(Lidian2_stMC2OBCInfo.uwCurrent&0xff);
  309. databuf[7] = (UBYTE)(Lidian2_stMC2OBCInfo.ucCurrentRatio);
  310. databuf[8] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed>>8);
  311. databuf[9] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed&0xFF);
  312. databuf[10] = Lidian2_stMC2OBCInfo.SOC;
  313. databuf[11] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance>>8);
  314. databuf[12] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance&0xFF);
  315. databuf[sendlen-1] = 0;
  316. for(i=0; i<(sendlen-1); i++)
  317. {
  318. databuf[sendlen-1] ^= databuf[i];
  319. }
  320. for(i=0; i<sendlen; i++)
  321. {
  322. UART_PutChar(ptUartTx, databuf[i]);
  323. }
  324. }
  325. /***************************************************************
  326. Function: SP_USART_RxData_Process;
  327. Description: Get Uart Data following the protocol
  328. Call by:
  329. Input Variables: N/A
  330. Output/Return Variables: N/A
  331. Subroutine Call: N/A;
  332. Reference: N/A
  333. ****************************************************************/
  334. void Lidian2_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
  335. {
  336. static UBYTE Data[255];
  337. //UWORD Cmd, ID;
  338. UWORD i;
  339. UWORD CheckxorResult=0;
  340. if(dataCount >= 20)
  341. {
  342. if(buf[0] == 0x01)//地址
  343. {
  344. if(buf[1] == 20)//帧长
  345. {
  346. if(buf[2] == 0x01)//命令号
  347. {
  348. if(buf[1] == dataCount)
  349. {
  350. for(i=0; i<dataCount; i++)//数据
  351. {
  352. Data[i] = buf[i];
  353. CheckxorResult ^= Data[i];
  354. }
  355. if(CheckxorResult == 0)// 校验和正常
  356. {
  357. Lidian2_USART_DataProcess(ptUartTx, Data);
  358. }
  359. }
  360. }
  361. }
  362. }
  363. }
  364. }
  365. void Lidian2_XinSiWeiLimitThrottleSpd(UBYTE throttleGear, UWORD speedPWM)
  366. {
  367. #if(HEYBIKEXINSIWEI_ENABLE == 1)
  368. speedPWM = (speedPWM & 0xFF);//只取低字节
  369. //助力始终分档限速
  370. ass_CurLimCoef.uwBikeSpdThresHold1 = ((SQWORD)1000 << 20) * (ass_ParaSet.uwAssistLimitBikeSpdStart + ass_ParaCong.swDeltaBikeSpeedLimit) *speedPWM / 255 /
  371. ((SQWORD)36 * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * FBASE); // Q20 3216 = Q10(3.1415926)
  372. ass_CurLimCoef.uwBikeSpdThresHold2 = ((SQWORD)1000 << 20) * (ass_ParaSet.uwAssistLimitBikeSpdStop + ass_ParaCong.swDeltaBikeSpeedLimit) *speedPWM / 255 /
  373. ((SQWORD)36 * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * FBASE); // Q20 3216 = Q10(3.1415926)
  374. ass_CurLimCoef.ulBikeSpdDeltInv = (1 << 20) / (ass_CurLimCoef.uwBikeSpdThresHold2 - ass_CurLimCoef.uwBikeSpdThresHold1);
  375. //转把有分档和不分档
  376. if(throttleGear == 0)
  377. {
  378. speedPWM = 255;
  379. }
  380. ass_stCadAssCoef.uwThrottleMaxBikeSpeed = (((ass_ParaCong.uwThrottleMaxSpdKmH+ass_ParaCong.swDeltaBikeSpeedLimit)<<4) + 8)*speedPWM/255;
  381. ass_stCadAssCoef.swThrottleMaxspd = ass_stCadAssCoef.swKmhToMSpdPu*((SLONG) ass_stCadAssCoef.uwThrottleMaxBikeSpeed + ASS_SPD_LIMIT_ERR) >> 4;
  382. #endif
  383. }
  384. /************************ (C) END OF FILE *********************************/