From 0d0e106cc6c0a7553f769ed438ca79368b8fe99b Mon Sep 17 00:00:00 2001 From: Tom Lonergan Date: Tue, 2 Jul 2024 01:26:48 +0100 Subject: [PATCH 1/2] bad optical flow we doing python --- config/pod.toml | 2 +- lib/sensors/optical_flow.py | 21 +++++++++++++++++++++ src/pod/main.cpp | 4 +++- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/sensors/optical_flow.py diff --git a/config/pod.toml b/config/pod.toml index 8c5658bc..6b6d3e71 100644 --- a/config/pod.toml +++ b/config/pod.toml @@ -9,7 +9,7 @@ host = "192.168.1.0" port = 4556 [raspberry] -nodes = ["state_machine", "mqtt_broker", "navigator"] +nodes = ["state_machine", "mqtt_broker", "navigator", "optical_flow"] [state_machine] # Possible transition tables are "full_run", diff --git a/lib/sensors/optical_flow.py b/lib/sensors/optical_flow.py new file mode 100644 index 00000000..d87ea286 --- /dev/null +++ b/lib/sensors/optical_flow.py @@ -0,0 +1,21 @@ +from pmw3901 import PMW3901 +import paho.mqtt.client as mqtt +import tomllib +import json +from time import sleep + +file = open('pod.toml', 'rb') +config = tomllib.load(file) +mqttc = mqtt.Client() +mqttc.connect(config['mqtt']['host'], config['mqtt']['port']) + + +sensor = PMW3901(spi_cs=0) +mqttc = mqtt.Client() + +while True: + (x, y) = sensor.get_motion() + measurement = {'x': x, 'y': y} + mqttc.publish('hyped/cart_2024/measurement/optical_flow', + json.dumps(measurement)) + sleep(0.05) diff --git a/src/pod/main.cpp b/src/pod/main.cpp index 2359ec6c..acaba404 100644 --- a/src/pod/main.cpp +++ b/src/pod/main.cpp @@ -33,7 +33,6 @@ int main(int argc, char **argv) return 1; } const std::string &ip = *optional_host_ip; - // parse the config file const toml::table config = toml::parse_file(config_file); const auto optional_ip = config["hostnames"][hostname].value(); @@ -80,6 +79,9 @@ int main(int argc, char **argv) } else if (node_name == "navigation") { auto navigator_config = config["navigation"]; hyped::navigation::Navigator::startNode(navigator_config, mqtt_ip, mqtt_port); + } else if (node_name == "optical_flow") { + execv("/usr/bin/python3", + const char *const argv[] = {"/usr/bin/python3", "optical_flow.py", nullptr};) } else { std::cerr << "Unknown node: " << node_name << "\n"; return 1; From 2b6929a4f0ef8e387252d4a65f084df22a198a14 Mon Sep 17 00:00:00 2001 From: Tom Lonergan Date: Tue, 2 Jul 2024 19:40:40 +0100 Subject: [PATCH 2/2] fix build --- lib/sensors/optical_flow.py | 7 ++++--- src/pod/main.cpp | 3 +-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/sensors/optical_flow.py b/lib/sensors/optical_flow.py index d87ea286..c293200b 100644 --- a/lib/sensors/optical_flow.py +++ b/lib/sensors/optical_flow.py @@ -2,7 +2,7 @@ import paho.mqtt.client as mqtt import tomllib import json -from time import sleep +import time file = open('pod.toml', 'rb') config = tomllib.load(file) @@ -15,7 +15,8 @@ while True: (x, y) = sensor.get_motion() - measurement = {'x': x, 'y': y} + measurement = {'header': {'timestamp': time.time(), 'priority': 1 + }, 'payload': {'x': x, 'y': y}} mqttc.publish('hyped/cart_2024/measurement/optical_flow', json.dumps(measurement)) - sleep(0.05) + time.sleep(0.05) diff --git a/src/pod/main.cpp b/src/pod/main.cpp index acaba404..95f48440 100644 --- a/src/pod/main.cpp +++ b/src/pod/main.cpp @@ -80,8 +80,7 @@ int main(int argc, char **argv) auto navigator_config = config["navigation"]; hyped::navigation::Navigator::startNode(navigator_config, mqtt_ip, mqtt_port); } else if (node_name == "optical_flow") { - execv("/usr/bin/python3", - const char *const argv[] = {"/usr/bin/python3", "optical_flow.py", nullptr};) + execl("/usr/bin/python3", "python", "optical_flow.py", nullptr); } else { std::cerr << "Unknown node: " << node_name << "\n"; return 1;