2020-05-18 18:58:18 +00:00
|
|
|
# NanoVNASaver
|
2020-06-25 17:52:30 +00:00
|
|
|
#
|
2020-05-18 18:58:18 +00:00
|
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
2020-06-25 17:52:30 +00:00
|
|
|
# Copyright (C) 2019, 2020 Rune B. Broberg
|
|
|
|
# Copyright (C) 2020 NanoVNA-Saver Authors
|
2019-09-01 21:13:21 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-05 12:56:40 +00:00
|
|
|
import math
|
2019-09-22 11:42:05 +00:00
|
|
|
import logging
|
2019-09-01 21:13:21 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
from PyQt5 import QtGui, QtCore
|
2019-09-01 21:13:21 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
from NanoVNASaver.RFTools import Datapoint
|
|
|
|
from .Square import SquareChart
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-11-08 12:58:14 +00:00
|
|
|
|
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
class SmithChart(SquareChart):
|
|
|
|
def __init__(self, name=""):
|
2019-11-08 12:58:14 +00:00
|
|
|
super().__init__(name)
|
|
|
|
self.chartWidth = 250
|
|
|
|
self.chartHeight = 250
|
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
self.setMinimumSize(self.chartWidth + 40, self.chartHeight + 40)
|
2019-11-08 12:58:14 +00:00
|
|
|
pal = QtGui.QPalette()
|
|
|
|
pal.setColor(QtGui.QPalette.Background, self.backgroundColor)
|
|
|
|
self.setPalette(pal)
|
|
|
|
self.setAutoFillBackground(True)
|
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
|
|
|
|
qp = QtGui.QPainter(self)
|
2020-06-21 18:54:23 +00:00
|
|
|
# qp.begin(self) # Apparently not needed?
|
2020-05-18 18:58:18 +00:00
|
|
|
self.drawSmithChart(qp)
|
|
|
|
self.drawValues(qp)
|
|
|
|
qp.end()
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def drawSmithChart(self, qp: QtGui.QPainter):
|
|
|
|
centerX = int(self.width()/2)
|
|
|
|
centerY = int(self.height()/2)
|
|
|
|
qp.setPen(QtGui.QPen(self.textColor))
|
|
|
|
qp.drawText(3, 15, self.name)
|
|
|
|
qp.setPen(QtGui.QPen(self.foregroundColor))
|
2020-06-15 11:27:00 +00:00
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX, centerY),
|
|
|
|
int(self.chartWidth / 2),
|
|
|
|
int(self.chartHeight / 2))
|
|
|
|
qp.drawLine(
|
|
|
|
centerX - int(self.chartWidth / 2),
|
|
|
|
centerY,
|
|
|
|
centerX + int(self.chartWidth / 2),
|
|
|
|
centerY)
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX + int(self.chartWidth/4), centerY),
|
|
|
|
int(self.chartWidth/4), int(self.chartHeight/4)) # Re(Z) = 1
|
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX + int(2/3*self.chartWidth/2), centerY),
|
|
|
|
int(self.chartWidth/6), int(self.chartHeight/6)) # Re(Z) = 2
|
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX + int(3 / 4 * self.chartWidth / 2), centerY),
|
|
|
|
int(self.chartWidth / 8), int(self.chartHeight / 8)) # Re(Z) = 3
|
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX + int(5 / 6 * self.chartWidth / 2), centerY),
|
|
|
|
int(self.chartWidth / 12), int(self.chartHeight / 12)) # Re(Z) = 5
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX + int(1 / 3 * self.chartWidth / 2), centerY),
|
|
|
|
int(self.chartWidth / 3), int(self.chartHeight / 3)) # Re(Z) = 0.5
|
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX + int(1 / 6 * self.chartWidth / 2), centerY),
|
|
|
|
int(self.chartWidth / 2.4), int(self.chartHeight / 2.4)) # Re(Z) = 0.2
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawArc(centerX + int(3/8*self.chartWidth), centerY, int(self.chartWidth/4),
|
|
|
|
int(self.chartWidth/4), 90*16, 152*16) # Im(Z) = -5
|
|
|
|
qp.drawArc(centerX + int(3/8*self.chartWidth), centerY, int(self.chartWidth/4),
|
|
|
|
-int(self.chartWidth/4), -90 * 16, -152 * 16) # Im(Z) = 5
|
|
|
|
qp.drawArc(centerX + int(self.chartWidth/4), centerY, int(self.chartWidth/2),
|
|
|
|
int(self.chartHeight/2), 90*16, 127*16) # Im(Z) = -2
|
|
|
|
qp.drawArc(centerX + int(self.chartWidth/4), centerY, int(self.chartWidth/2),
|
|
|
|
-int(self.chartHeight/2), -90*16, -127*16) # Im(Z) = 2
|
2020-06-15 11:27:00 +00:00
|
|
|
qp.drawArc(centerX, centerY,
|
|
|
|
self.chartWidth, self.chartHeight,
|
|
|
|
90*16, 90*16) # Im(Z) = -1
|
|
|
|
qp.drawArc(centerX, centerY,
|
|
|
|
self.chartWidth, -self.chartHeight,
|
|
|
|
-90 * 16, -90 * 16) # Im(Z) = 1
|
|
|
|
qp.drawArc(centerX - int(self.chartWidth / 2), centerY,
|
|
|
|
self.chartWidth * 2, self.chartHeight * 2,
|
|
|
|
int(99.5*16), int(43.5*16)) # Im(Z) = -0.5
|
|
|
|
qp.drawArc(centerX - int(self.chartWidth / 2), centerY,
|
|
|
|
self.chartWidth * 2, -self.chartHeight * 2,
|
|
|
|
int(-99.5 * 16), int(-43.5 * 16)) # Im(Z) = 0.5
|
|
|
|
qp.drawArc(centerX - self.chartWidth * 2, centerY,
|
|
|
|
self.chartWidth * 5, self.chartHeight * 5,
|
|
|
|
int(93.85 * 16), int(18.85 * 16)) # Im(Z) = -0.2
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawArc(centerX - self.chartWidth*2, centerY,
|
2020-06-15 11:27:00 +00:00
|
|
|
self.chartWidth*5, -self.chartHeight*5,
|
|
|
|
int(-93.85 * 16), int(-18.85 * 16)) # Im(Z) = 0.2
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2019-12-12 14:16:37 +00:00
|
|
|
self.drawTitle(qp)
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.setPen(self.swrColor)
|
|
|
|
for swr in self.swrMarkers:
|
|
|
|
if swr <= 1:
|
|
|
|
continue
|
|
|
|
gamma = (swr - 1)/(swr + 1)
|
|
|
|
r = round(gamma * self.chartWidth/2)
|
|
|
|
qp.drawEllipse(QtCore.QPoint(centerX, centerY), r, r)
|
2020-06-15 11:27:00 +00:00
|
|
|
qp.drawText(
|
|
|
|
QtCore.QRect(centerX - 50, centerY - 4 + r, 100, 20),
|
|
|
|
QtCore.Qt.AlignCenter, str(swr))
|
2020-05-18 18:58:18 +00:00
|
|
|
|
2019-11-08 12:58:14 +00:00
|
|
|
def drawValues(self, qp: QtGui.QPainter):
|
|
|
|
if len(self.data) == 0 and len(self.reference) == 0:
|
|
|
|
return
|
|
|
|
pen = QtGui.QPen(self.sweepColor)
|
|
|
|
pen.setWidth(self.pointSize)
|
|
|
|
line_pen = QtGui.QPen(self.sweepColor)
|
|
|
|
line_pen.setWidth(self.lineThickness)
|
2020-05-18 18:58:18 +00:00
|
|
|
highlighter = QtGui.QPen(QtGui.QColor(20, 0, 255))
|
|
|
|
highlighter.setWidth(1)
|
2019-12-09 15:22:25 +00:00
|
|
|
qp.setPen(pen)
|
|
|
|
for i in range(len(self.data)):
|
|
|
|
x = self.getXPosition(self.data[i])
|
2020-05-18 18:58:18 +00:00
|
|
|
y = int(self.height()/2 + self.data[i].im * -1 * self.chartHeight/2)
|
|
|
|
qp.drawPoint(x, y)
|
2019-12-09 15:22:25 +00:00
|
|
|
if self.drawLines and i > 0:
|
2020-05-18 18:58:18 +00:00
|
|
|
prevx = self.getXPosition(self.data[i-1])
|
|
|
|
prevy = int(self.height() / 2 + self.data[i-1].im * -1 * self.chartHeight / 2)
|
2019-12-09 15:22:25 +00:00
|
|
|
qp.setPen(line_pen)
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawLine(x, y, prevx, prevy)
|
2019-12-09 15:22:25 +00:00
|
|
|
qp.setPen(pen)
|
2020-05-18 18:58:18 +00:00
|
|
|
pen.setColor(self.referenceColor)
|
|
|
|
line_pen.setColor(self.referenceColor)
|
2019-12-09 15:22:25 +00:00
|
|
|
qp.setPen(pen)
|
2020-05-18 18:58:18 +00:00
|
|
|
if len(self.data) > 0:
|
|
|
|
fstart = self.data[0].freq
|
|
|
|
fstop = self.data[len(self.data)-1].freq
|
|
|
|
else:
|
|
|
|
fstart = self.reference[0].freq
|
|
|
|
fstop = self.reference[len(self.reference)-1].freq
|
2019-12-09 15:22:25 +00:00
|
|
|
for i in range(len(self.reference)):
|
2020-05-18 18:58:18 +00:00
|
|
|
data = self.reference[i]
|
|
|
|
if data.freq < fstart or data.freq > fstop:
|
|
|
|
continue
|
|
|
|
x = self.getXPosition(data)
|
|
|
|
y = int(self.height()/2 + data.im * -1 * self.chartHeight/2)
|
|
|
|
qp.drawPoint(x, y)
|
2019-12-09 15:22:25 +00:00
|
|
|
if self.drawLines and i > 0:
|
2020-05-18 18:58:18 +00:00
|
|
|
prevx = self.getXPosition(self.reference[i-1])
|
|
|
|
prevy = int(self.height() / 2 + self.reference[i-1].im * -1 * self.chartHeight / 2)
|
2019-12-09 15:22:25 +00:00
|
|
|
qp.setPen(line_pen)
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawLine(x, y, prevx, prevy)
|
2019-12-09 15:22:25 +00:00
|
|
|
qp.setPen(pen)
|
2020-05-18 18:58:18 +00:00
|
|
|
# Now draw the markers
|
|
|
|
for m in self.markers:
|
|
|
|
if m.location != -1:
|
|
|
|
x = self.getXPosition(self.data[m.location])
|
|
|
|
y = self.height() / 2 + self.data[m.location].im * -1 * self.chartHeight / 2
|
|
|
|
self.drawMarker(x, y, qp, m.color, self.markers.index(m)+1)
|
2019-12-09 15:22:25 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def getXPosition(self, d: Datapoint) -> int:
|
|
|
|
return int(self.width()/2 + d.re * self.chartWidth/2)
|
2019-11-08 12:58:14 +00:00
|
|
|
|
|
|
|
def getYPosition(self, d: Datapoint) -> int:
|
2020-05-18 18:58:18 +00:00
|
|
|
return int(self.height()/2 + d.im * -1 * self.chartHeight/2)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def heightForWidth(self, a0: int) -> int:
|
|
|
|
return a0
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def mouseMoveEvent(self, a0: QtGui.QMouseEvent) -> None:
|
|
|
|
if a0.buttons() == QtCore.Qt.RightButton:
|
|
|
|
a0.ignore()
|
2019-11-17 20:45:08 +00:00
|
|
|
return
|
2020-05-18 18:58:18 +00:00
|
|
|
x = a0.x()
|
|
|
|
y = a0.y()
|
|
|
|
absx = x - (self.width() - self.chartWidth) / 2
|
|
|
|
absy = y - (self.height() - self.chartHeight) / 2
|
|
|
|
if absx < 0 or absx > self.chartWidth or absy < 0 or absy > self.chartHeight \
|
|
|
|
or len(self.data) == len(self.reference) == 0:
|
|
|
|
a0.ignore()
|
2019-11-17 20:45:08 +00:00
|
|
|
return
|
2020-05-18 18:58:18 +00:00
|
|
|
a0.accept()
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
if len(self.data) > 0:
|
|
|
|
target = self.data
|
2019-11-17 20:45:08 +00:00
|
|
|
else:
|
2020-05-18 18:58:18 +00:00
|
|
|
target = self.reference
|
|
|
|
positions = []
|
|
|
|
for d in target:
|
|
|
|
thisx = self.width() / 2 + d.re * self.chartWidth / 2
|
|
|
|
thisy = self.height() / 2 + d.im * -1 * self.chartHeight / 2
|
|
|
|
positions.append(math.sqrt((x - thisx)**2 + (y - thisy)**2))
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
minimum_position = positions.index(min(positions))
|
|
|
|
m = self.getActiveMarker()
|
|
|
|
if m is not None:
|
|
|
|
m.setFrequency(str(round(target[minimum_position].freq)))
|
|
|
|
m.frequencyInput.setText(str(round(target[minimum_position].freq)))
|
|
|
|
return
|