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

Added Tesla safety changes. #132

Merged
merged 11 commits into from
Oct 4, 2018
Merged

Added Tesla safety changes. #132

merged 11 commits into from
Oct 4, 2018

Conversation

zax123
Copy link
Contributor

@zax123 zax123 commented Aug 14, 2018

Strictly the safety changes to panda code to add support for pre-autopilot Tesla Model S with the EPAS harness and giraffe designed by @kalud and @zax123 installed.

int tesla_ignition_started = 0;

// interp function that holds extreme values
float tesla_interpolate(struct lookup_t xy, float x)
Copy link
Contributor

Choose a reason for hiding this comment

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

just moved a generic interpolation function to safety.h I recommend using that one.

@zax123
Copy link
Contributor Author

zax123 commented Aug 14, 2018

OK, I removed the function from safety_tesla.h and I'm using the generic interpolate function in safety.h now, thanks!

board/safety.h Outdated
@@ -49,6 +49,7 @@ int controls_allowed = 0;
#include "safety/safety_gm.h"
#include "safety/safety_ford.h"
#include "safety/safety_cadillac.h"
#include "safety/safety_tesla.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

move this inside #ifdef PANDA: legacy boards and pedal don't support floats.

board/safety.h Outdated
@@ -100,6 +102,7 @@ const safety_hook_config safety_hook_registry[] = {
{SAFETY_GM, &gm_hooks},
{SAFETY_FORD, &ford_hooks},
{SAFETY_CADILLAC, &cadillac_hooks},
{SAFETY_TESLA, &tesla_hooks},
Copy link
Contributor

Choose a reason for hiding this comment

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

move this inside #ifdef PANDA: legacy boards and pedal don't support floats.

… don't support floats used in Tesla safety code
@zax123
Copy link
Contributor Author

zax123 commented Aug 15, 2018

Done, good eye for finding that!

board/safety.h Outdated
@@ -49,6 +49,9 @@ int controls_allowed = 0;
#include "safety/safety_gm.h"
#include "safety/safety_ford.h"
#include "safety/safety_cadillac.h"
#ifdef PANDA
#include "safety/safety_tesla.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is done and will be in my next commit.

int hour = (to_push->RDLR & 0x1F000000) >> 24;
int minute = (to_push->RDHR & 0x3F00) >> 8;
int second = (to_push->RDLR & 0x3F0000) >> 16;
current_car_time = (hour * 3600) + (minute * 60) + second;
Copy link
Contributor

@rbiasini rbiasini Aug 15, 2018

Choose a reason for hiding this comment

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

using car time from CAN seems unnecessary complicated and potentially unreliable. I recommend re-using:

    uint32_t ts = TIM2->CNT;
    uint32_t ts_elapsed = get_ts_elapsed(ts, tesla_ts_lever_last);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got rid of this section of code. It was going to be used if we only used double-pulls to activate openpilot. We now are activating on a single pull. Double-pull may activate other features, but safety-wise, we only need to monitor a single pull because nothing changes from a safety perspective when you pull twice.

Copy link
Contributor

Choose a reason for hiding this comment

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

cool

// 2m/s are added to be less restrictive
const struct lookup_t TESLA_LOOKUP_ANGLE_RATE_UP = {
{2., 7., 17.},
{5., .8, .25}};
Copy link
Contributor

@rbiasini rbiasini Aug 15, 2018

Choose a reason for hiding this comment

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

above 17m/s, this is twice than what is in toyota_safety_ipas.h. Isn't it too much?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry for my ignorance, I didn't copy this piece of code. It looks like the difference is the .25 in Tesla, which is .15 in Toyota. Is that the part that concerns you?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we have a couple engineers and coders who tested and tweaked the values in this code and I believe that that value was necessary in order to take even some modest curves at a decent speed. I don't think it's excessive. There are still some curves that the model can't take which I believe it should. I can definitely change this to .15 for upstreaming purposes, but I have a feeling users will likely change it back because of the turns it would disengage on. Definitely your call, please let me know.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, let's keep these numerical details last. It comes down to testing the max steer injection case:
https://medium.com/@comma_ai/how-to-write-a-car-port-for-openpilot-7ce0785eda84

eac_status = ((to_push->RDHR >> 21)) & 0x7;
// For human steering override we must not disable controls when eac_status == 0
// Additional safety: we could only allow eac_status == 0 when we have human steerign allowed
if ((controls_allowed == 1) && (eac_status != 0) && (eac_status != 1) && (eac_status != 2))
Copy link
Contributor

@rbiasini rbiasini Aug 16, 2018

Choose a reason for hiding this comment

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

If we don't want to disable on user steer override (let's think about it), then I would remove this check. Does not seem safety relevant.
of course openpilot will not allow to engage when eac_status==3 (for example), but this check here seems unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you saying you don't want the redundant check inside safety_tesla that ensures the the EPAS is not in EAC_FAULT(3) status?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, it would be safety relevant only if we want to disengage on user steer override.
I assume that if EAC is faulted, then it won't respond to steer commands.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, as soon as EAC faults, it disables steer commands and the user must take control. However, there is no feedback to openpilot indicating this, so I'm thinking it would be good for us to set controls_allowed = 0 because then feedback gets back to OP and it can throw an alert on the screen and/or play an alert sound. What do you think?

//get latest steering wheel angle
if (addr == 0x00E)
{
int angle_meas_now = (int)((((to_push->RDLR & 0x3F) << 8) + ((to_push->RDLR >> 8) & 0xFF)) * 0.1 - 819.2);
Copy link
Contributor

Choose a reason for hiding this comment

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

we should just use floats and define a new sample_t struct that contains floats

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like this code was copied from the Toyota IPAS safety code. Did you want to make this generic somehow and move it into safety.h?

Copy link
Contributor

Choose a reason for hiding this comment

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

exactly. safety_toyota_ipas.h is a good starting point, but hasn't reached the level of safety we want. safety_tesla.h will be the first "approved" angle control safety logic.

{

// add 1 to not false trigger the violation
int delta_angle_up = (int)(interpolate(TESLA_LOOKUP_ANGLE_RATE_UP, tesla_speed) * 25. + 1.);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think all of these should be floats

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, so, we could change these to floats, but then the toyota_ipas safety code is different. Should we make them the same?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, I would be happy to generalize those functions and use them in both places

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you would be so kind to do that, I would appreciate it, just because I didn't write that code myself and would likely introduce more bugs trying to fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, once the new functions are defined, I'll take care of using them in safety_toyota_ipas

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, so you'd like the Tesla team to generalize them and put them in safety.h using floats, or were you going to do that? Just want to be clear...

Copy link
Contributor

Choose a reason for hiding this comment

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

tesla team shoul'd do that :)

Better enclose Tesla-relevant code in ifdef PANDA due to use of floats
int delta_angle_down = (int)(interpolate(TESLA_LOOKUP_ANGLE_RATE_DOWN, tesla_speed) * 25. + 1.);
int highest_desired_angle = tesla_desired_angle_last + (tesla_desired_angle_last > 0 ? delta_angle_up : delta_angle_down);
int lowest_desired_angle = tesla_desired_angle_last - (tesla_desired_angle_last > 0 ? delta_angle_down : delta_angle_up);
int TESLA_MAX_ANGLE = (int)(interpolate(TESLA_LOOKUP_MAX_ANGLE, tesla_speed) + 1.);
Copy link
Contributor

Choose a reason for hiding this comment

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

we also need a simple check that ensures the rate limits are observed at each step (I only see the real time check). See safety_gm.h, for example.

{
// We should not be able to STEER under these conditions
// Other sending is fine (to allow human override)
steer_allowed = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

can't this be replaced by controls_allowed?

controls_allowed = 0;
puts("Steering commads disallowed");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

what is blocking messages to be sent when controls_allowed == 0? Seems like this function would return True.

@zax123
Copy link
Contributor Author

zax123 commented Aug 26, 2018

Hi @rbiasini, we've made all the requested changes as per our conversations above. Can you please check on the new code and let me know if anything else is missing?

Thanks!

@rbiasini
Copy link
Contributor

@zax123 , thanks. Will review within this week

}

// update array of samples
update_fsample(&tesla_angle_meas, angle_meas_now);
Copy link
Contributor

Choose a reason for hiding this comment

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

tesla_angle_meas is unused

{
// add 1 to not false trigger the violation
float delta_angle_up = interpolate(TESLA_LOOKUP_ANGLE_RATE_UP, tesla_speed) * 25. + 1.;
float delta_angle_down = interpolate(TESLA_LOOKUP_ANGLE_RATE_DOWN, tesla_speed) * 25. + 1.;
Copy link
Contributor

Choose a reason for hiding this comment

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

why the 25 factor? here the check should be done each step, right?

@rbiasini
Copy link
Contributor

rbiasini commented Sep 4, 2018

@zax123 , there is a minor conflict and a bug in the angle commanded rate limiter check (see my comment). Other than that it looks good.
My only worry is that the RT measured steering angle check will false trigger. I don't have a good solution in mind for this, except that maybe you can do the RT rate limiter check on the commanded angle (but I realize that checking the measured angle is more meaningful...).

Anyway, Once the merge conflict + bug are fixed, we can merge this PR. After that, you guys should write a regression test (see panda/tests/safety/) and we'll add the Tesla to the list of community supported cars (assuming the official branch uses this safety code :)). Finally, bounty will be paid!
Good job Tesla guys!

Robert Cotran added 3 commits September 18, 2018 16:48
Do interpolation check at every step (not every 25)

Change tesla safety constant number to 8, not 7
@zax123
Copy link
Contributor Author

zax123 commented Sep 18, 2018

Hi @rbiasini ,

Thanks for keeping this alive. I've made the necessary changes. For some reason, there's still a merge conflict with the SAFETY_TESLA number. It no longer conflicts with the SAFETY_HYUNDAI number.

The other changes are done. I'd just like it if you could confirm this is all ok before we move on to making the unit test file.

Thank you!

Rob

@zax123
Copy link
Contributor Author

zax123 commented Sep 18, 2018

PS. Congratulations on your promotion!!

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.

None yet

3 participants