Add MetricsLogging

This commit is contained in:
Konstantin Lampalzer 2021-09-06 18:51:52 +02:00
parent 60f252c37a
commit 3fe3139961
6 changed files with 168 additions and 61 deletions

View file

@ -2,6 +2,7 @@ import atexit
from enum import IntEnum
from compLib.LogstashLogging import Logging
from compLib.MetricsLogging import MetricsLogging
from compLib.Spi import Spi, Register
MOTOR_COUNT = 4
@ -45,14 +46,13 @@ class Motor(object):
Spi.write(Register.PWM_4_CTRL, 1, int(mode))
@staticmethod
def power_raw(port: int, percent: float):
def power_raw(port: int, percent: float, log_metric = True):
"""Set specified motor to percentage power
:param port: Port, which the motor is connected to. 1-4
: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!")
@ -60,6 +60,9 @@ class Motor(object):
if percent < -100 or percent > 100:
raise IndexError("Invalid Motor speed specified! Speed is between -100 and 100 percent!")
if log_metric:
MetricsLogging.put("MotorRaw", percent, port)
mode = MotorMode.COAST
if percent < 0:
percent = abs(percent)
@ -80,12 +83,14 @@ class Motor(object):
:param percent: Percentage of max speed. between -100 and 100
:raises: IndexError
"""
raw_power = 0
if percent > 0:
Motor.power_raw(port, Motor.__linearizePower(MOTOR_CURVE, percent))
raw_power = Motor.__linearizePower(MOTOR_CURVE, percent)
elif percent < 0:
Motor.power_raw(port, -Motor.__linearizePower(MOTOR_CURVE, -percent))
else:
Motor.power_raw(port, 0)
raw_power = -Motor.__linearizePower(MOTOR_CURVE, -percent)
MetricsLogging.put("Motor", percent, port, False)
Motor.power_raw(port, raw_power)
@staticmethod
def all_off():