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

Fix PWM/Oneshot calibration #21711

Merged
merged 7 commits into from
Jun 16, 2023
Merged

Fix PWM/Oneshot calibration #21711

merged 7 commits into from
Jun 16, 2023

Conversation

MaEtUgR
Copy link
Member

@MaEtUgR MaEtUgR commented Jun 9, 2023

Solved Problem

When testing #21513 I found (again) that PWM calibration does not do what I expect and leads to it almost being impossible to configure PWM outputs consistently once a calibration was performed. That's why I opened #21634 and want to fix that first.

Fixes #21634

Solution

  • Make PWM calibration range consistent 1000-2000us (which maps to 125-250us for Oneshot) independent of minimum and maximum PWM configuration. These parameters represent the PWM value where the motors start spinning and stops going faster and producing more thrust. These definitions are separate from the calibration and do not match the same values for any ESC I've ever used. This was also discussed for example here: Send PWM_MIN value after a PWM_MAX value while calibrating ESCs #11663 (comment)
  • Make sure the motor output is always released and reverts to normal operation after a calibration even if it fails!
  • Change the timing of the ESC calibration to mostly give more time for the calibration. It's worth to give the ESC enough time for the steps compared to having some ESCs fail or calibrate to wrong values. In my experience the ESC's timeouts are pretty long because the calibration process is originally a manual one.
  • I added a safety check where an increase in measured current upon applying the high PWM value leats to an immediate abortion. I did this because on my test setup, the battery detection doesn't work reliably and this is an additional layer of preventing a screw-up.

Changelog Entry

For release notes:

Bugfix: Fix PWM/Oneshot calibration to consistent range
Documentation: Need to clarify https://docs.px4.io/main/en/advanced_config/esc_calibration.html#esc-calibration 

Alternatives

I want to also allow PWM calibration when the battery detection is not possible e.g. because the user has no power module or it needs to stay connected to power the autopilot. I suggest we take this PR without additional functionality together with #21513 first for the 1.14 release and deal with the manual calibration with the corresponding UI as a next step. The problem right now is the tight integration with the QGC UI being very limited. The first step would be to at least have a backward-compatible MAVLink shell way of doing that.

EDIT: I allowed to calibrate without battery detection because of the following reasons:

  • There are many setups where at the time of ESC calibration the user has either no power module or it needs to stay connected to power the autopilot.
  • Without this the ESC calibration aborts after a timeout if battery detection doesn't work. The problem is if the user still connects the battery as he gets instructed to and the calibration aborts then the ESCs are in calibration mode and after abortion just calibrate to the wrong value.
  • There's no additional safety by aborting the calibration if the battery cannot be detected during the timeout because a pixhawk board without power module will report a battery status from the ADC driverand in it that no battery is connected which is the best it can do. In this situation the motors will still spin if the ESCs are powered.

So the only disadvantage is that the UI does not show when the user did actually not plug the battery which is much less bad than not allowing a calibration at all and aborting in the worst moment which makes the ESC calibrate to the wrong value. So I suggest this as the in-between step and fixing the UI next.

Test coverage

  • Hardware testing of PWM and Oneshot on Holybro QAV250 with pixhawk 4 mini. I get extremely consistent calibrations with well better tolerances than what's needed for useful vehicle operation.

image

I'm testing with more ESCs now.

@MaEtUgR MaEtUgR requested a review from dagar June 9, 2023 16:34
@MaEtUgR MaEtUgR self-assigned this Jun 9, 2023
MaEtUgR added 6 commits June 13, 2023 15:51
to make them easy to search for and check the arguments.
- Change timings for a more reliable calibration.
- Make sure there's an error message when battery measurement is not
available also when it gets disabled after system boot in the power
just above the calibration button.
- Safety check if measured electrical current goes up after issuing the
high calibration value for the case the user did not unplug power
and the detection either fails or is not enforced.
Some ESCs e.g. Gaui enter the menu relatively quickly if the
signal is high for too long. To solve that it's kept high shorter.
Also all tested ESCs detect the low signal within a shorter time
so no need to wait longer.
Before this the ESC calibration aborts if battery detection doesn't work.
The problem is if the user still connects the battery as he gets instructed
and the calibration aborts then the ESCs are in calibration mode and
after abortion calibrate to the wrong value.

Also I realized there's no additional safety by aborting the calibration
if the battery cannot be detected during the timeout because a pixhawk
board without power module will report a battery status from the
ADC driverand in it that no battery is connected which is the best
it can do. In this situation the motors will still spin if the
ESCs are powered.
@MaEtUgR MaEtUgR force-pushed the maetugr/pwm-calibration branch from 69ea5be to e83363f Compare June 13, 2023 13:51
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Jun 13, 2023

I did testing on 5 different PWM ESCs and adjusted the timing to work with all of them. Also I allowed ESC calibration without battery detection see my edit in the "Alternatives" section above.

@DronecodeBot
Copy link

This pull request has been mentioned on Discussion Forum for PX4, Pixhawk, QGroundControl, MAVSDK, MAVLink. There might be relevant details there:

https://discuss.px4.io/t/px4-maintainers-call-june-13-2023/32587/1

bkueng
bkueng previously approved these changes Jun 15, 2023
battery_status_sub.update();

if ((now - timeout_start) < 1_s && (battery_status_sub.get().current_a > current_before_calibration + 1.f)) {
// Safety termination, current rises immediately, user didn't unplug power before
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I did with multiple setups. If the current is picked up by the power module it reacts very fast, aborts and the motor(s) don't have time to spin up. It also triggered with a single small motor and no propeller attached. I made it only trigger in the first second because there can be a 1A current spike measured during a normal calibration later on which makes finding a safe threshold much harder.

It can't provide a guarantee because the user could be powering the ESC independently of the power module and then no current is measured. But in my eyes it's a worthwhile safety layer because it would have caught point 1. in #21634.

Here's a plot from one of my tests. The current spike leads to an immediate abortion and basically just a stutter of the motors:
image

I should probably change the timeout check for this one too then ⚙️

Co-authored-by: Beat Küng <beat-kueng@gmx.net>
@MaEtUgR MaEtUgR force-pushed the maetugr/pwm-calibration branch from 67bc737 to 24c9ee0 Compare June 15, 2023 14:33
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Jun 15, 2023

Thanks a lot for the review @bkueng ! I think I don't completely understand the timestamp wrapping. Let me know if it's better now with the last commit.

Copy link
Member

@bkueng bkueng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I don't completely understand the timestamp wrapping.

It's only a problem in certain cases, because unsigned int is used: if you have a-b and b>a, it wraps. Specifically it happens when taking the current time, then doing an orb copy which was published after the current time. That can't happen in your case, but there's various places where it can, and so it's better to be consistent everywhere.

@MaEtUgR MaEtUgR merged commit 053d302 into main Jun 16, 2023
@MaEtUgR MaEtUgR deleted the maetugr/pwm-calibration branch June 16, 2023 10:05
MaEtUgR added a commit to PX4/PX4-user_guide that referenced this pull request Jun 27, 2023
For the 1.14 release because we already changed the actuator
configuration and users will need to go through their setups I also
corrected the PWM calibration and the default PWM values.

See PX4/PX4-Autopilot#21711
and PX4/PX4-Autopilot#21513

This is the documentation change with it. And needs to be
referenced by the release notes.
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Jun 27, 2023

Here's the documentation change for this together with the new PWM defaults (#21513): PX4/PX4-user_guide#2599

MaEtUgR added a commit to PX4/PX4-user_guide that referenced this pull request Jul 5, 2023
For the 1.14 release because we already changed the actuator
configuration and users will need to go through their setups I also
corrected the PWM calibration and the default PWM values.

See PX4/PX4-Autopilot#21711
and PX4/PX4-Autopilot#21513

This is the documentation change with it. And needs to be
referenced by the release notes.
MaEtUgR added a commit to PX4/PX4-user_guide that referenced this pull request Jul 10, 2023
For the 1.14 release because we already changed the actuator
configuration and users will need to go through their setups I also
corrected the PWM calibration and the default PWM values.

See PX4/PX4-Autopilot#21711
and PX4/PX4-Autopilot#21513

This is the documentation change with it. And needs to be
referenced by the release notes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[WIP] PWM calibration problem
3 participants