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 Battery Parameter Migration #15616

Merged
merged 7 commits into from
Sep 1, 2020
Merged

Fix Battery Parameter Migration #15616

merged 7 commits into from
Sep 1, 2020

Conversation

MaEtUgR
Copy link
Member

@MaEtUgR MaEtUgR commented Aug 25, 2020

Describe problem solved by this pull request
@cmic0 tried to change BAT1_V_EMPTY. Parameter was changed, it differed from BAT_V_EMPTY and after a reboot (starting simulation again) it was back to the old value.

Battery parameter migration from BAT_* to BAT1_* had the following issues:

  1. In simulation the SimulatorBattery broke the chain of ModuleParams parent/child tree which the logic is relying on. Additionally it overwrote the parameters with hardcoded values internally.
  2. The parameter migration prefers to migrate old over new if the values differ on boot but the flag _first_parameter_update was initialized false and hence always false. This didn't have an effect most of the time because the previous_old_val is zero on initialization and if the new parameter isn't zero it went the right path.
  3. Some drivers using the battery class e.g. INA266 didn't clear the parameter_update subscription once they checked it's updated.
  4. Some parts were hard to read.

Describe your solution

  1. Remove SimulatorBattery, it only did harm.
  2. Initialize _first_parameter_update with true.
  3. Copy subscription data to a dummy to reset after it was updated.
  4. Refactoring.

Test data / coverage
SITL testing.

It loads the battery parameters but then overwrites them
with hardcoded values and it breaks the ModuleParams
parent/child hierarchy. Both is undesired.
@MaEtUgR MaEtUgR added the bug label Aug 25, 2020
@MaEtUgR MaEtUgR requested a review from dagar August 25, 2020 18:02
@MaEtUgR MaEtUgR self-assigned this Aug 25, 2020
@@ -186,7 +186,8 @@ Battery::updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float curre
void
Battery::publish()
{
orb_publish_auto(ORB_ID(battery_status), &_orb_advert, &_battery_status, &_orb_instance);
int dummy; // We're not interested in the instance ID we get
orb_publish_auto(ORB_ID(battery_status), &_battery_status_pub, &_battery_status, &dummy);
Copy link
Member

Choose a reason for hiding this comment

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

If you're going to change this how about replacing it with a uORB::PublicationMulti<battery_status_s>?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about that but don't know what's the equivalent of this auto publication. Don't you have to specify the ID in advance? Would you just use the const int _index; of the battery for that?

Copy link
Member Author

@MaEtUgR MaEtUgR Aug 26, 2020

Choose a reason for hiding this comment

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

I tried to add it. Works with one battery (SITL). Is that the correct use?
Does it automatically choose the next free instance?

@dagar
Copy link
Member

dagar commented Aug 25, 2020

Nice work @MaEtUgR. The battery param migration was a real mess that I'm looking forward to purging once the QGC side is in place.

@@ -119,7 +119,7 @@ if [ $AUTOCNF = yes ]
then
param set SYS_AUTOSTART $REQUESTED_AUTOSTART

param set BAT_N_CELLS 3
param set BAT_N_CELLS 4
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. The BatterySimulator is hardcoding this value now so it's what we're already using in SITL atm: https://github.com/PX4/Firmware/blob/4088c2581f38333a306d3cd55aa66b617a51eb93/src/modules/simulator/battery_simulator/BatterySimulator.hpp#L88
  2. IMHO 4S is far more common than 3S these days which was not the case when this was originally set up.
  3. It doesn't matter, the simulated battery generates a voltage that considers the cell count so voltages "just look more familiar" 🤷‍♂️

@@ -230,6 +214,5 @@ class Battery : public ModuleParams
uint8_t _warning;
hrt_abstime _last_timestamp;

orb_advert_t _orb_advert{nullptr};
int _orb_instance;
orb_advert_t _battery_status_pub{nullptr};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
orb_advert_t _battery_status_pub{nullptr};
uORB::PublicationMulti<battery_status_s> _battery_status_pub{ORB_ID(battery_status)};

@MaEtUgR MaEtUgR force-pushed the battery-param-migration-fix branch from e7c302c to 9770e67 Compare August 26, 2020 09:01
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Aug 26, 2020

@dagar Thanks for the quick review!!

  • fixed analog_battery.cpp include (made CI fail)
  • fixed syslink_main namespace bracketing (made CI fail)
  • removed unnecessary parameter_update struct (review comment)
  • reworded some things (wasn't happy)

Diff: https://github.com/PX4/Firmware/compare/e7c302c4c9eb0122864b10b5310257b39ea5e3cd..dc1870d2dd414d7765a7611289a30cbb96b790e5

@MaEtUgR MaEtUgR force-pushed the battery-param-migration-fix branch 2 times, most recently from 12bdef1 to dc1870d Compare August 26, 2020 09:30
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Aug 26, 2020

I added a commit to switch to PublicationMulti like suggested in #15616 (comment) and #15616 (comment)

@MaEtUgR MaEtUgR force-pushed the battery-param-migration-fix branch from a153aa5 to 233c2c9 Compare August 26, 2020 10:06
src/modules/battery_status/analog_battery.cpp Outdated Show resolved Hide resolved
src/lib/battery/battery.h Show resolved Hide resolved
src/lib/battery/battery.h Show resolved Hide resolved
The subscription to parameter updates has to get
copied otherwise the change detection will not get
reset for next time.
This was not a problem before because battery.h
included the adc driver and implicitly snprintf
was defined through there.
@MaEtUgR MaEtUgR force-pushed the battery-param-migration-fix branch from 233c2c9 to 6cb5644 Compare August 26, 2020 12:48
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Aug 26, 2020

@julianoes Thanks for the review! I hope I addressed the comments to your liking. If you want to discuss migration cases I'm happy to do so.

@dagar dagar added this to the Release v1.11.0 milestone Aug 28, 2020
@dagar
Copy link
Member

dagar commented Aug 28, 2020

Looks good to me. Let's do an extra round of manual testing here because I'm going to cherry-pick this as the last change for v1.11.

@MaEtUgR MaEtUgR merged commit 1d562aa into master Sep 1, 2020
@MaEtUgR MaEtUgR deleted the battery-param-migration-fix branch September 1, 2020 08:25
@MaEtUgR
Copy link
Member Author

MaEtUgR commented Sep 1, 2020

@dagar Thanks for reviewing!
Release port like discussed: #15654

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants