Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pytools/app_rel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
telemetry_dir_name = 'telemetry'

mon = Monitor(monitor_bus_name=mon_bus_name, argv=sys.argv)
zero = DataSourceConst('zero', value=0)

rp = EwRelativePosition([
DataSourceConst("boat", value="airplane-white"),
DataSourceSinus('plane_phi', mult=360),
DataSourceConst('plane_r', value=60),
DataSourceConst('zero', value=20), # alt
DataSourceSinus('plane_course', iphase=0.0, mult=360),

DataSourceConst("boat", value="aim-yellow"),
DataSourceSinus('base_phi', iphase=0.5, mult=360),
DataSourceConst('base_r', value=120),
zero, # alt
zero, # course

DataSourceConst("boat", value="boat-white"), # icon
DataSourceSinus('target_phi', iphase=-0.5, mult=360),
DataSourceConst('target_r', value=160),
zero, # alt
zero, # course
])



mon.add_widget(EwGroup([rp]))

mon.app_window.resize(800, 800)
Expand Down
39 changes: 39 additions & 0 deletions pytools/ds/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Union

from mymath import to_cartesian, to_polar
from .datasources import DataSourceBasic, NoDataStub


class DataSourceCoordsCartesian(DataSourceBasic):
def __init__(self, name, phi, r, scale=1.0, decimals=3, **kwargs):
super().__init__(name, **kwargs)
self._phi = phi
self._r = r
self._scale = scale
self._decimals = decimals

def connect(self):
pass

def read(self) -> Union[float, int, str, NoDataStub]:
return to_cartesian(phi=self._phi,
r=self._r,
scale=self._scale,
decimals=self._decimals)


class DataSourceCoordsPolar(DataSourceBasic):
def __init__(self, name, x, y, scale=1.0, decimals=3, **kwargs):
super().__init__(name, **kwargs)
self._x = x
self._y = y
self._scale = scale
self._decimals = decimals

def connect(self):
pass

def read(self) -> Union[float, int, str, NoDataStub]:
return to_polar(x=self._x, y=self._y,
scale=self._scale,
decimals=self._decimals)
7 changes: 3 additions & 4 deletions pytools/ds/datasources.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import math
import time
from abc import abstractmethod
from typing import List, Union
import time
import math

from PyQt5.QtWidgets import QLineEdit


class NoDataStub:
Expand Down Expand Up @@ -107,6 +105,7 @@ def read(self) -> Union[float, int, str, NoDataStub]:
self.time += self.delta_time
return self.time


class DataSourceSinus(DataSourceBasic):
def __init__(self, name, *, omega=1.0, iphase=0.0, bias=0.0, **kwargs):
super().__init__(name, **kwargs)
Expand Down
Empty file added pytools/ds/tests/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions pytools/ds/tests/test_coords_cartesian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import mymath
from unittest import TestCase

from ds.common import DataSourceCoordsCartesian


# Sample coords:
#
# •4 | •1
# |
# ----------+-----------
# |
# •3 | •2

class TestDataSourceCartesian(TestCase):
def test_pt1(self):
ds = DataSourceCoordsCartesian("cartesian", phi=45, r=mymath.hypot(10, 10), decimals=2)
[x, y] = ds.read()
self.assertEqual(10.0, x)
self.assertEqual(10.0, y)

def test_pt2(self):
ds = DataSourceCoordsCartesian("cartesian", phi=45+90, r=mymath.hypot(10, 10), decimals=2)
[x, y] = ds.read()
self.assertEqual(10.0, x)
self.assertEqual(-10.0, y)

def test_pt3(self):
ds = DataSourceCoordsCartesian("cartesian", phi=45 + 180, r=mymath.hypot(10, 10), decimals=2)
[x, y] = ds.read()
self.assertEqual(-10.0, x)
self.assertEqual(-10.0, y)

def test_pt4(self):
ds = DataSourceCoordsCartesian("cartesian", phi=45 + 270, r=mymath.hypot(10, 10), decimals=2)
[x, y] = ds.read()
self.assertEqual(-10.0, x)
self.assertEqual(10.0, y)
42 changes: 42 additions & 0 deletions pytools/ds/tests/test_coords_polar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import mymath
from unittest import TestCase

from ds.common import DataSourceCoordsPolar


# Sample coords:
#
# •4 | •1
# |
# ----------+-----------
# |
# •3 | •2

class TestDataSourcePolar(TestCase):
def test_pt1(self):
decimals = 3
polar_ds = DataSourceCoordsPolar("to_polar", 10, 10, decimals=decimals)
[phi, r] = polar_ds.read()
self.assertEqual(45.0, phi)
self.assertEqual(round(mymath.hypot(10, 10), decimals), r)

def test_pt2(self):
decimals = 3
polar_ds = DataSourceCoordsPolar("to_polar", x=10, y=-10, decimals=decimals)
[phi, r] = polar_ds.read()
self.assertEqual(phi, 45.0 + 90.0)
self.assertEqual(round(mymath.hypot(10, 10), decimals), r)

def test_pt3(self):
decimals = 3
polar_ds = DataSourceCoordsPolar("to_polar", x=-10, y=-10, decimals=decimals)
[phi, r] = polar_ds.read()
self.assertEqual(phi, 45.0 + 180.0)
self.assertEqual(r, round(mymath.hypot(10, 10), decimals))

def test_pt4(self):
decimals = 3
polar_ds = DataSourceCoordsPolar("to_polar", x=-10, y=10, decimals=decimals)
[phi, r] = polar_ds.read()
self.assertEqual(phi, 45.0 + 270.0)
self.assertEqual(round(mymath.hypot(10, 10), decimals), r)
44 changes: 12 additions & 32 deletions pytools/ew/relative_position.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import math
from typing import List, Union, Dict

from PyQt5 import QtGui
from PyQt5.QtCore import Qt, QPointF, pyqtSignal
from PyQt5.QtGui import QPainter, QPen, QPixmap, QBrush, QFont

from mymath import to_polar, to_cartesian, translate_point, rev_translate_point
from ds import DataSourceBasic, NoDataStub

from .common import MyQtWidget, rel_path
from .widgets import EwBasic

Expand All @@ -15,31 +16,6 @@
ds_tuple_size = 5


def to_polar(x, y, scale=1.0):
pt = QPointF(x, y)
r = math.sqrt(pt.x() * pt.x() + pt.y() * pt.y()) / scale
degrees = math.degrees(math.atan2(-y, x)) + 90
phi = (degrees + 360) % 360
return [round(phi, 2), round(r, 3)]


def to_cartesian(r, phi, scale=1.0):
_phi_rad = math.radians(phi)
_x = scale * r * math.sin(_phi_rad)
_y = -(scale * r * math.cos(_phi_rad))
return [_x, _y]


def rtranslate_point(width, height, pt: QPointF, zero_offset_x=0.0, zero_offset_y=0.0):
dx, dy = width / 2 + zero_offset_x, height / 2 + zero_offset_y
return QPointF(pt.x() - dx, - (pt.y() - dy))


def translate_point(width, height, pt: QPointF, zero_offset_x=0.0, zero_offset_y=0.0):
dx, dy = width / 2 + zero_offset_x, height / 2 + zero_offset_y
return QPointF(pt.x() + dx, pt.y() + dy)


class EwRelativePosition(MyQtWidget, EwBasic):
target_changed = pyqtSignal(float, float, name='targetChanged')
scale_changed = pyqtSignal(float, name='zoomChanged')
Expand All @@ -55,16 +31,18 @@ def __init__(self, icon='aim-yellow'):
self.azimuth = 0.0

def draw(self, canvas, center, scale):
[_x, _y] = to_cartesian(self.r, self.phi, scale)
[_x, _y] = to_cartesian(r=self.r, phi=self.phi, scale=scale)
self.svg_icon.setX(_x + center[0])
self.svg_icon.setY(_y + center[1])
self.svg_icon.setY(-_y + center[1])
self.svg_icon.setRotation(self.azimuth)
canvas.setPen(QPen(Qt.darkGray, 1, Qt.DotLine, Qt.RoundCap))
canvas.drawLine(translate_point(canvas.device().width(), canvas.device().height(),
canvas.drawLine(translate_point(canvas.device().width(),
canvas.device().height(),
QPointF(0, 0),
center[0], center[1]),
translate_point(canvas.device().width(), canvas.device().height(),
QPointF(_x, _y),
translate_point(canvas.device().width(),
canvas.device().height(),
QPointF(_x, -_y),
center[0], center[1]))

def __init__(self, data_sources: List[DataSourceBasic], fixed_size=None, **kwargs):
Expand Down Expand Up @@ -117,7 +95,8 @@ def wheelEvent(self, event: QtGui.QWheelEvent) -> None:
self.zoom_out()

def _emit_target_chg(self, x, y):
pt = rtranslate_point(self.rect().width(), self.rect().height(), QPointF(x, y), self.center[0], self.center[1])
pt = rev_translate_point(self.rect().width(), self.rect().height(), QPointF(x, y), self.center[0],
self.center[1])
[phi, r] = to_polar(pt.x(), pt.y(), scale=self.scale_m)
# noinspection PyUnresolvedReferences
self.target_changed.emit(phi, r)
Expand Down Expand Up @@ -175,6 +154,7 @@ def paintEvent(self, event):

def radraw_handler(self, vals: List[Union[float, int, str, NoDataStub]], vals_map: Dict):
count = len(vals) // ds_tuple_size

def check_stub(d):
return d if not isinstance(d, NoDataStub) else 0.0

Expand Down
8 changes: 4 additions & 4 deletions pytools/ew/widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import math
import mymath
from abc import abstractmethod
from typing import List, Union, Dict, Tuple

Expand Down Expand Up @@ -456,11 +456,11 @@ def paintEvent(self, event):
_scaleX = _width / _originalWidth
_scaleY = _height / _originalHeight

roll_rad = math.pi * self._roll / 180.0
roll_rad = mymath.pi * self._roll / 180.0
delta = 1.7 * self._pitch

_faceDeltaX_new = _scaleX * delta * math.sin(roll_rad)
_faceDeltaY_new = _scaleY * delta * math.cos(roll_rad)
_faceDeltaX_new = _scaleX * delta * mymath.sin(roll_rad)
_faceDeltaY_new = _scaleY * delta * mymath.cos(roll_rad)

offs_x = _faceDeltaX_new - self._faceDeltaX_old
offs_y = _faceDeltaY_new - self._faceDeltaY_old
Expand Down
3 changes: 3 additions & 0 deletions pytools/images/vehicle/boat-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions pytools/images/vehicle/boat.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions pytools/monitor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sys
import time
from typing import List

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QThread, QObject, pyqtSignal, QThreadPool, QRunnable, pyqtSlot
from PyQt5.QtCore import QObject, pyqtSignal, QThreadPool, QRunnable, pyqtSlot
from PyQt5.QtWidgets import QPushButton, QApplication


Expand Down Expand Up @@ -90,7 +89,8 @@ def run(self):
Your code goes in this function
"""
print("Thread start")
self.job(self.process_events)
self.job()
self.process_events()
print("Thread finished")


Expand Down
Loading