From 1391b0372423229ddaf92d25532425ec038698b2 Mon Sep 17 00:00:00 2001 From: Nikolay Khabarov <2xl@mail.ru> Date: Sat, 13 May 2017 03:52:36 +0300 Subject: [PATCH] reorganise project --- cnc/__init__.py | 0 config.py => cnc/config.py | 2 - coordinates.py => cnc/coordinates.py | 2 - gcode.py => cnc/gcode.py | 2 - gmachine.py => cnc/gmachine.py | 6 +-- cnc/hal.py | 50 +++++++++++++++++++ cnc/hal_raspberry/__init__.py | 0 hal_rpi.py => cnc/hal_raspberry/hal.py | 8 ++- rpgpio.py => cnc/hal_raspberry/rpgpio.py | 1 - .../hal_raspberry/rpgpio_private.py | 2 - hal_virtual.py => cnc/hal_virtual.py | 3 +- main.py => cnc/main.py | 2 +- pulses.py => cnc/pulses.py | 2 - deploy.sh | 4 +- hal.py | 8 --- pycnc | 7 +++ tests/rpgpio-test.sh | 4 +- 17 files changed, 69 insertions(+), 34 deletions(-) create mode 100755 cnc/__init__.py rename config.py => cnc/config.py (97%) rename coordinates.py => cnc/coordinates.py (99%) rename gcode.py => cnc/gcode.py (99%) rename gmachine.py => cnc/gmachine.py (97%) create mode 100644 cnc/hal.py create mode 100755 cnc/hal_raspberry/__init__.py rename hal_rpi.py => cnc/hal_raspberry/hal.py (97%) rename rpgpio.py => cnc/hal_raspberry/rpgpio.py (99%) rename rpgpio_private.py => cnc/hal_raspberry/rpgpio_private.py (99%) rename hal_virtual.py => cnc/hal_virtual.py (99%) rename main.py => cnc/main.py (98%) rename pulses.py => cnc/pulses.py (99%) delete mode 100644 hal.py create mode 100755 pycnc diff --git a/cnc/__init__.py b/cnc/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/config.py b/cnc/config.py similarity index 97% rename from config.py rename to cnc/config.py index b985a04..004db76 100644 --- a/config.py +++ b/cnc/config.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Hardware limitations config STEPPER_PULSE_LINGTH_US = 2 STEPPER_MAX_VELOCITY_MM_PER_MIN = 1800 # mm per min diff --git a/coordinates.py b/cnc/coordinates.py similarity index 99% rename from coordinates.py rename to cnc/coordinates.py index 67cbad9..d325af9 100644 --- a/coordinates.py +++ b/cnc/coordinates.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - import math diff --git a/gcode.py b/cnc/gcode.py similarity index 99% rename from gcode.py rename to cnc/gcode.py index a2655a6..6f9e897 100644 --- a/gcode.py +++ b/cnc/gcode.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - import re from coordinates import Coordinates diff --git a/gmachine.py b/cnc/gmachine.py similarity index 97% rename from gmachine.py rename to cnc/gmachine.py index 8030059..23563e5 100644 --- a/gmachine.py +++ b/cnc/gmachine.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - import time import logging @@ -32,7 +30,9 @@ class GMachine(object): self.reset() hal.init() - def destroy(self): + def release(self): + """ Return machine to original position and free all resources. + """ self.home() hal.join() diff --git a/cnc/hal.py b/cnc/hal.py new file mode 100644 index 0000000..ac3c4fb --- /dev/null +++ b/cnc/hal.py @@ -0,0 +1,50 @@ +# This implementation allows to use different hardware. +# Imported module contains functions for hardware access fo some board/SoC. +# List of HAL methods that should be implemented in each module: +# def init(): +# """ Initialize GPIO pins and machine itself, including calibration if +# needed. Do not return till all procedure is completed. +# """ +# logging.info("initialize hal") +# do_something() +# +# +# def spindle_control(percent): +# """ Spindle control implementation. +# :param percent: Spindle speed in percent. 0 turns spindle off. +# """ +# logging.info("spindle control: {}%".format(percent)) +# do_something() +# +# +# def move_linear(delta, velocity): +# """ Move head to specified distance with specified speed. +# :param delta: Coordinated object, delta position in mm +# :param velocity: velocity in mm per min +# """ +# do_something() +# +# +# def join(): +# """ Wait till motors work. +# """ +# do_something() + + +# check which module to import +try: + from hal_raspberry.hal import * +except ImportError: + print("----- Hardware not detected, using virtual environment -----") + print("----- Use M111 command to enable more detailed debug -----") + from hal_virtual import * + +# check if all methods that is needed is implemented +if 'init' not in locals(): + raise NotImplementedError("hal.init() not implemented") +if 'spindle_control' not in locals(): + raise NotImplementedError("hal.spindle_control() not implemented") +if 'move_linear' not in locals(): + raise NotImplementedError("hal.move_linear() not implemented") +if 'join' not in locals(): + raise NotImplementedError("hal.join() not implemented") diff --git a/cnc/hal_raspberry/__init__.py b/cnc/hal_raspberry/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/hal_rpi.py b/cnc/hal_raspberry/hal.py similarity index 97% rename from hal_rpi.py rename to cnc/hal_raspberry/hal.py index 9f923e7..36e4c59 100644 --- a/hal_rpi.py +++ b/cnc/hal_raspberry/hal.py @@ -1,13 +1,11 @@ -#!/usr/bin/env python - import logging import time import rpgpio -from pulses import PulseGeneratorLinear -from coordinates import Coordinates -from config import * +from cnc.pulses import PulseGeneratorLinear +from cnc.coordinates import Coordinates +from cnc.config import * # Stepper motors channel for RPIO STEPPER_CHANNEL = 0 diff --git a/rpgpio.py b/cnc/hal_raspberry/rpgpio.py similarity index 99% rename from rpgpio.py rename to cnc/hal_raspberry/rpgpio.py index 4a1aa00..e51c122 100755 --- a/rpgpio.py +++ b/cnc/hal_raspberry/rpgpio.py @@ -8,7 +8,6 @@ import sys import struct - class GPIO(object): MODE_OUTPUT = 1 MODE_INPUT_NOPULL = 2 diff --git a/rpgpio_private.py b/cnc/hal_raspberry/rpgpio_private.py similarity index 99% rename from rpgpio_private.py rename to cnc/hal_raspberry/rpgpio_private.py index a34967b..55ad072 100644 --- a/rpgpio_private.py +++ b/cnc/hal_raspberry/rpgpio_private.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - import os import mmap import struct diff --git a/hal_virtual.py b/cnc/hal_virtual.py similarity index 99% rename from hal_virtual.py rename to cnc/hal_virtual.py index 701e94f..0aa6b62 100644 --- a/hal_virtual.py +++ b/cnc/hal_virtual.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - import logging import time @@ -11,6 +9,7 @@ from coordinates import Coordinates It checks PulseGenerator with some tests. """ + def init(): """ Initialize GPIO pins and machine itself, including calibration if needed. Do not return till all procedure is completed. diff --git a/main.py b/cnc/main.py similarity index 98% rename from main.py rename to cnc/main.py index f4e0482..d045e93 100755 --- a/main.py +++ b/cnc/main.py @@ -51,7 +51,7 @@ def main(): except KeyboardInterrupt: pass print("\r\nExiting...") - machine.destroy() + machine.release() if __name__ == "__main__": main() diff --git a/pulses.py b/cnc/pulses.py similarity index 99% rename from pulses.py rename to cnc/pulses.py index 2cd2137..aedb22a 100644 --- a/pulses.py +++ b/cnc/pulses.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - from __future__ import division import math import logging diff --git a/deploy.sh b/deploy.sh index 294831e..ff96a5f 100755 --- a/deploy.sh +++ b/deploy.sh @@ -5,7 +5,7 @@ ADDR=pi@192.168.0.208 if [ ! -z $1 ]; then ADDR=$1 fi -tar -cvjSf $(dirname "$0")/pycnc.tar.bz2 $(dirname "$0")/*.py > /dev/null +find . -name "*.py" -o -name "pycnc" | tar -cjf $(dirname "$0")/pycnc.tar.bz2 -T - sshpass -p${PASS} scp $(dirname "$0")/pycnc.tar.bz2 "${ADDR}:~/pycnc" sshpass -p${PASS} ssh -t ${ADDR} "(cd ~/pycnc && tar xvf pycnc.tar.bz2) > /dev/null" &> /dev/null -sshpass -p${PASS} ssh -t ${ADDR} "sudo pypy ~/pycnc/main.py" +sshpass -p${PASS} ssh -t ${ADDR} "sudo pypy ~/pycnc/pycnc" diff --git a/hal.py b/hal.py deleted file mode 100644 index 49426fa..0000000 --- a/hal.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python - -try: - from hal_rpi import * -except ImportError: - print("----- Hardware not detected, using virtual environment -----") - print("----- Use M111 command to enable more detailed debug -----") - from hal_virtual import * diff --git a/pycnc b/pycnc new file mode 100755 index 0000000..500d0b2 --- /dev/null +++ b/pycnc @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +import cnc.main + +if __name__ == "__main__": + cnc.main.main() + diff --git a/tests/rpgpio-test.sh b/tests/rpgpio-test.sh index 444b1b7..f9937c0 100755 --- a/tests/rpgpio-test.sh +++ b/tests/rpgpio-test.sh @@ -5,6 +5,6 @@ ADDR=pi@192.168.0.208 if [ ! -z $1 ]; then ADDR=$1 fi -sshpass -p${PASS} scp $(dirname "$0")/../rpgpio_private.py "${ADDR}:~" -sshpass -p${PASS} scp $(dirname "$0")/../rpgpio.py "${ADDR}:~" +sshpass -p${PASS} scp $(dirname "$0")/../cnc/hal_raspberry/rpgpio_private.py "${ADDR}:~" +sshpass -p${PASS} scp $(dirname "$0")/../cnc/hal_raspberry/rpgpio.py "${ADDR}:~" sshpass -p${PASS} ssh -t ${ADDR} "sudo ~/rpgpio.py"