/** ****************************************************************************** * @file uart_J.c * @author * @version V1.0.0 * @date 23-12-2015 * @brief uart_J 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_J.h" #include "string.h" #include "usart.h" #include "canAppl.h" #include "syspar.h" #include "bikebrake.h" #include "bikethrottle.h" /****************************** * * Parameter * ******************************/ J_OBC2MCInfo_Struct_t J_stOBC2MCInfo; /****************************** * * Functions * ******************************/ /*************************************************************** Function: Description: OBC set MC parameter(轮径 电流阈值/功率阈值 限速值) Call by: Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void J_OBCSetPara(JOBC_SetInfo_Struct_t obc_setinfo) { UWORD SpeedLimitation = 0; UWORD WheelPerimeter = 0; /*参数设定*/ /*限速阈值更新*/ SpeedLimitation = (UWORD)(obc_setinfo.Set_Bit.ucSpeedLimitation + 10); if(MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart != SpeedLimitation) { MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStart = SpeedLimitation; MC_UpcInfo.stAssistInfo.uwAssistLimitBikeSpdStop = SpeedLimitation + 2; MC_UpcInfo.stBikeInfo.uwThrottleMaxSpdKmH = SpeedLimitation; MC_UpcInfo.stBikeInfo.uwSaveFlg = 1; MC_UpcInfo.stAssistInfo.uwSaveFlg = 1; cp_stFlg.ParaSaveEEFlg = TRUE; cp_stFlg.ParaUpdateFlg = TRUE; cp_stFlg.ParaAInfoUpdateFlg = TRUE; cp_stFlg.ParaBikeInfoUpdateFlg = TRUE; cp_stFlg.ParaAssistUpdateFinishFlg = TRUE; } /*轮径*/ /*轮径更新*/ if(obc_setinfo.Set_Bit.ucWheelDiameterCode <= 5) { WheelPerimeter = (obc_setinfo.Set_Bit.ucWheelDiameterCode*2 + 16) * 8; //3.14*2.54=7.9756=8 } else if(obc_setinfo.Set_Bit.ucWheelDiameterCode == 6) { WheelPerimeter = 219; //3.14*700mm=2198mm } else { WheelPerimeter = 223; //3.14*2.54*28 } if(MC_UpcInfo.stBikeInfo.uwWheelPerimeter != WheelPerimeter) { MC_UpcInfo.stBikeInfo.uwWheelPerimeter = WheelPerimeter; MC_UpcInfo.stBikeInfo.uwSaveFlg = 1; cp_stFlg.ParaSaveEEFlg = TRUE; cp_stFlg.ParaUpdateFlg = TRUE; cp_stFlg.ParaBikeInfoUpdateFlg = TRUE; cp_stFlg.ParaAssistUpdateFinishFlg = TRUE; } } /*************************************************************** Function: ; 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 J_USART_DataProcess(USART_Buf_TypeDef* ptUartTx, UBYTE* Data) { /*数据获取*/ memcpy(&J_stOBC2MCInfo.stCtrlInfo, &Data[1], 1); memcpy(&J_stOBC2MCInfo.stSetInfo, &Data[2], 1); /*数据更新*/ if(Data[0] == 0x53) { MC_ControlCode.GearSt = MC_GearSt_OFF;//起始符发送 S,此时需控制器停止电机驱动 } else if(J_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.Walk == 1) { MC_ControlCode.GearSt = MC_GearSt_WALK; } else if(J_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 5) { MC_ControlCode.GearSt = MC_GearSt_SMART; } else if(J_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 4) { MC_ControlCode.GearSt = MC_GearSt_Torque_TURBO; } else if(J_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 3) { MC_ControlCode.GearSt = MC_GearSt_Torque_SPORT; } else if(J_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 2) { MC_ControlCode.GearSt = MC_GearSt_Torque_NORM; } else if(J_stOBC2MCInfo.stCtrlInfo.Ctrl_Bit.ucGearSt == 1) { MC_ControlCode.GearSt = MC_GearSt_Torque_ECO; } else { MC_ControlCode.GearSt = MC_GearSt_OFF; } if(J_stOBC2MCInfo.stCtrlInfo.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; //J_OBCSetPara();//参数更新 J_SendData_OBC(ptUartTx,Data[3]); } /*************************************************************** Function: Description: Send Data to OBC Call by: Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void J_SendData_OBC(USART_Buf_TypeDef* ptUartTx, UBYTE checkAdjust) { UBYTE i = 0; UBYTE databuf[8] = {0}; UWORD checkxor = 0; UBYTE datalen = 8; UWORD Speed = 0; UBYTE Current = 0, Vlot = 0, errorCode = 0; /*数据发送*/ if(1 == MC_ErrorCode.ERROR_Bit.Protect_UnderVoltage) { Vlot = 0;//欠压 } else if(MC_RunInfo.SOC == 100) { Vlot = 5; //100% } else { Vlot = (UBYTE)((MC_RunInfo.SOC / 20) + 1);//1:0-19; 2:20-39; 3:40-59; 4:60-79; 5:80-99 } Current = (UBYTE)(abs(MC_RunInfo.BusCurrent/1000)*3);//电流 if(bikespeed_stFreGetOut.uwLPFFrequencyPu == 0) { Speed = 0; } else { Speed = (UWORD)((SQWORD)(1000 << 20) / bikespeed_stFreGetOut.uwLPFFrequencyPu / FBASE); //车轮转动一圈时间,ms } if(1 == MC_ErrorCode.ERROR_Bit.Protect_OverCurrent) { errorCode = 0x21;//电流异常 } else if(1 == MC_ErrorCode.ERROR_Bit.Fault_Throttle) { errorCode = 0x22;//转把异常 } else if(1 == MC_ErrorCode.ERROR_Bit.Fault_PhaseLine) { errorCode = 0x23;//电机缺相 } else if(1 == MC_ErrorCode.ERROR_Bit.Fault_HallSensor) { errorCode = 0x24;//电机霍尔异常 } /*else if(0) { errorCode = 0x25;//刹车异常 }*/ databuf[0] = 0x46;//'F' databuf[1] = Vlot; databuf[2] = Current; databuf[3] = (UBYTE)((Speed & 0xFF00)>>8); databuf[4] = (UBYTE)(Speed & 0xFF); databuf[5] = checkAdjust; databuf[6] = errorCode; for(i=1; i<(datalen-1); i++) { checkxor ^= databuf[i]; } databuf[datalen-1] = checkxor; for(i=0; i