Skip to content

Commit

Permalink
implement camera to controller comms
Browse files Browse the repository at this point in the history
  • Loading branch information
bluskript committed May 11, 2022
1 parent a9a0f7f commit 48441ae
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 56 deletions.
12 changes: 6 additions & 6 deletions camera/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
from handlers.display import DisplayHandler
from handlers.noop import NoopHandler
from handlers.test import TestHandler

from lib.camera import Camera
from lib.ipc import IPC, new_fifo_ipc
from lib.streaming import StreamingFrameHandler

if __name__ == "__main__":
try:
ipc = IPC()
handler = DisplayHandler(None, False)
ipc = new_fifo_ipc("socket")
handler = DisplayHandler(ipc, False)
handler = StreamingFrameHandler(handler, constants.SERVER_ADDRESS)
#joe = cv2.imread("cha.jpg")
#joe = cv2.resize(joe, (600, 600))
#while True:
# joe = cv2.imread("cha.jpg")
# joe = cv2.resize(joe, (600, 600))
# while True:
# handler.handle_frame(joe)
cam = Camera(handler)
cam.run()
Expand Down
55 changes: 30 additions & 25 deletions camera/lib/ipc.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
from os import mkfifo
import os
import unittest
from os import mkfifo
from typing import BinaryIO


class IPC:
def __init__(self, io: BinaryIO):
self.io = io

def send_data(self, data: bytes):
size = len(data)
self.io.write(size.to_bytes(4, 'little')) # 4 bytes = 32 bits
self.io.write(data)
self.io.flush()

def receive_data(self) -> bytes:
size = int.from_bytes(self.io.read(2), byteorder='little')
return self.io.read(size)
def __init__(self, io: BinaryIO):
self.io = io

def send_data(self, data: bytes):
size = len(data)
self.io.write(size.to_bytes(4, "little")) # 4 bytes = 32 bits
self.io.write(data)
self.io.flush()

def receive_data(self) -> bytes:
size = int.from_bytes(self.io.read(2), byteorder="little")
return self.io.read(size)


def new_fifo_ipc(path: str):
mkfifo(path, 0o660)
reader = open(path, 'wb')
return IPC(reader)
writer = open(path, "rb+", 0)
print("c")
return IPC(writer)


class TestIPC(unittest.TestCase):
def test_serialize(self):
test_path = "camerastream"
try:
os.remove(test_path)
except OSError:
pass
comms = new_fifo_ipc(test_path)
comms.send_data(b"hello uwu")
print("done")
def test_serialize(self):
test_path = "camerastream"
try:
os.remove(test_path)
except OSError:
pass
comms = new_fifo_ipc(test_path)
comms.send_data(b"hello uwu")
print("done")


if __name__ == "__main__":
unittest.main()
unittest.main()
2 changes: 0 additions & 2 deletions controller/.cargo/config

This file was deleted.

6 changes: 5 additions & 1 deletion controller/src/math/angles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use std::f32::consts::PI;

pub fn distance(first: f32, second: f32) -> f32 {
let phi = (first - second).abs() % (2.0 * PI);
return if phi > PI { (2.0 * PI) - phi } else { phi };
if phi > PI {
(2.0 * PI) - phi
} else {
phi
}
}

#[test]
Expand Down
7 changes: 3 additions & 4 deletions controller/src/math/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub struct Vec2 {
pub fn angle_between(first: Vec2, second: Vec2) -> f64 {
let first_rad = first.angle_rad();
let second_rad = second.angle_rad();
let diff = second_rad - first_rad;
return diff;
second_rad - first_rad
}

pub fn dot(first: Vec2, second: Vec2) -> f64 {
Expand Down Expand Up @@ -69,10 +68,10 @@ impl Mul<f64> for Vec2 {
type Output = Self;

fn mul(self, rhs: f64) -> Self::Output {
return Vec2 {
Vec2 {
x: self.x * rhs,
y: self.y * rhs,
};
}
}
}

Expand Down
1 change: 1 addition & 0 deletions controller/src/modules/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Module for Camera {
let data = ipc::read_msgpack::<modules::state::CameraMessage, _>(file)
.await
.with_context(|| "failed to read packet")?;
tracing::debug!("received camera packet: {:?}", data);
state.lock().data.camera_data = data;
}
let (camera_config, data) = {
Expand Down
11 changes: 5 additions & 6 deletions controller/src/modules/line_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ use test_case::test_case;
pub async fn test_flips(first: &[bool], second: &[bool], flip: bool) {
let mutex = Arc::new(Mutex::new(State::default()));
let mut line = Line::default();
let mut state = mutex.lock();
state.line_detections = Vec::from(first);
mutex.lock().line_detections = Vec::from(first);
line.tick(&mut Arc::clone(&mutex)).await.unwrap();
state.print_state();
state.line_detections = Vec::from(second);
mutex.lock().print_state();
mutex.lock().line_detections = Vec::from(second);
line.tick(&mut Arc::clone(&mutex)).await.unwrap();
state.print_state();
mutex.lock().print_state();

assert_eq!(state.line_flipped, flip);
assert_eq!(mutex.lock().line_flipped, flip);
}

#[test_case(Vec2::new(-0.1, 0.0), Vec2::new(0.1, 0.0), true; "crosses line when crosses axis")]
Expand Down
27 changes: 16 additions & 11 deletions debugger/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,21 @@
{{ frame_count }}
</div>
</div>
<img
class="h-full"
v-if="active_tab === 'Camera'"
:src="`http://${host}${camera_port}/stream.mjpg`"
/>
<div class="h-full flex justify-center items-center">
<img
class="max-w-full max-h-full"
v-if="active_tab === 'Camera'"
:src="`http://${host}${camera_port}/stream.mjpg`"
/>
</div>
<LineView
v-if="current_frame && active_tab === 'Line'"
:data="current_frame"
/>
<BallView v-if="current_frame && active_tab === 'Ball'" :data="current_frame" />
<BallView
v-if="current_frame && active_tab === 'Ball'"
:data="current_frame"
/>
<JSONEditor
root
label="config"
Expand Down Expand Up @@ -163,11 +168,11 @@ import BaseTextField from "./components/BaseTextField.vue";
import JSONEditor from "./components/JSONEditor.vue";
import LineView from "./LineView.vue";
import {
Config,
DataObject,
DataSource,
ServerSource,
TextSource
Config,
DataObject,
DataSource,
ServerSource,
TextSource,
} from "./logic/dataSources";
const host = useLocalStorage("host", "127.0.0.1");
Expand Down
2 changes: 1 addition & 1 deletion scripts/camera.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ while read -r filename event; do
tput setaf 3
echo "detected camera changes, uploading..."
tput sgr0
rsync -avz ./camera $PI_USER@$1:/home/$PI_USER/ez-aquarii/camera
rsync -avz ./camera $PI_USER@$1:/home/$PI_USER/ez-aquarii
done

0 comments on commit 48441ae

Please sign in to comment.