TSDZ2 mid drive with 860C, 850C or SW102 displays only -- Flexible OpenSource firmware (Casainho code only)

Hi, I have installed this firmware hex files (v03 for 36 V ) on my bike with an 36 V motor and batterie. The power is amazing.
Thank You Casainho for this great developement.
I am wondering why the power is quite strong also on assist level 1. Is ther anything I can change in the configuration? I would like to do more myself in low assist levels, 4 and 5 are in a good range.

Maybe I have to compile the firmware and try other parameters.
Is there a Wiki how to do it from scratch? My only experiance is programming from 3D printer firmware.

Best Regards

Dirk
 
Just use lower power 250w is ok on level 2
Hold down both top and on off buttons to change

This is in config.h
Code:
#define MOTOR_ASSISTANCE_CAN_START_WITHOUT_PEDAL_ROTATION 1
#define ASSIST_LEVEL_0      0.0
#define ASSIST_LEVEL_1      1.0
#define ASSIST_LEVEL_2      2.0
#define ASSIST_LEVEL_3      3.0
#define ASSIST_LEVEL_4      6.0
#define ASSIST_LEVEL_5      9.0
 
I set mine to 500w will try 250 w
For me the response is like : n
Level 1: 5
Level 2: 6
.
.
Level 5 : 9

Is there any help page / Video ... how to get this firmware compiled myself on Windows?
 
Hi Dirk, compiling the code isn't hard at all. I've made some fixes in the makefile for compiling on Windows, those changes are already in the latest master branch of the project.

1. Install SDCC (https://sourceforge.net/projects/sdcc/files/sdcc-win64/3.6.0/). Make sure to add it to your PATH (option in one of the last screens of the installer... )
2. Git clone the project or download zip file: https://github.com/OpenSource-EBike-firmware/TongSheng_TSDZ2_motor_controller_firmware (make sure to get the master branch)
3. Use any text editor to make the desired firmware changes. Also take note of the motor type you're using (36v/48v).
4. Double-click Start_Compiling.bat to compile the firmware. The resulting main.ihx can be programmed in the controller.
 
Casainho and endless cadence I did some more testing and I reduced the foc angle down from 130 to 100 on my 36v

At 100 I still had 110+ cadence but the power was not consistent at 550w it was bouncing between 550 and 475 using level 5 and around 36km/hr.

I would say about 112 may be the sweet spot but I've had enough flashes for today ;)

Dirko. There are many videos on how to setup eclipse on Windows. Don't worry about open ocd. But you need to get sdcc
The processor is stm8
Don't bother trying to build in eclipse since it's only acting as an editor. To build set up an external process to the file startcompiling.bat
On the first line of the bat file are all your paths. Make sure you fix this line if you don't use those paths. Have your stlink connected and it will also flash for you.

question to endless caidence
are you getting all these errors on compile ?
Code:
C:\TongSheng_TSDZ2_motor_controller_firmware-master>PATH = C:/Program Files/Java/jre1.8.0_181/bin/server;C:/Program Files/Java/jre1.8.0_181/bin;C:/Program Files/Java/jre1.8.0_181/lib/amd64;C:\Program Files (x86)\STMicroelectronics\st_toolset\asm;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Users\tayla\AppData\Local\Microsoft\WindowsApps;C:\adb;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility;C:\Users\tayla\AppData\Local\Microsoft\WindowsApps;;C:\Users\tayla\Desktop;;C:\Program Files (x86)\STMicroelectronics\st_toolset\stvp;C:\sdcc_eclipse\sdcc\bin;C:\TongSheng_TSDZ2_motor_controller_firmware-master\tools\cygwin\bin 

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del main.hex 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\main.hex

C:\TongSheng_TSDZ2_motor_controller_firmware-master>make -f Makefile_windows clean 
Cleaning files...

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.obj 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.obj

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.lsr 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.lsr

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.lst 

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.cod 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.cod

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.sym 

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.s19 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.s19

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.err 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.err

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.grp 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.grp

C:\TongSheng_TSDZ2_motor_controller_firmware-master>del *.map 
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.lst
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.sym
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.map
Could Not Find C:\TongSheng_TSDZ2_motor_controller_firmware-master\*.elf
Done.

C:\TongSheng_TSDZ2_motor_controller_firmware-master>make -f Makefile_windows 
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -owatchdog.c watchdog.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -otorque_sensor.c torque_sensor.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -ouart.c uart.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -opwm.c pwm.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -omotor.c motor.c
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp129. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp18. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp18. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp18. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp18. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp0. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp0. Please contact sdcc authors with source code to reproduce.
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -owheel_speed_sensor.c wheel_speed_sensor.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -obrake.c brake.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -opas.c pas.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -oadc.c adc.c
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp5. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp5. Please contact sdcc authors with source code to reproduce.
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -otimers.c timers.c
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -oebike_app.c ebike_app.c
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp56. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp56. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp56. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp56. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp1. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp1. Please contact sdcc authors with source code to reproduce.
ebike_app.c:319: warning 116: left shifting more than size of object changed to zero
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp25. Please contact sdcc authors with source code to reproduce.
sdcc -c -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  -outils.c utils.c
sdcc -IStdPeriphLib/inc -I.  -mstm8 -Ddouble=float --std-c99 --nolospre --out-fmt-ihx --debug  main.c StdPeriphLib/src/stm8s_iwdg.rel StdPeriphLib/src/stm8s_itc.rel StdPeriphLib/src/stm8s_clk.rel StdPeriphLib/src/stm8s_gpio.rel StdPeriphLib/src/stm8s_uart2.rel StdPeriphLib/src/stm8s_tim1.rel StdPeriphLib/src/stm8s_tim2.rel StdPeriphLib/src/stm8s_tim3.rel StdPeriphLib/src/stm8s_exti.rel StdPeriphLib/src/stm8s_adc1.rel watchdog.rel torque_sensor.rel uart.rel pwm.rel motor.rel wheel_speed_sensor.rel brake.rel pas.rel adc.rel timers.rel ebike_app.rel utils.rel
main.c:120: warning 126: unreachable code
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp0. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp0. Please contact sdcc authors with source code to reproduce.

C:\TongSheng_TSDZ2_motor_controller_firmware-master>STVP_CmdLine -BoardName=ST-LINK -ProgMode=SWIM -Port=USB -Device=STM8S105x6 -FileProg=main.ihx -verbose -no_loop 
STVP_CmdLine version 1.6

Verbose ON
Log activity ON
Display Progress OFF
Message box warning if protect option byte ON
Loop mode OFF
Erase device OFF
Blank check device OFF
Verify device ON

Configuration:
BoardName=ST-LINK ST-LINK_ID=0 Device=STM8S105x6 Port=USB ProgMode=SWIM

>>> Filling PROGRAM MEMORY image in computer with Blank Value
<<< Filling PROGRAM MEMORY image in computer succeeds

>>> Loading file main.ihx in PROGRAM MEMORY image in computer
<<< Loading file succeeds

Hit 'Esc' key to abort during communication.

>>> Programming PROGRAM MEMORY
<<< Programming PROGRAM MEMORY succeeds

>>> Verifying PROGRAM MEMORY
<<< Verifying PROGRAM MEMORY succeeds

C:\TongSheng_TSDZ2_motor_controller_firmware-master>pause
Press any key to continue . . .
 
Hi jbalat, no not all these errors. But I get those warnings too. Please get the latest master as this would fix some of those errors. I've also removed the programming at the end because I prefer to use STVP (GUI) for that. And the script is called Start_Compiling (not programming) after all :D
 
EndlessCadence said:
Hi jbalat, no not all these errors. But I get those warnings too. Please get the latest master as this would fix some of those errors. I've also removed the programming at the end because I prefer to use STVP (GUI) for that. And the script is called Start_Compiling (not programming) after all :D
I think wiki is open for editing by anyone that has a github ID, would be great if any of you guys could write instructions for Windows.
 
casainho said:
EndlessCadence said:
Hi jbalat, no not all these errors. But I get those warnings too. Please get the latest master as this would fix some of those errors. I've also removed the programming at the end because I prefer to use STVP (GUI) for that. And the script is called Start_Compiling (not programming) after all :D
I think wiki is open for editing by anyone that has a github ID, would be great if any of you guys could write instructions for Windows.
I'll do that!
 
Thank you for helping!
will try to get the new version in
in the "config-example.h" the values where quite different :

#define ASSIST_LEVEL_0 0.00
#define ASSIST_LEVEL_1 0.44
#define ASSIST_LEVEL_2 0.66
#define ASSIST_LEVEL_3 1.00
#define ASSIST_LEVEL_4 1.5
#define ASSIST_LEVEL_5 2.25

will try and reply this evening!
 
casainho said:
I think wiki is open for editing by anyone that has a github ID, would be great if any of you guys could write instructions for Windows.

When I tried I think I was allowed to edit but not upload any photos.
 
EndlessCadence said:
casainho said:
I think wiki is open for editing by anyone that has a github ID, would be great if any of you guys could write instructions for Windows.
I'll do that!
You can copy-paste from the tutorial of the Kunteng-Project ;)

https://opensourceebikefirmware.bitbucket.io/windows_instructions/

regards
stancecoke
 
hey guys just trying to understand the code in ebike_app.c

I assume this is where we scale up the power based on cadence ?

Code:
 // cadence percentage (in x256)
  ui16_temp = (((uint16_t) ui8_pas_cadence_rpm) << 8) / ((uint16_t) PAS_MAX_CADENCE_RPM);
  // limit the calculated value to be no more than PAS_MAX_CADENCE_RPM x256
  if (ui8_pas_cadence_rpm > PAS_MAX_CADENCE_RPM)
  {
    ui16_temp = ((uint16_t) PAS_MAX_CADENCE_RPM) << 8;
  }

  // human power: pedal torque * pedal cadence
  // do not apply human power with lower cadence
  if (ui8_pas_cadence_rpm > 25)
  {
    // calc human power
    ui8_pedal_human_power = ((((uint16_t) ui8_torque_sensor) * ui16_temp) >> 8);

    // now scale human power with assist level
    f_temp = ((float) ui8_pedal_human_power) * f_get_assist_level ();
 
jbalat said:
hey guys just trying to understand the code in ebike_app.c

I assume this is where we scale up the power based on cadence ?

Code:
 // cadence percentage (in x256)
  ui16_temp = (((uint16_t) ui8_pas_cadence_rpm) << 8) / ((uint16_t) PAS_MAX_CADENCE_RPM);
  // limit the calculated value to be no more than PAS_MAX_CADENCE_RPM x256
  if (ui8_pas_cadence_rpm > PAS_MAX_CADENCE_RPM)
  {
    ui16_temp = ((uint16_t) PAS_MAX_CADENCE_RPM) << 8;
  }

  // human power: pedal torque * pedal cadence
  // do not apply human power with lower cadence
  if (ui8_pas_cadence_rpm > 25)
  {
    // calc human power
    ui8_pedal_human_power = ((((uint16_t) ui8_torque_sensor) * ui16_temp) >> 8);

    // now scale human power with assist level
    f_temp = ((float) ui8_pedal_human_power) * f_get_assist_level ();
Yes!
 
Dirkro said:
Thank you for helping!
will try to get the new version in
in the "config-example.h" the values where quite different :

#define ASSIST_LEVEL_0 0.00
#define ASSIST_LEVEL_1 0.44
#define ASSIST_LEVEL_2 0.66
#define ASSIST_LEVEL_3 1.00
#define ASSIST_LEVEL_4 1.5
#define ASSIST_LEVEL_5 2.25

will try and reply this evening!
I need to delete config-example.h because it did make sense on KT firmware project but not for this one. The config.h should be take as example but needs the comments/explanations from config-example.h.
 
EndlessCadence said:
casainho said:
I need to delete config-example.h because it did make sense on KT firmware project but not for this one. The config.h should be take as example but needs the comments/explanations from config-example.h.
That's what I was thinking as well.
Maybe you can do it? I am short of time....
 
casainho said:
EndlessCadence said:
casainho said:
I need to delete config-example.h because it did make sense on KT firmware project but not for this one. The config.h should be take as example but needs the comments/explanations from config-example.h.
That's what I was thinking as well.
Maybe you can do it? I am short of time....
Yes I can do this tonight, no problem. Is config.h complete (except for the comments)?
 
EndlessCadence said:
Yes I can do this tonight, no problem. Is config.h complete (except for the comments)?
Yes it is.
 
EndlessCadence said:
Hi Dirk, compiling the code isn't hard at all. I've made some fixes in the makefile for compiling on Windows, those changes are already in the latest master branch of the project.


3. Use any text editor to make the desired firmware changes. Also take note of the motor type you're using (36v/48v).

Hi,
need help
I cannot find the switch for 36 V / 48 V
 
Dirkro said:
Hi,
need help
I cannot find the switch for 36 V / 48 V

@Dirkro, see information below. There is no switch. I believe the firmware defaults is for a 48v motor. I have to try the 36v settings (see comments in the code) too.

casainho said:
EndlessCadence said:
Can you elaborate a bit more if you have the time? I would like to know the correct settings for a 36v motor (and 48v battery).
I hope you understand on the comments and variable names:

Code:
void calc_foc_angle (void)
{
  uint16_t ui16_temp;
  uint32_t ui32_temp;
  uint16_t ui16_e_phase_voltage;
  uint32_t ui32_i_phase_current_x2;
  uint32_t ui32_l_x1048576;
  uint32_t ui32_w_angular_velocity_x16;
  uint16_t ui16_iwl_128;


  // FOC implementation by calculating the angle between phase current and rotor magnetic flux (BEMF)
  // 1. phase voltage is calculate
  // 2. I*w*L is calculated, where I is the phase current. L was a measured value for 48V motor.
  // 3. inverse sin is calculated of (I*w*L) / phase voltage, were we obtain the angle
  // 4. previous calculated angle is applied to phase voltage vector angle and so the
  // angle between phase current and rotor magnetic flux (BEMF) is kept at 0 (max torque per amp)

  // calc E phase voltage
  ui16_temp = ui16_adc_battery_voltage_filtered_10b * ADC10BITS_BATTERY_VOLTAGE_PER_ADC_STEP_X512;
  ui16_temp = (ui16_temp >> 8) * ui8_duty_cycle;
  ui16_e_phase_voltage = ui16_temp >> 9;

  // calc I phase current
  if (ui8_duty_cycle > 10)
  {
    ui16_temp = ((uint16_t) ui8_adc_battery_current_filtered_10b) * ADC_BATTERY_CURRENT_PER_ADC_STEP_X512;
    ui32_i_phase_current_x2 = ui16_temp / ui8_duty_cycle;
  }
  else
  {
    ui32_i_phase_current_x2 = 0;
  }

  // calc W angular velocity: erps * 6.3
  ui32_w_angular_velocity_x16 = ui16_motor_speed_erps * 101;

  // 36V motor: L = 76uH
  // 48V motor: L = 135uH
//  ui32_l_x1048576 = 142; // 1048576 = 2^20 | 48V
//  ui32_l_x1048576 = 80; // 1048576 = 2^20 | 36V

  // ui32_l_x1048576 = 142 <--- THIS VALUE WAS verified experimentaly on 2018.07 to be near the best value for a 48V motor,
  // test done with a fixed mechanical load, duty_cycle = 200 and 100 and measured battery current was 16 and 6 (10 and 4 amps)
  ui32_l_x1048576 = 142;

  // calc IwL
  ui32_temp = ui32_i_phase_current_x2 * ui32_l_x1048576;
  ui32_temp *= ui32_w_angular_velocity_x16;
  ui16_iwl_128 = ui32_temp >> 18;

  // calc FOC angle
  ui8_foc_angle = asin_table (ui16_iwl_128 / ui16_e_phase_voltage);

  // low pass filter FOC angle
  ui16_foc_angle_accumulated -= ui16_foc_angle_accumulated >> 4;
  ui16_foc_angle_accumulated += ui8_foc_angle;
  ui8_foc_angle = ui16_foc_angle_accumulated >> 4;
}
 
jbalat said:
Casainho and endless cadence I did some more testing and I reduced the foc angle down from 130 to 100 on my 36v

At 100 I still had 110+ cadence but the power was not consistent at 550w it was bouncing between 550 and 475 using level 5 and around 36km/hr.
When you have a good value (after testing), please say. Maybe we can include as an option to get higher cadence.
 
I understand a bit

would like to change the lines like this :

// 36V motor: L = 76uH
// 48V motor: L = 135uH
// ui32_l_x1048576 = 142; // 1048576 = 2^20 | 48V
// ui32_l_x1048576 = 80; // 1048576 = 2^20 | 36V

// ui32_l_x1048576 = 142 <--- THIS VALUE WAS verified experimentaly on 2018.07 to be near the best value for a 48V motor,
// test done with a fixed mechanical load, duty_cycle = 200 and 100 and measured battery current was 16 and 6 (10 and 4 amps)
// ui32_l_x1048576 = 142; // 48 V
ui32_l_x1048576 = 80; //36V

But in which file did you find it?
Cannot find the section
Thanks in advance

Dirk

P.S. found it , was in motor.c
 
I improved a bit more the communications, by reducing the package size but being able to increase the size however a small delay should happens for some configurations (delay should depend on the package size and errors when transmitting, I expect now a delay of no more than 2 seconds).

I release V0.4 and added this configurations to LCD3:

3: various
-- 0: motor voltage type: default value: 0. 0 for 48V brushless motor (TSDZ2 52V version has a 48V brushless motor); 1 for 36V brushless motor. This configuration is needed for FOC calculations.
-- 1: motor assistance startup without pedal rotation: default value: 0 (disabled). 1 to enable this feature. Recommended to keep at 0 value if you don't have brake sensors installed.
-- 2: PAS max cadence: default value: 110. This value is used for calculation of human pedal power that will after be used for motor assist level.

Full configurations: https://github.com/OpenSource-EBike-firmware/TSDZ2_wiki/wiki/TSDZ2-and-KT-LCD3-advanced-features-with-Flexible-OpenSource-firmwares

If you guys knows of more configurations, please ask here or on the issues list.
 
Can be showed the human power in the display instead of the istant power?
 
casainho said:
I improved a bit more the communications, by reducing the package size but being able to increase the size however a small delay should happens for some configurations (delay should depend on the package size and errors when transmitting, I expect now a delay of no more than 2 seconds).

I release V0.4 and added this configurations to LCD3:
....
Very nice! I've created a PR for you which contains the following changes:
  • Windows makefile fixed (yup, again :p )
  • Comments are now moved from config-example.h to config.h
  • Deleted config-example.h
 
Back
Top