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

GM long: add pitch-compensation #25485

Closed

Conversation

twilsonco
Copy link
Contributor

@twilsonco twilsonco commented Aug 19, 2022

(This, but without model-predicted pitch, and this is entirely contained in gm/carcontroller)

Description/justification

This PR adds pitch compensation to longitudinal control for GM cars, resulting in a large decrease in longitudinal error (versus the long plan) when pitch changes or when on an incline/decline.

Adds longitudinal pitch compensation to be used in cars that use gas/brake control, where the command specifies an amount of acceleration force rather than an amount of acceleration relative to the road. The implementation is contained in car controller, as a type of passive long feedforward, transparent to the controller.

To allow this to account for longitudinal actuator delay to allow for smoothing of the pitch signal, future model pitch is used to create a smoothed, predictive pitch.

Though not shown here, another benefit of this improvement is that if more than ACCEL_MAX acceleration is required to maintain speed up a steep hill, stock will not be able to maintain speed. With this improvement that is no longer the case.

actuators.accelPitchCompensated is used to determine the gas command, which specifies an acceleration force. The brake command on gm specifies acceleration relative to the ground, so the actuators.accel value is the correct value for friction brakes, even when going downhill.
To support this assertion, see that gas command does not match up with aEgo:
image
While brake command does match with aEgo
image
I also verified this extensively (though not with proper documentation) when developing "one pedal mode" in my openpilot fork, which involved artificially sending a constant brake command and observing the car's braking behavior. I found that the same brake command produces far more braking force on a decline, and far less (or none) on an incline.

However, when openpilot requests negative acceleration in order to stop behind a lead when going uphill, it would engage the friction brakes sooner (at the same time it would now, without this improvement), despite the negative gravitational acceleration, and resulting in more braking than in necessary, since it's being assisted by gravity.
So you'd like for openpilot to postpone friction brake use when decelerating uphill, but it can become a problem when stopping, as the pitch-compensated acceleration may remain positive even though openpilot would like to stop, resulting in late stopping behind leads.
The solution is to smoothly switch to non-pitch-compensated acceleration for determining brakes at low speed.
I've been using this method on my fork for about a month and it keeps the efficiency and brake-avoidance benefits of pitch-compensation without introducing possible problems when stopping behind leads.

(Any issues caused by the gas and brake signals having different physical meanings will also be minimized by using the correct gas/brake threshold acceleration value)

This should also be used on other makes that specify acceleration force with gas or brake commands

Route: c11fcb510a549332|2022-08-19--09-03-54--0

@sshane sshane added enhancement controls controls/planner related gm labels Aug 19, 2022
@sshane sshane changed the title GM skinny long pitch compensation GM: add pitch compensation to long control Aug 19, 2022
@sshane sshane changed the title GM: add pitch compensation to long control GM: pitch-compensated longitudinal control Aug 19, 2022
@sshane sshane changed the title GM: pitch-compensated longitudinal control GM: add pitch-compensation Aug 19, 2022
@sshane sshane changed the title GM: add pitch-compensation GM long: add pitch-compensation Aug 19, 2022
@twilsonco
Copy link
Contributor Author

twilsonco commented Aug 19, 2022

Route: c11fcb510a549332|2022-08-19--09-03-54--0

Verification

Here we compare master (no pitch) with present and future pitch compensation on a couple steep steps of a hill, going up and down the steps. "Present pitch" is from the above route.

Pitch compensation cuts speed error in half, and lowers acceleration error by 13%.
Additionally, this eliminates the problem of MAX_ACCEL (2m/s^2) being insufficient for climbing steep hills; with pitch compensation op can accelerate up any hill the car can.

(In the future, when Comma has sufficiently verified the model predicted pitch, I'd like to include future pitch in order to compensate for long actuator delay, which, as you can see here, decreases speed and acceleration error by an additional 46% compared to when using instantaneous pitch, and is tunable depending on the value of long actuator delay. Compared to master, future pitch compensation reduces speed and acceleration error by 74% and 53% respectively, resulting in greater passenger comfort and improved vehicle efficiency.)

pitch compare

comment

don't initialize filter

log and use new_actuators.accel

calculate accel_g for all frames

record smoothed pitch to pitchDEPRECATED

do initialize filter

indentation

no save deprecatedPitch

comment
@twilsonco twilsonco force-pushed the pr-gm-long-pitch-compensation-poorman branch from 704b6a6 to f26c961 Compare August 19, 2022 18:05
@twilsonco twilsonco marked this pull request as ready for review August 19, 2022 18:06
@sshane
Copy link
Contributor

sshane commented Aug 17, 2023

It's possible we need something similar to this for Toyotas to undo the accel compensation the PCM does, so we should try to make this as generic as possible.

@sshane sshane self-assigned this Aug 17, 2023
@twilsonco
Copy link
Contributor Author

So something in between this and the previous implementation that was unnecessarily couched inside of liveParams?

@adeebshihadeh
Copy link
Contributor

adeebshihadeh commented Aug 17, 2023

This isn't that large; blocking this on abstracting it out seems like premature optimization.

@twilsonco
Copy link
Contributor Author

The additional calculation of accel_g in the else block necessary at all. If that's not there, then accel is only changed at the lower rate but logged at 100Hz, making the data appear choppy in logs.

selfdrive/car/gm/carcontroller.py Show resolved Hide resolved
Comment on lines +82 to +84
pitch = clip(CC.orientationNED[1], self.pitch.x - PITCH_MAX_DELTA, self.pitch.x + PITCH_MAX_DELTA)
pitch = clip(pitch, PITCH_MIN, PITCH_MAX)
self.pitch.update(pitch)
Copy link
Contributor

Choose a reason for hiding this comment

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

Feels like the delta clip and the filter do similar things, any reason for both?


self.packer_pt = CANPacker(DBC[self.CP.carFingerprint]['pt'])
self.packer_obj = CANPacker(DBC[self.CP.carFingerprint]['radar'])
self.packer_ch = CANPacker(DBC[self.CP.carFingerprint]['chassis'])

def update(self, CC, CS):
actuators = CC.actuators
accel = actuators.accel
Copy link
Contributor

Choose a reason for hiding this comment

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

use class variable so you don't need to set it when not an active control frame. check toyota/carcontroller

@@ -25,13 +34,15 @@ def __init__(self, dbc_name, CP, VM):
self.lka_icon_status_last = (False, False)

self.params = CarControllerParams()
self.pitch = FirstOrderFilter(0., 0.09 * 4, DT_CTRL * 4) # runs at 25 Hz
Copy link
Contributor

Choose a reason for hiding this comment

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

if we only use the filter, ensure that going from min to max pitch doesn't violate your selected rate limit above

if not CC.longActive:
# Stock ECU sends max regen when not enabled
self.apply_gas = self.params.MAX_ACC_REGEN
self.apply_brake = 0
else:
brake_accel = actuators.accel + accel_g * interp(CS.out.vEgo, BRAKE_PITCH_FACTOR_BP, BRAKE_PITCH_FACTOR_V)
Copy link
Contributor

Choose a reason for hiding this comment

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

if we change brake like this we probably should change gas

Comment on lines +112 to +114
else:
accel_g = ACCELERATION_DUE_TO_GRAVITY * apply_deadzone(self.pitch.x, PITCH_DEADZONE) # driving uphill is positive pitch
accel += accel_g
Copy link
Contributor

Choose a reason for hiding this comment

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

use class variable

@simontheflutist
Copy link

simontheflutist commented Oct 28, 2023

With a view towards #30341, second-order effects would change the calculation from pitch to sin(pitch) cos(roll). I can't say for sure if it's necessary, but freeway ramps, which can have pronounced pitch and roll, come to mind.

@twilsonco
Copy link
Contributor Author

Thanks @simontheflutist.

Oops forgot to close this. This too will be superseded by #29659.

@twilsonco
Copy link
Contributor Author

Closed this in favor of #29659 which is now closed. Reopening

@twilsonco twilsonco reopened this Nov 19, 2023
@github-actions github-actions bot added the car vehicle-specific label Nov 19, 2023
Copy link
Contributor

This PR has had no activity for 30 days. It will be automatically closed in 7 days if there is no activity.

@github-actions github-actions bot added the stale label Dec 20, 2023
@twilsonco
Copy link
Contributor Author

I'll get Shanes's proposed changes done and a new test route so that this can be merged

@github-actions github-actions bot removed the stale label Dec 21, 2023
@twilsonco twilsonco marked this pull request as draft December 22, 2023 21:49
Copy link
Contributor

This PR has had no activity for 30 days. It will be automatically closed in 7 days if there is no activity.

@github-actions github-actions bot added the stale label Jan 22, 2024
Copy link
Contributor

This PR has been automatically closed due to inactivity. Feel free to re-open once activity resumes.

Copy link
Contributor

It looks like you didn't use one of the Pull Request templates. Please check the contributing docs. Also make sure that you didn't modify any of the checkboxes or headings within the template.

@github-actions github-actions bot removed the stale label Jan 31, 2024
Copy link
Contributor

github-actions bot commented Mar 1, 2024

This PR has had no activity for 30 days. It will be automatically closed in 7 days if there is no activity.

@github-actions github-actions bot added the stale label Mar 1, 2024
@nworb-cire nworb-cire deleted the pr-gm-long-pitch-compensation-poorman branch March 26, 2024 22:45
@sshane sshane added the tuning label Aug 20, 2024
@sshane
Copy link
Contributor

sshane commented Aug 20, 2024

We've moved the car interfacing code to our opendbc repository, which is now the new home for car ports and fingerprints. Please re-open your pull request against opendbc at your convenience by using this command below. This will transform all changes under selfdrive/car/ to opendbc_repo/opendbc/car/. Make sure you have initialized submodules beforehand and have checked out the latest opendbc commit.

PR_NUMBER=33045
curl -L https://github.com/commaai/openpilot/pull/$PR_NUMBER.patch | sed -e 's/selfdrive\/car/opendbc_repo\/opendbc\/car/g' | git apply -v --reject

Simply replace the PR number with your own. Once done, add the files, fix any conflicts, and open a new PR. Alternatively, you may start a new PR from scratch if that is easier for you.

@sshane sshane closed this Aug 20, 2024
@sshane
Copy link
Contributor

sshane commented Sep 20, 2024

If you would like to re-open, you can run our new maneuver tool and share the before and after reports. Here are some examples of the report showing a clear and obvious improvement, enabling us to merge a new tune faster and easier:

commaai/opendbc#1248

commaai/opendbc#1260

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

Successfully merging this pull request may close these issues.

4 participants