From 4a5f630d405fed3d316a077c0eff7527012bff2d Mon Sep 17 00:00:00 2001 From: Konstantin Lampalzer Date: Sat, 23 Jan 2021 13:37:40 +0100 Subject: [PATCH] Logging, more documentation --- compLib/LogstashLogging.py | 13 +++++++++++++ compLib/Motor.py | 10 +++++++--- docs/source/conf.py | 2 +- docs/source/lib/Api.rst | 10 +++++----- docs/source/lib/Battery.rst | 8 ++++---- docs/source/lib/Buzzer.rst | 8 ++++---- docs/source/lib/IRSensor.rst | 2 +- docs/source/lib/Logging.rst | 22 ++++++++++++++++++++++ docs/source/lib/Motor.rst | 8 ++++---- docs/source/lib/Servo.rst | 2 +- docs/source/lib/Vision.rst | 8 ++++---- 11 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 docs/source/lib/Logging.rst diff --git a/compLib/LogstashLogging.py b/compLib/LogstashLogging.py index 8ff02e3..61418c4 100644 --- a/compLib/LogstashLogging.py +++ b/compLib/LogstashLogging.py @@ -26,6 +26,7 @@ asynchronousLogstashHandler = AsynchronousLogstashHandler( database_path='logs.db' ) + class StreamToLogger(object): """ Fake file-like stream object that redirects writes to a logger instance. @@ -53,6 +54,7 @@ class StreamToLogger(object): self.linebuf = '' asynchronousLogstashHandler.flush() + try: r = requests.head(f"http://{host}:{port}") if r.status_code == 400: @@ -67,12 +69,23 @@ except requests.exceptions.ConnectionError as identifier: print(f"Could not connect to {host}!") pass + class Logging(object): + """Can be used to manually use the logger or turn up the logging level used for debugging this library. + """ @staticmethod def get_logger() -> logging.Logger: + """Get the logger object used to communicate with logstash + + :return: Python logger + :rtype: logging.Logger + """ return logstash_logger @staticmethod def set_debug(): + """Turns up the logging level of the library to debug. Should be used, when debugging with the + Competition organizers + """ logstash_logger.setLevel(logging.DEBUG) diff --git a/compLib/Motor.py b/compLib/Motor.py index 8c2556e..664acf9 100644 --- a/compLib/Motor.py +++ b/compLib/Motor.py @@ -16,16 +16,20 @@ class Motor(object): """ @staticmethod - def power(port: int, percent: int): + def power(port: int, percent: float): """Set specified motor to percentage power :param port: Port, which the motor is connected to. 0-3, 0 -> top left, 3 -> top right :param percent: Percentage of max speed. between -100 and 100 + :raises: IndexError """ Logging.get_logger().debug(f"Motor.power {port} {percent}") if port < 0 or port >= MOTOR_COUNT: - raise IndexError("Invalid Motor port specified!") + raise IndexError("Invalid Motor port specified!") + + if percent < -100 or percent > 100: + raise IndexError("Invalid Motor speed specified! Speed is between -100 and 100 percent!") forward = True if percent < 0: @@ -36,7 +40,7 @@ class Motor(object): if port == 1: forward = not forward - adjusted_speed = int(min(max(0, percent), 100) * MOTOR_PERCENTAGE_MULT) + adjusted_speed = int(min(max(0.0, percent), 100.0) * MOTOR_PERCENTAGE_MULT) if forward: pwm.setMotorPwm(port * 2, adjusted_speed) diff --git a/docs/source/conf.py b/docs/source/conf.py index dd8f84a..a455a50 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -35,7 +35,7 @@ extensions = [ 'sphinx_rtd_theme' ] -autodoc_mock_imports = ["smbus", "compLib.PCA9685", "RPi"] +autodoc_mock_imports = ["smbus", "compLib.PCA9685", "RPi", "pigpio", "flask"] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] diff --git a/docs/source/lib/Api.rst b/docs/source/lib/Api.rst index 44b2a43..7517d76 100644 --- a/docs/source/lib/Api.rst +++ b/docs/source/lib/Api.rst @@ -1,29 +1,29 @@ .. _lib_api: Api -===== +**** Seeding -******** +======== .. autoclass:: compLib.Api.Seeding :members: Double Elimination -******************* +=================== .. autoclass:: compLib.Api.DoubleElim :members: Position -********* +======== .. autoclass:: compLib.Api.Position :members: Examples -********* +======== Calling Seeding API --------------------- diff --git a/docs/source/lib/Battery.rst b/docs/source/lib/Battery.rst index 3a1d26c..c7d1fb1 100644 --- a/docs/source/lib/Battery.rst +++ b/docs/source/lib/Battery.rst @@ -1,20 +1,20 @@ .. _lib_battery: Battery -######## +******** Class Documentation -******************** +==================== .. autoclass:: compLib.Battery.Battery :members: Examples -********* +========= Printing percentage -==================== +-------------------- .. code-block:: python diff --git a/docs/source/lib/Buzzer.rst b/docs/source/lib/Buzzer.rst index 9ce4584..56a81c4 100644 --- a/docs/source/lib/Buzzer.rst +++ b/docs/source/lib/Buzzer.rst @@ -1,19 +1,19 @@ .. _lib_buzzer: Buzzer -####### +******* Class Documentation -******************** +==================== .. autoclass:: compLib.Buzzer.Buzzer :members: Examples -********* +========= Turning buzzer on and off -========================== +-------------------------- .. code-block:: python diff --git a/docs/source/lib/IRSensor.rst b/docs/source/lib/IRSensor.rst index f955b0a..72e60f9 100644 --- a/docs/source/lib/IRSensor.rst +++ b/docs/source/lib/IRSensor.rst @@ -1,7 +1,7 @@ .. _lib_irsensor: Infrared Sensor -================ +**************** .. autoclass:: compLib.IRSensor.IRSensor :members: \ No newline at end of file diff --git a/docs/source/lib/Logging.rst b/docs/source/lib/Logging.rst new file mode 100644 index 0000000..5fe6d41 --- /dev/null +++ b/docs/source/lib/Logging.rst @@ -0,0 +1,22 @@ +.. _lib_logging: + +Logging +******* + +Class Documentation +==================== + +.. autoclass:: compLib.LogstashLogging.Logging + :members: + +Examples +========= + +Turn up the logging +-------------------- + +.. code-block:: python + + from compLib.LogstashLogging import Logging + + Logging.set_debug() \ No newline at end of file diff --git a/docs/source/lib/Motor.rst b/docs/source/lib/Motor.rst index a8be270..5febcfe 100644 --- a/docs/source/lib/Motor.rst +++ b/docs/source/lib/Motor.rst @@ -1,19 +1,19 @@ .. _lib_motor: Motor -###### +****** Class Documentation -******************** +==================== .. autoclass:: compLib.Motor.Motor :members: Examples -********* +========= Driving straight (maybe) -========================= +------------------------- .. code-block:: python diff --git a/docs/source/lib/Servo.rst b/docs/source/lib/Servo.rst index 5f1c65a..d991b73 100644 --- a/docs/source/lib/Servo.rst +++ b/docs/source/lib/Servo.rst @@ -1,7 +1,7 @@ .. _lib_servo: Servo -===== +****** .. autoclass:: compLib.Servo.Servo :members: \ No newline at end of file diff --git a/docs/source/lib/Vision.rst b/docs/source/lib/Vision.rst index 8793ad4..736cc60 100644 --- a/docs/source/lib/Vision.rst +++ b/docs/source/lib/Vision.rst @@ -1,7 +1,7 @@ .. _lib_vision: Vision -===== +******* This module provides an interface for grabbing an rtmp stream and using the images to do some processing in opencv. @@ -13,7 +13,7 @@ How do I use this module? 4. You can view the http stream of your processed images in a web browser Opencv Stream -************* +============== Because of the rtmp stream needing to buffer some frames and waiting for P-Frames, importing this module might take up to 5 Seconds. @@ -22,10 +22,10 @@ to 5 Seconds. :members: Examples -********* +========= Using the Vision Module ---------------------- +------------------------- .. code-block:: python