2018-09-26 15:23:55 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
|
|
|
import rospy
|
|
|
|
import rospkg
|
|
|
|
|
|
|
|
import math
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from enums.clutch_state import ClutchState as CS
|
|
|
|
from enums.stop_state import StopState as SS
|
|
|
|
|
2018-09-26 15:55:58 +02:00
|
|
|
from master_restrictions import Restrictions
|
|
|
|
|
2018-09-26 15:23:55 +02:00
|
|
|
from python_qt_binding.QtGui import QPixmap
|
|
|
|
from python_qt_binding.QtGui import QTextCursor
|
2018-09-26 23:01:04 +02:00
|
|
|
from python_qt_binding.QtGui import QStandardItem
|
|
|
|
from python_qt_binding.QtGui import QStandardItemModel
|
|
|
|
from python_qt_binding.QtGui import QBrush
|
2018-09-26 15:23:55 +02:00
|
|
|
from python_qt_binding.QtWidgets import QMessageBox
|
2018-09-26 23:01:04 +02:00
|
|
|
from python_qt_binding.QtWidgets import QHeaderView
|
|
|
|
from python_qt_binding.QtWidgets import QAbstractItemView
|
|
|
|
from python_qt_binding.QtCore import Qt
|
2018-09-26 15:23:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class QtWrapper:
|
|
|
|
|
|
|
|
def __init__(self,widget):
|
|
|
|
self.widget = widget
|
|
|
|
self.displayed_robots_id_list = []
|
|
|
|
|
|
|
|
self.master_stop_state = None
|
2018-09-26 23:01:04 +02:00
|
|
|
self.init_robots_list()
|
2018-09-26 15:55:58 +02:00
|
|
|
|
|
|
|
# self.disable_sliders_tracking()
|
|
|
|
|
2018-10-01 14:04:38 +02:00
|
|
|
ui_directory_path = '{0}/ui'.format(rospkg.RosPack().get_path('safety_master_plugin'))
|
|
|
|
|
|
|
|
self.distance_pixmap = QPixmap('{0}/Distance.png'.format(ui_directory_path))
|
|
|
|
self.angular_pixmap = QPixmap('{0}/Angular.png'.format(ui_directory_path))
|
|
|
|
self.linear_pixmap = QPixmap('{0}/Linear.png'.format(ui_directory_path))
|
2018-09-27 11:42:55 +02:00
|
|
|
self.widget.distanceImage.setPixmap(self.distance_pixmap)
|
|
|
|
self.widget.angularImage.setPixmap(self.angular_pixmap)
|
|
|
|
self.widget.linearImage.setPixmap(self.linear_pixmap)
|
2018-09-26 15:23:55 +02:00
|
|
|
|
|
|
|
def set_sliders_to_initial_values(self):
|
2018-09-26 15:55:58 +02:00
|
|
|
self.widget.distanceSlider.setMaximum(200)
|
|
|
|
self.widget.angularSlider.setMaximum(200)
|
|
|
|
self.widget.linearSlider.setMaximum(200)
|
|
|
|
|
2018-10-01 13:54:22 +02:00
|
|
|
self.widget.distanceSlider.setValue(75)
|
2018-09-26 15:55:58 +02:00
|
|
|
self.widget.angularSlider.setValue(100)
|
|
|
|
self.widget.linearSlider.setValue(100)
|
|
|
|
|
2018-09-26 23:01:04 +02:00
|
|
|
def init_robots_list(self):
|
|
|
|
self.stdItemModel = QStandardItemModel( 0, 6, self.widget.robotsList)
|
|
|
|
self.stdItemModel.setHeaderData( 0, Qt.Horizontal, 'Robot' )
|
|
|
|
self.stdItemModel.setHeaderData( 1, Qt.Horizontal, 'Stan baterii' )
|
|
|
|
self.stdItemModel.setHeaderData( 2, Qt.Horizontal, 'Stan robota' )
|
|
|
|
self.stdItemModel.setHeaderData( 3, Qt.Horizontal, 'Stan sprzegla' )
|
|
|
|
self.stdItemModel.setHeaderData( 4, Qt.Horizontal, 'Predk. lin.' )
|
|
|
|
self.stdItemModel.setHeaderData( 5, Qt.Horizontal, 'Predk. obr.' )
|
|
|
|
|
|
|
|
self.widget.robotsList.setModel(self.stdItemModel)
|
|
|
|
|
|
|
|
self.widget.robotsList.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
|
|
|
|
self.widget.robotsList.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
|
|
|
|
self.widget.robotsList.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
|
|
|
|
self.widget.robotsList.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
|
|
|
|
self.widget.robotsList.horizontalHeader().setSectionResizeMode(4, QHeaderView.Stretch)
|
|
|
|
self.widget.robotsList.horizontalHeader().setSectionResizeMode(5, QHeaderView.Stretch)
|
|
|
|
|
|
|
|
self.widget.robotsList.horizontalHeader().setSectionsMovable(False)
|
|
|
|
# self.widget.robotsList.setSelectionBehavior(QAbstractItemView.SelectRows)
|
|
|
|
self.widget.robotsList.setSelectionMode(QAbstractItemView.NoSelection)
|
|
|
|
|
2018-09-26 15:23:55 +02:00
|
|
|
|
|
|
|
def disable_sliders_tracking(self):
|
|
|
|
self.widget.distanceSlider.setTracking(False)
|
|
|
|
self.widget.angularSlider.setTracking(False)
|
|
|
|
self.widget.linearSlider.setTracking(False)
|
|
|
|
|
|
|
|
|
|
|
|
def set_master_stop_state_updated_callback(self,callback_function):
|
|
|
|
self.master_stop_state_updated_callback = callback_function
|
|
|
|
|
|
|
|
def set_restrictions_updated_callback(self,callback_function):
|
|
|
|
self.restrictions_updated_callback = callback_function
|
|
|
|
|
|
|
|
def handle_restrictions_update(self):
|
2018-09-26 15:55:58 +02:00
|
|
|
distance = (float(self.widget.distanceSlider.value())/100)
|
|
|
|
angular_velocity = (float(self.widget.angularSlider.value())/100)
|
|
|
|
linear_velocity = (float(self.widget.linearSlider.value())/100)
|
|
|
|
|
|
|
|
self.widget.distanceLabel.setText("{:.2f}".format(distance))
|
|
|
|
self.widget.angularLabel.setText("{:.2f}".format(angular_velocity))
|
|
|
|
self.widget.linearLabel.setText("{:.2f}".format(linear_velocity))
|
|
|
|
|
|
|
|
restrictions = Restrictions(distance,angular_velocity,linear_velocity)
|
|
|
|
|
|
|
|
self.restrictions_updated_callback(restrictions)
|
2018-09-26 15:23:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
def connect_signals(self):
|
2018-09-26 15:55:58 +02:00
|
|
|
self.widget.distanceSlider.valueChanged.connect(self.handle_restrictions_update)
|
|
|
|
self.widget.angularSlider.valueChanged.connect(self.handle_restrictions_update)
|
|
|
|
self.widget.linearSlider.valueChanged.connect(self.handle_restrictions_update)
|
2018-09-26 15:23:55 +02:00
|
|
|
self.widget.masterstopButton.clicked.connect(self.handle_masterstopButton_clicked)
|
|
|
|
|
2018-09-26 15:55:58 +02:00
|
|
|
self.set_sliders_to_initial_values()
|
|
|
|
|
2018-09-26 15:23:55 +02:00
|
|
|
def handle_masterstopButton_clicked(self):
|
|
|
|
if self.master_stop_state == SS.RUNNING:
|
|
|
|
self.master_stop_state_updated_callback()
|
|
|
|
else:
|
|
|
|
reply = QMessageBox.warning(self.widget,"UWAGA","Na pewno chcesz odblokowac wszystkie roboty?",QMessageBox.Yes,QMessageBox.No)
|
|
|
|
|
|
|
|
if reply == QMessageBox.Yes:
|
|
|
|
self.master_stop_state_updated_callback()
|
|
|
|
|
|
|
|
def display_master_stop_on(self):
|
|
|
|
self.widget.masterstopButton.setStyleSheet("QPushButton { color: black; background-color: green; font: bold 20px}")
|
|
|
|
self.widget.masterstopButton.setText('Zatrzymaj roboty')
|
|
|
|
|
|
|
|
def display_master_stop_off(self):
|
|
|
|
self.widget.masterstopButton.setStyleSheet("QPushButton { color: black; background-color: red; font: bold 20px}")
|
|
|
|
self.widget.masterstopButton.setText('Odblokuj roboty')
|
|
|
|
|
|
|
|
|
2018-09-26 23:01:04 +02:00
|
|
|
def update_robot_info(self,robot_info):
|
|
|
|
items_to_add = []
|
|
|
|
items_to_add.append(QStandardItem('PIONIER{}'.format(robot_info.robot_id)))
|
|
|
|
items_to_add.append(QStandardItem('{:.2f}'.format(robot_info.battery_voltage)))
|
|
|
|
|
|
|
|
if robot_info.state == SS.RUNNING:
|
|
|
|
item = QStandardItem('Dziala')
|
|
|
|
item.setBackground(QBrush(Qt.green))
|
|
|
|
items_to_add.append(item)
|
|
|
|
elif robot_info.state == SS.STOPPED:
|
|
|
|
item = QStandardItem('Zatrzymany')
|
|
|
|
item.setBackground(QBrush(Qt.red))
|
|
|
|
items_to_add.append(item)
|
|
|
|
else:
|
|
|
|
raise ValueError('Undefined value of robot_info.robot_state')
|
|
|
|
|
|
|
|
if robot_info.clutch == CS.ENGAGED:
|
|
|
|
item = QStandardItem('Sprzegniete')
|
|
|
|
item.setBackground(QBrush(Qt.green))
|
|
|
|
items_to_add.append(item)
|
|
|
|
elif robot_info.clutch == CS.DISENGAGED:
|
|
|
|
item = QStandardItem('Rozprzegniete')
|
|
|
|
item.setBackground(QBrush(Qt.red))
|
|
|
|
items_to_add.append(item)
|
|
|
|
else:
|
|
|
|
raise ValueError('Undefined value of robot_info.clutch_state')
|
2018-09-26 15:23:55 +02:00
|
|
|
|
2018-09-26 23:01:04 +02:00
|
|
|
linear_vel = math.sqrt(robot_info.linear_velocity[0]**2 + robot_info.linear_velocity[1]**2)
|
|
|
|
items_to_add.append(QStandardItem('{:.2f}'.format(linear_vel)))
|
|
|
|
|
|
|
|
items_to_add.append(QStandardItem('{:.2f}'.format(robot_info.angular_velocity[2])))
|
2018-09-26 15:23:55 +02:00
|
|
|
|
|
|
|
|
2018-09-26 23:01:04 +02:00
|
|
|
items_list = self.stdItemModel.findItems('PIONIER{0}'.format(robot_info.robot_id),Qt.MatchExactly)
|
|
|
|
if len(items_list) > 0:
|
|
|
|
for item in items_list:
|
|
|
|
row_number = item.row()
|
|
|
|
else:
|
|
|
|
row_number = self.stdItemModel.rowCount()
|
2018-09-26 15:23:55 +02:00
|
|
|
|
2018-09-26 23:01:04 +02:00
|
|
|
for i,item in enumerate(items_to_add):
|
|
|
|
item.setTextAlignment(Qt.AlignCenter)
|
|
|
|
item.setEditable(0)
|
|
|
|
self.stdItemModel.setItem(row_number,i,item)
|
2018-09-26 17:25:38 +02:00
|
|
|
|
2018-09-26 15:23:55 +02:00
|
|
|
def master_stopped(self):
|
|
|
|
self.master_stop_state = SS.STOPPED
|
|
|
|
self.display_master_stop_off()
|
|
|
|
self.log_info('Przycisk masterSTOP zostal nacisniety. Zatrzymuje roboty')
|
|
|
|
|
|
|
|
def master_started(self):
|
|
|
|
self.master_stop_state = SS.RUNNING
|
|
|
|
self.display_master_stop_on()
|
|
|
|
self.log_info('Przycisk masterSTOP odcisniety')
|
|
|
|
|
|
|
|
|
2018-09-26 17:25:38 +02:00
|
|
|
def add_robot(self,robot_id):
|
2018-09-26 23:01:04 +02:00
|
|
|
|
|
|
|
if robot_id not in self.displayed_robots_id_list:
|
|
|
|
new_robot_info=QStandardItem('PIONIER{}'.format(robot_id))
|
|
|
|
new_robot_info.setTextAlignment(Qt.AlignCenter)
|
|
|
|
new_robot_info.setEditable(0)
|
|
|
|
# new_robot_info.setBackground(QBrush(Qt.white))
|
|
|
|
self.stdItemModel.setItem(self.stdItemModel.rowCount(),0,new_robot_info)
|
|
|
|
|
|
|
|
self.displayed_robots_id_list.append(robot_id)
|
|
|
|
self.log_info('PIONIER{0} polaczony'.format(robot_id))
|
|
|
|
else:
|
|
|
|
raise RuntimeError('Adding robot, which is already in master GUI')
|
2018-09-26 17:25:38 +02:00
|
|
|
|
|
|
|
def remove_robot(self,robot_id):
|
2018-09-26 23:01:04 +02:00
|
|
|
items_list = self.stdItemModel.findItems('PIONIER{0}'.format(robot_id),Qt.MatchExactly)
|
|
|
|
|
|
|
|
if len(items_list) > 0:
|
|
|
|
for item in items_list:
|
|
|
|
row_number = item.row()
|
|
|
|
self.stdItemModel.removeRow(row_number)
|
|
|
|
|
2018-09-26 17:25:38 +02:00
|
|
|
self.log_info('PIONIER{0} rozlaczony'.format(robot_id))
|
|
|
|
self.displayed_robots_id_list.remove(robot_id)
|
|
|
|
|
2018-09-26 15:23:55 +02:00
|
|
|
def log_info(self,info_text):
|
|
|
|
time = datetime.datetime.now().strftime('[%H:%M:%S]')
|
|
|
|
|
|
|
|
cursor = self.widget.logsBrowser.textCursor()
|
|
|
|
cursor.movePosition(QTextCursor.End)
|
|
|
|
self.widget.logsBrowser.setTextCursor(cursor)
|
2018-09-27 11:50:25 +02:00
|
|
|
# self.widget.logsBrowser.insertHtml('<font color="black">' + str(time) + '. ' + info_text + '</font><br>')
|
|
|
|
self.widget.logsBrowser.insertHtml('<font color="black"> {0} {1}</font><br>'.format(time,info_text))
|
2018-09-26 15:23:55 +02:00
|
|
|
self.scroll_to_bottom()
|
|
|
|
|
|
|
|
def log_warning(self,warning_text):
|
|
|
|
time = datetime.datetime.now().strftime('[%H:%M:%S]')
|
|
|
|
|
|
|
|
cursor = self.widget.logsBrowser.textCursor()
|
|
|
|
cursor.movePosition(QTextCursor.End)
|
|
|
|
self.widget.logsBrowser.setTextCursor(cursor)
|
|
|
|
self.widget.logsBrowser.textCursor().movePosition(QTextCursor.End)
|
2018-09-27 11:50:25 +02:00
|
|
|
self.widget.logsBrowser.insertHtml('<font color="orange"> {0} {1}</font><br>'.format(time,warning_text))
|
2018-09-26 15:23:55 +02:00
|
|
|
self.scroll_to_bottom()
|
|
|
|
|
|
|
|
def log_error(self,error_text):
|
|
|
|
time = datetime.datetime.now().strftime('[%H:%M:%S]')
|
|
|
|
|
|
|
|
cursor = self.widget.logsBrowser.textCursor()
|
|
|
|
cursor.movePosition(QTextCursor.End)
|
|
|
|
self.widget.logsBrowser.setTextCursor(cursor)
|
|
|
|
self.widget.logsBrowser.textCursor().movePosition(QTextCursor.End)
|
2018-09-27 11:50:25 +02:00
|
|
|
self.widget.logsBrowser.insertHtml('<font color="red"> {0} {1}</font><br>'.format(time,error_text))
|
2018-09-26 15:23:55 +02:00
|
|
|
self.scroll_to_bottom()
|
|
|
|
|
|
|
|
def scroll_to_bottom(self):
|
|
|
|
scrollbar = self.widget.logsBrowser.verticalScrollBar()
|
|
|
|
scrollbar.setValue(scrollbar.maximum())
|