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.
		
		
		
		
			
				
					60 lines
				
				1.6 KiB
			
		
		
			
		
	
	
					60 lines
				
				1.6 KiB
			| 
											6 years ago
										 | #!/usr/bin/env python3
 | ||
| 
											6 years ago
										 | # type: ignore
 | ||
|  | 
 | ||
| 
											6 years ago
										 | import os
 | ||
|  | import argparse
 | ||
|  | import struct
 | ||
| 
											6 years ago
										 | from collections import deque
 | ||
|  | from statistics import mean
 | ||
| 
											6 years ago
										 | 
 | ||
|  | from cereal import log
 | ||
|  | import cereal.messaging as messaging
 | ||
|  | 
 | ||
|  | if __name__ == "__main__":
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   parser = argparse.ArgumentParser(description='Sniff a communication socket')
 | ||
| 
											6 years ago
										 |   parser.add_argument('--addr', default='127.0.0.1')
 | ||
|  |   args = parser.parse_args()
 | ||
|  | 
 | ||
|  |   if args.addr != "127.0.0.1":
 | ||
|  |     os.environ["ZMQ"] = "1"
 | ||
| 
											1 year ago
										 |     messaging.reset_context()
 | ||
| 
											6 years ago
										 | 
 | ||
|  |   poller = messaging.Poller()
 | ||
|  |   messaging.sub_sock('can', poller, addr=args.addr)
 | ||
|  | 
 | ||
|  |   active = 0
 | ||
|  |   start_t = 0
 | ||
|  |   start_v = 0
 | ||
|  |   max_v = 0
 | ||
|  |   max_t = 0
 | ||
| 
											6 years ago
										 |   window = deque(maxlen=10)
 | ||
| 
											6 years ago
										 |   avg = 0
 | ||
|  |   while 1:
 | ||
|  |     polld = poller.poll(1000)
 | ||
|  |     for sock in polld:
 | ||
|  |       msg = sock.receive()
 | ||
| 
											2 years ago
										 |       with log.Event.from_bytes(msg) as log_evt:
 | ||
|  |         evt = log_evt
 | ||
| 
											6 years ago
										 | 
 | ||
|  |       for item in evt.can:
 | ||
|  |         if item.address == 0xe4 and item.src == 128:
 | ||
|  |           torque_req = struct.unpack('!h', item.dat[0:2])[0]
 | ||
|  |           # print(torque_req)
 | ||
|  |           active = abs(torque_req) > 0
 | ||
|  |           if abs(torque_req) < 100:
 | ||
|  |             if max_v > 5:
 | ||
|  |               print(f'{start_v} -> {max_v} = {round(max_v - start_v, 2)} over {round(max_t - start_t, 2)}s')
 | ||
|  |             start_t = evt.logMonoTime / 1e9
 | ||
|  |             start_v = avg
 | ||
|  |             max_t = 0
 | ||
|  |             max_v = 0
 | ||
|  |         if item.address == 0x1ab and item.src == 0:
 | ||
|  |           motor_torque = ((item.dat[0] & 0x3) << 8) + item.dat[1]
 | ||
|  |           window.append(motor_torque)
 | ||
| 
											6 years ago
										 |           avg = mean(window)
 | ||
| 
											6 years ago
										 |           #print(f'{evt.logMonoTime}: {avg}')
 | ||
|  |           if active and avg > max_v + 0.5:
 | ||
|  |             max_v = avg
 | ||
|  |             max_t = evt.logMonoTime / 1e9
 |