Skip to content

Turrets

HorizontalTurretControl (TurretControl)

Turret control for the horizontal dimension with custom parameters

Attributes:

Name Type Description
MAX_DEFLECTION int

30° right maximum

MIN_DEFLECTION int

-30° left maximum

MAPPING_FACTOR float

-1 (invert)

T float

0.04s (slower moevement)

TurretControl (Joystick)

Base class, to control one dimension of the turret with a joystick

Attributes:

Name Type Description
MAX_DEFLECTION int

max deflection of the turret in degree

MIN_DEFLECTION int

min deflection of the turret in degree

MAPPING_FACTOR float

used to scale the mapped turret position

T float

Delay of the PT1 element used to controll the turret position

THRESHHOLD float

threshold under which a joystick value is recognized as 0

servo ServoHAL

Instance of ServoHAL to control a servo

__init__(self, seat, name, pin, config, rgb_cb=<function TurretControl.<lambda> at 0x75b9ca00>) special

Parameters:

Name Type Description Default
seat int

controller seat

required
name str

Name of the control

required
pin int

Pin the servo is connected to

required
offset

Servo offset from the zero position in degree

required
Source code in controls/turrets.py
def __init__(self, seat: int, name: str, pin: int, config: dict, rgb_cb: Callable = lambda _: None):
    """
        Arguments:
            seat: controller seat
            name: Name of the control
            pin: Pin the servo is connected to
            offset: Servo offset from the zero position in degree
    """

    super().__init__(seat, name)

    self.servo: ServoHAL = ServoInvertedHAL(pin) if config["inverted"] else ServoHAL(pin)

    self.MIN_DEFLECTION += config["offset"]
    self.MAX_DEFLECTION += config["offset"]

    self.rgb_cb = rgb_cb

    logging.info(f"Turret {self.name} initialized")

close(self, seat=0) async

Cancel task to control the servo and close servo connection

Parameters:

Name Type Description Default
seat int

number of the seat

0
Source code in controls/turrets.py
async def close(self, seat: int = 0):
    """
        Cancel task to control the servo and close servo connection

        Arguments:
            seat: number of the seat
    """
    await self.reset(seat)
    self.servo.close()

get_direction(self, seat, pos) async

Set turretposition whenn the joystick position changes

Parameters:

Name Type Description Default
seat int

number of the seat

required
pos float

position of the joystick between -1 and 1

required
Source code in controls/turrets.py
async def get_direction(self, seat: int, pos: float):
    """
        Set turretposition whenn the joystick position changes

        Arguments:
            seat: number of the seat
            pos: position of the joystick between -1 and 1
    """

    range = self.MAX_DEFLECTION - self.MIN_DEFLECTION

    self._joystick_pos = (((pos * self.MAPPING_FACTOR) + 1) / 2 * range + self.MIN_DEFLECTION)
    self.rgb_cb(pos)

    logging.debug(f"Set position of Turret {self.name} to {self._joystick_pos}")

init(self, seat=0) async

Set up task to control the servo

Parameters:

Name Type Description Default
seat int

number of the seat

0
Source code in controls/turrets.py
async def init(self, seat: int = 0):
    """
        Set up task to control the servo

        Arguments:
            seat: number of the seat
    """

    if self._position_task:
        if not self._position_task.cancelled:
            self._position_task.cancel()

    self._joystick_pos = ((self.MAX_DEFLECTION - self.MIN_DEFLECTION) / 2 + self.MIN_DEFLECTION)
    self._position_task = create_task(self.setPosition())
    logging.debug(f"Init servo at position {self._joystick_pos}")

reset(self, seat=0) async

Cancel task to control the servo and reset servo_pos

Parameters:

Name Type Description Default
seat int

number of the seat

0
Source code in controls/turrets.py
async def reset(self, seat: int = 0):
    """
        Cancel task to control the servo and reset servo_pos

        Arguments:
            seat: number of the seat
    """

    if self._position_task:
        self._position_task.cancel()
        self._position_task = None

    self._servo_pos = 0
    self.servo.setPosition(0)

setPosition(self) async

Loop to set servo position depending on the joystick position

Source code in controls/turrets.py
async def setPosition(self):
    """
        Loop to set servo position depending on the joystick position
    """

    DT = 0.002

    while True:
        self._servo_pos = self._servo_pos + (self._joystick_pos - self._servo_pos) / self.T * DT

        if self._servo_pos > self.MAX_DEFLECTION:
            self._servo_pos = self.MAX_DEFLECTION
        elif self._servo_pos < self.MIN_DEFLECTION:
            self._servo_pos = self.MIN_DEFLECTION

        self.servo.setPosition(self._servo_pos)
        await sleep(DT)

VerticalTurretControl (TurretControl)

Turret control for the vertical dimension with custom parameters

Attributes:

Name Type Description
MAX_DEFLECTION int

30° bottom maximum

MIN_DEFLECTION int

-20° top maximum

MAPPING_FACTOR float

1 (no scaling)

T float

0.02s (fast movement)

Back to top