Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smooth Start feature and cleanups, closes #63 #73

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

dzid26
Copy link

@dzid26 dzid26 commented Aug 18, 2023

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:

  • The whole premise is that we can't trust current sensing for low current targets that are needed during ride start. So the feature uses a duty cycle which as it stands gives enough resolution to request low torque values. - In this scenario Voltage targets are simply easier to set than current or torque.
  • voltage targets should be universal for motors with different ratings (36V / 48V) and shall produce the same amount of torque per volt, given that bemf is taken into account.
  • So BEMF calculation function is for maintaining the torque once the motor starts rotating - this is motor specific and depends on the motor K-factor. I determined K-factor based on available measurements and calculations. The base equation is simple to maintain torque target across ERPS spectrum: Duty% = (V_target+B_bemf)/V_Bat
  • I hardcoded voltage calibrations values in the code. The values and thresholds were selected so that the startup torque feels safe across different situations. One of the benchmarks was trying to start on an extremely steep hill and be in control of the front wheel lift. Of course, this can depend on the bike, and if someone really needs to they can change it in the code.
  • For the same reason, the calibration interpolates over raw pedal force. This is to ensure consistency across different bikes and settings people chose for the ADC curve. Again, we always want the start to be predictable and gentle, no matter what other settings do.

Other:

  • Torque rate limits (motor acceleration) were just a bandaid for bad current control, adding to overall bad system response - with Smooth Start there is no reason to keep them below 100.
  • Startup Assist without pedaling should be set to 120 to fully take advantage of smooth torque at the startup
  • Startup Boost should be on if used in Power Assist mode
  • I unconditionally disabled CADENCE_SENSOR_STANDARD_MODE_SCHMITT_TRIGGER_THRESHOLD. This setting is evil and reduces rider safety on complicated terrains. This feature will make the bike go when you need to stop. I crashed because of it when climbing a hill and that's why it's hardcoded to 0.
    (Sure, for cargo bikes, it doesn't matter, but for cargo bikes nothing matters, so who cares. If someone wants an unsafe feature, they can change it back in the code.)

@chgpalmer
Copy link

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
Copy link
Author

dzid26 commented Aug 25, 2023

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
Copy link

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
Copy link
Author

dzid26 commented Aug 25, 2023

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
Copy link

chgpalmer commented Aug 25, 2023

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
Copy link
Author

dzid26 commented Aug 25, 2023

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
Copy link

chgpalmer commented Sep 22, 2023

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
Copy link
Author

dzid26 commented Sep 22, 2023

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
Copy link
Author

dzid26 commented Oct 27, 2023

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
Copy link
Author

dzid26 commented Nov 11, 2023

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
Copy link
Owner

@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
Copy link
Author

dzid26 commented Nov 25, 2023

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).

  • yeas - abrupt and late entry of assistance is what it solves.
  • I don't remember what else was I claiming that it solves
  • What is AWP?

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
Copy link

chgpalmer commented Nov 25, 2023

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

@emmebrusa
Copy link
Owner

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
Copy link
Author

dzid26 commented Nov 29, 2023

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

  • I tried to disable awp. Yes, the torque is late and abrupt and it reminds me how bad it was before this change. Ughh.

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
Copy link
Author

dzid26 commented Apr 8, 2024

Rebased.

@emmebrusa
Copy link
Owner

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
Copy link
Author

dzid26 commented Apr 9, 2024

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
Copy link
Author

dzid26 commented Apr 15, 2024

@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 dzid26 force-pushed the ebike_work branch 3 times, most recently from 26a3a2e to 5e09233 Compare April 20, 2024 11:10
@dzid26
Copy link
Author

dzid26 commented Apr 20, 2024

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
Copy link
Owner

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

@dzid26
Copy link
Author

dzid26 commented Apr 22, 2024

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
Copy link
Owner

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
Copy link
Author

dzid26 commented May 17, 2024

d5335ac 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
Copy link
Author

dzid26 commented May 17, 2024

Removed ui8_g_duty_cycle < PWM_DUTY_CYCLE_MIN_FRICTION condition that was achieving nothing

dzid26 added 10 commits June 4, 2024 18:46
fix: allow for inverted range similar to map_ui8
Smooth torque aplication on startup.
Slightly reduce duty cycle ramping by starting from a small non-zero value

Created BEMF calculation

Hold last bemf value when FOC is active.

Smooth start: added BEMF reduction factor

Smooth start: fixed walk assist - special case

Smooth start: also for the throttle

Removed pedal filtering to slightly reduce delay

Added wheel speed dependency for cadence modes.
fixes low cadence, zero torque issue
Fixes small jerk during Smooth Startup.
@dzid26 dzid26 mentioned this pull request Jun 11, 2024
@dzid26 dzid26 marked this pull request as draft June 18, 2024 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants