Skip to content

Commit

Permalink
Fixed crashes
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Hernández Cordero <[email protected]>
  • Loading branch information
ahcorde committed Feb 6, 2024
1 parent 85101c1 commit 70051fc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
18 changes: 14 additions & 4 deletions src/rqt_gauges/speedometer_gauge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from PyQt5.QtCore import QObject, QPoint, QPointF, Qt
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QPoint, QPointF, Qt
from PyQt5.QtGui import (QColor, QConicalGradient, QFont, QFontMetrics, QPainter, QPen,
QPolygon, QPolygonF, QRadialGradient)
from PyQt5.QtWidgets import QWidget
Expand All @@ -13,6 +13,8 @@ class SpeedometerGauge(QWidget):
# gauge and the units the numbers are displaying. The class contains methods used to modify
# the values explained before, the marked number of the gauge and the whole design as well.

updateValueSignal = pyqtSignal(float)

def __init__(self, parent=None):
# Constructor method of the class, initializes all the variables needed to create
# the gauge.
Expand Down Expand Up @@ -80,9 +82,9 @@ def __init__(self, parent=None):
self.gauge_color_outer_radius_factor = 1
self.gauge_color_inner_radius_factor = 0.9

self.update()
# self.update()
self.setGaugeTheme()
self.rescale_method()
# self.rescale_method()

def setGaugeTheme(self):
# This method defines the theme of the gauge, it is used to stablish the colors for each
Expand Down Expand Up @@ -227,6 +229,7 @@ def draw_filled_polygon(self, outline_pen_with=0):
painter_filled_polygon.setBrush(grad)

painter_filled_polygon.drawPolygon(colored_scale_polygon)
painter_filled_polygon.end()

def draw_big_scaled_marker(self):
my_painter = QPainter(self)
Expand All @@ -245,6 +248,7 @@ def draw_big_scaled_marker(self):
for i in range(self.scalaCount+1):
my_painter.drawLine(int(scale_line_lenght), 0, int(scale_line_outer_start), 0)
my_painter.rotate(steps_size)
my_painter.end()

def create_scale_marker_values_text(self):
painter = QPainter(self)
Expand Down Expand Up @@ -275,6 +279,7 @@ def create_scale_marker_values_text(self):

text = [x - int(w/2), y - int(h/2), int(w), int(h), Qt.AlignCenter, text]
painter.drawText(text[0], text[1], text[2], text[3], text[4], text[5])
painter.end()

def create_fine_scaled_marker(self):
my_painter = QPainter(self)
Expand All @@ -290,6 +295,7 @@ def create_fine_scaled_marker(self):
for i in range((self.scalaCount * self.scala_subdiv_count)+1):
my_painter.drawLine(int(scale_line_lenght), 0, int(scale_line_outer_start), 0)
my_painter.rotate(steps_size)
my_painter.end()

def create_value_text(self):
painter = QPainter(self)
Expand Down Expand Up @@ -317,6 +323,7 @@ def create_value_text(self):
y = int(text_radius * math.sin(math.radians(angle)))
text = [x - int(w/2), y - int(h/2), int(w), int(h), Qt.AlignCenter, text]
painter.drawText(text[0], text[1], text[2], text[3], text[4], text[5])
painter.end()

def create_units_text(self):
painter = QPainter(self)
Expand Down Expand Up @@ -345,6 +352,7 @@ def create_units_text(self):
y = int(text_radius * math.sin(math.radians(angle)))
text = [x - int(w/2), y - int(h/2), int(w), int(h), Qt.AlignCenter, text]
painter.drawText(text[0], text[1], text[2], text[3], text[4], text[5])
painter.end()

def draw_big_needle_center_point(self):
painter = QPainter(self)
Expand All @@ -363,6 +371,7 @@ def draw_big_needle_center_point(self):
painter.setBrush(grad)

painter.drawPolygon(colored_scale_polygon)
painter.end()

def draw_outer_circle(self):
painter = QPainter(self)
Expand All @@ -381,6 +390,7 @@ def draw_outer_circle(self):
painter.setBrush(radialGradient)

painter.drawPolygon(colored_scale_polygon)
painter.end()

def draw_needle(self):
painter = QPainter(self)
Expand All @@ -391,10 +401,10 @@ def draw_needle(self):
(self.maxValue - self.minValue)) + 90 + self.scale_angle_start_value)

painter.drawConvexPolygon(self.value_needle[0])
painter.end()

def resizeEvent(self, event):
self.rescale_method()
pass

def paintEvent(self, event):

Expand Down
11 changes: 8 additions & 3 deletions src/rqt_gauges/speedometer_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, node):
self.max_value.textChanged.connect(self.updateMaxValue)
self.units.currentTextChanged.connect(self.updateUnits)
self.subscribe_button.pressed.connect(self.updateSubscription)
self.speedometer_gauge.updateValueSignal.connect(self.updateValue)

@pyqtSlot()
def updateMinValue(self):
Expand All @@ -58,6 +59,10 @@ def updateMaxValue(self):
else:
self.speedometer_gauge.setMaxValue(180)

@pyqtSlot(float)
def updateValue(self, value):
self.speedometer_gauge.updateValue(value)

@pyqtSlot(str)
def updateUnits(self, new_units):
self.speedometer_gauge.units = new_units
Expand Down Expand Up @@ -87,8 +92,8 @@ def speedometer_callback(self, msg):
value = f(value)
if value is not None:
if type(value) == int or type(value) == float or type(value) == str:
self.speedometer_gauge.updateValue(float(value))
self.speedometer_gauge.updateValueSignal.emit(float(value))
else:
self.speedometer_gauge.updateValue(self.speedometer_gauge.minValue)
self.speedometer_gauge.updateValueSignal.emit(self.speedometer_gauge.minValue)
else:
self.speedometer_gauge.updateValue(self.speedometer_gauge.minValue)
self.speedometer_gauge.updateValueSignal.emit(self.speedometer_gauge.minValue)
4 changes: 3 additions & 1 deletion src/rqt_gauges/steering_wheel_gauge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from PyQt5.QtCore import QRect, Qt
from PyQt5.QtCore import pyqtSignal, QRect, Qt
from PyQt5.QtGui import QBrush, QColor, QFont, QFontMetrics, QPainter, QPen
from PyQt5.QtWidgets import QWidget

Expand All @@ -13,6 +13,8 @@ class SteeringWheelGauge(QWidget):
# methods used to modify the values explained before, the marked number of the gauge
# and the whole design as well.

updateValueSignal = pyqtSignal(float)

def __init__(self, parent=None):
# Constructor method of the class, initializes all the variables needed to create
# # the gauge.
Expand Down
11 changes: 8 additions & 3 deletions src/rqt_gauges/steering_wheel_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, node):

self.max_value.textChanged.connect(self.updateMaxValue)
self.subscribe_button.pressed.connect(self.updateSubscription)
self.steering_wheel_gauge.updateValueSignal.connect(self.updateValue)

@pyqtSlot()
def updateMaxValue(self):
Expand Down Expand Up @@ -64,14 +65,18 @@ def updateSubscription(self):
self.steering_wheel_callback,
10)

@pyqtSlot(float)
def updateValue(self, value):
self.steering_wheel_gauge.updateValue(value)

def steering_wheel_callback(self, msg):
value = msg
for f in self.field_evals:
value = f(value)
if value is not None:
if type(value) == int or type(value) == float or type(value) == str:
self.steering_wheel_gauge.updateValue(float(value))
self.steering_wheel_gauge.updateValueSignal.emit(float(value))
else:
self.steering_wheel_gauge.updateValue(0)
self.steering_wheel_gauge.updateValueSignal.emit(0.0)
else:
self.steering_wheel_gauge.updateValue(0)
self.steering_wheel_gauge.updateValueSignal.emit(0.0)
23 changes: 18 additions & 5 deletions src/rqt_gauges/throttle_brake_pedals_widget.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from ament_index_python.resources import get_resource
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QWidget
from python_qt_binding import loadUi
from rosidl_runtime_py.utilities import get_message
Expand All @@ -12,6 +12,9 @@

class ThrottleBrakePedalsWidget(QWidget):

updateThrottleValueSignal = pyqtSignal(int)
updateBrakeValueSignal = pyqtSignal(int)

def __init__(self, node):
super().__init__()
self.setObjectName('ThrottleBrakePedals_widget')
Expand Down Expand Up @@ -40,6 +43,8 @@ def __init__(self, node):
# Signals Connection
self.throttle_subscribe.pressed.connect(self.throttleUpdateSubscription)
self.brake_subscribe.pressed.connect(self.brakeUpdateSubscription)
self.updateThrottleValueSignal.connect(self.updateThrottle)
self.updateBrakeValueSignal.connect(self.updateBrake)

@pyqtSlot()
def throttleUpdateSubscription(self):
Expand All @@ -59,6 +64,16 @@ def throttleUpdateSubscription(self):
self.throttle_callback,
10)

@pyqtSlot(int)
def updateThrottle(self, value):
self.throttle_pedal.setValue(value)
self.throttle_label.setText(str(value / 100.0))

@pyqtSlot(int)
def updateBrake(self, value):
self.brake_pedal.setValue(value)
self.brake_label.setText(str(value / 100.0))

@pyqtSlot()
def brakeUpdateSubscription(self):
if self.node.destroy_subscription(self.brake_sub):
Expand All @@ -84,8 +99,7 @@ def throttle_callback(self, msg):
if value is not None and (type(value) == int or type(value) == float
or type(value) == str):
if value <= 1 and value >= 0:
self.throttle_pedal.setValue(int(value*100))
self.throttle_label.setText(str(value))
self.updateThrottleValueSignal.emit(int(value*100))
else:
print('The throttle pedal value is not between 0 and 1')
else:
Expand All @@ -98,8 +112,7 @@ def brake_callback(self, msg):
if value is not None and (type(value) == int or type(value) == float
or type(value) == str):
if value <= 1 and value >= 0:
self.brake_pedal.setValue(int(value*100))
self.brake_label.setText(str(value))
self.updateBrakeValueSignal.emit(int(value*100))
else:
print('The brake pedal value is not between 0 and 1')
else:
Expand Down

0 comments on commit 70051fc

Please sign in to comment.