From fd263bc16d4c3a99052b7b614b0264d91293d038 Mon Sep 17 00:00:00 2001
From: Mathieu Beligon <mathieu@feedly.com>
Date: Sun, 8 Mar 2020 14:41:45 -0400
Subject: [PATCH] [init] Moved project from cv-code repo

---
 .gitignore                                    | 138 ++++++++++++++++++
 README.md                                     |   6 +-
 computer_vision_launcher/__init__.py          |   0
 .../cv_program_launcher.py                    |  21 +++
 fake_cv_program.py                            |  24 +++
 main.py                                       |   7 +
 6 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100644 .gitignore
 create mode 100644 computer_vision_launcher/__init__.py
 create mode 100644 computer_vision_launcher/cv_program_launcher.py
 create mode 100644 fake_cv_program.py
 create mode 100644 main.py

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1c76ae1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,138 @@
+# Mac
+.idea
+
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+pip-wheel-metadata/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+cover/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+#   For a library or package, you might want to ignore these files since the code is
+#   intended to run in multiple environments; otherwise, check them in:
+# .python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# pytype static type analyzer
+.pytype/
\ No newline at end of file
diff --git a/README.md b/README.md
index c95cc69..0e3b5e8 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,7 @@
 # Ros Bridge
 
-Bridge between CV code (python3) and ROS
\ No newline at end of file
+Bridge between CV code (python3) and ROS
+
+## Setup
+
+python2.7
diff --git a/computer_vision_launcher/__init__.py b/computer_vision_launcher/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/computer_vision_launcher/cv_program_launcher.py b/computer_vision_launcher/cv_program_launcher.py
new file mode 100644
index 0000000..aac1df2
--- /dev/null
+++ b/computer_vision_launcher/cv_program_launcher.py
@@ -0,0 +1,21 @@
+from os import pipe, fdopen
+from subprocess import Popen
+
+
+class CVProgramLauncher:
+    def __init__(self, team_color):
+        self.team_color = team_color
+        self._cv_process = None
+
+    def __enter__(self):
+        fd_input, fd_output = pipe()
+        self._input_fd = fdopen(fd_input)
+        self._cv_process = Popen(["python", "./fake_cv_program.py", str(fd_output), self.team_color])
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self._cv_process.terminate()
+        self._cv_process.wait()
+
+    def get_next_position(self):
+        return self._input_fd.readline()
diff --git a/fake_cv_program.py b/fake_cv_program.py
new file mode 100644
index 0000000..bd3a165
--- /dev/null
+++ b/fake_cv_program.py
@@ -0,0 +1,24 @@
+import json
+import signal
+import sys
+from math import pi
+from os import fdopen
+from random import random
+from time import sleep
+
+
+output_fd, team_color = sys.argv[1:]
+
+output_fds = fdopen(int(output_fd), "w", buffering=1)
+
+
+def exit_gracefully(signum: int, frame):
+    print("exiting gracefully")
+    exit()
+
+
+signal.signal(signal.SIGTERM, exit_gracefully)
+
+while True:
+    sleep(1)
+    output_fds.write(json.dumps({"d": 12 * random(), "theta": pi / 3, "team": team_color}) + "\n")
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..79e4da3
--- /dev/null
+++ b/main.py
@@ -0,0 +1,7 @@
+from computer_vision_launcher.cv_program_launcher import CVProgramLauncher
+
+
+if __name__ == "__main__":
+    with CVProgramLauncher("red") as cv_program:
+        for i in range(2):
+            print i, cv_program.get_next_position()
-- 
GitLab