emmebrusa / TSDZ2-Smart-EBike-1

TSDZ2 Open Source Firmware adapted to VLCD5-VLCD6-XH18 displays
GNU General Public License v3.0
141 stars 34 forks source link

Smooth Start feature and cleanups, closes #63 #73

Open dzid26 opened 1 year ago

dzid26 commented 1 year ago

Changes:

Smooth start

Why Smooth Start is great - summary for the layman:

This feature uses direct duty cycle control instead of low-resolution current control for greater torque accuracy at low speeds. The level of Smooth Start torque is entirely determined by raw pedal input (or throttle) at low speed. Then the torque gradually increases when the speed increase (or if pedal force exceeded half of the maximum force). This helps avoid sudden jerks or jolts, leading to a smoother and more controlled acceleration. Smooth and controlled acceleration enhances the user's confidence and contributes to overall safety. The function gives riders a feel that they are using a bicycle and not a motorcycle when starting the ride. Inadvertently, this adds to the overall safety of the system and protects the hardware from damage. Smooth Startup control allows to relax of other global torque rate limitations and reduce delay in the motor's response to inputs, ensuring quicker and more immediate acceleration. This enhances confidence for mountain biking, particularly when riding on loose or steep terrain, where riders may be susceptible to losing their balance. Independence from other settings at low speed: The initial rate at which torque is increased is consistent across different assist levels or pedal ADC mapping. This ensures predictability and allows the rider to learn their e-bike response. The torque ramping feature deactivates when cadence, wheel speed, or pedal pressure reaches internally hardcoded thresholds. Thresholds have been calibrated to allow a smooth transition from the startup torque to full motor assistance (determined by the assist level). Consequently, this feature can be used with all assist levels and other modifiers. Even functions that previously were very jerky (e.g. torque mode), now are ridable thanks to initial torque modulation. Recommended Settings for Motor Acceleration and Deceleration: The text suggests specific values for motor acceleration (likely for ramping up torque) and deceleration (likely for slowing down the motor's output). The values "exactly 120 and 100" are provided as optimal settings for achieving the best response from the system.

Assistance settings tab: image

Remarks regarding Voltage targets choice:

Other:

chgpalmer commented 1 year ago

I gave this firmware a quick go and it feels very nice. I haven't tried startup assist w/o pedalling much before, but with that enabled plus smooth startup I'm finding trackstands and wheelies easy, and it's much nicer pulling off from a standstill which will make traffic lights on the commute better

dzid26 commented 1 year ago

I gave this firmware a quick go and it feels very nice. I haven't tried startup assist w/o pedalling much before, but with that enabled plus smooth startup I'm finding trackstands and wheelies easy, and it's much nicer pulling off from a standstill which will make traffic lights on the commute better

I am glad you like it. Track stand was a disaster before.

Which motor version are you running? I am hoping someone can confirm this with 36V motor...

chgpalmer commented 1 year ago

I do, thanks! I'm running a 48v TSDZ2B with a 48v battery (60 miles odo) which I got a few weeks ago from pswpower aliexpress store

My only remaining gripes with the motor are the power dropoff with high cadences (I guess the motor is hitting max RPM?), and the delay when pushing the pedals from zero load. Maybe you know the reasons for this, I'm going to investigate the code

dzid26 commented 1 year ago

To improve torque at a high cadence, definitely try the Field weakening option. It's not very efficient though, so don't do too much pedaling in that high cadence region.

Regarding the delay. It happens when the bike has a non-zero speed. ~~It is related to the rear cassette freewheel, motor one-way clutch, and motor acceleration. ~~

When you start to pedal, the crank can rotate up to 360deg / 20 clutch teeth * gear ratio, which is around 15 deg or so depending on the gear. During those 15 degrees, the cadence is already significant, but there is no torque reported by the sensor until the cassette engages. Once there is torque, the motor starts to accelerate - but it doesn't transmit any torque until its speed matches crank speed.
So those two factors are mechanical.

Mechanically, you could improve by installing one of those mountain bike quick engagement freewheels.

But there are software improvements possible as well, and they go like this: When bike speed is above a threshold:

  1. start accelerating the motor (with a very low-duty cycle) - when the cadence sensor reports pedals rotating
  2. or, hold the motor slightly preloaded, so that when you start pedaling, the motor will accelerate by itself. Then the cadence could be obtained from the motor hall sensors which are even faster than the cadence sensor.

The first feature should be simple and I wanted to implement it on top of the Smooth Start.
Second would require some overrides of safety states and it may not work all that well.

chgpalmer commented 1 year ago

The field weakening option is fantastic! I should have tried that earlier

The delay I feel is subtle, I am pretty sure it is the motor rather than the transmission slop as the transmission slop I am used to from normal bikes - I can feel it most when rolling at a slow speed in a slightly high gear and then immediately loading the pedals (with a little hop), it is ~0.5 seconds until I hear the motor spin and feel the pull I suspect (2) would solve the problem, as this isn't an issue when moving away from a trackstand for example. Hopefully (1) is sufficient though as the pedals will rotate due to the mechanical slop

In any case it's not related to this PR, but thank you for answering my questions :-)

dzid26 commented 1 year ago

I am pretty sure it is the motor rather than the transmission slop

You right. I lost my train of thought a bit with the cassette freewheel. It's not so much due to spacing of the paws, but simply the fact that rear wheel rotates. And during all that time that you take to acceelerate your legs to synchronize to wheel speed, the motor does nothing. Especially if you have low gear selected.

By observing for pedal movement and accelerating the motor immediately, the delay can be reduced. (1)

chgpalmer commented 1 year ago

I've been riding with this every day to work and I've had no issues in general The one issue I did find was that if I start cycling too early after the screen starts, the torque sensor isn't calibrated properly. I used to solve this by turning the display off and on without pedalling but that no longer works, it seems the bike has to be stationary to calibrate properly now. No big deal, I just make sure to leave it until it's finished displaying battery stats and that seems to be long enough

I've bought a DM-02 for a second bike so I'll have that to compare against soon :-) Unfortunately the firmware is closed source for now and I don't think it's as refined

dzid26 commented 1 year ago

I used to solve this by turning the display off and on without pedalling

This still works. Make sure your weight is mostly supported by the seat when turning on the display while riding.

dzid26 commented 11 months ago

24fe1ba improves speed calculation and partially solves #71.
Before, when the wheel was locked at 10kph, that speed would linger for a couple of seconds and then suddenly disappear.
This phantom speed influenced any feature that relied on speed reading (interpolation, safety limits), including Smooth Start from this PR - and was most visible when stopping quickly and then accelerating again, before speed went to 0.

After this commit, the speed decays almost immediately (if the next pulse didn't arrive after the same period as the last one) and it typically goes 9, 7, 6, 5, 4, 0 kph.- in a matter of a couple of seconds as well. That is what is visible on the display anyway.

dzid26 commented 10 months ago

Last commit improves speed calculation and partially solves #71. Before, when the wheel was locked at 10kph, that speed would linger for a couple of seconds and then suddenly disappear. This phantom speed influenced any feature that relied on speed reading (interpolation, safety limits), including Smooth Start from this PR - and was most visible when stopping quickly and then accelerating again, before speed went to 0.

After this commit, the speed decays almost immediately (if the next pulse didn't arrive after the same period as the last one) and it typically goes 9, 7, 6, 5, 4, 0 kph.- in a matter of a couple of seconds as well. That is what is visible on the display anyway.

I removed this last commit, as I haven't tested it enough and I found an issue with too sudden wheel speed increase on first pulse causing torque drop and walk mode interruptions for some reason. I created a branch for it and will create a separate PR in the future.

emmebrusa commented 10 months ago

@dzid26 These days I tried your modification, I didn't like it. There is something wrong. You write that it solves startup problems, problems that I have never encountered (apart from one, the abrupt and late entry of assistance with AWP disabled and in Torque mode).

I tried in Power and Hybrid modes. During the first 4/5 pedal strokes it is very difficult, it seems that the motor is braked, even with maximum assistance. And if I try to push harder it doesn't change. It seems to do a long default acceleration ramp, regardless of how hard you push the pedals.

I don't know what to think, it will be due to a different response of the 36V motor with 36V battery, or to a mechanical difference, sprocket/chainring ratio. Or we have a different vision of what assistance should look like. I'll try to delve deeper.

dzid26 commented 10 months ago

Indeed, it doesn't sound like what I experience at all.

@dzid26 These days I tried your modification, I didn't like it. There is something wrong. You write that it solves startup problems, problems that I have never encountered (apart from one, the abrupt and late entry of assistance with AWP disabled and in Torque mode).

I tried in Power and Hybrid modes. During the first 4/5 pedal strokes it is very difficult, it seems that the motor is braked, even with maximum assistance. And if I try to push harder it doesn't change. It seems to do a long default acceleration ramp, regardless of how hard you push the pedals.

I don't know what to think, it will be due to a different response of the 36V motor with a 36V battery, or to a mechanical difference, sprocket/chainring ratio? Or we have a different vision of what assistance should look like. I'll try to delve deeper.

  • I don't think gear ratio matters for your issue. Of course, I start at different gears, and feels good regardless - I guess I just learned how it responds. (First gear on the steep hill could matter the most as lifting the front wheel is related to the amount of assist and ratio, rider and bike geometry). It comes with a certain feel by default, and if needed it can be adjusted (not adjustable in java).
  • Maybe it is due to your 36V motor. I was expecting, that for 0 speed, the torque should be generated the same. - That was one thing that I wished I could test. The theory behind this was that 36V would have x less winding turns and so x less resistance. So for the same voltage, the currents will be scaled by x as well. And since torque is linearly proportional to the number of winding turns. In the end, x/x cancels out and so torque is the same for the same voltage for both motors. (Torque would only slightly vary depending on winding temperature.) However, now I think maybe that was a silly assumption, because of course if there are fewer winding turns, they are probably made of a thicker wire, so the resistance would be much less for the 36V. (Unless I had a good reason to believe resistance is proportional to winding turns and not remember now what it was). But that would mean the resulting current would be bigger for 36V, and you would be getting much more starting torque, not less. So I am not sure what is going on.
  • Are you not getting torque when stepping on the pedal with brakes on? Can you at least hear motor phase activation?
chgpalmer commented 10 months ago

For reference, this is my config which has been working very well: config.zip

config
emmebrusa commented 10 months ago

Sorry for shortened, AWP = Assist without padaling. With AWP enabled, never had any problems. With AWP disabled, in Power mode, there is only lag. in Hybrid mode, the input is abrupt and late, not very abrupt in ECO, very abrupt in TURBO. But only with AWP disabled.

With your modification, even with 100, 100, 120, there is little assistance at the start, for the first 4-5 turns of the pedal, up to normal speed. With AWP enabled or disabled there is little difference, the first turn of the pedals provides a few W. It's clear that there's something wrong, it's a question of understanding what. Maybe it's just a parameter in your code that you need to change. I'm thinking of trying it on the 860C, where it would be possible to edit them live.

More than the motor voltage, it could be the motor/battery voltage ratio, you use 48/52, I use 36/36, but I also tried 36/48 and the motor is much more reactive, you have to dose the push on the pedals so as not to make the bike wheelie.

As regards the sprocket/chainring ratio, I didn't explain myself, I meant the number of teeth of the chainring, I consider the standard 42T unusable on the trails, then I tried the 34T for a year, then on both bikes I mounted the 30T, with 29" wheels is the best solution ever. The difference is notable, you pedal with a wider cadence band and you make better use of the motor's rpm range.

dzid26 commented 10 months ago

Nooo, don't use it without AWP. It defeats the purpose. It's all about removing that initial delay. #63 image

When you click the Smooth Start checkbox in Java, AWP gets enabled automatically. Beyond this automation, the recommended settings are provided in the manual:

image

I think you mean that the ratio of the first gear matters. Yes. - Of course, unlike the taller ratios, I cannot try the shorter ratios that I have for the first gear. But I think my setup is very reasonable for MTB. For what it's worth, I have 44t in the front, 50t in the rear, and 26" wheel. I can climb hills that I couldn't without the motor. I will switch to 42 front in the future, as I too rarely use the top gear. Anyway, my cadence is good even on steep hills, but that's also a function of rider weight, motor current, and rider strength.

Ok, so back to the actual problem. Battery voltage should not matter, as it is taken into account. https://github.com/dzid26/TSDZ2-Smart-EBike/blob/ebike_work/src/controller/ebike_app.c#L772

Are you sure your AWP is enabled and the threshold set to 120? Could it be that it's not cleared in the EEPROM? I don't need to tell you - you are the expert.

If that's confirmed, you could just try a different SMOOTH_START_PEDAL_FACTOR and recompile. Increase it 2x, to notice the change.

Just to reiterate, the torque should feel quite subtle at the beginning (slow cadence) - you lay your leg on the pedals and you should feel just a little torque even in low gears. Only if you push much harder or go faster, does it transition to full support.

dzid26 commented 5 months ago

Rebased.

emmebrusa commented 5 months ago

I've now seen new pull requests. I will review them before the new version is released. I think I have already adopted some. I also want to update you about the "Smooth start" feature, in November after a week of testing, I set it aside. The problem of abrupt starts exists, but I don't consider the solution you have created to be advantageous, the low cadence assistance is always too weak. In the new version, I kept the name "Smooth start", but used a simple additional ramp in hybrid and torque mode. In power mode it is not necessary. I will notify you before release.

dzid26 commented 5 months ago

The problem of abrupt starts exists, but I don't consider the solution you have created to be advantageous, the low cadence assistance is always too weak.

Based on your description, it doesn't seems you are experiencing the same thing as I do.

I still don't know if you ever disabled Assist without pedaling. It looked to me last time that this was the cause of the abrupt start.

Also, we may have different expectations from the ebike behavior. One practical test is whether you can reliably track stand on it without motor throwing you out of balance (e.g. https://www.youtube.com/watch?v=TNQEJueTpdI).

With the current target control, I don't think it is possible to achieve it. Basically, by the time current sensor sees any current, it is already too much torque. (Perhaps the issue of low resolution of the current feedback is exacerbated on the 48V motor). Thus low torque control can only be done by controlling duty cycle directly - in the open loop.. Also it is implemented in a way that for large force the duty cycle is not limited anymore and you'd get full assistance (even at 0rpm cadence).

It's a pity that it didn't work for you.

dzid26 commented 5 months ago

@emmebrusa
I will just add that Smooth Start feature works the worst in Power Assist modes because the pedal force builds up before cadence sensor starts to rotate and heave significant value. This result in a delay and a small step that follows cadence sensor value jumps.

This effect can be somewhat reduced by enabling Startup Boost which causes current command to appear earlier and thus it is also limited to a lower level by the Smooth Start and is overall more smooth. I automated Startup Boost selection in JavaConfigurator and added info to the documentation.

dzid26 commented 5 months ago

Actually, the issue with power assist was that the current target was rounded to zero for low cadence values. Instead of enabling startup-boost, I added a fix to round up the current which also solves initial jerk.

Although I don't know why power assist mode even exists. It reduces to a "torque gain as function of cadence". But then it is voltage dependant which seems wrong. And then there is a startup boost.

power assist

The whole thing is way over-complicated and could be done with a lookup table. But why? People were afraid of torque multiplication at low speed?
In that case I think smooth start solves it. And at higher cadence gets out of the way.

I recommend Torque mode (or eMTB) for most consistent assistance.

emmebrusa commented 5 months ago

Maybe that's why we don't understand each other, I use and recommend Power mode.

dzid26 commented 5 months ago

Maybe that's why we don't understand each other, I use and recommend Power mode.

Why do you prefer to have more assist the faster you pedal? Or the opposite question - If you like a lot of torque gain at high cadence, why not also at low cadence?

My rationale for torque mode is that it provides always the same assist - so the ebike still feels like a bicycle but lighter. Power assist feels to me like a turbocharged car - you get nothing at first, then you get a lot and then a little (at least on 48V motor). I think it is more exciting that way. :)

In any case, power assist jerk is gone after the 68f23fc, if you want to try it.

emmebrusa commented 5 months ago

My rationale for torque mode is that it provides always the same assist - so the ebike still feels like a bicycle but lighter.

I don't agree with this statement of yours, it's the Power mode that is more like a bicycle. With Torque mode, you have good assistance at low cadence, but it decreases as the cadence increases, at 80/90 it is very difficult to pedal. On the contrary, in Power mode it is more difficult at low cadence, like a normal bicycle. But to make up for this there is Startup boost. You probably haven't grasped the potential of Startup boost, it's not just a help at the start, you can also change the response curve of the torque sensor with respect to the cadence. You can see it in the Startup_boost.xls graph. It allows for very advanced customization.

I'll tell you my experience. I started cycling and being interested in OSF 5 years ago. As an improvised cyclist I pedaled at 50/60 cadence. I started with @marcoq's version in eMTB mode (which was different from the current one, it also depended on the cadence) and it was fine for my needs at the time. When I adopted @buba's OSF 20beta1 version I didn't feel comfortable with the new eMTB, not even Torque and Power satisfied me. I then tried to restore the Startup boost feature that @buba had deleted. A disaster, unusable. I had to find another solution. The first idea was the Hybrid mode, it worked, I used it for a few months with satisfaction, until I came up with the right idea for Startup boost. If I had found the solution for Startup boost right away, there would be no Hybrid mode. Since then I've only been using Power mode, with Startup boost enabled and customized. I then replaced the chainring with a 30T one and I learned to pedal at a higher cadence 80/90, I realized that I struggle less and the motor has better efficiency, heats up less and has greater autonomy.

dzid26 commented 4 months ago

https://github.com/emmebrusa/TSDZ2-Smart-EBike-1/pull/73/commits/d5335ac0550f93d2d8b93df5f70eeffecfa5cdec maxes out PWM_CYCLES_COUNTER_MAX such that ui16_motor_speed_erps starts outping values from 0erps insteasd of 5erps - this removes small jerk when slowly pressing on a pedal.

Additionally forced push moving the logic into a function: void apply_smooth_start(void).

dzid26 commented 4 months ago

Removed ui8_g_duty_cycle < PWM_DUTY_CYCLE_MIN_FRICTION condition that was achieving nothing