123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /**
- ******************************************************************************
- * @file uartSpecial.c
- * @author
- * @version V1.0.0
- * @date 23-12-2015
- * @brief uartSpecial function.
- ******************************************************************************
- *
- * COPYRIGHT(c) 2015 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "uart_Lidian2.h"
- #include "string.h"
- #include "usart.h"
- #include "canAppl.h"
- #include "syspar.h"
- #include "bikebrake.h"
- #include "bikethrottle.h"
- #include "Customization.h"
- /******************************
- *
- * Parameter
- *
- ******************************/
- Lidian2_OBC2MCInfo_Struct_t Lidian2_stOBC2MCInfo;
- Lidian2_MC2OBCInfo_Struct_t Lidian2_stMC2OBCInfo;
- UBYTE ucWalk_cnt;
- /******************************
- *
- * Functions
- *
- ******************************/
- /***************************************************************
- Function: SP_OBCSetPara;
- Description: OBC set MC parameter(轮径 电流阈值/功率阈值 低压阈值 限速值)
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lidian2_OBCSetPara(void)
- {
- /*参数设定*/
- /*轮径*/
- #if 0
- UWORD tmpdata;
- tmpdata = Lidian2_stOBC2MCInfo.uwWheelDiameter*8/10;
- if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != tmpdata)
- {
- MC_UpcInfo.stBikeInfo.uwWheelPerimeter = tmpdata;
- MC_UpcInfo.stBikeInfo.uwSaveFlg = 1;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.ParaBikeInfoUpdateFlg = TRUE;
- cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
- }
-
- /*功率限幅阈值更新*/
- #if (IPM_VOLTAGE_SEL == IPM_VOLTAGE_48V)
- UWORD uwVolt = 480;
- #elif (IPM_VOLTAGE_SEL == IPM_VOLTAGE_36V)
- UWORD uwVolt = 360;
- #else
- UWORD uwVolt = 360;
- #endif
- if(MC_UpcInfo.stTestParaInfo.uwPwrLimit != ((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500))
- {
- MC_UpcInfo.stTestParaInfo.uwPwrLimit = (UWORD)((Lidian2_stOBC2MCInfo.ucCurrentLimiation*uwVolt) + 500);
- MC_UpcInfo.stTestParaInfo.uwSaveFlg = 1;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.TestParaInfoUpdateFlg = TRUE;
- cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
- }
-
- /*低压保护阈值更新*/
- if(MC_UpcInfo.stMContorlInfo.uwAlamUVolV != Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation)
- {
- MC_UpcInfo.stMContorlInfo.uwAlamUVolV = Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation;
- MC_UpcInfo.stMContorlInfo.uwSaveFlg = 1;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.ParaMCInfoUpdateFlg = TRUE;
- cp_stFlg.ParaMotorDriveUpdateFinishFlg = TRUE;
- }
- #endif
-
- /*限速阈值更新*/
- if(Lidian2_stOBC2MCInfo.ucSpeedLimiation != (MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart + MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit))//有变化时更新
- {
- SWORD deltaSpeed = 0;
- deltaSpeed = (Lidian2_stOBC2MCInfo.ucSpeedLimiation - MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart);
- if(deltaSpeed > 0)
- {
- deltaSpeed = 0;
- }
- if(deltaSpeed != MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit)
- {
- MC_UpcInfo.stBikeInfo2.swDeltaBikeSpeedLimit = deltaSpeed;
- ass_ParaCong.swDeltaBikeSpeedLimit = deltaSpeed;
- MC_UpcInfo.stBikeInfo2.uwSaveFlg = TRUE;
- cp_stFlg.ParaSaveEEFlg = TRUE;
- cp_stFlg.ParaUpdateFlg = TRUE;
- cp_stFlg.ParaBikeInfo2UpdateFlg = TRUE;
- cp_stFlg.ParaAssistUpdateFinishFlg = TRUE;
- }
- }
- Lidian2_XinSiWeiLimitThrottleSpd(Lidian2_stOBC2MCInfo.stCtrlInfo2.Ctrl_Bit.ThrottleGearEnable,Lidian2_stOBC2MCInfo.uwThrottlePWM);
- }
- /***************************************************************
- Function: SP_USART_DataProcess;
- Description: Get OBC GearSt and LightControl information
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lidian2_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data)
- {
- /*数据获取*/
- Lidian2_stOBC2MCInfo.ucPowerStyle = Data[3];
- Lidian2_stOBC2MCInfo.ucGearSt = Data[4];
- memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo1, &Data[5], 1);
- Lidian2_stOBC2MCInfo.ucSpeedMagnetSteel = Data[6];
- Lidian2_stOBC2MCInfo.uwWheelDiameter = (UWORD)((Data[7]<<8)+Data[8]);//0.1inch
- Lidian2_stOBC2MCInfo.ucAssistSensitivity = Data[9];
- Lidian2_stOBC2MCInfo.ucAssistPowerMode = Data[10];
- Lidian2_stOBC2MCInfo.ucInnerSpeedMagnetSteel = Data[11];
- Lidian2_stOBC2MCInfo.ucSpeedLimiation = Data[12];//km/h
- Lidian2_stOBC2MCInfo.ucCurrentLimiation = Data[13];
- Lidian2_stOBC2MCInfo.uwUnderVoltageLimiation = (UWORD)((Data[14]<<8)+Data[15]);
- Lidian2_stOBC2MCInfo.uwThrottlePWM = (UWORD)((Data[16]<<8)+Data[17]);
- memcpy(&Lidian2_stOBC2MCInfo.stCtrlInfo2, &Data[18], 1);
- #if 0
- /*数据更新*/
- /*WALK挡位逻辑为:WALK优先级最高,仪表长按"-"进入WALK档或退出WALK档,每次长按发送10帧bit1为1*/
- if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
- {
- ucWalk_cnt++;
- }
- else
- {
- ucWalk_cnt = 0;
- }
-
- if(ucWalk_cnt >= 8)
- {
- ucWalk_cnt = 0;
- if(MC_ControlCode.GearSt == MC_GearSt_WALK)
- {
- MC_ControlCode.GearSt = MC_GearSt_OFF;
- }
- else
- {
- MC_ControlCode.GearSt = MC_GearSt_WALK;
- }
- }
- #else
- //长按-键进入助推,松开退出
- if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Walk == 1)
- {
- MC_ControlCode.GearSt = MC_GearSt_WALK;
- }
- else
- {
- MC_ControlCode.GearSt = MC_GearSt_OFF;
- }
-
- #endif
- //0-OFF, 1<=GearSt<6:ECO, 6<=GearSt<9:NORM, 9<=GearSt<12:SPORT, 12<=GearSt<15:TURBO, 15==GearSt:SMART
- if(MC_ControlCode.GearSt != MC_GearSt_WALK)
- {
- if(Lidian2_stOBC2MCInfo.ucGearSt == 15)
- {
- MC_ControlCode.GearSt = MC_GearSt_SMART;
- }
- else if(Lidian2_stOBC2MCInfo.ucGearSt >= 12)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO;
- }
- else if(Lidian2_stOBC2MCInfo.ucGearSt >= 9)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT;
- }
- else if(Lidian2_stOBC2MCInfo.ucGearSt >= 6)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_NORM;
- }
- else if(Lidian2_stOBC2MCInfo.ucGearSt >= 1)
- {
- MC_ControlCode.GearSt = MC_GearSt_Torque_ECO;
- }
- else
- {
- MC_ControlCode.GearSt = MC_GearSt_OFF;
- }
- }
-
- if(Lidian2_stOBC2MCInfo.stCtrlInfo1.Ctrl_Bit.Light == 1) //大灯
- {
- MC_ControlCode.LightSwitch = MC_LightSwitch_ON;
- }
- else
- {
- MC_ControlCode.LightSwitch = MC_LightSwitch_OFF;
- }
-
- MC_RunInfo.GearSt = MC_ControlCode.GearSt; //当前助力档位更新
- MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch; //大灯状态更新
- ulOBC_ComTimeOutCount = cp_ulSystickCnt;
-
- Lidian2_OBCSetPara();//参数更新
-
- Lidian2_SendData_OBC(ptUartTx);//数据回传
- }
- /***************************************************************
- Function: SP_SendData_OBC;
- Description: Send Data to OBC
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lidian2_SendData_OBC(USART_Buf_TypeDef* ptUartTx)
- {
- UBYTE i = 0;
- UBYTE sendlen = 14;
- UBYTE databuf[14] = {0};
-
- if(MC_ControlCode.GearSt == MC_GearSt_WALK)
- {
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 1;
- }
- else
- {
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Walk = 0;
- }
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.HallErr = MC_ErrorCode.ERROR_Bit.Fault_HallSensor;
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ThrottleErr = MC_ErrorCode.ERROR_Bit.Fault_Throttle;
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.ControllerErr = MC_ErrorCode.ERROR_Bit.Fault_MCU;
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.UnderVoltageErr = MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage;
- if(bikeCruise.CruiseMode == 1)
- {
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 1;
- }
- else
- {
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.Curise = 0;
- }
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.BrakeErr = 0;//暂未判断刹把故障
- Lidian2_stMC2OBCInfo.stMC_StatusInfo1.Status_Bit.RotorLock = MC_ErrorCode.ERROR_Bit.Fault_PhaseLine;
-
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Horizontal = 1;//不实现
- if(cp_stFlg.RunModelSelect == CadAssist)
- {
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.CadenceSen;
- }
- else if(cp_stFlg.RunModelSelect == TorqAssist)
- {
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = alm_unBikeCode.bit.TorqSen;
- }
- else
- {
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.AssistSensorErr = 0;
- }
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Brake = BikeBrake_blGetstate();
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.CommErr = 0;//暂不判断通信故障
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.Charging = 0;//MC无法判断充电
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.SpeedLimited = 0;
- Lidian2_stMC2OBCInfo.stMC_StatusInfo2.Status_Bit.GearSt = Lidian2_stOBC2MCInfo.ucGearSt;
-
- Lidian2_stMC2OBCInfo.uwCurrent = (UWORD)abs(MC_RunInfo.BusCurrent/1000);
- Lidian2_stMC2OBCInfo.ucCurrentRatio = 0;//未知用途
- if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0)
- {
- Lidian2_stMC2OBCInfo.uwSpeed = 0;
- }
- else
- {
- Lidian2_stMC2OBCInfo.uwSpeed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms
- }
- Lidian2_stMC2OBCInfo.SOC = MC_RunInfo.SOC;//由BMS实现
- Lidian2_stMC2OBCInfo.uwRemainDistance = 0;//由BMS实现
- databuf[0] = 0x02;
- databuf[1] = sendlen;
- databuf[2] = 0x01;
-
- memcpy(&databuf[3],&Lidian2_stMC2OBCInfo.stMC_StatusInfo1,4);
- databuf[4]=0;
- databuf[5] =(UBYTE)(Lidian2_stMC2OBCInfo.uwCurrent>>8);
- databuf[6] =(UBYTE)(Lidian2_stMC2OBCInfo.uwCurrent&0xff);
- databuf[7] = (UBYTE)(Lidian2_stMC2OBCInfo.ucCurrentRatio);
- databuf[8] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed>>8);
- databuf[9] = (UBYTE)(Lidian2_stMC2OBCInfo.uwSpeed&0xFF);
- databuf[10] = Lidian2_stMC2OBCInfo.SOC;
- databuf[11] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance>>8);
- databuf[12] = (UBYTE)(Lidian2_stMC2OBCInfo.uwRemainDistance&0xFF);
-
- databuf[sendlen-1] = 0;
- for(i=0; i<(sendlen-1); i++)
- {
- databuf[sendlen-1] ^= databuf[i];
- }
-
- for(i=0; i<sendlen; i++)
- {
- UART_PutChar(ptUartTx, databuf[i]);
- }
- }
- /***************************************************************
- Function: SP_USART_RxData_Process;
- Description: Get Uart Data following the protocol
- Call by:
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: N/A;
- Reference: N/A
- ****************************************************************/
- void Lidian2_USART_RxData_Process(USART_Buf_TypeDef* ptUartTx, UBYTE* buf, UWORD dataCount)
- {
- static UBYTE Data[255];
- //UWORD Cmd, ID;
- UWORD i;
- UWORD CheckxorResult=0;
-
- if(dataCount >= 20)
- {
- if(buf[0] == 0x01)//地址
- {
- if(buf[1] == 20)//帧长
- {
- if(buf[2] == 0x01)//命令号
- {
- if(buf[1] == dataCount)
- {
- for(i=0; i<dataCount; i++)//数据
- {
- Data[i] = buf[i];
- CheckxorResult ^= Data[i];
- }
-
- if(CheckxorResult == 0)// 校验和正常
- {
- Lidian2_USART_DataProcess(ptUartTx, Data);
- }
- }
- }
- }
- }
- }
- }
- void Lidian2_XinSiWeiLimitThrottleSpd(UBYTE throttleGear, UWORD speedPWM)
- {
- #if(HEYBIKEXINSIWEI_ENABLE == 1)
- speedPWM = (speedPWM & 0xFF);//只取低字节
- //助力始终分档限速
- ass_CurLimCoef.uwBikeSpdThresHold1 = ((SQWORD)1000 << 20) * (ass_ParaSet.uwAssistLimitBikeSpdStart + ass_ParaCong.swDeltaBikeSpeedLimit) *speedPWM / 255 /
- ((SQWORD)36 * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * FBASE); // Q20 3216 = Q10(3.1415926)
- ass_CurLimCoef.uwBikeSpdThresHold2 = ((SQWORD)1000 << 20) * (ass_ParaSet.uwAssistLimitBikeSpdStop + ass_ParaCong.swDeltaBikeSpeedLimit) *speedPWM / 255 /
- ((SQWORD)36 * (ass_ParaCong.uwWheelPerimeter + ass_ParaCong.swDeltPerimeter) * FBASE); // Q20 3216 = Q10(3.1415926)
- ass_CurLimCoef.ulBikeSpdDeltInv = (1 << 20) / (ass_CurLimCoef.uwBikeSpdThresHold2 - ass_CurLimCoef.uwBikeSpdThresHold1);
- //转把有分档和不分档
- if(throttleGear == 1)
- {
- speedPWM = 255;
- }
- ass_stCadAssCoef.uwThrottleMaxBikeSpeed = (((ass_ParaCong.uwThrottleMaxSpdKmH+ass_ParaCong.swDeltaBikeSpeedLimit)<<4) + 8)*speedPWM/255;
- ass_stCadAssCoef.swThrottleMaxspd = ass_stCadAssCoef.swKmhToMSpdPu*((SLONG) ass_stCadAssCoef.uwThrottleMaxBikeSpeed + ASS_SPD_LIMIT_ERR) >> 4;
- #endif
- }
- /************************ (C) END OF FILE *********************************/
|