KT motor controllers -- Flexible OpenSource firmware for BMSBattery S/Kunteng KT motor controllers (0.25kW up to 5kW)

casainho

10 GW
Joined
Feb 14, 2011
Messages
6,047
25-1.png


index.php


This OpenSource firmware/embedded software runs on the BMSBattery S/Kunteng KT motor controllers.
The main advantages of this controllers are:
1. sine wave/"FOC": motors run silent and very efficient
2. cheap: starting at 20€
3. widely available: many online shops sell them and ship to worldwide
4. various sizes e powers: from 0.25kW up to 5kW (72V, 70A, 24 mosfets)
5. supports LCD and Bluetooth mobile app
6. schematic available: important if you want to mod/hack, repair or even build your own controller
7. generic brushless motor controller: can work as a generic brushless motor controller and be commanded by an Arduino or other similar external microcontroller board. This feature is only available when using our OpenSource firmware.

The main advantages of our OpenSource firmware/embedded software are:
1. sine wave/"FOC" max efficiency: motors run silent and very efficient and is possible to tune a parameter to get the best efficiency possible for a specific motor
2. flexible and OpenSource: more than customization, any feature for some specific need can be easily implemented and for free (no need to pay any commercial license)
3. throttle, PAS and torque sensor: all of them are supported
4. LCD3 and LCD5: all of them are supported
5. regen/ebrake: works very well on the direct drive motors
6. motor torque controller/motor current controller
7. motor speed controller
8. generic brushless motor controller: using the UART connection (the same that is used for the LCD and Bluetooth module), this controller can work as a generic brushless motor controller. Can be commanded by an Arduino or other similar external microcontroller board.

Please read the project status, how to install the firmware, history and motivation on his website: https://opensourceebikefirmware.bitbucket.io/windows_instructions/
 
I updated the documentation notes file and seems that S06S controller continually output by UART serial port the information that is presented on the LCD controller. Also the LCD controller sends back some configurations to the S06S controller. I bet we can just connect a UART Bluetooth serial module instead of the LCD controller and make a simple Android to control the S06S controller!!

Thanks to the guy that documented the S06S LCD communications protocol:

Screenshot from 2017-04-26 15-35-05.png
 
I did install the SDCC compiler for STM8 and I prepared the main.c and gpio.c files, they build correctly but I do not have yet a board to test.
I also have the OpenOCD scripts that will let me flash and debug the firmware.
I did commit the code to: https://github.com/OpenSource-EBike-firmware/BMSBattery_S_controllers_firmware

I also updated the notes file with more information, mainly about how to install the development and attached some datasheets and application notes about motor control with STM8. This notes file about now about 80MBytes and can be found here: https://github.com/OpenSource-EBike-firmware/documentation/blob/master/OpenSource_EBike_firmware-CHERRYTREE_xml.ctd
file.php
 

Attachments

  • Screenshot from 2017-04-30 18-34-52.png
    Screenshot from 2017-04-30 18-34-52.png
    59.4 KB · Views: 89,857
I received the cheap dev board with STM8 and started to put the development tools together. Finally I just run the printf() example I found on github. So for now I was able to compile C code, debug it (breakpoints, step run, see/change variables in real time, inspect/write memory) and send strings over UART serial -- like original BMSBaterry S series controller do when communicating the S LCDs.

The firmware sources are here: https://github.com/OpenSource-EBike-firmware/BMSBattery_S_controllers_firmware

Debug STM8 using STlinkV2, OpenOCD and Eclipse:
file.php


The printf() output over UART serial:
file.php
 

Attachments

  • z01.png
    z01.png
    82.2 KB · Views: 89,850
  • z02.png
    z02.png
    9.8 KB · Views: 98,281
On the last days I also finished a robust version of the firmware for FOC motor control of electric unicycles, that should also run on the hoverboards since they use the same microcontroller and IMU ics. I will use this firmware as a base for the EBike controllers.

The firmware was feature yesterday on the popular news site HACKADAY: http://hackaday.com/2017/05/07/open-source-firmware-for-hoverboards/

Video showing the firmware in action, controlling a motor using FOC. The motor is a direct drive one:
[youtube]2nDZW4-uVys[/youtube]
 
Finally found a way to have the documentation on the note taking software CherryTree but also have it on github as HTML page, this means google should be able to index it and users will found all the documentation when searching for it, like the main keyworks.

My workflow will be like this: keep improving the CherryTree notes file and commit the source file to github - anyone can submit an improvement/pull request to that file. At the same time, export from CherryTree to HTML and commit also to github, the output will be an update HTML page on the link: https://opensource-ebike-firmware.github.io/Controllers--BMSBattery_S06x--various_info--01.html

Screenshot from 2017-05-08 12-50-08.jpg
 
Today I received the BMSBattery S06S controllers and I was able to program and debug a first firmware that sends some strings over the wires that goes to the LCD :)

I am updating my notes/documentation for this project here: https://opensourceebikefirmware.bitbucket.io/About_the_project.html

View attachment 4
2017-05-18 16.42.07.resized.jpg

View attachment 2
View attachment 1
 
izeman said:
that's awesome work and i follow it closely. do you think there are more powerful controllers that may use your software (when it's done) as well? 250w is so super tiny ... ;)
Thank you for your feedback!! This forum misses a "like" button.

All the S series controllers from BMSBattery, the ones with 6 and 12 mosfets. Seems the more powerful they have is: "S12SN 72V1KW 40A Torque Simulation Sine Wave Controller".

This controller seems to be developed by http://www.szktdz.com/en/. Potential for this firmware work or be ported easily for every controller done with STM8S. Seems that GreenEBikeKit also sells the same controllers under different name.
 
izeman said:
that's awesome work and i follow it closely. do you think there are more powerful controllers that may use your software (when it's done) as well? 250w is so super tiny ... ;)

All the S-series controllers from BMSBattery and probably all the modern KT controllers.
 
d8veh said:
izeman said:
that's awesome work and i follow it closely. do you think there are more powerful controllers that may use your software (when it's done) as well? 250w is so super tiny ... ;)

All the S-series controllers from BMSBattery and probably all the modern KT controllers.
Hi d8veh. I found a lot of your messages helping, since some years ago, on this forum and others -- many respect for you. Really happy that you following this thread.

Can you please point a webshop for that KT controllers? I am listing here the alternative controllers with STM8: https://opensourceebikefirmware.bitbucket.io/Controllers.html

I updated the documentation (https://opensourceebikefirmware.bitbucket.io/About_the_project.html) and here is the news:

New information I have:

STM8S105C6T6
• pin 2: resistor 2200R and a capacitor to GND. This pin goes to blue wire of undocumented connector with 2 wires (blue and black(GND))
• pin 3: resistor 2200R and a capacitor to GND. Resistor 10k to VCC. This pin goes to a header pin that is not wired
• pin 10: through one resistor, goes to BRAKE wire
• pin 15: ?? goes to a big diode..
• pin 16: goes to pin 7 LM358 (shunt current)
• pin 17: goes to pin output of ACS711 (phase B current)

Components:
• LM317 (voltage regulator): outputs 14.8V for the mosfet drivers
• 78L05 (voltage regulator): outputs 5V for the VCC
• LM358 (dual op-amp):
◇ pin7: outputs a value from shunt current (motor total current)
▪ 1.63V when motor is stopped (zero current)
◇ pin1: always at 5V. Seems it is not used and is connected to 5V with a resistor
• ACS711 (hall effect current sensor):
◇ output pin goes through a resistor (and a capacitor to ground) to pin STM8S105C6T6 pin 17 PB5/AIN5. This measures a motor phase B current (green phase wire)
▪ 2.5V when motor is stopped (zero current)
◇ fault pin is not used and is connected with a resistor to 5V

Possible motor control scheme
his cheap controllers are a 2nd generation. The first generation like BMSBAttery KU63 did only 6 step and had a shunt to measure motor total current, so no FOC nor current in phase +90º with rotor that is one of the main objectives of FOC.
The 2nd generation controllers, costs only 22€ but added a current sensor ACS712 in ne phase. Now the wave forms are sinusoidal and I think they look at the phase current to make it in +90º in phase with rotor, achieving the most important objective of FOC that is keep the motor rotating using the less energy possible (that will also result in more silent motor) -- amazing for a controller that came inside a metal box and water prof and just by 22€. It uses hall sensors only for track motor rotor position.

Explanation example of AN-8201 FCM8201 Three-Phase Sine-Wave BLDC Motor Controller document:

At 2., the middle HA is where motor flux is and the I phase current must be at 90º after, but as seen, there is a delta teta added to the point of 90º (that point is at end of positive value of HA).
At 3., the motor phase voltage (that we control) we advance delta teta and the result is the phase current will now be at 90º of middle HA (flux) -- this is the point where the motor will have more torque per amp used, the most efficient point:
234.png
 
Hummm... For sure looks a good controller for 22 bucks. But it is a bit far from FOC.
Yes, by maintaining rotor wingdings orthogonal to the stator field you get maximum Torque production.
However, what this enhanced controller does is that it is always trying to apply a correction to fix the angle issue that the controller itself created (confusing, right?).

FOC starts with the separation of two basics things: Magnetic Flux and Torque. So, FOC already has a pretty good estimation of the angle at any given moment (not only to maintain at 90, but to change that when doing Field-Weakening)
If you do not have those two vectors, you are basically in speed control. Some are more fancy them others, but they are still Speed controllers.

Correction: The angle estimation is unknown at standstill (if sensorless). We have to use some other fancy stuff to go around the motor startup.
 
Hackaday, the popular website for Hacking news, did a quick reference to this project: http://hackaday.com/2017/05/21/hackaday-links-may-21-2017/
Is not 100% correct but I think is ok and the readers can always follow the link to here and get the full information.

file.php
 
I finish the code that reads the hall sensors signals - using a pin change interrupt. The firmware is here: https://github.com/OpenSource-EBike-firmware/BMSBattery_S_controllers_firmware

#define EXTI_PORTE_IRQHANDLER 7
void EXTI_PORTE_IRQHandler(void) __interrupt(EXTI_PORTE_IRQHANDLER)
{
unsigned char hall_sensors = 0;

// read hall sensors signal pins and mask other pins
hall_sensors = (GPIO_ReadInputData (HALL_SENSORS__PORT) & (HALL_SENSORS_MASK)); // sequence: 1; 3; 2; 6; 4; 5

printf("%d\n", hall_sensors);
}

int main()
{
uart_init ();
hall_sensor_init ();
enableInterrupts ();

while (1) ;
}
 
I did finish taking the information about pin connections to STM8 so I can start writing the firmware. I also documented the PWM signals including the dead time, please see here:

https://opensourceebikefirmware.bitbucket.io/Controllers--BMSBattery_S06x--S06S--PWM_signals.html

---
I got one S06P controller today and compared to S06S controller:

The S06P controller don't do sine wave motor control but just 6 steps. When compared to the S06S controller:
• don't have the phase current sensor (the place is on the board but is empty)
• have a secondary board that connects to the same pins as hall sensors:
◇ have a STM8S003F3P6 (with available header to be programmed)
◇ have an LM339
◇ I would say this board does BEMF signal output to simulate the hall sensors signal, just like on the KU63 controller circuit

See the pictures here: https://opensourceebikefirmware.bitbucket.io/Controllers--BMSBattery_S06x--S06P.html
 
Got the ADC (pooling for now) working, reading throttle value.
Also got PWM working (PWM symetric to come) and duty_cycle value is defined by throttle value:

Code:
int main()
{
  //set clock at the max 16MHz
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

  gpio_init ();
  brake_init ();

  // hold here while brake is pressed -- this is a protection for development
  while (!brake_is_set()) ;

  uart_init ();
  hall_sensor_init ();
  pwm_init ();
  adc_init ();
  enableInterrupts();

  while (1)
  {
    static uint16_t adc_value;
    adc_value = adc_read_throttle ();
    pwm_set_duty_cycle_channel1 (adc_value);
    pwm_set_duty_cycle_channel2 (adc_value);
    pwm_set_duty_cycle_channel3 (adc_value);
  }
}

Next I need to put PWM working symmetric and adding the dead-time. Then I will have all the tools to run the motor:
- hall sensors working
- PWM working
- throttle working
 
Good work! Thank you!
Please don't forget PAS sensor, trottle is not legal in Europe.


Sent usingTapatalk
 
Steu851 said:
Good work! Thank you!
Please don't forget PAS sensor, trottle is not legal in Europe.
It is far to be finished, as for be with the same quality as original firmware, I need to look at phase sensor current and find a way to adapt it to be synchronize with rotor position (hall sensors signal).

I don't use PAS, maybe you can help if you say what could be the algorithm for PAS. I know that PAS is connected to a digital only input pin to the STM8, so firmware will only be able to read it as being ON or OFF state.
 
PAS a hall sensor, used to know if pedals are tuning, one feature not present in the original firmware nice to have is to enable trottle only if pedals are tuning, always, not only when starting, we are used to call this feature 'gas legalizer'.
All legal ebike here doesn't have trottle at all, we can choose motor assistance only from display.
I apologize for my bad english.

Sent usingTapatalk
 
Got the motor running!! But for now just as 6 steps (the same as KU63, JUxx controllers), just like the original firmware does when starts the motor and run it at very low speeds, before commutate to sinewave.
As you can see on the video, throttle and brake works. I didn't implement any current control yet, I think I will leave that to the end of the project while I am running with a lab power supply.

Project documentation here: https://opensourceebikefirmware.bitbucket.io/About_the_project.html

Firmware sources: https://github.com/OpenSource-EBike-firmware/BMSBattery_S_controllers_firmware

[youtube]mrmxdoyH_Hs[/youtube]

The code as of today:

Code:
int main()
{
  //set clock at the max 16MHz
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

  gpio_init ();
  brake_init ();
  while (brake_is_set()) ; // hold here while brake is pressed -- this is a protection for development
  uart_init ();
  hall_sensor_init ();
  pwm_init ();
  adc_init ();
  enableInterrupts();

  hall_sensors_read_and_action (); // needed to start the motor

  while (1)
  {
    static uint16_t adc_value;
    adc_value = adc_read_throttle ();
    adc_value = map (adc_value, ADC_THROTTLE_MIN_VALUE, ADC_THROTTLE_MAX_VALUE, 0, 1023);
    pwm_value = adc_value;

    pwm_set_duty_cycle_channel1 (pwm_value);
    pwm_set_duty_cycle_channel2 (pwm_value);
    pwm_set_duty_cycle_channel3 (pwm_value);
  }
}

// HALL SENSORS signal
void EXTI_PORTE_IRQHandler(void) __interrupt(EXTI_PORTE_IRQHANDLER)
{
  hall_sensors_read_and_action ();
}

void hall_sensors_read_and_action (void)
{
  unsigned char hall_sensors = 0;

  // read hall sensors signal pins and mask other pins
  hall_sensors = (GPIO_ReadInputData (HALL_SENSORS__PORT) & (HALL_SENSORS_MASK));

  switch (hall_sensors)
  {
    case 3:
      pwm_phase_a_enable_pwm ();
      pwm_phase_b_disable ();
      pwm_phase_c_enable_low ();

    break;

    case 1:
      pwm_phase_a_enable_pwm ();
      pwm_phase_b_enable_low ();
      pwm_phase_c_disable ();
    break;

    case 5:
      pwm_phase_a_disable ();
      pwm_phase_b_enable_low ();
      pwm_phase_c_enable_pwm ();
    break;

    case 4:
      pwm_phase_a_enable_low ();
      pwm_phase_b_disable ();
      pwm_phase_c_enable_pwm ();
    break;

    case 6:
      pwm_phase_a_enable_low ();
      pwm_phase_b_enable_pwm ();
      pwm_phase_c_disable ();
    break;

    case 2:
      pwm_phase_a_disable ();
      pwm_phase_b_enable_pwm ();
      pwm_phase_c_enable_low ();
    break;

    default:
    return;
    break;
  }
}
 
While trying to run the motor with a more advanced control scheme that original firmware does (a scheme that is used with FOC motor control) I found some issues that I tough could be because of motor and started this thread: https://endless-sphere.com/forums/viewtopic.php?f=2&t=88651

The questions are: why the original firmware of S06S (sinewave) controller makes the motor rotating with more noise, takes more current and the power supply voltage increases when a more simple way to run the motor with 6 steps (my OpenSource firmware) make less noise, uses less current and the power supply voltage don't change as expected????
And another way to control that I am trying with my OpenSource firmware, that is even more advanced than sinewave, gives even much more noise, takes much more current and increases much more the power supply voltage!!

I have some clues. The mosfet drivers are called "inverter" and as on DC-DC converters, the inverter can boost the voltage make it much more bigger than input voltage. The 6 steps uses PWM ON and the low side mosfets are always ON/OFF with no PWM. The other modes like sinewave have PWM in low side mosfets... I think the issue is a wrong dead time between high side mosfets and low side, that short circuit the motor windings or mosfets a bit and that will boost the voltage (thanks also to the capacitors on the board) but also asks more current from the power supply and will make the noise on the motor.

I will try to play with the dead time configuration on the firmware/microcontroller but I don't know what are the limitations of the board/mosfet drivers...

I already measured the dead time of original firmware:
33-12.png


33-13.png


More info here: https://opensourceebikefirmware.bitbucket.io/Controllers--BMSBattery_S06x--S06S--PWM_signals.html
 
KingQueenWong said:
How many wires should be connected?My motor failed to rotate. :(
Hi KingQueenWong!! Nice to see you here also!!!

Can you please elaborate a bit more so I can help you?? Like: do you have the same S06S controller? which motor are you using? where you able to flash the firmware??
 
Motor: Q100 rear driving E-bike Hub Motor
Voltage:24V
Power:350W
RPM:328
Hall sensor and sensorless Waterproof

Controller:
S06S 250W Torque Simulation Sine Wave Controller

You can enlarge the pictrue for more connected wires detail.
 
Back
Top