Skip to content

Commit 4557733

Browse files
authored
Feature/tinklad throttling + linting (commaai#118)
* Throttle tinlkad's attemptToSendPendingMessages * Remove get_git_remote from registration. It's defined in version already. * tinklad linting * tesla linting
1 parent 9c4b0fa commit 4557733

10 files changed

+31
-32
lines changed

selfdrive/car/tesla/ACC_module.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def _calc_follow_button(self, CS, lead_car,speed_limit_kph, speed_limit_valid, s
357357
print ("Ratio: {0:.1f}% lead: {1:.1f}m avail: {2:.1f}kph vRel: {3:.1f}kph Angle: {4:.1f}deg".format(
358358
ratio, lead_dist_m, available_speed_kph, lead_car.vRel * CV.MS_TO_KPH, CS.angle_steers))
359359
self.last_update_time = current_time_ms
360-
if msg != None:
360+
if msg is not None:
361361
print ("ACC: " + msg)
362362
return button
363363

@@ -371,7 +371,7 @@ def _should_autoengage_cc(self, CS, lead_car=None):
371371
and CS.v_ego >= self.MIN_CRUISE_SPEED_MS
372372
and _current_time_millis() > self.fast_decel_time + 2000)
373373

374-
slow_lead = lead_car and lead_car.dRel > 0 and lead_car.vRel < 0 or self._fast_decel_required(CS, lead_car)
374+
slow_lead = lead_car and lead_car.dRel > 0 and lead_car.vRel < 0 or self._fast_decel_required(CS, lead_car) # pylint: disable=chained-comparison
375375

376376
# "Autoresume" mode allows cruise to engage even after brake events, but
377377
# shouldn't trigger DURING braking.

selfdrive/car/tesla/PCC_module.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def calc_follow_speed_ms(self, CS, alca_enabled):
567567
elif lead_dist_m < MIN_SAFE_DIST_M:
568568
new_speed_kph = MIN_PCC_V_KPH
569569
# In a 10 meter cruise zone, lets match the car in front
570-
elif lead_dist_m > MIN_SAFE_DIST_M and lead_dist_m < safe_dist_m + 2: # BB we might want to try this and rel_speed_kph > 0:
570+
elif safe_dist_m + 2 > lead_dist_m > MIN_SAFE_DIST_M: # BB we might want to try this and rel_speed_kph > 0:
571571
min_vrel_kph_map = OrderedDict([
572572
# (distance in m, min allowed relative kph)
573573
(0.5 * safe_dist_m, 3.0),
@@ -625,7 +625,7 @@ def calc_follow_speed_ms(self, CS, alca_enabled):
625625
# Don't accelerate during manual turns, curves or ALCA.
626626
new_speed_kph = min(new_speed_kph, self.last_speed_kph)
627627
#BB Last safety check. Zero if below MIN_SAFE_DIST_M
628-
if (lead_dist_m > 0) and (lead_dist_m < MIN_SAFE_DIST_M) and (rel_speed_kph < 3.):
628+
if (MIN_SAFE_DIST_M > lead_dist_m > 0) and (rel_speed_kph < 3.):
629629
new_speed_kph = MIN_PCC_V_KPH
630630
self.last_speed_kph = new_speed_kph
631631
return new_speed_kph * CV.KPH_TO_MS

selfdrive/car/tesla/interface.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
from cereal import car, tesla
33
from common.numpy_fast import clip, interp
4-
from common.realtime import sec_since_boot, DT_CTRL
4+
from common.realtime import DT_CTRL
55
from selfdrive.config import Conversions as CV
66
from selfdrive.controls.lib.drive_helpers import create_event, EventTypes as ET, get_events
77
from selfdrive.controls.lib.vehicle_model import VehicleModel
@@ -451,10 +451,10 @@ def update(self, c, can_strings):
451451
# NO_ENTRY events, so controlsd will display alerts. Also not send enable events
452452
# too close in time, so a no_entry will not be followed by another one.
453453
# TODO: button press should be the only thing that triggers enble
454-
if ((cur_time - self.last_enable_pressed) < 0.2 and
454+
if ((cur_time - self.last_enable_pressed) < 0.2 and # pylint: disable=chained-comparison
455455
(cur_time - self.last_enable_sent) > 0.2 and
456456
ret.cruiseState.enabled) or \
457-
(enable_pressed and get_events(events, [ET.NO_ENTRY])):
457+
(enable_pressed and get_events(events, [ET.NO_ENTRY])):
458458
if ret.seatbeltUnlatched:
459459
self.CC.DAS_211_accNoSeatBelt = 1
460460
self.CC.warningCounter = 300

selfdrive/car/tesla/radar_interface.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class RadarInterface(RadarInterfaceBase):
5656
tinklaClient = TinklaClient()
5757

5858
def __init__(self,CP):
59+
super().__init__(self)
5960
# radar
6061
self.pts = {}
6162
self.extPts = {}
@@ -84,7 +85,7 @@ def update(self, can_strings):
8485
time.sleep(0.05)
8586
return car.RadarData.new_message(),self.extPts.values()
8687

87-
if can_strings != None:
88+
if can_strings is not None:
8889
vls = self.rcp.update_strings(can_strings)
8990
self.updated_messages.update(vls)
9091

selfdrive/car/tesla/readconfig.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,5 @@ def get_value(self, name_of_variable):
408408
# Legacy support
409409
def read_config_file(into, config_path = default_config_file_path):
410410
config_file = ConfigFile()
411-
config_file.read(into, config_path)
411+
config_file.read(into, config_path)
412+

selfdrive/manager.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import errno
66
import signal
77
import subprocess
8+
import time
89
from selfdrive.tinklad.tinkla_interface import TinklaClient
910
from cereal import tinkla
1011
from selfdrive.car.tesla.readconfig import CarSettings
@@ -321,7 +322,7 @@ def system(cmd):
321322
output=e.output[-1024:],
322323
returncode=e.returncode)
323324

324-
def sendUserInfoToTinkla(params):
325+
def sendUserInfoToTinkla(params, tinklaClient):
325326
carSettings = CarSettings()
326327
gitRemote = params.get("GitRemote")
327328
gitBranch = params.get("GitBranch")
@@ -368,9 +369,9 @@ def manager_thread():
368369
logger_dead = False
369370

370371
# Tinkla interface
371-
global tinklaClient
372+
last_loop_iteration_time = 0
372373
tinklaClient = TinklaClient()
373-
sendUserInfoToTinkla(params)
374+
sendUserInfoToTinkla(params=params, tinklaClient=tinklaClient)
374375

375376
while 1:
376377
msg = messaging.recv_sock(thermal_sock, wait=True)
@@ -382,7 +383,11 @@ def manager_thread():
382383
start_managed_process("uploader")
383384

384385
# Attempt to send pending messages if there's any that queued while offline
385-
tinklaClient.attemptToSendPendingMessages()
386+
# Seems this loop runs every second or so, throttle to once every 30s
387+
now = time.time()
388+
if now - last_loop_iteration_time >= 30:
389+
tinklaClient.attemptToSendPendingMessages()
390+
last_loop_iteration_time = now
386391

387392
if msg.thermal.freeSpace < 0.05:
388393
logger_dead = True

selfdrive/registration.py

-8
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ def get_subscriber_info():
5555
return ""
5656
return ret
5757

58-
def get_git_remote():
59-
try:
60-
local_branch = subprocess.run(["git", "name-rev", "--name-only", "HEAD"], capture_output=True).stdout.decode("utf-8").strip()
61-
tracking_remote = subprocess.run(["git", "config", "branch."+local_branch+".remote"],capture_output=True).stdout.decode("utf-8").strip()
62-
return subprocess.run(["git", "config", "remote."+tracking_remote+".url"],capture_output=True).stdout.decode("utf-8").strip()
63-
except subprocess.CalledProcessError:
64-
return ""
65-
6658
def register():
6759
params = Params()
6860
params.put("Version", version)

selfdrive/tinklad/airtable_publisher.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ async def send_info(self, info, isData= False):
4646
data_dict = self.__generate_airtable_user_info_dict(info)
4747

4848
# Early return if no changes
49-
if self.latest_info_dict != None:
49+
if self.latest_info_dict is not None:
5050
print(LOG_PREFIX + "latest_info. data=%s" % (self.latest_info_dict))
5151
if data_dict == self.latest_info_dict:
5252
print(LOG_PREFIX + "send_info no update necessary*")
5353
return
5454

5555
print(LOG_PREFIX + "Sending info. data=%s" % (data_dict))
56-
if self.userRecordId != None:
56+
if self.userRecordId is not None:
5757
await self.__update_user(data_dict)
5858

59-
if info.openPilotId != None and info.openPilotId != '':
59+
if (info.openPilotId is not None) and info.openPilotId != '':
6060
self.openPilotId = info.openPilotId
6161

6262
response = await self.at.get(USERS_TABLE, limit=1, filter_by_formula=("{openPilotId} = '%s'" % (self.openPilotId)))
@@ -76,7 +76,7 @@ async def send_info(self, info, isData= False):
7676
print(LOG_PREFIX + "*send_info competed*")
7777

7878
async def send_event(self, event):
79-
if self.openPilotId is None and self.latest_info_dict != None:
79+
if self.openPilotId is None and self.latest_info_dict is not None:
8080
self.openPilotId = self.latest_info_dict[self.userKeys.openPilotId]
8181

8282
event_dict = self.__generate_airtable_user_event_dict(event)
@@ -103,7 +103,7 @@ def __generate_airtable_user_event_dict(self, event):
103103
value = event.value.intValue
104104
elif value == self.eventValueTypes.floatValue:
105105
value = event.value.floatValue
106-
openPilotId = event.openPilotId if (event.openPilotId != None) else (self.openPilotId if (self.openPilotId != None) else "")
106+
openPilotId = event.openPilotId if (event.openPilotId is not None) else (self.openPilotId if (self.openPilotId is not None) else "")
107107
dictionary = event.to_dict()
108108
dictionary[self.eventKeys.value] = value
109109
dictionary[self.eventKeys.openPilotId] = openPilotId
@@ -118,14 +118,14 @@ async def __update_user(self, data):
118118

119119
def __is_notfound_response(self, response):
120120
try:
121-
return response["error"] != None and response["error"]["code"] == 422
121+
return response["error"] is not None and response["error"]["code"] == 422
122122
except: # pylint: disable=bare-except
123123
count = response["records"].__len__()
124124
return count == 0
125125

126126
def __is_error_response(self, response):
127127
try:
128-
return response["error"] != None
128+
return response["error"] is not None
129129
except: # pylint: disable=bare-except
130130
return False
131131

@@ -179,7 +179,7 @@ def create_payload(data):
179179
return {'fields': data}
180180

181181

182-
class Airtable(object):
182+
class Airtable():
183183
def __init__(self, base_id, api_key, dict_class=OrderedDict):
184184
"""Create a client to connect to an Airtable Base.
185185

selfdrive/tinklad/pqueue.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _destroy(self):
7373
shutil.rmtree(self.path)
7474
os.makedirs(self.path)
7575

76-
def _qsize(self, len=len): # pylint: disable=redefined-builtin
76+
def _qsize(self, len=len): # pylint: disable=redefined-builtin,arguments-differ
7777
return self.info['size']
7878

7979
def _put(self, item):

selfdrive/tinklad/tinklad.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ async def messageLoop(self, sock):
163163
print(LOG_PREFIX + "Unsupported message version: %0.2f (supported version: %0.2f)" % (tinklaInterface.version, cereal.tinkla.interfaceVersion))
164164
continue
165165
messageType = tinklaInterface.message.which()
166-
if messageType != messageKeys.action:
167-
print(LOG_PREFIX + "> Received message. Type: '%s'" % messageType)
166+
#if messageType != messageKeys.action:
167+
print(LOG_PREFIX + "> Received message. Type: '%s'" % messageType)
168168
if messageType == messageKeys.userInfo:
169169
info = tinklaInterface.message.userInfo
170170
await self.setUserInfo(info)

0 commit comments

Comments
 (0)