Skip to content

Commit

Permalink
feat(klipper): fully working camera-streamer
Browse files Browse the repository at this point in the history
  • Loading branch information
truelecter committed Sep 11, 2023
1 parent afa2a02 commit 66ce5aa
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 164 deletions.
404 changes: 404 additions & 0 deletions cells/klipper/modules/camera-streamer.nix

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions cells/klipper/packages/camera-streamer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,15 @@ in
$preInstallPhase
mkdir -p $out/bin
cp camera-streamer list-devices $out/bin/
$postInstallPhase
'';

meta = with lib; {
description = "Touchscreen GUI that interfaces with Klipper via Moonraker";
homepage = "https://klipperscreen.readthedocs.io/en/latest/";
description = "High-performance low-latency camera streamer for Raspberry PI's";
homepage = "https://github.com/ayufan/camera-streamer/tree/main";
platforms = platforms.linux;
license = licenses.gpl3Only;
# license = licenses.gpl3Only;
};
}
90 changes: 33 additions & 57 deletions cells/nixos/hosts/voron/_camera.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,44 @@
pkgs.camera-streamer
];

tl.services.tailscale-tls.enable = true;

services.go2rtc = {
enable = true;
settings = {
ffmpeg.bin = "${lib.getBin pkgs.ffmpeg_6-full}/bin/ffmpeg";
streams = {
nozzle = "ffmpeg:device?video=/dev/v4l/by-id/usb-XCG-221208-J_3DO_NOZZLE_CAMERA_4K_01.00.00-video-index0&video_size=640x480&framerate=30&input_format=yuyv422#video=h264#hardware=v4l2m2m";
printer = "ffmpeg:device?video=/dev/v4l/by-id/usb-046d_HD_Pro_Webcam_C920_89E7787F-video-index0&video_size=640x480&framerate=30&input_format=h264#rotate=180#video=h264#hardware=v4l2m2mls";
};
log = {
format = "text";
level = "debug";
};
};
};

# services.mediamtx = {
# enable = false;
# settings = {
# hlsAlwaysRemux = true;
# hlsVariant = "lowLatency";
# hlsSegmentDuration = "200ms";
# hlsPartDuration = "200ms";

# hlsEncryption = true;
# hlsServerKey = "${config.tl.services.tailscale-tls.target}/key.key";
# hlsServerCert = "${config.tl.services.tailscale-tls.target}/cert.crt";

# paths = {
# cam = {
# runOnInit = builtins.replaceStrings ["\n"] [""] "${pkgs.ffmpeg_5-full}/bin/ffmpeg -vcodec h264 -framerate 25 -video_size 640x480 -f v4l2
# -i /dev/v4l/by-id/usb-046d_HD_Pro_Webcam_C920_89E7787F-video-index0
# -c:v libx264 -preset ultrafast -pix_fmt yuv420p
# -flags low_delay -strict experimental -g 50 -crf 18
# -f rtsp rtsp://localhost:8554/cam";
# runOnInitRestart = true;
# # runOnReady = "/bin/sh -c '${pkgs.v4l-utils}/bin/v4l2-ctl -c focus_auto=0; ${pkgs.v4l-utils}/bin/v4l2-ctl -c sharpness=140 -c focus_absolute=30'";
# };
# nozzle = {
# runOnInit = builtins.replaceStrings ["\n"] [""] "${pkgs.ffmpeg_5-full}/bin/ffmpeg -vcodec h264 -framerate 25 -video_size 640x480 -f v4l2
# -i /dev/v4l/by-id/usb-XCG-221208-J_3DO_NOZZLE_CAMERA_4K_01.00.00-video-index0
# -c:v libx264 -preset ultrafast -pix_fmt yuv420p
# -flags low_delay -strict experimental -g 50 -crf 18
# -f rtsp rtsp://localhost:8555/cam";
# runOnInitRestart = true;
# runOnReady = "/bin/sh -c '${pkgs.v4l-utils}/bin/v4l2-ctl -c focus_auto=0; ${pkgs.v4l-utils}/bin/v4l2-ctl -c sharpness=140 -c focus_absolute=30'";
# };
# };
# };
# };

users.groups.dma-heap = {};

services.udev.extraRules = ''
SUBSYSTEM=="dma_heap", GROUP="dma-heap", MODE="0660"
'';

# systemd.services.mediamtx = {
# after = ["tailscale-tls.service"];
# partOf = ["tailscale-tls.service"];
tl.services.camera-streamer.instances = {
nozzle = {
settings = {
camera = {
path = "/dev/v4l/by-id/usb-XCG-221208-J_3DO_NOZZLE_CAMERA_4K_01.00.00-video-index0";
width = 640;
height = 480;
fps = 60;
};

http = {
port = 8080;
listen = "0.0.0.0";
};
};

# serviceConfig.SupplementaryGroups = lib.mkForce "video tailscale-tls dma-heap";
# };
nginx.enable = true;
};
printer = {
settings = {
camera = {
path = "/dev/v4l/by-id/usb-046d_HD_Pro_Webcam_C920_89E7787F-video-index0";
width = 640;
height = 480;
fps = 60;
format = "H264";
};

http.port = 8081;
};

nginx.enable = true;
};
};
}
27 changes: 26 additions & 1 deletion cells/nixos/hosts/voron/_hardware-configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
grub.enable = false;
generic-extlinux-compatible = {
enable = true;
configurationLimit = 1;
configurationLimit = 5;
};
raspberryPi = {
enable = false;
Expand Down Expand Up @@ -62,6 +62,31 @@

hardware = {
enableRedistributableFirmware = true;
deviceTree = {
filter = "bcm2711-rpi-cm4.dtb";
overlays = [
# Needed for USB
{
name = "xhci-fix";
dtsText = ''
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2711";
fragment@0 {
//target-path = "/scb/xhci@7e9c0000";
target = <&xhci>;
__overlay__ {
status = "okay";
};
};
};
'';
}
#
];
};
};

environment.systemPackages = with pkgs; [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# [temperature_sensor CM4]
# sensor_type: temperature_host

[temperature_fan CM4]
sensor_type: temperature_host
pin: PB9
target_temp: 60.0
min_temp: 0
max_temp: 80 # highest is 85

hardware_pwm: true
max_power: 0.8
tachometer_pin: ^PC15
tachometer_ppr: 2

shutdown_speed: 0.3
kick_start_time: 0.5
off_below: 0.19

control: pid
pid_Kp: 2.0
pid_Ki: 5.0
pid_Kd: 0.5
pid_deriv_time: 2.0
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ square_corner_velocity: 5.0
[temperature_sensor MCU]
sensor_type: temperature_mcu

[temperature_sensor SoC]
sensor_type: temperature_host

[stepper_x]
step_pin: PE2
dir_pin: !PB4
Expand Down Expand Up @@ -167,15 +164,6 @@ max_power: 0.8
tachometer_pin: ^PC14
tachometer_ppr: 2

[controller_fan skirt_fan_2]
pin: PB9
kick_start_time: 0.5
heater: heater_bed
hardware_pwm: true
max_power: 0.8
tachometer_pin: ^PC15
tachometer_ppr: 2

[board_pins]
aliases:
EXP1_1=PE9, EXP1_2=PE10,
Expand Down
59 changes: 17 additions & 42 deletions cells/nixos/hosts/voron/_klipper/klipper-screen.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,49 +76,24 @@
# environment.systemPackages = [pkgs.gnome.adwaita-icon-theme];

hardware.deviceTree = {
filter = "bcm2711-rpi-cm4.dtb";
overlays = [
# {
# name = "rpi-ft5406";
# dtboFile = "${pkgs.device-tree_rpi.overlays}/rpi-ft5406.dtbo";
# }
{
name = "xhci-fix";
dtsText = ''
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2711";
fragment@0 {
//target-path = "/scb/xhci@7e9c0000";
target = <&xhci>;
__overlay__ {
status = "okay";
};
};
};
'';
}
{
name = "disable-bt";
dtboFile = "${pkgs.device-tree_rpi.overlays}/disable-bt.dtbo";
}
{
name = "vc4-kms-v3d-pi4";
dtboFile = "${pkgs.device-tree_rpi.overlays}/vc4-kms-v3d-pi4.dtbo";
}
{
name = "vc4-kms-dsi-7inch-overlay";
dtboFile = "${pkgs.device-tree_rpi.overlays}/vc4-kms-dsi-7inch.dtbo";
}
overlays = let
overlay = name: {
name = name;
dtboFile = "${pkgs.device-tree_rpi.overlays}/${name}.dtbo";
};
in [
(overlay "disable-bt")
(overlay "vc4-kms-v3d-pi4")
(overlay "vc4-kms-dsi-7inch")
# replace with vc4-kms-dsi-waveshare-panel when kernel is updated
(overlay "rpi-ft5406")
];
};

# hardware.raspberry-pi."4" = {
# dwc2 = {
# enable = false;
# dr_mode = "host";
# };
# };
hardware.raspberry-pi."4" = {
i2c0 = {
enable = true;
frequency = 50000;
};
};
}
14 changes: 0 additions & 14 deletions cells/nixos/hosts/voron/_klipper/moonraker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,4 @@ in {
};

security.polkit.enable = true;

systemd.services.moonraker = {
# script = lib.mkForce ''
# cp /etc/moonraker.cfg ${moonrakerCfg.stateDir}/config/moonraker-temp.cfg
# chmod u+w ${moonrakerCfg.stateDir}/config/moonraker-temp.cfg
# exec ${pkgs.moonraker}/bin/moonraker -c ${moonrakerCfg.stateDir}/config/moonraker-temp.cfg -d ${moonrakerCfg.stateDir}/
# '';

serviceConfig = {
SupplementaryGroups = config.users.users.klipper.extraGroups;
# Environment = "PYTHONPATH=${pkgs.python39Packages.libgpiod}";
# BindReadOnlyPaths = "${pkgs.python39Packages.libgpiod}/lib/python3.9:/usr/lib/python3.9:norbind";
};
};
}
1 change: 1 addition & 0 deletions cells/nixos/hosts/voron/_minimize.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
environment.systemPackages = [
pkgs.pciutils
pkgs.usbutils
pkgs.i2c-tools
];

# environment.noXlibs = true;
Expand Down
34 changes: 0 additions & 34 deletions cells/nixos/hosts/voron/_wifi.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,6 @@
# ];
# };

# hardware.deviceTree.overlays = [
# {
# name = "dwc2";
# dtsText = ''
# /dts-v1/;
# /plugin/;

# /{
# compatible = "brcm,bcm2711";

# fragment@0 {
# target = <&usb>;
# #address-cells = <1>;
# #size-cells = <1>;
# dwc2_usb: __overlay__ {
# compatible = "brcm,bcm2835-usb";
# dr_mode = "host";
# g-np-tx-fifo-size = <32>;
# g-rx-fifo-size = <558>;
# g-tx-fifo-size = <512 512 512 512 512 256 256>;
# status = "okay";
# };
# };

# __overrides__ {
# dr_mode = <&dwc2_usb>, "dr_mode";
# g-np-tx-fifo-size = <&dwc2_usb>,"g-np-tx-fifo-size:0";
# g-rx-fifo-size = <&dwc2_usb>,"g-rx-fifo-size:0";
# };
# };
# '';
# }
# ];

boot = {
extraModprobeConfig = ''
options cfg80211 ieee80211_regdom="UA"
Expand Down

0 comments on commit 66ce5aa

Please sign in to comment.