• Hello ES! We could use some help to get us past the finish line on building the new knowledgebase for the forum.
    Can you donate? Please see our fundraising page. Thank you!

FOC Open Source Firmware for Bafang CAN bus controllers with GD32F303 processor

Hi. I flashed the latest version of the *.BIN file to my M820 powered by a 48V battery. Here are my observations:

  1. At a standstill (when not moving and not putting pressure on the pedals), the display constantly shows 44W in every assist mode.
  2. Canable alternatingly displays "MOTOR TEMPERATURE" as either 106 or 173 degrees Celsius. On the stock firmware, it correctly shows 30 degrees Celsius in the "CONTROLLER TEMPERATURE" field.
  3. Walk Assist, when pressed, starts at full power for a brief moment (no longer than 1 second).
  4. When I start pedaling, the motor sometimes acts as if it were disengaged; for about 0.5 seconds, it sounds like it revs up high without transferring any power to the chainring (even though that shouldn't be physically possible).
  5. Upon starting to pedal, the power is delivered abruptly (instead of smoothly/progressively). To be clear, it's not a massive surge, but roughly adequate to the pedal pressure – it's just the delivery that is too sudden.
  6. When I stop pedaling, the motor power is cut off instantly. A slight overrun would feel much more natural and pleasant.
Of course, I understand that this is just the beginning of the project and these issues will be systematically addressed/fixed, but I wanted to share what I noticed :)

I also have a quick question – after flashing the FW and calibrating, do I need to adjust anything else? Is there a micro-guide somewhere on what needs to be tweaked, if anything at all?

I'm really rooting for this project because it's a great motor and it deserves a good, customizable FW. Too bad it's so noisy :-D
 
Thank you for the feedback.
At a standstill (when not moving and not putting pressure on the pedals), the display constantly shows 44W in every assist mode.
That's an artefact of the hardcoded current offset. Every system will have a slightly different offset. We can autocalibrate the offset at startup someday ;)

Canable alternatingly displays "MOTOR TEMPERATURE" as either 106 or 173 degrees Celsius. On the stock firmware, it correctly shows 30 degrees Celsius in the "CONTROLLER TEMPERATURE" field.
Yes, as written above, I have not adapted the temperature reading for the M820, it's different from the M510/560 obviously. But a good hint, this can be the reason for the setpoint dropping.

Walk Assist, when pressed, starts at full power for a brief moment (no longer than 1 second).
It doesn't start with full power, but with a quite low hard coded motor current. It's disabled when the shown speed is bigger than 6kph.

  1. When I start pedaling, the motor sometimes acts as if it were disengaged; for about 0.5 seconds, it sounds like it revs up high without transferring any power to the chainring (even though that shouldn't be physically possible).
  2. Upon starting to pedal, the power is delivered abruptly (instead of smoothly/progressively). To be clear, it's not a massive surge, but roughly adequate to the pedal pressure – it's just the delivery that is too sudden.
You would have to provide logs from the debug output. My system is working fine. I can't debug that issue without any data from the distance.

When I stop pedaling, the motor power is cut off instantly. A slight overrun would feel much more natural and pleasant.
:ROFLMAO: Try to please everyone. So far, people have always complained that the overrun lasts too long..
But you can tune that yourself by the current decay parameter.

I also have a quick question – after flashing the FW and calibrating, do I need to adjust anything else? Is there a micro-guide somewhere on what needs to be tweaked, if anything at all?
There is the ReadMe in the repo and the Wiki at GitHub. Volunteers for improving the manual are highly welcome 😁

I just see we have first contributions! Thank you, @glodnickim! (y)
 
Last edited:
Canable alternatingly displays "MOTOR TEMPERATURE" as either 106 or 173 degrees Celsius. On the stock firmware, it correctly shows 30 degrees Celsius in the "CONTROLLER TEMPERATURE" field.
I've just fixed that, the temperature is on PB1 on the M820, not on PB0.
The calibration is not correct yet, I guess. The shown temperature seems to be too high. But for safety reasons better than too low :)

regards
stancecoke
 

Attachments

  • EBiCS_for_M820_v0.0022.zip
    30 KB · Views: 3
stancecoke thanks for all your efforts but...
I was not able to upload this firmware. I have a M820 with a DP C030 and I use the Windmeile interface.
The uploading reach 100% and all seems fine but then the Canable window shows a continuous scrolling...
The calibration doesn't work. I tried several times and every time I had to disconnect the battery to reset the controller because it was stuck with ERR30.
A couple of times the error disappeared as the calibration was ended but the system was stuck, not permitting me to set the parameters like voltage, amps etc. (and the canable windows showed a continuous scrolling)
I have no problem with all other FW, the original ones or the Fake Taxi, they all upload flawlessly.
Sorry to report that...
 
How long did you wait?
for the first two/three times I waited for very long, listening at every sound and movement of the motor... nothing happened.
(the chain was removed)
One thing I remember seeing in the scrolling windows was ACK ERROR or something like that.
 
I waited for very long
Have you switched off and then restarted the system after the firmware update?
I will cross-check tomorrow, if the calibration goes well with the latest bin file, I haven't used the position sensor calibration for a while, as I'm flashing with the STLink normally. This doesn't erase the virtual EEPROM, so I don't have to rerun the calibration after flashing.
 
Last edited:
I have found your EBiCS soft very interesting altrenative for Bafang firmware. My qustion is if I can uplaod EBiCS_for_M510+M560_BL3+38_V0.008 to Bafang M560 controller FC1 48V?
 
you can install it. If something goes wrong, there should be no problem reverting to the original software.
 
I had to disconnect the battery to reset the controller
OK, the update procedure ends up in E30, but this doesn't matter. Just remove the battery once and restart the system. Then it starts normally. I've changed the position sensor calibration a little, so it's more similar to the orignal procedure. It does not change anything in the function but might give the user a better feeling, that everything is OK ;)


I did a test ride with this firmware flashed by the canable tool, everything worked as expected.
One hint: if you have read in the controller settings with the original firmware in the Canable session, don't write this settings to the EBiCS firmware after flashing! Start a new Canable session first and read in the default values of EBiCS!
 

Attachments

  • EBiCS_for_M820_v0.0023.zip
    30 KB · Views: 5
Last edited:
stancecoke, I see you're busy with the M820 firmware, but perhaps you have a minute to make a small fix for the M560.

I occasionally had an issue with the motor jerking after I stopped pedaling. With firmware 0.008, I figured out what the problem was. Sometimes the torque sensor readings without a load increase, apparently as the motor heats up. I've seen readings over 760 mV without a load in the calorie field. In this situation, after I stop pedaling, the motor starts up with the slightest forward pedaling motion, which happens constantly on downhills. Could you increase the torque sensor threshold to 800 mV? This won't be noticeable while riding, even the slightest pedal pressure produces readings over 1000.

I also have another suggestion. One of the disadvantages of Bafang engines is the sharp reaction to the throttle trigger. This issue can be partially solved by increasing the maximum throttle voltage, but in this case the full power will not be available. Adding a curve showing the output current as a function of throttle trigger position, say, 5 points, should solve this problem.
 
Adding a curve showing the output current as a function of throttle trigger position, say, 5 points, should solve this problem.
I don't understand this issue. I don't know, how linear the throttle output is with the throttle position and if it is the same for all common throttles. Which behaviour do you want?! Of course we could add a nonlinear function or a lookup table for the throttle value vs the iq setpoint. But will this be an improvement really? I can't believe. I know, the VESC offers this feature, but I never used it. The max motor current in throttle and torque sensor operation is scaled for each level and settable in the full assist tab already.
 
Last edited:
VESC also has duty cycle control mode when you can control RPM using the throttle instead of controlling current.
 
Last edited:
I also noticed a similar problem in the m560, in some rare cases the motor continued to run even without pedaling, probably due to slightly erroneous readings from the torque sensor. I think the throttle itself works fine.
 
I don't understand this issue. I don't know, how linear the throttle output is with the throttle position and if it is the same for all common throttles. Which behaviour do you want?! Of course we could add a nonlinear function or a lookup table for the throttle value vs the iq setpoint. But will this be an improvement really? I can't believe. I know, the VESC offers this feature, but I never used it. The max motor current in throttle and torque sensor operation is scaled for each level and settable in the full assist tab already.
min throttle voltage - 0% current
20% voltage - 35% current
40% - 50%
60% - 60%
80% - 70%
max throttle voltage - 100% current

The first column is fixed, the second one except the first row is adjustable.
 
Hm, do you really think, you can feel a difference with this slight changes?! I can't believe that.🤷‍♂️
Is there any measured data of throttle position vs. throttle voltage?
Some threads can be found here in the forum:
https://endless-sphere.com/sphere/threads/how-to-change-my-throttle-output-signal.37190/
https://endless-sphere.com/sphere/t...hrottle-to-limit-top-speed.99894/post-1679211
But I don't know if this is typical.
I can send the throttle raw value to the cal field, if some volunteers are willing to measure and post their curves...
 
Last edited:
A throttle curve is used to increase the throttle resolution in certain areas of the throttle travel at a cost of lowering the resolution in other areas. For example you can set it so that you have more resolution at low power and less at high power, it can be useful in high powered vehicles to make them more controllable.
 
of the throttle travel
Therefore we need measurements first, as the voltage signal will not be linear with the throttle travel.
I would prefer a mathematical function instead of a look up table, as the look up table would need quite many user settable parameters.

Could you increase the torque sensor threshold to 800 mV
Done, but not tested, as I have the M820 on my test bike at the moment. I've added some improvements from the M820 branch also. The CAL field shows the raw ADC data of the throttle signal now. Would be great, if you could provide a graph with throttle ADC counts vs. throttle travel.
 

Attachments

  • EBiCS_for_M560_BL38_v0.0081.zip
    30.1 KB · Views: 6
Last edited:
you can install it. If something goes wrong, there should be no problem reverting to the original software.
Next question, can I upload soft for 52V while my battery is 48V so the controller rated for 52v nominal battery would auto shut down too early with about 50% charge remaining of the 48V battery. What parameter should I change in bafang_canable_pro?
 
Would be great, if you could provide a graph with throttle ADC counts vs. throttle travel.
I've tested a standard Chinese thumb throttle on my M820, and the results are sobering.
The Bafang controller apparently does not use a voltage divider to scale down from the 5V level to the 3.3V level.
This further reduces the already limited travel of the thumb throttle. The usable range lies between 20% and 70% of the mechanical travel. So half of the mechanical travel is wasted.
I don't know if original BAFANG throttles show a better behaviour.

1779818129661.png

1779818524700.png
1779818615193.png
 
Last edited:
Hm, do you really think, you can feel a difference with this slight changes?! I can't believe that.🤷‍♂️
Is there any measured data of throttle position vs. throttle voltage?
Some threads can be found here in the forum:
https://endless-sphere.com/sphere/threads/how-to-change-my-throttle-output-signal.37190/
https://endless-sphere.com/sphere/t...hrottle-to-limit-top-speed.99894/post-1679211
But I don't know if this is typical.
I can send the throttle raw value to the cal field, if some volunteers are willing to measure and post their curves...
I think it'll be possible to find a curve that will improve throttle modulation. I've provided the numbers as an example.
Yes, the throttle response with the Hall sensor is the same as in your link, I took an oscillogram.
 
I think it'll be possible to find a curve that will improve throttle modulation
To be honest, I have no motivation, as I see no realistic chance to achieve significant improvements. The really usable throttle travel is so small, you will never be able to set the trottle position exactly, especially during riding offroad... So it is irrelevant if you get the needed torque from the motor at 55% throttle travel or at 60% throttle travel... If you provide a real measurement, that your throttle has a way bigger usable travel, we can talk again ;)
 
Last edited:
To be honest, I have no motivation, as I see no realistic chance to achieve significant improvements. The really usable throttle travel is so small, you will never be able to set the trottle position exactly, especially during riding offroad... So it is irrelevant if you get the needed torque from the motor at 55% throttle travel or at 60% throttle travel... If you provide a real measurement, that your throttle has a way bigger usable travel, we can talk again ;)
ChatGPT Image 27 мая 2026 г., 20_27_00.png
Done.

It's strange that I saw a different curve on the oscilloscope, not a linear one. And the multimeter shows different voltages. Perhaps the signal is already being converted in the controller.

I think we can sacrifice sensitivity at the beginning of the stroke to improve sensitivity at higher power. For example, let's make the power at 1000 mV the same as it is now at 1500-2000 mV, and then linearly from there. Probably a dependence with one turning point, which can be adjusted for both voltage and power, will be sufficient.
 
Is the y-axis of your graph the value from the cal field? This are not mV but ADC counts. 12 bit ADC resolution so 3.3V are 4095 ADC counts. The M560 will have a voltage divider from 5V to 3.3V
 
Is the y-axis of your graph the value from the cal field? This are not mV but ADC counts. 12 bit ADC resolution so 3.3V are 4095 ADC counts. The M560 will have a voltage divider from 5V to 3.3V
Yes, the value from the cal field.
 
Back
Top