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

Woohoo Success !!!!!!!!!!!!!
Thank You
:mrgreen:
 
jbalat said:
Woohoo Success !!!!!!!!!!!!!
Thank You
:mrgreen:
What you mean? Please explain...
 
Dude it's finally working exactly how I want it.
112 cadence and full power all the way to the end with graceful loss of power above 112rpm
550w at level 5 demanded and delivered by the motor at 40km/hr with a low battery and little pressure on the pedals. Awesome efficiency less than 220w at 32km/hr on a dual suspension mtb with nobby tires
And lots more ;)
 
jbalat said:
Dude it's finally working exactly how I want it.
112 cadence and full power all the way to the end with graceful loss of power above 112rpm
550w at level 5 demanded and delivered by the motor at 40km/hr with a low battery and little pressure on the pedals. Awesome efficiency less than 220w at 32km/hr on a dual suspension mtb with nobby tires
And lots more ;)
Can you please say what changes you did? Because I can use your values for #define a 36V motor.
 
All I changed was the FOC from 142 to 130
And the max over speed erps from 520 to 580 that’s all

I’m using the latest code.

It would be good to have that defined in the code since I’m not sure how many users will be able to compile their own versions.

I can’t believe how far we have gone with this in the past 2 weeks. I have been riding my tsdz2 every day. Today is the first day I did not flash it :)

Tomorrow I will ride my Trek again so I can compare
 
I released V0.3 of firmwares, mainly because now TSDZ2 motor controller firmware verifies if option bytes are correct and if not, flash it self the option bytes. To resume: no need to change option bytes, just erase the STM8 and flash the firmware!!
 
I wrote a few wiki pages about on how to install the LCD3 and TSDZ2 motor controller firmwares:

https://github.com/OpenSource-EBike-firmware/TSDZ2_wiki/wiki
 
@casainho
Nice work! I can't wait to try it...

I don't have the Tongsheng brakes nor the hidden brake wire (whatever it is called) can I use the opensource firmware? Or do you advice against it? I'm prepared to experiment but I wouldn't like the bike to run away from me :D
 
EndlessCadence said:
@casainho
Nice work! I can't wait to try it...

I don't have the Tongsheng brakes nor the hidden brake wire (whatever it is called) can I use the opensource firmware? Or do you advice against it? I'm prepared to experiment but I wouldn't like the bike to run away from me :D
The firmware can now be configured to immediately start the motor assist as soon you press the pedals OR as soon you press the pedals and you rotate a bit the pedals, this way avoid possible run away.

Anyway, I personally prefer to use the brake sensors and they can be the most cheap and popular used on ebikes.
 
yes its fairly safe to use without brakes, works just like the original firmware. My feeling is for safety this should be the default in the firmware since most people wont have ebrakes
 
Casainho do you now recommend to erase the memory before we flash ? Is this for all tabs ?
 
jbalat said:
yes its fairly safe to use without brakes, works just like the original firmware. My feeling is for safety this should be the default in the firmware since most people wont have ebrakes
You are right, I will make it the default. I am being trying to expand the options on LCD3 and that will also be there as also the motor type: 36 or 48V motor.

jbalat said:
Casainho do you now recommend to erase the memory before we flash ? Is this for all tabs ?
I recommend a full erase. On EEPROM the firmware checks for first address to have a key/specifc value and if not it will write default values to there. This means the user needs/should always configure after flashing.

Don't know what you mean by tabs.
 
casainho said:
EndlessCadence said:
@casainho
Nice work! I can't wait to try it...

I don't have the Tongsheng brakes nor the hidden brake wire (whatever it is called) can I use the opensource firmware? Or do you advice against it? I'm prepared to experiment but I wouldn't like the bike to run away from me :D
The firmware can now be configured to immediately start the motor assist as soon you press the pedals OR as soon you press the pedals and you rotate a bit the pedals, this way avoid possible run away.

Anyway, I personally prefer to use the brake sensors and they can be the most cheap and popular used on ebikes.
Can we configure this using the LCD3 display? I'm in the middle of converting to the LCD3 and the opensource firmware. Everything went fine so far but the bike tried to run away once, luckily I could grab the brakes but the display showed that the motor continued to deliver more then 100W! A power cycle fixed the issue but I would like to turn off MOTOR_ASSISTANCE_CAN_START_WITHOUT_PEDAL_ROTATION for safety as I don't have e-brakes. I didn't do a real ride yet but I can't wait! Need to mount everything securely first...

First impression, again I didn't do a ride yet: the display shows a lot of very useful data! Despite what others are saying, I already love it. Sorry guys :p
 
@casainho: I've just created a PR for you which fixes the makefile for Windows. Yes, I've started developing :D Mainly for changing some configuration for now.

Btw: what's the difference (in configuration) between the 36v and 48v hex files? I can't seem to really find this in the source...
Has something to do with the difference in characteristics due to difference in motor windings between 36v and 48v versions?
 
EndlessCadence said:
@casainho: I've just created a PR for you which fixes the makefile for Windows. Yes, I've started developing :D Mainly for changing some configuration for now.

Btw: what's the difference (in configuration) between the 36v and 48v hex files? I can't seem to really find this in the source...
Has something to do with the difference in characteristics due to difference in motor windings between 36v and 48v versions?
Yes, they are different there is a difference on motor.c when calc FOC angle.

I plan this to be configuramed on LCD3.
 
casainho said:
EndlessCadence said:
@casainho: I've just created a PR for you which fixes the makefile for Windows. Yes, I've started developing :D Mainly for changing some configuration for now.

Btw: what's the difference (in configuration) between the 36v and 48v hex files? I can't seem to really find this in the source...
Has something to do with the difference in characteristics due to difference in motor windings between 36v and 48v versions?
Yes, they are different there is a difference on motor.c when calc FOC angle.

I plan this to be configuramed on LCD3.
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).
 
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:
Dude it's finally working exactly how I want it.
112 cadence and full power all the way to the end with graceful loss of power above 112rpm
550w at level 5 demanded and delivered by the motor at 40km/hr with a low battery and little pressure on the pedals. Awesome efficiency less than 220w at 32km/hr on a dual suspension mtb with nobby tires
And lots more ;)
Do you recommend you settings for the 36V motor? did you use the "FOC value" that gave you more speed/pedal cadence or the one that you tested as having more torque??
 
Yes I would recommend my settings. Very happy with them

I was aiming for max power at Max cadence

With foc 80 I could not get more than 90 cadence and 440w when I set 550w
now with 130 foc I get 120 cadence and 550w

Also Max erps is 700
and Max Pas cadence is 130 so I can achieve max PWM duty of 254

I don't know if 130 is the best but I tried 100 and I still wasn't happy so somewhere between may be slightly better but I'm happy with this.
 
jbalat said:
now with 130 foc I get 120 cadence and 550w

With this you will get field weakening, and the additional power you see will be blown into heat, not into mechanical power (I guess :wink: )....

regards
stancecoke
 
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;
}
Thanks! This means that the coils of the motor are the only difference here (ui32_l_x1048576) ? Looking at the default settings in the firmware I must be using the wrong configuration for my 36V motor now :) Will fix this and see if it makes any difference in cadence as well.

@jbalat, are you using value 76 or 80 for ui32_l_x1048576? I believe you have a 36V motor as well?
edit: nope, in the other thread I found that you are using 130, why is that? :eek:
 
See my post above.
I set my power to 550w but it would only give me that power at low cadence. As I got to a higher cadence the power kept decreasing to like 440w.
Trial and error. Run a few tests for yourself and see.

I’m not planning on riding at 120 cadence since I know it won’t be efficient but I just didn’t want the motor to restrict me in any way
 
jbalat said:
Yes I would recommend my settings. Very happy with them

I was aiming for max power at Max cadence

With foc 80 I could not get more than 90 cadence and 440w when I set 550w
now with 130 foc I get 120 cadence and 550w

Also Max erps is 700
and Max Pas cadence is 130 so I can achieve max PWM duty of 254

I don't know if 130 is the best but I tried 100 and I still wasn't happy so somewhere between may be slightly better but I'm happy with this.

jbalat said:
See my post above.
I set my power to 550w but it would only give me that power at low cadence. As I got to a higher cadence the power kept decreasing to like 440w.
Trial and error. Run a few tests for yourself and see.

I’m not planning on riding at 120 cadence since I know it won’t be efficient but I just didn’t want the motor to restrict me in any way
Let's make some calculations based on other information we have:

A user verified that internal gear ratio is 41.9 (well, other user says it is instead 38!!). Both motors 36 or 48V are rate at max 4000RPM, I assume at nominal voltage 36V or 48V.
So 4000/41.4 = 96RPM cadence OR 4000/38= 105RPM. Can some one please verify the gear ration by looking at gears teeth numbers???0

Also, the motors has 8 pole pairs, so at the max nominal speed of 4000RPM, the ERPS is: (4000/60)*8 = 533 ERPS.

So, when jbalat changed the FOC calculation by using a different L (inductance) of the motor, the motor could than run at 4375 ((700/8)*60) (and a gear ratio of 33?? -- 4375/130). As stancecoke, it is with field weakening that we can get motor going at higher speeds than the max nominal rated speed, and in this case it will use more energy to do and will have less torque, and it is not running at max efficiency possible.

Also, I think this "deviation" of the best FOC angle is constant over all the speed range meaning it is has not the best efficiency possible in all the speed range.... because I think field weakening should be done only after the motor speed approach the max nominal speed.

Anyway, seems that jbalat got what it was looking for even at expense of some loss of efficiency (but how much loss? is that expressive? I don't know and maybe it is not other way would be an issue for jbalat).

So, I think I will keep the original values of:
Code:
  // 36V motor: L = 76uH
  // 48V motor: L = 135uH
//  ui32_l_x1048576 = 142; // 1048576 = 2^20 | 48V
//  ui32_l_x1048576 = 80; // 1048576 = 2^20 | 36V
And put a comment for the values of Jabalat as other users may want the same.

I think the 36V motor has lower L than the 48V motor. If Jabalat get field weakening with ui32_l_x1048576 = 130, this means the L is then for sure less than the 135uH of the 48V, maybe the 76uH is correct... Let's see if someone is able to test it.
 
It would be good to be able to change the FOC advance while you are riding with up and down arrows so we can optimise it.
Is this something someone can do on a separate branch?

set up the bike on a bike trainer.. Use another menu to set your throttle then change the advance at different loads, and speeds
 
Back
Top