Skip to content

Commit 4413ae6

Browse files
committed
cleanups and small refactors
1 parent 4febc0d commit 4413ae6

5 files changed

Lines changed: 20 additions & 77 deletions

File tree

selfdrive/pandad/pandad.cc

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,6 @@ void process_peripheral_state(Panda *panda, PubMaster *pm, bool no_fan_control)
295295
static int prev_ir_pwr = 999;
296296
static uint32_t prev_frame_id = UINT32_MAX;
297297
static bool driver_view = false;
298-
static bool not_car = false;
299-
static uint64_t last_not_car_check = 0;
300298

301299
// TODO: can we merge these?
302300
static FirstOrderFilter integ_lines_filter(0, 30.0, 0.05);
@@ -305,18 +303,6 @@ void process_peripheral_state(Panda *panda, PubMaster *pm, bool no_fan_control)
305303
{
306304
sm.update(0);
307305

308-
// Check notCar periodically to disable IR on body
309-
if (sm.frame % 100 == 0 || last_not_car_check == 0) {
310-
std::string cp_bytes = params.get("CarParamsPersistent");
311-
if (cp_bytes.size() > 0) {
312-
AlignedBuffer aligned_buf;
313-
capnp::FlatArrayMessageReader cmsg(aligned_buf.align(cp_bytes.data(), cp_bytes.size()));
314-
cereal::CarParams::Reader CP = cmsg.getRoot<cereal::CarParams>();
315-
not_car = CP.getNotCar();
316-
}
317-
last_not_car_check = sm.frame;
318-
}
319-
320306
if (sm.updated("deviceState") && !no_fan_control) {
321307
// Fan speed
322308
uint16_t fan_speed = sm["deviceState"].getDeviceState().getFanSpeedPercentDesired();
@@ -350,15 +336,25 @@ void process_peripheral_state(Panda *panda, PubMaster *pm, bool no_fan_control)
350336
}
351337
}
352338

353-
// Disable IR on body or on input timeout
354-
if (not_car || nanos_since_boot() - last_driver_camera_t > 1e9) {
339+
// Disable IR on input timeout
340+
if (nanos_since_boot() - last_driver_camera_t > 1e9) {
355341
ir_pwr = 0;
356342
}
357343

358344
if (ir_pwr != prev_ir_pwr || sm.frame % 100 == 0) {
359-
int16_t ir_panda = util::map_val(ir_pwr, 0, 100, 0, MAX_IR_PANDA_VAL);
345+
std::string cp_bytes = params.get("CarParams");
346+
if (cp_bytes.size() > 0) {
347+
AlignedBuffer aligned_buf;
348+
capnp::FlatArrayMessageReader cmsg(aligned_buf.align(cp_bytes.data(), cp_bytes.size()));
349+
cereal::CarParams::Reader CP = cmsg.getRoot<cereal::CarParams>();
350+
if (CP.getNotCar()) {
351+
ir_pwr = 0;
352+
}
353+
}
354+
355+
int16_t ir_panda = util::map_val(ir_pwr, 0, 100, 0, MAX_IR_PANDA_VAL);
360356
panda->set_ir_pwr(ir_panda);
361-
Hardware::set_ir_power(ir_pwr);
357+
Hardware::set_ir_power(ir_pwr);
362358
prev_ir_pwr = ir_pwr;
363359
}
364360
}

selfdrive/ui/layouts/main.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self):
2424

2525
self._pm = messaging.PubMaster(['bookmarkButton'])
2626

27-
self._is_body = ui_state.is_body
27+
self._is_body = ui_state.is_body()
2828
self._sidebar = BodySidebar() if self._is_body else Sidebar()
2929
self._current_mode = MainState.HOME
3030
self._prev_onroad = False
@@ -117,12 +117,9 @@ def _on_onroad_clicked(self):
117117

118118
def _render_main_content(self):
119119
if self._is_body: # overlay sidebar but recompute boundaries for proper click events
120-
if self._sidebar.is_visible:
121-
self._layouts[self._current_mode].set_parent_rect(
122-
rl.Rectangle(self._rect.x, self._rect.y + BODY_SIDEBAR_HEIGHT,
123-
self._rect.width, self._rect.height - BODY_SIDEBAR_HEIGHT))
124-
else:
125-
self._layouts[self._current_mode].set_parent_rect(None)
120+
parent_rect = rl.Rectangle(self._rect.x, self._rect.y + BODY_SIDEBAR_HEIGHT,
121+
self._rect.width, self._rect.height - BODY_SIDEBAR_HEIGHT) if self._sidebar.is_visible else None
122+
self._layouts[self._current_mode].set_parent_rect(parent_rect)
126123
self._layouts[self._current_mode].render(self._rect)
127124
if self._sidebar.is_visible:
128125
self._sidebar.render(self._sidebar_rect)

selfdrive/ui/mici/layouts/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self):
2222

2323
self._pm = messaging.PubMaster(['bookmarkButton'])
2424

25-
self._is_body = ui_state.is_body
25+
self._is_body = ui_state.is_body()
2626
self._prev_onroad = False
2727
self._prev_standstill = False
2828
self._prev_joystick_debug_mode = False
@@ -107,8 +107,7 @@ def _handle_transitions(self):
107107
# When car leaves standstill, pop nav stack and scroll to onroad
108108
CS = ui_state.sm["carState"]
109109
if not CS.standstill and self._prev_standstill:
110-
if not self._is_body or ui_state.joystick_debug_mode:
111-
gui_app.pop_widgets_to(self, lambda: self._scroll_to(self._onroad_layout))
110+
gui_app.pop_widgets_to(self, lambda: self._scroll_to(self._onroad_layout))
112111
self._prev_standstill = CS.standstill
113112

114113
# On body, react to joystick debug mode changes while onroad:

selfdrive/ui/soundd.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ def __init__(self):
7575

7676
self.spl_filter_weighted = FirstOrderFilter(0, 2.5, FILTER_DT, initialized=False)
7777

78-
self._webrtc_lock = threading.Lock()
79-
self._webrtc_chunks: deque[np.ndarray] = deque()
80-
self._webrtc_size = 0
81-
8278
def load_sounds(self):
8379
self.loaded_sounds: dict[int, np.ndarray] = {}
8480

@@ -115,36 +111,10 @@ def get_sound_data(self, frames): # get "frames" worth of data from the current
115111

116112
return ret * self.current_volume
117113

118-
def push_webrtc_audio(self, samples: np.ndarray):
119-
with self._webrtc_lock:
120-
if self._webrtc_size > SAMPLE_RATE: # 1 second max buffer
121-
self._webrtc_chunks.clear()
122-
self._webrtc_size = 0
123-
self._webrtc_chunks.append(samples)
124-
self._webrtc_size += samples.size
125-
126-
def pop_webrtc_audio(self, frames: int) -> np.ndarray:
127-
out = np.zeros(frames, dtype=np.float32)
128-
written = 0
129-
with self._webrtc_lock:
130-
while written < frames and self._webrtc_chunks:
131-
chunk = self._webrtc_chunks[0]
132-
take = min(frames - written, chunk.size)
133-
out[written:written + take] = chunk[:take]
134-
written += take
135-
if take >= chunk.size:
136-
self._webrtc_chunks.popleft()
137-
else:
138-
self._webrtc_chunks[0] = chunk[take:]
139-
self._webrtc_size -= take
140-
return out
141-
142114
def callback(self, data_out: np.ndarray, frames: int, time, status) -> None:
143115
if status:
144116
cloudlog.warning(f"soundd stream over/underflow: {status}")
145117
sound = self.get_sound_data(frames)
146-
webrtc = self.pop_webrtc_audio(frames)
147-
np.add(sound, webrtc, out=sound)
148118
np.clip(sound, -1.0, 1.0, out=sound)
149119
data_out[:frames, 0] = sound
150120

@@ -181,27 +151,11 @@ def get_stream(self, sd):
181151
sd._initialize()
182152
return sd.OutputStream(channels=1, samplerate=SAMPLE_RATE, callback=self.callback, blocksize=SAMPLE_BUFFER)
183153

184-
def _drain_webrtc_audio(self, webrtc_sock):
185-
"""Drain all pending webrtcAudioData messages from the raw socket.
186-
SubMaster only reads one message per socket per update(), which drops
187-
~60% of audio frames at the 20 Hz loop rate."""
188-
while True:
189-
raw = webrtc_sock.receive(non_blocking=True)
190-
if raw is None:
191-
break
192-
evt = log.Event.from_bytes(raw)
193-
raw_bytes = evt.webrtcAudioData.data
194-
if len(raw_bytes) > 0:
195-
pcm_float = np.frombuffer(raw_bytes, dtype=np.int16).astype(np.float32)
196-
pcm_float /= 32768.0
197-
self.push_webrtc_audio(pcm_float)
198-
199154
def soundd_thread(self):
200155
# sounddevice must be imported after forking processes
201156
import sounddevice as sd
202157

203158
sm = messaging.SubMaster(['selfdriveState', 'soundPressure', 'soundRequest'])
204-
webrtc_sock = messaging.sub_sock('webrtcAudioData')
205159

206160
with self.get_stream(sd) as stream:
207161
rk = Ratekeeper(20)
@@ -214,8 +168,6 @@ def soundd_thread(self):
214168
self.spl_filter_weighted.update(sm["soundPressure"].soundPressureWeightedDb)
215169
self.current_volume = self.calculate_volume(float(self.spl_filter_weighted.x))
216170

217-
self._drain_webrtc_audio(webrtc_sock)
218-
219171
self.get_audible_alert(sm)
220172

221173
rk.keep_time()

system/loggerd/loggerd.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ struct EncoderSettings {
4747
}
4848

4949
static EncoderSettings StreamEncoderSettings() {
50-
// int _stream_bitrate = getenv("STREAM_BITRATE") ? atoi(getenv("STREAM_BITRATE")) : 10'000'000;
5150
return EncoderSettings{.encode_type = cereal::EncodeIndex::Type::QCAMERA_H264, .bitrate = 4'000'000, .gop_size = 5};
5251
}
5352
};

0 commit comments

Comments
 (0)