You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					91 lines
				
				2.4 KiB
			
		
		
			
		
	
	
					91 lines
				
				2.4 KiB
			| 
								 
											9 years ago
										 
									 | 
							
								#!/usr/bin/env python
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								import os
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								from selfdrive.can.parser import CANParser
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								from cereal import car
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								from common.realtime import sec_since_boot
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								import zmq
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								from selfdrive.services import service_list
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								import selfdrive.messaging as messaging
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								def _create_nidec_can_parser():
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								  dbc_f = 'acura_ilx_2016_nidec.dbc'
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  radar_messages = [0x400] + range(0x430, 0x43A) + range(0x440, 0x446)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  signals = zip(['RADAR_STATE'] +
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                ['LONG_DIST'] * 16 + ['NEW_TRACK'] * 16 + ['LAT_DIST'] * 16 +
							 | 
						||
| 
								 | 
							
								                ['REL_SPEED'] * 16,
							 | 
						||
| 
								 | 
							
								                [0x400] + radar_messages[1:] * 4,
							 | 
						||
| 
								 | 
							
								                [0] + [255] * 16 + [1] * 16 + [0] * 16 + [0] * 16)
							 | 
						||
| 
								 | 
							
								  checks = zip([0x445], [20])
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								  return CANParser(os.path.splitext(dbc_f)[0], signals, checks, 1)
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								class RadarInterface(object):
							 | 
						||
| 
								 | 
							
								  def __init__(self):
							 | 
						||
| 
								 | 
							
								    # radar
							 | 
						||
| 
								 | 
							
								    self.pts = {}
							 | 
						||
| 
								 | 
							
								    self.track_id = 0
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.radar_fault = False
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.delay = 0.1  # Delay of radar
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    # Nidec
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.rcp = _create_nidec_can_parser()
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    context = zmq.Context()
							 | 
						||
| 
								 | 
							
								    self.logcan = messaging.sub_sock(context, service_list['can'].port)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def update(self):
							 | 
						||
| 
								 | 
							
								    canMonoTimes = []
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    updated_messages = set()
							 | 
						||
| 
								 | 
							
								    while 1:
							 | 
						||
| 
								 | 
							
								      tm = int(sec_since_boot() * 1e9)
							 | 
						||
| 
								 | 
							
								      updated_messages.update(self.rcp.update(tm, True))
							 | 
						||
| 
								 | 
							
								      if 0x445 in updated_messages:
							 | 
						||
| 
								 | 
							
								        break
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    for ii in updated_messages:
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								      cpt = self.rcp.vl[ii]
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								      if ii == 0x400:
							 | 
						||
| 
								 | 
							
								        # check for radar faults
							 | 
						||
| 
								 | 
							
								        self.radar_fault = cpt['RADAR_STATE'] != 0x79
							 | 
						||
| 
								 | 
							
								      elif cpt['LONG_DIST'] < 255:
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								        if ii not in self.pts or cpt['NEW_TRACK']:
							 | 
						||
| 
								 | 
							
								          self.pts[ii] = car.RadarState.RadarPoint.new_message()
							 | 
						||
| 
								 | 
							
								          self.pts[ii].trackId = self.track_id
							 | 
						||
| 
								 | 
							
								          self.track_id += 1
							 | 
						||
| 
								 | 
							
								        self.pts[ii].dRel = cpt['LONG_DIST']  # from front of car
							 | 
						||
| 
								 | 
							
								        self.pts[ii].yRel = -cpt['LAT_DIST']  # in car frame's y axis, left is positive
							 | 
						||
| 
								 | 
							
								        self.pts[ii].vRel = cpt['REL_SPEED']
							 | 
						||
| 
								 | 
							
								        self.pts[ii].aRel = float('nan')
							 | 
						||
| 
								 | 
							
								        self.pts[ii].yvRel = float('nan')
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        self.pts[ii].measured = True
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								      else:
							 | 
						||
| 
								 | 
							
								        if ii in self.pts:
							 | 
						||
| 
								 | 
							
								          del self.pts[ii]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    ret = car.RadarState.new_message()
							 | 
						||
| 
								 | 
							
								    errors = []
							 | 
						||
| 
								 | 
							
								    if not self.rcp.can_valid:
							 | 
						||
| 
								 | 
							
								      errors.append("commIssue")
							 | 
						||
| 
								 | 
							
								    if self.radar_fault:
							 | 
						||
| 
								 | 
							
								      errors.append("fault")
							 | 
						||
| 
								 | 
							
								    ret.errors = errors
							 | 
						||
| 
								 | 
							
								    ret.canMonoTimes = canMonoTimes
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    ret.points = self.pts.values()
							 | 
						||
| 
								 | 
							
								    return ret
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								if __name__ == "__main__":
							 | 
						||
| 
								 | 
							
								  RI = RadarInterface()
							 | 
						||
| 
								 | 
							
								  while 1:
							 | 
						||
| 
								 | 
							
								    ret = RI.update()
							 | 
						||
| 
								 | 
							
								    print(chr(27) + "[2J")
							 | 
						||
| 
								 | 
							
								    print ret
							 |