Difference between revisions of "Tesla Bosch Radar"

From Tinkla: Tinkering with Tesla
m (OP Code Changes)
m
Line 28: Line 28:
 
=== Panda code changes ===
 
=== Panda code changes ===
  
If you are on one of my Tesla OP branches (0.5.10 and above), the [[The Panda|Panda]] code already has all you need to get the Tesla Bosch Radar working.  
+
If you are on one of my Tesla OP branches (0.5.10 and above), the [[The Panda|Panda]] code already has all you need to get the Tesla Bosch Radar working.  
  
 
If you want to implement the code for another car, you will have to modify the code as follow:
 
If you want to implement the code for another car, you will have to modify the code as follow:
Line 98: Line 98:
 
=== OP Code Changes ===
 
=== OP Code Changes ===
  
If you are on one of my Tesla OP branches (0.5.10 and above), the OP code already has all you need to get the Tesla Bosch Radar working.  
+
If you are on one of my Tesla OP branches (0.5.10 and above), the OP code already has all you need to get the Tesla Bosch Radar working.  
  
 
If you want to implement the code for another car, you will have to modify the code as follow:
 
If you want to implement the code for another car, you will have to modify the code as follow:

Revision as of 14:59, 9 May 2019

Giraffe rev B Radar Diagram.png
Giraffe rev A.png
The Giraffe rev A board. Use CAN1 for radar..png

The Bosch radar made for Tesla (Bosch MRRevo14, and with few different part numbers like 1038224-00-A/B or 1057551-00-B) have been used on Tesla Model S cars from October 2014 (AP1) until they have been replaced by the Continental radar with AP2.5). The main difference between 1038224-00-B and 1038224-00-A is that the A version does not have the heater element for winter weather (to melt snow and ice).

The radar has a range of about 160m and for the short beam a cone of 45°.

Bosch radars

Many other car manufacturers use the same radar hardware, including Honda, Nissan, VW, Audi, etc. But Tesla (AFAIK) is the only one that reads and processes raw data from the radar instead of letting the radar make the decision about longitudinal control action needed (acceleration or deceleration).

This radar can ben used on any OP supported car. To insall the radar for a non-Tesla car please find a branch that has the code already done or modify your branch and add the code needed to make the radar talk to the Panda code for your car.

Deconding the radar

With a little help from a group of enthusiasts, we were able to reverse engineer all the messages that AP sends to the radar in order to make it "talk". Then we identified the structure of the CAN messages that describe each of the 32 objects this radar can detect. Signals like longitudinal distance, lateral distance (vs radar), longitudinal relative speed, lateral relative speed, longitudinal acceleration, certainty of detection, etc are all now processed and sent to the radar daemon of OpenPilot, improving longitudinal control for the car.

What you need

In order to get the radar working on your car, you need to buy a radar, a mounting bracket (few options here), a connector and then to create a harness and run the cable back to the Giraffe. As a first temporary setup for my research, I used an Audi bracket which I installed on my car's nose cone. Then I created the harness and connected it for both CAN and power to my Giraffe rev B using a car relay. Please also make sure you add the 120Ω resistor between CAN+ and CAN-.

If you have a Giraffe rev A, you will have to solder the CAN wires from the radar directly to the Giraffe. We are using the middle CAN of the Panda (CAN2 when you think of them as CAN1-CH, CAN3-EPAS) and the pins where you should solder your connector are marked in this diagrams. 
Also, you will have to add the same 120Ω resistor between CAN+ and CAN-.

As for power for the radar, if you don't feel like doing my relay setup from the Giraffe rev B, I recommend tapping into Pin 6 of the EPAS harness (which is "ignition" power) and drive a relay the same way i drove it from the Giraffe, but in this case add a 5 Amp fuse on the power from battery to the relay.

For other cars that want to use the Tesla Bosch Radar, please find an unused CAN in your Panda safety implementation and use that CAN to communicate with the radar. The radar has to be on its own specific CAN because any message outside of the ones that it is supposed to receive will make it stop working.

Panda code changes

If you are on one of my Tesla OP branches (0.5.10 and above), the Panda code already has all you need to get the Tesla Bosch Radar working. 

If you want to implement the code for another car, you will have to modify the code as follow:

  • find an existing CAN message on your car that is generated at 100Hz. We will use this message as a clock to trigger all the other messages needed by radar.
  • take a look at my test_teslaradar branch which uses the fake message code in safety_teslaradar.h
  • edit safety_yourcar.h (specific for your car) and add the following:
- add the include for safety_teslaradar.h at the top
#include "safety_teslaradar.h"
- in the rx_hook function, call the radar rx hook
teslaradar_rx_hook(to_push);
- in the rx_hook function, find the CAN message that provides the speed and parse it (this is a Honda example) saving the value in the actual_speed_kph variable
 //speed for radar
 if ((to_push->RIR>>21) == 0x309) {
   // first 2 bytes
   actual_speed_kph = (int)((((to_push->RDLR & 0xFF) << 8) + ((to_push->RDLR >>8) & 0xFF))*0.01);
 }
- in the fwd_hook function, make sure you do not forward any of the radar messages to any other CAN bus (in this example, the radar is on CAN bus 2)
 if(bus_num == 2) {
   return -1;
 }
- in the tx_hook function add interceptor for the message used to send info to the radar; in this example we use 0x560 as the CAN message ID; it this does not work for your car (already in use) you will have to change it to another ID that is not in use
 //check if this is a teslaradar vin message
 //capture message for radarVIN and settings
 if ((to_send->RIR >> 21) == 0x560) {
   int id = (to_send->RDLR & 0xFF);
   int radarVin_b1 = ((to_send->RDLR >> 8) & 0xFF);
   int radarVin_b2 = ((to_send->RDLR >> 16) & 0xFF);
   int radarVin_b3 = ((to_send->RDLR >> 24) & 0xFF);
   int radarVin_b4 = (to_send->RDHR & 0xFF);
   int radarVin_b5 = ((to_send->RDHR >> 8) & 0xFF);
   int radarVin_b6 = ((to_send->RDHR >> 16) & 0xFF);
   int radarVin_b7 = ((to_send->RDHR >> 24) & 0xFF);
   if (id == 0) {
     tesla_radar_should_send = (radarVin_b2 & 0x01);
     radarPosition =  ((radarVin_b2 >> 1) & 0x03);
     radarEpasType = ((radarVin_b2 >> 3) & 0x07);
     tesla_radar_trigger_message_id = (radarVin_b3 << 8) + radarVin_b4;
     tesla_radar_can = radarVin_b1;
     radar_VIN[0] = radarVin_b5;
     radar_VIN[1] = radarVin_b6;
     radar_VIN[2] = radarVin_b7;
     tesla_radar_vin_complete = tesla_radar_vin_complete | 1;
   }
   if (id == 1) {
     radar_VIN[3] = radarVin_b1;
     radar_VIN[4] = radarVin_b2;
     radar_VIN[5] = radarVin_b3;
     radar_VIN[6] = radarVin_b4;
     radar_VIN[7] = radarVin_b5;
     radar_VIN[8] = radarVin_b6;
     radar_VIN[9] = radarVin_b7;
     tesla_radar_vin_complete = tesla_radar_vin_complete | 2;
   }
   if (id == 2) {
     radar_VIN[10] = radarVin_b1;
     radar_VIN[11] = radarVin_b2;
     radar_VIN[12] = radarVin_b3;
     radar_VIN[13] = radarVin_b4;
     radar_VIN[14] = radarVin_b5;
     radar_VIN[15] = radarVin_b6;
     radar_VIN[16] = radarVin_b7;
     tesla_radar_vin_complete = tesla_radar_vin_complete | 4;
   }
   else {
     return 0;
   }
 }

OP Code Changes

If you are on one of my Tesla OP branches (0.5.10 and above), the OP code already has all you need to get the Tesla Bosch Radar working. 

If you want to implement the code for another car, you will have to modify the code as follow:

  • go to /data/openpilot/selfdrive/car/yourcar (of course, yourcar matches one of the supported OP cars like honda/toyota/etc.)
  • edit yourcarcan.py and add the code needed to send the VIN and other info to the Panda
def create_radar_VIN_msg(id,radarVIN,radarCAN,radarTriggerMessage,useRadar,radarPosition,radarEpasType):
  msg_id = 0x560
  msg_len = 8
  msg = create_string_buffer(msg_len)
  if id == 0:
    struct.pack_into('BBBBBBBB', msg, 0, id,radarCAN,useRadar + (radarPosition << 1) + (radarEpasType << 3), ((radarTriggerMessage >> 8) & 0xFF),(radarTriggerMessage & 0xFF),ord(radarVIN[0]),ord(radarVIN[1]),ord(radarVIN[2]))
  if id == 1:
    struct.pack_into('BBBBBBBB', msg, 0, id,ord(radarVIN[3]),ord(radarVIN[4]),ord(radarVIN[5]),ord(radarVIN[6]),ord(radarVIN[7]),ord(radarVIN[8]),ord(radarVIN[9]))
  if id == 2:
    struct.pack_into('BBBBBBBB', msg, 0, id,ord(radarVIN[10]),ord(radarVIN[11]),ord(radarVIN[12]),ord(radarVIN[13]),ord(radarVIN[14]),ord(radarVIN[15]),ord(radarVIN[16]))
  return [msg_id, 0, msg.raw, 0]
  • edit carcontroller.py and add the code needed to send the VIN and other specific radar info to Panda
- in the __init__ portion add
self.radarVin_idx = 0
- in the update portion add the below code, updating as needed the CAN bus to be used for radar (in this example 2) and the 100Hz message we will use to trigger messages (in this example 0x17c)
#if using radar, we need to send the VIN
if CS.useTeslaRadar and (frame % 100 == 0):
  can_sends.append(hondacan.create_radar_VIN_msg(self.radarVin_idx, CS.radarVIN, 2, 0x17c, CS.useTeslaRadar, CS.radarPosition, CS.radarEpasType))
  self.radarVin_idx += 1
  self.radarVin_idx = self.radarVin_idx  % 3
  • copy from my repo the radar_interface.py file and modify if needed
  • copy from my repo the radard.py to /data/openpilot/selfdrive/controls and modify as needed
  • copy from my repo the radar_helpers.py to /data/openpilot/selfdrive/controls/lib and modify as needed