Skip to content

Commit

Permalink
Merge pull request #17 from fergusL/grpc-stuff
Browse files Browse the repository at this point in the history
gRPC c++ driver/python server
  • Loading branch information
fergusL authored Aug 5, 2019
2 parents 0f508f4 + 45c5bc2 commit e71562a
Show file tree
Hide file tree
Showing 32 changed files with 9,275 additions and 29 deletions.
116 changes: 87 additions & 29 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,98 @@
Huntsman Telescope dome control
-------------------------------
===============================

.. image:: http://img.shields.io/badge/powered%20by-AstroPy-orange.svg?style=flat
:target: http://www.astropy.org
:alt: Powered by Astropy Badge

Code to run the Huntsman Telescope dome control hardware

Overview
--------

The Huntsman Telescope dome control software has two components,
one written in c++ the other in python.

The C++ component is a X2Dome driver that interfaces with TheSkyX
and passes commands through to the python component. This is done
using the `gRPC <https://grpc.io/>`_ framework, allowing the c++
component to pass infomation to the python component.

The python component is implemented on a raspberry pi that controls
the dome rotation motor. It controls the activation of the motor
and tracks the dome position using an encoder. It returns infomation
(such as dome position) to the c++ component using gRPC as well.

The c++ code is built around Software Bisque's X2 standard. For more
infomation on this `see here <https://www.bisque.com/x2standard/class_x2_dome.html#a7ffd792950cdd0abe1b022e7a8caff9e>`.

Requirements
---------------

``grpc python`` See instructions `here <https://grpc.io/docs/quickstart/python/>`_.
``grpc c++`` See instructions `here <https://grpc.io/docs/quickstart/cpp/>`_.

Getting Started
---------------

The code for the Huntsman dome driver is contained in the
``domehunter/protos/src`` directory. This directory contains both
human written files and files automatically generated by gRPC
tools. The human written files are,

* ``main.cpp`` - establishes main library to X2 driver (mostly copy/paste from example)
* ``main.h`` - header for main.cpp
* ``x2dome.cpp`` - the library code that serves the RPC from TSX to python server
* ``x2dome.h`` - header for x2dome.cpp
* ``hx2dome.proto`` - language agnostic RPC definitions used by everthing
* ``hx2dome.proto_server.py`` - python server that receives RPC from TSX

The remaining cpp and python files are automatically produced
by gRPC and shouldn't need to be looked at. If for some reason
you want to generate these files yourself, see the
*gRPC automatically generated files* section below.

The files for compilation and installation are found in the
``domehunter/protos/`` directory. The relevant files are,


* ``domelistHuntsmanDome.txt``
* ``TheSkyX_plugin_install.sh``
* ``Makefile``

The first two are files are used to install the compiled c++
driver. You should be able to simply run the shell script once
the driver is compiled and located in the ``domehunter/protos/``
directory, with filename ``libHuntsmanDome.so``.

|
In order to compile the driver simply run the makefile recipe.

|
Once the driver is installed in TheSkyX, it can be selected from
the dome selection menu. Before issuing any commands, start the
``domehunter/protos/src/hx2dome.proto_server.py`` in a new terminal.
When you issue a command through TheSkyX, the c++ driver will send
a remote procedure call through to the gRPC python server. Currently
the server will just return a dummy response to the c++ driver,
which will be passed to TheSkyX. In the future the gRPC python server
will be used to issue commands to the dome hardware.

gRPC automatically generated files
----------------------------------

In the ``domehunter/protos/`` directory there are a number of shell
scripts. These can be used to generate the gRPC files within the ``src/``
directory. These scripts contain path variables that may need to be
adjusted to your local machine. You shouldn't need to worry about
this as the generated files are committed to the repositry and
shouldn't need to be generated.





License
-------
Expand All @@ -18,34 +104,6 @@ which is licensed under the BSD 3-clause licence. See the licenses folder for
more information.


Contributing
------------

We love contributions! huntsman-dome is open source,
built on open source, and we'd love to have you hang out in our community.

**Imposter syndrome disclaimer**: We want your help. No, really.

There may be a little voice inside your head that is telling you that you're not
ready to be an open source contributor; that your skills aren't nearly good
enough to contribute. What could you possibly offer a project like this one?

We assure you - the little voice in your head is wrong. If you can write code at
all, you can contribute code to open source. Contributing to open source
projects is a fantastic way to advance one's coding skills. Writing perfect code
isn't the measure of a good developer (that would disqualify all of us!); it's
trying to create something, making mistakes, and learning from those
mistakes. That's how we all improve, and we are happy to help others learn.

Being an open source contributor doesn't just mean writing code, either. You can
help out by writing documentation, tests, or even giving feedback about the
project (and yes - that includes giving feedback about the contribution
process). Some of these contributions may be the most valuable to the project as
a whole, because you're coming to the project with fresh eyes, so you can see
the errors and assumptions that seasoned contributors have glossed over.

Note: This disclaimer was originally written by
`Adrienne Lowe <https://github.com/adriennefriend>`_ for a
`PyCon talk <https://www.youtube.com/watch?v=6Uj746j9Heo>`_, and was adapted by
huntsman-dome based on its use in the README file for the
`MetPy project <https://github.com/Unidata/MetPy>`_.
36 changes: 36 additions & 0 deletions domehunter/protos/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Makefile for libHuntsmanDome

CXX = g++
CFLAGS = -fPIC -Wall -Wextra -O2 -g -DSB_LINUX_BUILD -I. -I./src/licensedinterfaces/\
-L/usr/local/lib -lprotobuf -pthread -lgrpc++ -pthread -I/usr/local/include

CPPFLAGS = -fPIC -Wall -Wextra -O2 -g -DSB_LINUX_BUILD -I. -I./src/licensedinterfaces/ -I/usr/local/include\
-L/usr/local/lib -lprotobuf -pthread -lgrpc++ -pthread

CXXFLAGS += -std=c++11

LDFLAGS = -shared -lstdc++ -L/usr/local/lib -lprotobuf -pthread -lgrpc++\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl


RM = rm -f
STRIP = strip
TARGET_LIB = libHuntsmanDome.so

SRCS = src/main.cpp src/x2dome.cpp src/hx2dome.grpc.pb.cpp src/hx2dome.pb.cpp
OBJS = $(SRCS:.cpp=.o)

.PHONY: all
all: ${TARGET_LIB}

$(TARGET_LIB): $(OBJS)
$(CXX) $^ ${LDFLAGS} -o $@
$(STRIP) $@ >/dev/null 2>&1 || true

$(SRCS:.cpp=.d):%.d:%.cpp
$(CXX) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -MM $< >$@


.PHONY: clean
clean:
${RM} ${TARGET_LIB} ${OBJS}
43 changes: 43 additions & 0 deletions domehunter/protos/TheSkyX_plugin_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash


TheSkyX_Install=`/usr/bin/find ~/Library/Application\ Support/Software\ Bisque/ -name TheSkyXInstallPath.txt`
echo "TheSkyX_Install = $TheSkyX_Install"

if [ ! -f "$TheSkyX_Install" ]; then
echo TheSkyXInstallPath.txt not found
TheSkyX_Path=`/usr/bin/find ~/ -maxdepth 3 -name TheSkyX`
if [ -d "$TheSkyX_Path" ]; then
TheSkyX_Path="${TheSkyX_Path}/Contents"
else
echo TheSkyX application was not found.
exit 1
fi
else
TheSkyX_Path=$(<"$TheSkyX_Install")
fi

echo "Installing to $TheSkyX_Path"


if [ ! -d "$TheSkyX_Path" ]; then
echo TheSkyX Install dir not exist
exit 1
fi

if [ -d "$TheSkyX_Path/Resources/Common/PlugIns64" ]; then
PLUGINS_DIR="PlugIns64"
else
PLUGINS_DIR="PlugIns"
fi

cp "./domelist HuntsmanDome.txt" "$TheSkyX_Path/Resources/Common/Miscellaneous Files/"
cp "./libHuntsmanDome.so" "$TheSkyX_Path/Resources/Common/$PLUGINS_DIR/DomePlugIns/"

app_owner=`/usr/bin/stat -c "%u" "$TheSkyX_Path" | xargs id -n -u`
if [ ! -z "$app_owner" ]; then
chown $app_owner "$TheSkyX_Path/Resources/Common/Miscellaneous Files/domelist HuntsmanDome.txt"
chown $app_owner "$TheSkyX_Path/Resources/Common/$PLUGINS_DIR/DomePlugIns/libHuntsmanDome.so"
fi
chmod 755 "$TheSkyX_Path/Resources/Common/$PLUGINS_DIR/DomePlugIns/libHuntsmanDome.so"

4 changes: 4 additions & 0 deletions domehunter/protos/domelist HuntsmanDome.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//See hardwarelist.txt for details on this file format.
//Version|Manufacturer|Model|Comment|MapsTo|PlugInDllName|X2Developer|Windows|Mac|Linux|
1|Huntsman Telescope|Huntsman Telescope Dome Controller v1| | |libHuntsmanDome||0|0|1|

39 changes: 39 additions & 0 deletions domehunter/protos/generate_grpc_cpp_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

if [ "$1" == "clean" ]; then
rm libhuntsmandome*
rm src/*.grpc.pb.*
rm src/*.pb.*
rm *.o
rm .qmake.stash
else
HDOME_PATH="$HOME/Documents/REPOS"
PROTOS_PATH="$HDOME_PATH/huntsman-dome/domehunter/protos/src/"
PROTO_PATH1="/usr/local/include/google/protobuf/"
PROTO_PATH2="$HDOME_PATH/huntsman-dome/domehunter/protos/src/hx2dome.proto"
GRPC_CPP_PLUGIN_PATH="$(which grpc_cpp_plugin)"

echo -e "Generating GRPC C++ code\n"

echo -e "protoc -I $PROTOS_PATH --cpp_out=. src/hx2dome.proto\n"
protoc -I "$PROTOS_PATH" --cpp_out=. hx2dome.proto

echo -e "protoc -I $PROTOS_PATH --grpc_out=. --proto_path=$PROTO_PATH1 $PROTO_PATH2 --plugin=protoc-gen-grpc=$GRPC_CPP_PLUGIN_PATH\n"
protoc -I "$PROTOS_PATH" --grpc_out=. --proto_path="$PROTO_PATH1" "$PROTO_PATH2" --plugin=protoc-gen-grpc="$GRPC_CPP_PLUGIN_PATH"

echo -e "Moving generated GRPC C++ code to src/\n"
mv hx2dome.grpc.pb.cc src/hx2dome.grpc.pb.cpp
mv hx2dome.pb.cc src/hx2dome.pb.cpp
mv *.pb.h src/

#echo -e "Generating Makefile from project file.\n"
#qmake

#echo -e "Running Generated Makefile.\n"
#make

#echo -e "Cleaning out object files.\n"
#rm *.o

echo -e "Done.\n"
fi
22 changes: 22 additions & 0 deletions domehunter/protos/generate_grpc_python_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

if [ "$1" == "clean" ]; then
rm src/*pb2_grpc.py
rm src/*pb2.py
else
HDOME_PATH="$HOME/Documents/REPOS"
PROTOS_PATH="$HDOME_PATH/huntsman-dome/domehunter/protos/src/"
PROTO_PATH1="/usr/local/include/google/protobuf/"
PROTO_PATH2="$HDOME_PATH/huntsman-dome/domehunter/protos/src/hx2dome.proto"

echo -e "\nGenerating GRPC Python code\n"

echo -e "python -m grpc_tools.protoc -I=$PROTOS_PATH --python_out=. --grpc_python_out=. --proto_path=$PROTO_PATH1 $PROTO_PATH2\n"

python -m grpc_tools.protoc -I=$PROTOS_PATH --python_out=. --grpc_python_out=. --proto_path=$PROTO_PATH1 $PROTO_PATH2

echo -e "Moving generated GRPC Python code to src/\n"
mv *pb2* src/

echo -e "Done.\n"
fi
Loading

0 comments on commit e71562a

Please sign in to comment.