md10c_serial_bridge
読み取り中…
検索中…
一致する文字列を見つけられません
control_ui.py
[詳解]
1#!/usr/bin/env python3
2"""MD10C向けのPWM指令を配信するPyQt操作画面。"""
3
4import rclpy
5from rclpy.node import Node
6from std_msgs.msg import Int16
7
8from PyQt5.QtCore import Qt, QTimer
9from PyQt5.QtWidgets import (
10 QApplication,
11 QFrame,
12 QGridLayout,
13 QHBoxLayout,
14 QLabel,
15 QMainWindow,
16 QPushButton,
17 QSlider,
18 QSpinBox,
19 QVBoxLayout,
20 QWidget,
21)
22
23
24class Md10cControlNode(Node):
25 """Qtアプリから使うROSの指令配信処理。"""
26
27 def __init__(self) -> None:
28 super().__init__('md10c_control_ui')
29 self.declare_parameter('topic', '/md10c/cmd_pwm')
30 topic = self.get_parameter('topic').value
31 self._publisher = self.create_publisher(Int16, topic, 10)
32 self.get_logger().info(f'Publishing MD10C commands on {topic}')
33
34 def send_command(self, value: int) -> None:
35 message = Int16()
36 message.data = max(-255, min(255, int(value)))
37 self._publisher.publish(message)
38
39
40class Md10cControlWindow(QMainWindow):
41 """PWM出力を直接指定して操作する画面。停止操作も備える。"""
42
43 def __init__(self, node: Md10cControlNode) -> None:
44 super().__init__()
45 self._node = node
46 self.setWindowTitle('MD10C Uno モーター操作')
47 self.setMinimumSize(480, 340)
48 self.resize(560, 360)
49 self.setStyleSheet('''
50 QWidget {
51 background: #f2f3f4;
52 color: #252a2e;
53 font-family: "Noto Sans CJK JP", "Yu Gothic UI", sans-serif;
54 font-size: 13px;
55 }
56 QFrame#panel {
57 background: #ffffff;
58 border: 1px solid #c9ced2;
59 border-radius: 4px;
60 }
61 QLabel#mainTitle { color: #20262a; font-size: 20px; font-weight: 600; }
62 QLabel#subtle { color: #58636b; font-size: 12px; }
63 QLabel#value {
64 color: #20262a;
65 background: #f7f8f8;
66 border: 1px solid #d2d7da;
67 border-radius: 4px;
68 font-family: "DejaVu Sans Mono", monospace;
69 font-size: 42px;
70 font-weight: 600;
71 padding: 7px;
72 }
73 QLabel#unit { color: #58636b; font-size: 12px; }
74 QSlider::groove:horizontal {
75 height: 6px; background: #d7dcdf; border-radius: 3px;
76 }
77 QSlider::sub-page:horizontal { background: #6d9277; border-radius: 3px; }
78 QSlider::add-page:horizontal { background: #d7dcdf; border-radius: 3px; }
79 QSlider::handle:horizontal {
80 background: #ffffff; border: 2px solid #63727b;
81 width: 16px; height: 16px; margin: -7px 0; border-radius: 9px;
82 }
83 QSlider::handle:horizontal:hover { background: #edf1ee; }
84 QSpinBox {
85 background: #ffffff; border: 1px solid #aeb7bc;
86 border-radius: 3px; padding: 6px 8px; min-width: 90px;
87 font-family: "DejaVu Sans Mono", monospace; font-size: 16px;
88 }
89 QPushButton {
90 background: #ffffff; border: 1px solid #aeb7bc;
91 border-radius: 3px; padding: 8px 14px;
92 }
93 QPushButton:hover { background: #e9edef; }
94 QPushButton#stopButton {
95 color: #ffffff; background: #a74742; border: 1px solid #963b36;
96 }
97 QPushButton#stopButton:hover { background: #913c38; }
98 ''')
99
100 central = QWidget(self)
101 central_layout = QVBoxLayout(central)
102 central_layout.setContentsMargins(16, 12, 16, 12)
103 content = QWidget(central)
104 content.setMaximumWidth(620)
105 content.setMaximumHeight(340)
106 central_layout.addStretch(1)
107 central_layout.addWidget(content, alignment=Qt.AlignHCenter)
108 central_layout.addStretch(1)
109
110 layout = QVBoxLayout(content)
111 layout.setContentsMargins(8, 8, 8, 8)
112 layout.setSpacing(10)
113
114 header = QHBoxLayout()
115 title = QLabel('モーター制御')
116 title.setObjectName('mainTitle')
117 title.setAlignment(Qt.AlignCenter)
118 header.addWidget(title)
119 layout.addLayout(header)
120
121 panel = QFrame()
122 panel.setObjectName('panel')
123 panel_layout = QVBoxLayout(panel)
124 panel_layout.setContentsMargins(18, 15, 18, 17)
125 panel_layout.setSpacing(12)
126 panel_heading = QHBoxLayout()
127 panel_heading.addWidget(QLabel('出力指令'))
128 panel_heading.addStretch(1)
129 panel_layout.addLayout(panel_heading)
130
131 value_row = QHBoxLayout()
132 value_row.addStretch(1)
133 self._value_label = QLabel('0')
134 self._value_label.setObjectName('value')
135 self._value_label.setAlignment(Qt.AlignCenter)
136 self._value_label.setMinimumWidth(210)
137 value_row.addWidget(self._value_label)
138 value_row.addSpacing(10)
139 unit_column = QVBoxLayout()
140 unit_column.addStretch(1)
141 value_unit = QLabel('/ 255')
142 value_unit.setObjectName('unit')
143 unit_column.addWidget(value_unit)
144 unit_column.addStretch(1)
145 value_row.addLayout(unit_column)
146 value_row.addStretch(1)
147 panel_layout.addLayout(value_row)
148
149 self._slider = QSlider(Qt.Horizontal)
150 self._slider.setRange(-255, 255)
151 self._slider.setValue(0)
152 self._slider.setSingleStep(5)
153 self._slider.setPageStep(25)
154 self._slider.setTickPosition(QSlider.TicksBelow)
155 self._slider.setTickInterval(51)
156 self._slider.valueChanged.connect(self._on_slider_changed)
157 panel_layout.addWidget(self._slider)
158
159 limits = QHBoxLayout()
160 reverse = QLabel('逆転 -255')
161 reverse.setObjectName('subtle')
162 center = QLabel('停止 0')
163 center.setObjectName('subtle')
164 forward = QLabel('正転 +255')
165 forward.setObjectName('subtle')
166 limits.addWidget(reverse)
167 limits.addStretch(1)
168 limits.addWidget(center)
169 limits.addStretch(1)
170 limits.addWidget(forward)
171 panel_layout.addLayout(limits)
172 layout.addWidget(panel)
173
174 controls = QGridLayout()
175 controls.setHorizontalSpacing(12)
176 control_label = QLabel('直接入力')
177 control_label.setObjectName('eyebrow')
178 controls.addWidget(control_label, 0, 0)
179 self._spinbox = QSpinBox()
180 self._spinbox.setRange(-255, 255)
181 self._spinbox.setSingleStep(5)
182 self._spinbox.valueChanged.connect(self._on_spinbox_changed)
183 controls.addWidget(self._spinbox, 0, 1)
184 controls.setColumnStretch(2, 1)
185
186 self._stop_button = QPushButton('停止')
187 self._stop_button.setObjectName('stopButton')
188 self._stop_button.setMinimumHeight(48)
189 self._stop_button.clicked.connect(self.stop_motor)
190 controls.addWidget(self._stop_button, 0, 3)
191 layout.addLayout(controls)
192
193 self.setCentralWidget(central)
194
195 # Qt owns the event loop. Spin ROS callbacks from a short GUI timer.
196 self._ros_timer = QTimer(self)
197 self._ros_timer.timeout.connect(self._spin_ros)
198 self._ros_timer.start(10)
199
200 def _on_slider_changed(self, value: int) -> None:
201 self._spinbox.blockSignals(True)
202 self._spinbox.setValue(value)
203 self._spinbox.blockSignals(False)
204 self._publish(value)
205
206 def _on_spinbox_changed(self, value: int) -> None:
207 self._slider.blockSignals(True)
208 self._slider.setValue(value)
209 self._slider.blockSignals(False)
210 self._publish(value)
211
212 def _publish(self, value: int) -> None:
213 self._value_label.setText(f'{value:+d}' if value > 0 else str(value))
214 self._node.send_command(value)
215
216 def stop_motor(self) -> None:
217 self._slider.setValue(0)
218 self._spinbox.setValue(0)
219 # Send explicitly even when the widgets already show zero.
220 self._publish(0)
221
222 def _spin_ros(self) -> None:
223 rclpy.spin_once(self._node, timeout_sec=0.0)
224
225 def closeEvent(self, event) -> None:
226 self._publish(0)
227 self._ros_timer.stop()
228 event.accept()
229
230
231def main(args=None) -> None:
232 rclpy.init(args=args)
233 # ROS arguments are consumed by rclpy; Qt should not parse ROS remap flags.
234 app = QApplication([])
235 node = None
236 try:
237 node = Md10cControlNode()
238 window = Md10cControlWindow(node)
239 window.show()
240 app.exec_()
241 finally:
242 if node is not None:
243 node.send_command(0)
244 node.destroy_node()
245 rclpy.shutdown()
246
247
248if __name__ == '__main__':
249 main()
Qtアプリから使うROSの指令配信処理。
PWM出力を直接指定して操作する画面。停止操作も備える。