Skip to content

Commit

Permalink
Bafang protocol now working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
stancecoke committed Jun 29, 2024
1 parent 8254c41 commit 527c1c3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
12 changes: 6 additions & 6 deletions Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@

//----------------------------------------------------------------------
//Battery bar settings for Kunteng and Bafang Display
#define BATTERY_LEVEL_1 32300
#define BATTERY_LEVEL_2 32900
#define BATTERY_LEVEL_3 34400
#define BATTERY_LEVEL_4 36800
#define BATTERY_LEVEL_5 38000
#define BATTERY_LEVEL_1 323000
#define BATTERY_LEVEL_2 329000
#define BATTERY_LEVEL_3 344000
#define BATTERY_LEVEL_4 368000
#define BATTERY_LEVEL_5 380000

//----------------------------------------------------------------------
//PI-control factor settings
Expand Down Expand Up @@ -74,7 +74,7 @@
//--------------------------------------------------------------------
//Speed settings
#define WHEEL_CIRCUMFERENCE 2200
#define GEAR_RATIO 11 //11 for BionX IGH3
#define GEAR_RATIO 98 //11 for BionX IGH3
#define SPEEDLIMIT 25
#define PULSES_PER_REVOLUTION 1
#define SPEEDSOURCE INTERNAL
Expand Down
2 changes: 1 addition & 1 deletion Inc/display_bafang.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ typedef struct
{
// Parameters to be send to display in operation mode:
uint8_t Battery; //
uint16_t Wheeltime_ms; // Unit:1ms
uint16_t Speed; // Unit:1ms
uint8_t Error; //
uint16_t Power; // Unit: 0.1W?!

Expand Down
6 changes: 3 additions & 3 deletions Src/display_bafang.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ void Bafang_Service(BAFANG_t* BF_ctx, uint8_t rx)

spd_tmp=BF_ctx->Rx.Wheeldiameter;

TxBuff[0]=(BF_ctx->Tx.Wheeltime_ms>>8);
TxBuff[1]=(BF_ctx->Tx.Wheeltime_ms&0xff);
TxBuff[0]=(BF_ctx->Tx.Speed>>8);
TxBuff[1]=(BF_ctx->Tx.Speed&0xff);
TxBuff[2]=TxBuff[0]+TxBuff[1]+32;
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&TxBuff, 3);
break;
Expand Down Expand Up @@ -175,7 +175,7 @@ void Bafang_Service(BAFANG_t* BF_ctx, uint8_t rx)
else if(BF_Message[0]==BF_CMD_STARTINFO) //we received an info
{

switch (BF_ctx->RxBuff[1])
switch (BF_Message[1])
{
case BF_CMD_LEVEL:
if (BF_Message[3]==BF_Message[0]+BF_Message[1]+BF_Message[2]) //checksum is correct, set poti_stat
Expand Down
24 changes: 12 additions & 12 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2046,16 +2046,16 @@ void kingmeter_update(void)


#if (SPEEDSOURCE == EXTERNAL)
KM.Tx.Wheeltime_ms = ((MS.Speed>>3)*PULSES_PER_REVOLUTION); //>>3 because of 8 kHz counter frequency, so 8 tics per ms
KM.Tx.Speed = ((MS.Speed>>3)*PULSES_PER_REVOLUTION); //>>3 because of 8 kHz counter frequency, so 8 tics per ms
#else
if(__HAL_TIM_GET_COUNTER(&htim2) < 12000)
{
KM.Tx.Wheeltime_ms = (MS.Speed*GEAR_RATIO*6)>>9; //>>9 because of 500kHZ timer2 frequency, 512 tics per ms should be OK *6 because of 6 hall interrupts per electric revolution.
KM.Tx.Speed = (MS.Speed*GEAR_RATIO*6)>>9; //>>9 because of 500kHZ timer2 frequency, 512 tics per ms should be OK *6 because of 6 hall interrupts per electric revolution.

}
else
{
KM.Tx.Wheeltime_ms = 64000;
KM.Tx.Speed = 64000;
}

#endif
Expand Down Expand Up @@ -2111,12 +2111,12 @@ void bafang_update(void)
{
/* Prepare Tx parameters */

if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_5)battery_percent_fromcapacity=75;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_4)battery_percent_fromcapacity=50;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_3)battery_percent_fromcapacity=30;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_2)battery_percent_fromcapacity=10;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_1)battery_percent_fromcapacity=5;
else battery_percent_fromcapacity=0;
if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_5)battery_percent_fromcapacity=95;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_4)battery_percent_fromcapacity=80;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_3)battery_percent_fromcapacity=50;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_2)battery_percent_fromcapacity=30;
else if(MS.Voltage*CAL_BAT_V>BATTERY_LEVEL_1)battery_percent_fromcapacity=20;
else battery_percent_fromcapacity=5;


BF.Tx.Battery = battery_percent_fromcapacity;
Expand All @@ -2125,14 +2125,14 @@ void bafang_update(void)
if(__HAL_TIM_GET_COUNTER(&htim2) < 12000)
{
#if (SPEEDSOURCE == EXTERNAL) // Adapt wheeltime to match displayed speedo value according config.h setting
BF.Tx.Wheeltime_ms = WHEEL_CIRCUMFERENCE*216/(MS.Speed*PULSES_PER_REVOLUTION); // Geschwindigkeit ist Weg pro Zeit Radumfang durch Dauer einer Radumdrehung --> Umfang * 8000*3600/(n*1000000) * Skalierung Bafang Display 200/26,6
BF.Tx.Speed = (external_tics_to_speedx100(MS.Speed)*20)>>8;// Geschwindigkeit ist Weg pro Zeit Radumfang durch Dauer einer Radumdrehung --> Umfang * 8000*3600/(n*1000000) * Skalierung Bafang Display 200/26,6
#else
BF.Tx.Wheeltime_ms = internal_tics_to_speedx100(MS.Speed); //missing factor has to be found
BF.Tx.Speed =(internal_tics_to_speedx100(MS.Speed)*20)>>8; //factor is *20/256, found empiric
#endif
}
else
{
BF.Tx.Wheeltime_ms = 0; //64000;
BF.Tx.Speed = 0;
}


Expand Down

0 comments on commit 527c1c3

Please sign in to comment.