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.
		
		
		
		
			
				
					63 lines
				
				1.6 KiB
			
		
		
			
		
	
	
					63 lines
				
				1.6 KiB
			| 
								 
											5 years ago
										 
									 | 
							
								#!/usr/bin/env python3
							 | 
						||
| 
								 | 
							
								import sys
							 | 
						||
| 
								 | 
							
								import numpy as np
							 | 
						||
| 
								 | 
							
								import matplotlib.pyplot as plt
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from sklearn import linear_model
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								from opendbc.car.toyota.values import STEER_THRESHOLD
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.tools.lib.logreader import LogReader
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								MIN_SAMPLES = 30 * 100
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								def to_signed(n, bits):
							 | 
						||
| 
								 | 
							
								  if n >= (1 << max((bits - 1), 0)):
							 | 
						||
| 
								 | 
							
								    n = n - (1 << max(bits, 0))
							 | 
						||
| 
								 | 
							
								  return n
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								def get_eps_factor(lr, plot=False):
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  engaged = False
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  steering_pressed = False
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  torque_cmd, eps_torque = None, None
							 | 
						||
| 
								 | 
							
								  cmds, eps = [], []
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  for msg in lr:
							 | 
						||
| 
								 | 
							
								    if msg.which() != 'can':
							 | 
						||
| 
								 | 
							
								      continue
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    for m in msg.can:
							 | 
						||
| 
								 | 
							
								      if m.address == 0x2e4 and m.src == 128:
							 | 
						||
| 
								 | 
							
								        engaged = bool(m.dat[0] & 1)
							 | 
						||
| 
								 | 
							
								        torque_cmd = to_signed((m.dat[1] << 8) | m.dat[2], 16)
							 | 
						||
| 
								 | 
							
								      elif m.address == 0x260 and m.src == 0:
							 | 
						||
| 
								 | 
							
								        eps_torque = to_signed((m.dat[5] << 8) | m.dat[6], 16)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        steering_pressed = abs(to_signed((m.dat[1] << 8) | m.dat[2], 16)) > STEER_THRESHOLD
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    if engaged and torque_cmd is not None and eps_torque is not None and not steering_pressed:
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      cmds.append(torque_cmd)
							 | 
						||
| 
								 | 
							
								      eps.append(eps_torque)
							 | 
						||
| 
								 | 
							
								    else:
							 | 
						||
| 
								 | 
							
								      if len(cmds) > MIN_SAMPLES:
							 | 
						||
| 
								 | 
							
								        break
							 | 
						||
| 
								 | 
							
								      cmds, eps = [], []
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  if len(cmds) < MIN_SAMPLES:
							 | 
						||
| 
								 | 
							
								    raise Exception("too few samples found in route")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  lm = linear_model.LinearRegression(fit_intercept=False)
							 | 
						||
| 
								 | 
							
								  lm.fit(np.array(cmds).reshape(-1, 1), eps)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  scale_factor = 1. / lm.coef_[0]
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  if plot:
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    plt.plot(np.array(eps) * scale_factor)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    plt.plot(cmds)
							 | 
						||
| 
								 | 
							
								    plt.show()
							 | 
						||
| 
								 | 
							
								  return scale_factor
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								if __name__ == "__main__":
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  lr = LogReader(sys.argv[1])
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  n = get_eps_factor(lr, plot="--plot" in sys.argv)
							 | 
						||
| 
								 | 
							
								  print("EPS torque factor: ", n)
							 |