Skip to content

Commit

Permalink
Dot projector and flood light brightness draggable boxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
zrezke committed Jan 19, 2024
1 parent 361b1ed commit a6c8033
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/re_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl ReUi {
if response.changed() {
state.last_update = instant::Instant::now();
}
if state.delay_ms < state.last_update.elapsed().as_millis() as f32 {
if state.delay_ms <= state.last_update.elapsed().as_millis() as f32 {
*value = Num::from_f64(state.value as f64);
}
response
Expand Down
28 changes: 28 additions & 0 deletions crates/re_viewer/src/depthai/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ impl BackendCommChannel {
);
}

pub fn set_dot_brightness(&mut self, brightness: u32) {
self.ws.send(
serde_json
::to_string(
&(WsMessage {
kind: WsMessageType::SetDotBrightness,
data: WsMessageData::SetDotBrightness(brightness),
..Default::default()
})
)
.unwrap()
);
}

pub fn set_flood_brightness(&mut self, brightness: u32) {
self.ws.send(
serde_json
::to_string(
&(WsMessage {
kind: WsMessageType::SetFloodBrightness,
data: WsMessageData::SetFloodBrightness(brightness),
..Default::default()
})
)
.unwrap()
);
}

pub fn receive(&mut self) -> Option<WsMessage> {
self.ws.receive()
}
Expand Down
22 changes: 22 additions & 0 deletions crates/re_viewer/src/depthai/depthai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ pub struct DeviceConfig {
#[serde(default = "StereoDepthConfig::default_as_option")]
pub depth: Option<StereoDepthConfig>,
pub ai_model: AiModel,
#[serde(skip)]
pub dot_brightness: u32,
#[serde(skip)]
pub flood_brightness: u32,
}

impl Default for DeviceConfig {
Expand All @@ -333,6 +337,8 @@ impl Default for DeviceConfig {
depth_enabled: true,
depth: Some(StereoDepthConfig::default()),
ai_model: AiModel::default(),
dot_brightness: 0,
flood_brightness: 0,
}
}
}
Expand Down Expand Up @@ -809,6 +815,12 @@ impl State {
}
re_log::warn!("{}", warning.message);
}
WsMessageData::SetDotBrightness(_brightness) => {
re_log::debug!("Set dot brightness received from backend.")
}
WsMessageData::SetFloodBrightness(_brightness) => {
re_log::debug!("Set flood brightness received from backend.")
}
}
}

Expand Down Expand Up @@ -874,6 +886,16 @@ impl State {
}
}

pub fn set_dot_brightness(&mut self, brightness: u32) {
println!("Setting dot brightness to {}", brightness);
self.backend_comms.set_dot_brightness(brightness);
}

pub fn set_flood_brightness(&mut self, brightness: u32) {
println!("Setting flood brightness to {}", brightness);
self.backend_comms.set_flood_brightness(brightness);
}

pub fn reset(&mut self) {
*self = Self::default();
}
Expand Down
10 changes: 10 additions & 0 deletions crates/re_viewer/src/depthai/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub enum WsMessageData {
Devices(Vec<depthai::DeviceInfo>),
DeviceProperties(depthai::DeviceProperties),
Pipeline((depthai::DeviceConfig, RuntimeOnly)),
SetFloodBrightness(u32),
SetDotBrightness(u32),
Error(depthai::Error),
Info(depthai::Info),
Warning(depthai::Warning),
Expand All @@ -94,6 +96,8 @@ pub enum WsMessageType {
Devices,
DeviceProperties,
Pipeline,
SetFloodBrightness,
SetDotBrightness,
Error,
Info,
Warning,
Expand Down Expand Up @@ -149,6 +153,12 @@ impl<'de> Deserialize<'de> for BackWsMessage {
WsMessageType::Warning => {
WsMessageData::Warning(serde_json::from_value(message.data).unwrap_or_default())
}
WsMessageType::SetDotBrightness => {
WsMessageData::SetDotBrightness(serde_json::from_value(message.data).unwrap())
}
WsMessageType::SetFloodBrightness => {
WsMessageData::SetFloodBrightness(serde_json::from_value(message.data).unwrap())
}
};

Ok(Self {
Expand Down
27 changes: 27 additions & 0 deletions crates/re_viewer/src/ui/device_settings_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,33 @@ impl DeviceSettingsPanel {
}
},
);
let dot_drag = ctx.re_ui.labeled_dragvalue(
ui,
egui::Id::from("Dot brightness [mA]"),
None,
"Dot brightness [mA]",
&mut device_config.dot_brightness,
0..=1200);
if dot_drag.drag_released() {
ctx.depthai_state.set_dot_brightness(device_config.dot_brightness);
} else if dot_drag.changed() && !dot_drag.dragged() {
// Dragging isn't ongoing, but the value changed
ctx.depthai_state.set_dot_brightness(device_config.dot_brightness);
}
let flood_drag = ctx.re_ui.labeled_dragvalue(
ui,
egui::Id::from("Flood light brightness [mA]"),
None,
"Flood light brightness [mA]",
&mut device_config.flood_brightness,
0..=1500,
);
if flood_drag.drag_released() {
ctx.depthai_state.set_flood_brightness(device_config.flood_brightness);
} else if flood_drag.changed() && !flood_drag.dragged() {
// Dragging isn't ongoing, but the value changed
ctx.depthai_state.set_flood_brightness(device_config.flood_brightness);
}
ctx.re_ui.labeled_toggle_switch(
ui,
"LR Check",
Expand Down
32 changes: 25 additions & 7 deletions rerun_py/depthai_viewer/_backend/config_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import atexit
import json
from enum import Enum
from enum import Enum, auto
from multiprocessing import Queue
from queue import Empty as QueueEmptyException
from signal import SIGINT, signal
Expand Down Expand Up @@ -37,12 +37,14 @@

class Action(Enum):
UPDATE_PIPELINE = 0
SELECT_DEVICE = 1
GET_SUBSCRIPTIONS = 2
SET_SUBSCRIPTIONS = 3
GET_PIPELINE = 4
RESET = 5 # When anything bad happens, a reset occurs (like closing ws connection)
GET_AVAILABLE_DEVICES = 6
SELECT_DEVICE = auto()
GET_SUBSCRIPTIONS = auto()
SET_SUBSCRIPTIONS = auto()
GET_PIPELINE = auto()
SET_FLOOD_BRIGHTNESS = auto()
SET_DOT_BRIGHTNESS = auto()
RESET = auto() # When anything bad happens, a reset occurs (like closing ws connection)
GET_AVAILABLE_DEVICES = auto()


def dispatch_action(action: Action, **kwargs) -> Message: # type: ignore[no-untyped-def]
Expand Down Expand Up @@ -123,6 +125,22 @@ async def ws_api(websocket: WebSocketServerProtocol) -> None:
print("Missing device id")
continue
await send_message(websocket, dispatch_action(Action.SELECT_DEVICE, device_id=device_id))
elif message_type == MessageType.SET_FLOOD_BRIGHTNESS:
data = message.get("data", {})
flood_brightness = data.get(message_type, None)
if flood_brightness is None:
print("Missing", message_type)
continue
await send_message(
websocket, dispatch_action(Action.SET_FLOOD_BRIGHTNESS, flood_brightness=flood_brightness)
)
elif message_type == MessageType.SET_DOT_BRIGHTNESS:
data = message.get("data", {})
dot_brightness = data.get(message_type, None)
if dot_brightness is None:
print("Missing dot", message_type)
continue
await send_message(websocket, dispatch_action(Action.SET_DOT_BRIGHTNESS, dot_brightness=dot_brightness))
else:
print("Unknown message type: ", message_type)
continue
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/depthai_viewer/_backend/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Device:

def __init__(self, device_id: str, store: Store):
self.id = device_id
self.set_oak(OakCamera(device_id))
self.set_oak(OakCamera(device_id, args={"irFloodBrightness": 0, "irDotBrightness": 0}))
self.store = store
self._packet_handler = PacketHandler(self.store, self.get_intrinsic_matrix)
print("Oak cam: ", self._oak)
Expand Down Expand Up @@ -261,7 +261,7 @@ def reconnect_to_oak(self) -> Message:
if self.id in available_devices:
break
try:
self.set_oak(OakCamera(self.id))
self.set_oak(OakCamera(self.id, args={"irFloodBrightness": 0, "irDotBrightness": 0}))
return InfoMessage("Successfully reconnected to device")
except RuntimeError as e:
print("Failed to create oak camera")
Expand Down
16 changes: 15 additions & 1 deletion rerun_py/depthai_viewer/_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,28 @@ def handle_action(self, action: Action, **kwargs) -> Message: # type: ignore[no
return self.store.pipeline_config # type: ignore[return-value]
elif action == Action.RESET:
return self.on_reset()
elif action == Action.SET_FLOOD_BRIGHTNESS:
self.store.set_flood_brightness(kwargs.get("flood_brightness", 0))
print("Set flood: ", kwargs.get("flood_brightness", 0))
if self._device:
self._device._oak.device.setIrFloodLightBrightness(self.store.flood_brightness)
return InfoMessage("Floodlight set successfully")
return ErrorMessage("No device selected")
elif action == Action.SET_DOT_BRIGHTNESS:
print("Set dot: ", kwargs.get("dot_brightness", 0))
self.store.set_dot_brightness(kwargs.get("dot_brightness", 0))
if self._device:
self._device._oak.device.setIrLaserDotProjectorBrightness(self.store.dot_brightness)
return InfoMessage("Dot projector set successfully")
return ErrorMessage("No device selected")
return ErrorMessage(f"Action: {action} not implemented")

def run(self) -> None:
"""Handles ws messages and polls OakCam."""
while True:
try:
action, kwargs = self.action_queue.get(timeout=0.0001)
print("Handling action: ", action)
# print("Handling action: ", action)
self.result_queue.put(self.handle_action(action, **kwargs))
except QueueEmptyException:
pass
Expand Down
2 changes: 2 additions & 0 deletions rerun_py/depthai_viewer/_backend/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class MessageType:
PIPELINE = "Pipeline" # Get or Set pipeline
DEVICES = "Devices" # Get device list
DEVICE = "DeviceProperties" # Get or set device
SET_FLOOD_BRIGHTNESS = "SetFloodBrightness" # Set floodlight
SET_DOT_BRIGHTNESS = "SetDotBrightness" # Set floodlight
ERROR = "Error" # Error message
INFO = "Info" # Info message
WARNING = "Warning" # Warning message
Expand Down
16 changes: 16 additions & 0 deletions rerun_py/depthai_viewer/_backend/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Store:
_pipeline_config: Optional[PipelineConfiguration] = None
_subscriptions: List[Topic] = []
_send_message_queue: Queue # type: ignore[type-arg]
_dot_brightness: int = 0
_flood_brightness: int = 0

def __init__(self) -> None:
self._send_message_queue = Queue()
Expand All @@ -22,6 +24,12 @@ def set_pipeline_config(self, pipeline_config: PipelineConfiguration) -> None:
def set_subscriptions(self, subscriptions: List[Topic]) -> None:
self._subscriptions = subscriptions

def set_dot_brightness(self, brightness: int) -> None:
self._dot_brightness = brightness

def set_flood_brightness(self, brightness: int) -> None:
self._flood_brightness = brightness

def reset(self) -> None:
self._pipeline_config = None
self._subscriptions = []
Expand All @@ -34,5 +42,13 @@ def pipeline_config(self) -> Optional[PipelineConfiguration]:
def subscriptions(self) -> List[Topic]:
return self._subscriptions

@property
def dot_brightness(self) -> int:
return self._dot_brightness

@property
def flood_brightness(self) -> int:
return self._flood_brightness

def send_message_to_frontend(self, message: Message) -> None:
self._send_message_queue.put(message)

0 comments on commit a6c8033

Please sign in to comment.