├── UpBoardSwitch.JPG ├── MultiCameraEthernet.png ├── AlwaysRunningServer.bash ├── setup.py ├── .gitignore ├── README.md ├── EtherSenseClient.py ├── EtherSenseServer.py └── LICENSE /UpBoardSwitch.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krejov100/EtherSense/HEAD/UpBoardSwitch.JPG -------------------------------------------------------------------------------- /MultiCameraEthernet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krejov100/EtherSense/HEAD/MultiCameraEthernet.png -------------------------------------------------------------------------------- /AlwaysRunningServer.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # this statement checks if there is an instance of the EtherSenseServer running 3 | if [[ ! `ps -eaf | grep "python EtherSenseServer.py" | grep -v grep` ]]; then 4 | # if not, EtherSenseServer is started with the PYTHONPATH set due to cron not passing Env 5 | PYTHONPATH=$HOME/.local/lib/python2.7/site-packages python EtherSenseServer.py 6 | fi 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # install the dependancies from pip 2 | try: 3 | from pip import main as pipmain 4 | except: 5 | from pip._internal import main as pipmain 6 | pipmain(['install', 'numpy']) 7 | pipmain(['install', 'python-crontab']) 8 | pipmain(['install', 'opencv-python']) 9 | pipmain(['install', 'pyrealsense2']) 10 | pipmain(['install', 'cron_descriptor']) 11 | 12 | # using python-crontab, setup a job that is ran on the minute to check if the server is running. 13 | # https://pypi.org/project/python-crontab/ 14 | from crontab import CronTab 15 | #using the system crontab file as sudo is required for librealsense aquisition 16 | system_cron = CronTab(tabfile='/etc/crontab', user=False) 17 | #requires the shell enviroment as ./AlwaysRunningServer.bash 18 | system_cron.env['SHELL'] = '/bin/bash' 19 | job = system_cron.new(command="cd /home/$(ls /home/)/EtherSense; ./AlwaysRunningServer.bash >> /tmp/error.log 2>&1", user='root') 20 | job.every_reboot() 21 | i = 0 22 | #this while loop means that the are entrys in the cron file for each 5 sec interval 23 | while i < 60: 24 | #the cron job is cd to the EtherSense dir then run AlwaysRunningServer.bash, logging to /temp/error.log 25 | #have to use this $(ls /home/) to find home dir, assuming no other user spaces. 26 | job = system_cron.new(command="sleep %i; cd /home/$(ls /home/)/EtherSense; ./AlwaysRunningServer.bash >> /tmp/error.log 2>&1"%i, user='root') 27 | 28 | # this line sets the frequance of server checking, stars for all time slices means every minute 29 | job.setall('* * * * *') 30 | system_cron.write() 31 | i += 5 32 | print('cron job set to run ' + job.description()) 33 | 34 | 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | 106 | #pycharm 107 | .idea/ 108 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EtherSense 2 | Ethernet client and server for RealSense using python's Asyncore. 3 | 4 | ## Prerequisites 5 | Installation and Setup of Server: 6 | These steps assume a fresh install of Ubuntu 18.04 on an UpBoard but has also been tested on an Intel NUC. 7 | 8 | ``` 9 | sudo apt-get update; sudo apt-get upgrade; 10 | 11 | sudo apt-get install python 12 | 13 | sudo apt-get install python-pip 14 | 15 | sudo apt-get install git 16 | ``` 17 | 18 | Clone the repo then run: 19 | 20 | ``` 21 | sudo python setup.py 22 | ``` 23 | 24 | This will first install the pip dependencies, followed by the creation of cronjobs in the /etc/crontab file that maintains an instance of the Server running whenever the device is powered. 25 | 26 | ## Overview 27 | Mulicast broadcast is used to establish connections to servers that are present on the network. 28 | Once a server receives a request for connection from a client, Asyncore is used to establish a TCP connection for each server. 29 | Frames are collected from the camera using librealsense pipeline. It is then resized and send in smaller chucks as to conform with TCP. 30 | 31 | ### UpBoard PoE 32 | Below shows use of a PoE switch and PoE breakout devices(avalible from online retailers) powering each dedicated UpBoard: 33 | This configuration should allow for a number of RealSense cameras to be connected over distances greater then 30m 34 | ![Example Image](https://github.com/krejov100/EtherSense/blob/master/UpBoardSwitch.JPG) 35 | The 5 RealSense cameras are connected to each UpBoard using the provided USB3 cables. 36 | 37 | ### Client Window 38 | Below shows the result of having connected to five cameras over the local network: 39 | ![Example Image](https://github.com/krejov100/EtherSense/blob/master/MultiCameraEthernet.png) 40 | The window titles indicate the port which the frames are being received over. 41 | 42 | ## Error Logging 43 | Errors are piped to a log file stored in /tmp/error.log as part of the command that is setup in /etc/crontab 44 | 45 | ## NOTES 46 | 47 | ### Power Considerations 48 | The UpBoards require a 5v 4Amp power supply. When using PoE breakout adaptors I have found some stability issues, for example the device kernel can crash when the HDMI port is connected. As such I recommend running the UpBoard as a headless server when using PoE. 49 | 50 | ### Network bandwidth 51 | It is currently very easy to saturate the bandwidth of the Ethernet connection I have tested 5 servers connected to the same client without issue beyond limited framerate: 52 | 53 | cfg.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) 54 | 55 | self.decimate_filter.set_option(rs.option.filter_magnitude, 2) 56 | 57 | There are a number of strategies that can be used to increase this bandwidth but are left to the user for brevity and the specific tradeoff for your application, these include: 58 | 59 | Transmitting frames using UDP and allowing for frame drop, this requires implementation of packet ordering. 60 | 61 | Reducing the depth channel to 8bit. 62 | 63 | Reducing the resolution further. 64 | 65 | The addition of compression, either frame wise or better still temporal. 66 | 67 | Local recording of the depth data into a buffer, with asynchronous frame transfer. 68 | 69 | ## TroubleShooting Tips 70 | 71 | I first of all suggest installing and configuring openssh-server on each of the UpBoards allowing remote connection from the client machine. 72 | 73 | Check that the UpBoards are avalible on the local network using "nmap -sP 192.168.2.*" 74 | 75 | Check that the server is running on the UpBoard using "ps -eaf | grep "python EtherSenseServer.py" 76 | 77 | Finally check the log file at /tmp/error.log 78 | 79 | There might still be some conditions where the Server is running but not in a state to transmit, help in narrowing these cases would be much appreciated. 80 | 81 | -------------------------------------------------------------------------------- /EtherSenseClient.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import pyrealsense2 as rs 3 | import sys, getopt 4 | import asyncore 5 | import numpy as np 6 | import pickle 7 | import socket 8 | import struct 9 | import cv2 10 | 11 | 12 | print('Number of arguments:', len(sys.argv), 'arguments.') 13 | print('Argument List:', str(sys.argv)) 14 | mc_ip_address = '224.0.0.1' 15 | local_ip_address = '192.168.0.1' 16 | port = 1024 17 | chunk_size = 4096 18 | 19 | def main(argv): 20 | multi_cast_message(mc_ip_address, port, 'EtherSensePing') 21 | 22 | 23 | #UDP client for each camera server 24 | class ImageClient(asyncore.dispatcher): 25 | def __init__(self, server, source): 26 | asyncore.dispatcher.__init__(self, server) 27 | self.address = server.getsockname()[0] 28 | self.port = source[1] 29 | self.buffer = bytearray() 30 | self.windowName = self.port 31 | # open cv window which is unique to the port 32 | cv2.namedWindow("window"+str(self.windowName)) 33 | self.remainingBytes = 0 34 | self.frame_id = 0 35 | 36 | def handle_read(self): 37 | if self.remainingBytes == 0: 38 | # get the expected frame size 39 | self.frame_length = struct.unpack('