├── .gitignore ├── src └── rqt_virtual_joy │ ├── __init__.py │ ├── virtual_joy_module.py │ └── joystickView.py ├── screenshot └── window.png ├── resource ├── input-gaming.png └── VirtualJoy.ui ├── CHANGELOG.rst ├── scripts └── rqt_virtual_joy ├── setup.py ├── plugin.xml ├── README.md ├── LICENSE ├── package.xml └── CMakeLists.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.pcd 2 | *.pyc -------------------------------------------------------------------------------- /src/rqt_virtual_joy/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /screenshot/window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aquahika/rqt_virtual_joystick/HEAD/screenshot/window.png -------------------------------------------------------------------------------- /resource/input-gaming.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aquahika/rqt_virtual_joystick/HEAD/resource/input-gaming.png -------------------------------------------------------------------------------- /CHANGELOG.rst: -------------------------------------------------------------------------------- 1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2 | Changelog for package rqt_virtual_joy 3 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 4 | 5 | 0.1.2 (2020-05-09) 6 | ------------------ 7 | 8 | * First Release 9 | * Contributors: Hikaru Sugiura 10 | -------------------------------------------------------------------------------- /scripts/rqt_virtual_joy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | 5 | from rqt_virtual_joy.virtual_joy_module import MyPlugin 6 | from rqt_gui.main import Main 7 | 8 | plugin = 'rqt_virtual_joy' 9 | main = Main(filename=plugin) 10 | sys.exit(main.main(standalone=plugin)) -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from distutils.core import setup 3 | from catkin_pkg.python_setup import generate_distutils_setup 4 | 5 | d = generate_distutils_setup( 6 | packages=['rqt_virtual_joy'], 7 | scripts=['scripts/rqt_virtual_joy'], 8 | package_dir={'': 'src'}, 9 | ) 10 | 11 | setup(**d) -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | An example Python GUI plugin to create a great user interface. 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | resource/input-gaming.png 17 | Great user interface to provide real value. 18 | 19 | 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rqt_virtual_joystick 2 | Simple rqt virtual joystick publish `sensor_msgs/Joy` message. 3 | 4 | 5 | 6 | ## Usage 7 | 8 | ``` 9 | rqt_virtual_joystick 10 | ``` 11 | 12 | or 13 | 14 | ``` 15 | rosrun rqt_virtual_joystick rqt_virtual_joystick 16 | ``` 17 | 18 | Check `Publish` box to start publishing. 19 | 20 | 21 | ## Options 22 | 23 | - -t [--topic] topic 24 | - Specify a initial topic to publish here. default: `/joy` 25 | - -r [--rate] hz 26 | - Initial publishing rate. default: `20Hz` 27 | - --type ( circle | square ) 28 | - Select initial joystick type. default: `circle` 29 | 30 | ## Author 31 | Hikaru Sugiura 32 | 33 | ## License 34 | 35 | `input-gaming.png` icon from Tango Project is licensed under `CC-BY-SA` 36 | http://tango.freedesktop.org/ 37 | 38 | Any other source codes are licenced under [MIT license](https://en.wikipedia.org/wiki/MIT_License). -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Hikaru Sugiura 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | rqt_virtual_joy 4 | 0.1.2 5 | The rqt_virtual_joy package 6 | 7 | Hikaru Sugiura 8 | 9 | MIT 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | Hikaru Sugiura 19 | 20 | std_msgs 21 | sensor_msgs 22 | 23 | catkin 24 | rospy 25 | rqt_gui 26 | rqt_gui_py 27 | rospy 28 | rqt_gui 29 | rqt_gui_py 30 | rospy 31 | rqt_gui 32 | rqt_gui_py 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/rqt_virtual_joy/virtual_joy_module.py: -------------------------------------------------------------------------------- 1 | import os 2 | import rospy 3 | import rospkg 4 | from sensor_msgs.msg import Joy 5 | 6 | from qt_gui.plugin import Plugin 7 | from python_qt_binding import loadUi 8 | from python_qt_binding.QtWidgets import QWidget,QGraphicsView 9 | from python_qt_binding.QtGui import QCursor 10 | from python_qt_binding import QtCore 11 | 12 | 13 | class MyPlugin(Plugin): 14 | 15 | def __init__(self, context): 16 | 17 | super(MyPlugin, self).__init__(context) 18 | 19 | # Give QObjects reasonable names 20 | self.setObjectName('MyPlugin') 21 | 22 | # Process standalone plugin command-line arguments 23 | from argparse import ArgumentParser 24 | parser = ArgumentParser() 25 | # Add argument(s) to the parser. 26 | parser.add_argument("-q", "--quiet", action="store_true", 27 | dest="quiet", 28 | help="Put plugin in silent mode") 29 | parser.add_argument("-t", "--topic", 30 | dest="topic", 31 | type=str, 32 | help="Set topic to publish [default:/joy]", 33 | default="/joy") 34 | parser.add_argument("-r", "--rate", 35 | dest="rate", 36 | type=float, 37 | help="Set publish rate [default:20]", 38 | default=20) 39 | parser.add_argument("--type", 40 | dest="type", 41 | type=str, 42 | choices=['circle', 'square'], 43 | default='circle') 44 | 45 | 46 | args, unknowns = parser.parse_known_args(context.argv()) 47 | if not args.quiet: 48 | print 'arguments: ', args 49 | print 'unknowns: ', unknowns 50 | 51 | # Create QWidget 52 | self._widget = QWidget() 53 | # Get path to UI file which should be in the "resource" folder of this package 54 | ui_file = os.path.join(rospkg.RosPack().get_path('rqt_virtual_joy'), 'resource', 'VirtualJoy.ui') 55 | # Extend the widget with all attributes and children from UI file 56 | loadUi(ui_file, self._widget) 57 | # Give QObjects reasonable names 58 | self._widget.setObjectName('MyPluginUi') 59 | # Show _widget.windowTitle on left-top of each plugin (when 60 | # it's set in _widget). This is useful when you open multiple 61 | # plugins at once. Also if you open multiple instances of your 62 | # plugin at once, these lines add number to make it easy to 63 | # tell from pane to pane. 64 | if context.serial_number() > 1: 65 | self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number())) 66 | # Add widget to the user interface 67 | context.add_widget(self._widget) 68 | 69 | self._widget.topicLineEdit.returnPressed.connect(self.topicNameUpdated) 70 | self._widget.topicLineEdit.setText(args.topic) # Default Topic 71 | self.updatePublisher() 72 | 73 | self._widget.publishCheckBox.stateChanged.connect(self.publishCheckboxChanged) 74 | self._widget.rateSpinBox.valueChanged.connect(self.publishRateSpinBoxChanged) 75 | self._widget.rateSpinBox.setValue(args.rate) 76 | 77 | self._widget.joy.xMoved.connect(self.receiveX) 78 | self._widget.joy.yMoved.connect(self.receiveY) 79 | 80 | self._widget.shapeSelectBox.addItem("square") 81 | self._widget.shapeSelectBox.addItem("circle") 82 | 83 | self._widget.shapeSelectBox.activated.connect(self.indexChanged) 84 | self._widget.shapeSelectBox.setCurrentText(args.type) # circle 85 | self._widget.joy.setMode(args.type) 86 | 87 | 88 | def topicNameUpdated(self): 89 | self.updatePublisher() 90 | 91 | 92 | def updatePublisher(self): 93 | topic = str(self._widget.topicLineEdit.text()) 94 | try: 95 | if self.pub != None: 96 | self.pub.unregister() 97 | except: 98 | pass 99 | self.pub = None 100 | self.pub = rospy.Publisher(topic, Joy,queue_size=10) 101 | 102 | def startIntervalTimer(self,msec): 103 | 104 | try: 105 | self._timer.stop() 106 | except: 107 | self._timer = QtCore.QTimer(self) 108 | self._timer.timeout.connect(self.processTimerShot) 109 | 110 | if msec > 0: 111 | self._timer.setInterval(msec) 112 | self._timer.start() 113 | 114 | 115 | def publishCheckboxChanged(self,status): 116 | self.updateROSPublishState() 117 | 118 | def publishRateSpinBoxChanged(self,status): 119 | self.updateROSPublishState() 120 | 121 | def updateROSPublishState(self): 122 | 123 | if self._widget.publishCheckBox.checkState() == QtCore.Qt.Checked: 124 | rate = self._widget.rateSpinBox.value() 125 | self.startIntervalTimer(float(1000.0/rate)) 126 | else: 127 | self.startIntervalTimer(-1) # Stop Timer (Stop Publish) 128 | 129 | 130 | def indexChanged(self,index): 131 | text = str(self._widget.shapeSelectBox.currentText()) 132 | self._widget.joy.setMode(str(text)) 133 | 134 | def receiveX(self,val): 135 | self.updateJoyPosLabel() 136 | 137 | def receiveY(self,val): 138 | self.updateJoyPosLabel() 139 | 140 | def updateJoyPosLabel(self): 141 | pos = self.getROSJoyValue() 142 | text = "({:1.2f},{:1.2f})".format(pos['x'],pos['y']) 143 | self._widget.joyPosLabel.setText(text) 144 | 145 | def processTimerShot(self): 146 | joy = self.getROSJoyValue() 147 | msg = Joy() 148 | msg.header.stamp = rospy.Time.now() 149 | msg.axes.append(float(joy['x'])) 150 | msg.axes.append(float(joy['y'])) 151 | 152 | button_num = 1 153 | while True: 154 | try: 155 | msg.buttons.append(eval("self._widget.button"+str(button_num)).isDown()) 156 | button_num+=1 157 | except: 158 | break 159 | 160 | try: 161 | self.pub.publish(msg) 162 | except: 163 | rospy.logwarn("publisher not initialized") 164 | pass 165 | 166 | def getROSJoyValue(self): 167 | return self._widget.joy.getJoyValue() 168 | #return self.convertREPCoordinate(self._widget.joy.getJoyValue()) 169 | 170 | def convertREPCoordinate(self,input): 171 | output = {} 172 | output['x'] = input['y'] 173 | output['y'] = input['x'] 174 | return output 175 | 176 | def shutdown_plugin(self): 177 | # TODO unregister all publishers here 178 | self.pub.unregister() 179 | pass 180 | 181 | def save_settings(self, plugin_settings, instance_settings): 182 | # TODO save intrinsic configuration, usually using: 183 | # instance_settings.set_value(k, v) 184 | pass 185 | 186 | def restore_settings(self, plugin_settings, instance_settings): 187 | # TODO restore intrinsic configuration, usually using: 188 | # v = instance_settings.value(k) 189 | pass 190 | 191 | #def trigger_configuration(self): 192 | # Comment in to signal that the plugin has a way to configure 193 | # This will enable a setting button (gear icon) in each dock widget title bar 194 | # Usually used to open a modal configuration dialog -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.3) 2 | project(rqt_virtual_joy) 3 | 4 | ## Compile as C++11, supported in ROS Kinetic and newer 5 | # add_compile_options(-std=c++11) 6 | 7 | ## Find catkin macros and libraries 8 | ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) 9 | ## is used, also find other catkin packages 10 | find_package(catkin REQUIRED COMPONENTS 11 | rospy 12 | rqt_gui 13 | rqt_gui_py 14 | ) 15 | 16 | ## System dependencies are found with CMake's conventions 17 | # find_package(Boost REQUIRED COMPONENTS system) 18 | 19 | 20 | ## Uncomment this if the package has a setup.py. This macro ensures 21 | ## modules and global scripts declared therein get installed 22 | ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html 23 | catkin_python_setup() 24 | 25 | ################################################ 26 | ## Declare ROS messages, services and actions ## 27 | ################################################ 28 | 29 | ## To declare and build messages, services or actions from within this 30 | ## package, follow these steps: 31 | ## * Let MSG_DEP_SET be the set of packages whose message types you use in 32 | ## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...). 33 | ## * In the file package.xml: 34 | ## * add a build_depend tag for "message_generation" 35 | ## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET 36 | ## * If MSG_DEP_SET isn't empty the following dependency has been pulled in 37 | ## but can be declared for certainty nonetheless: 38 | ## * add a exec_depend tag for "message_runtime" 39 | ## * In this file (CMakeLists.txt): 40 | ## * add "message_generation" and every package in MSG_DEP_SET to 41 | ## find_package(catkin REQUIRED COMPONENTS ...) 42 | ## * add "message_runtime" and every package in MSG_DEP_SET to 43 | ## catkin_package(CATKIN_DEPENDS ...) 44 | ## * uncomment the add_*_files sections below as needed 45 | ## and list every .msg/.srv/.action file to be processed 46 | ## * uncomment the generate_messages entry below 47 | ## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...) 48 | 49 | ## Generate messages in the 'msg' folder 50 | # add_message_files( 51 | # FILES 52 | # Message1.msg 53 | # Message2.msg 54 | # ) 55 | 56 | ## Generate services in the 'srv' folder 57 | # add_service_files( 58 | # FILES 59 | # Service1.srv 60 | # Service2.srv 61 | # ) 62 | 63 | ## Generate actions in the 'action' folder 64 | # add_action_files( 65 | # FILES 66 | # Action1.action 67 | # Action2.action 68 | # ) 69 | 70 | ## Generate added messages and services with any dependencies listed here 71 | # generate_messages( 72 | # DEPENDENCIES 73 | # std_msgs # Or other packages containing msgs 74 | # ) 75 | 76 | ################################################ 77 | ## Declare ROS dynamic reconfigure parameters ## 78 | ################################################ 79 | 80 | ## To declare and build dynamic reconfigure parameters within this 81 | ## package, follow these steps: 82 | ## * In the file package.xml: 83 | ## * add a build_depend and a exec_depend tag for "dynamic_reconfigure" 84 | ## * In this file (CMakeLists.txt): 85 | ## * add "dynamic_reconfigure" to 86 | ## find_package(catkin REQUIRED COMPONENTS ...) 87 | ## * uncomment the "generate_dynamic_reconfigure_options" section below 88 | ## and list every .cfg file to be processed 89 | 90 | ## Generate dynamic reconfigure parameters in the 'cfg' folder 91 | # generate_dynamic_reconfigure_options( 92 | # cfg/DynReconf1.cfg 93 | # cfg/DynReconf2.cfg 94 | # ) 95 | 96 | ################################### 97 | ## catkin specific configuration ## 98 | ################################### 99 | ## The catkin_package macro generates cmake config files for your package 100 | ## Declare things to be passed to dependent projects 101 | ## INCLUDE_DIRS: uncomment this if your package contains header files 102 | ## LIBRARIES: libraries you create in this project that dependent projects also need 103 | ## CATKIN_DEPENDS: catkin_packages dependent projects also need 104 | ## DEPENDS: system dependencies of this project that dependent projects also need 105 | catkin_package( 106 | # INCLUDE_DIRS include 107 | # LIBRARIES rqt_virtual_joy 108 | # CATKIN_DEPENDS rospy rqt_gui rqt_gui_py 109 | # DEPENDS system_lib 110 | ) 111 | 112 | ########### 113 | ## Build ## 114 | ########### 115 | 116 | ## Specify additional locations of header files 117 | ## Your package locations should be listed before other locations 118 | include_directories( 119 | # include 120 | ${catkin_INCLUDE_DIRS} 121 | ) 122 | 123 | ## Declare a C++ library 124 | # add_library(${PROJECT_NAME} 125 | # src/${PROJECT_NAME}/rqt_virtual_joy.cpp 126 | # ) 127 | 128 | ## Add cmake target dependencies of the library 129 | ## as an example, code may need to be generated before libraries 130 | ## either from message generation or dynamic reconfigure 131 | # add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) 132 | 133 | ## Declare a C++ executable 134 | ## With catkin_make all packages are built within a single CMake context 135 | ## The recommended prefix ensures that target names across packages don't collide 136 | # add_executable(${PROJECT_NAME}_node src/rqt_virtual_joy_node.cpp) 137 | 138 | ## Rename C++ executable without prefix 139 | ## The above recommended prefix causes long target names, the following renames the 140 | ## target back to the shorter version for ease of user use 141 | ## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" 142 | # set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") 143 | 144 | ## Add cmake target dependencies of the executable 145 | ## same as for the library above 146 | # add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) 147 | 148 | ## Specify libraries to link a library or executable target against 149 | # target_link_libraries(${PROJECT_NAME}_node 150 | # ${catkin_LIBRARIES} 151 | # ) 152 | 153 | ############# 154 | ## Install ## 155 | ############# 156 | 157 | # all install targets should use catkin DESTINATION variables 158 | # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html 159 | 160 | ## Mark executable scripts (Python etc.) for installation 161 | ## in contrast to setup.py, you can choose the destination 162 | # install(PROGRAMS 163 | # scripts/my_python_script 164 | # DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} 165 | # ) 166 | 167 | ## Mark executables for installation 168 | ## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html 169 | # install(TARGETS ${PROJECT_NAME}_node 170 | # RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} 171 | # ) 172 | 173 | ## Mark libraries for installation 174 | ## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html 175 | # install(TARGETS ${PROJECT_NAME} 176 | # ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 177 | # LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 178 | # RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} 179 | # ) 180 | 181 | ## Mark cpp header files for installation 182 | # install(DIRECTORY include/${PROJECT_NAME}/ 183 | # DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} 184 | # FILES_MATCHING PATTERN "*.h" 185 | # PATTERN ".svn" EXCLUDE 186 | # ) 187 | 188 | ## Mark other files for installation (e.g. launch and bag files, etc.) 189 | # install(FILES 190 | # # myfile1 191 | # # myfile2 192 | # DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} 193 | # ) 194 | 195 | ############# 196 | ## Testing ## 197 | ############# 198 | 199 | ## Add gtest based cpp test target and link libraries 200 | # catkin_add_gtest(${PROJECT_NAME}-test test/test_rqt_virtual_joy.cpp) 201 | # if(TARGET ${PROJECT_NAME}-test) 202 | # target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) 203 | # endif() 204 | 205 | ## Add folders to be run by python nosetests 206 | # catkin_add_nosetests(test) 207 | -------------------------------------------------------------------------------- /src/rqt_virtual_joy/joystickView.py: -------------------------------------------------------------------------------- 1 | from python_qt_binding import QtCore 2 | from python_qt_binding.QtGui import QPainter, QColor, QFont, QPen, QBrush 3 | from python_qt_binding.QtWidgets import QWidget,QGridLayout,QSizePolicy 4 | import math 5 | 6 | 7 | 8 | class JoystickView(QWidget): 9 | 10 | xMoved = QtCore.Signal(float) 11 | yMoved = QtCore.Signal(float) 12 | 13 | def __init__(self, parent = None): 14 | super(JoystickView, self).__init__(parent) 15 | self._initialized = False 16 | self._stickSize = 30 17 | 18 | self._stickView = JoystickPointView(self) 19 | self._stickView.xMoved.connect(self.receiveXMoved) 20 | self._stickView.yMoved.connect(self.receiveYMoved) 21 | self.setMode("square") 22 | 23 | 24 | def receiveXMoved(self,val): 25 | self.xMoved.emit(val) 26 | 27 | def receiveYMoved(self,val): 28 | self.yMoved.emit(val) 29 | 30 | 31 | def setMode(self,mode): 32 | self._mode = mode 33 | self._stickView.setMode(mode) 34 | self.repaint() 35 | 36 | 37 | def paintEvent(self,event): 38 | if not self._initialized: 39 | self.placeStickAtCenter() 40 | self._initialized = True 41 | 42 | borderWidth = 1 43 | joyRange = 80 44 | center = QtCore.QPoint(self.height()/2,self.width()/2) 45 | 46 | qp = QPainter() 47 | qp.begin(self) 48 | qp.setRenderHint(QPainter.Antialiasing, True) 49 | qp.setPen(QPen(QtCore.Qt.lightGray, borderWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,QtCore.Qt.RoundJoin)) 50 | 51 | if self._mode == "circle": 52 | 53 | qp.drawEllipse(center,joyRange,joyRange) 54 | 55 | if self._mode == "square": 56 | x = center.x() - joyRange 57 | y = center.y() - joyRange 58 | width = joyRange * 2 59 | height = joyRange * 2 60 | qp.drawRect(x,y,width,height) 61 | 62 | qp.end() 63 | 64 | super(JoystickView,self).paintEvent(event) 65 | 66 | def placeStickAtCenter(self): 67 | stickInitPosH = self.height()/2 - self._stickSize /2 68 | stickInitPosW = self.width()/2 - self._stickSize /2 69 | self._stickView.setGeometry(stickInitPosH,stickInitPosW,self._stickSize,self._stickSize) 70 | 71 | def getJoyValue(self): 72 | return self._stickView.getJoyValue() 73 | 74 | 75 | 76 | class JoystickPointView(QWidget): 77 | 78 | xMoved = QtCore.Signal(float) 79 | yMoved = QtCore.Signal(float) 80 | 81 | def __init__(self,parent = None): 82 | super(JoystickPointView,self).__init__(parent) 83 | self._range = 80 84 | self._mode = "circle" 85 | 86 | 87 | def paintEvent(self,event): 88 | super(JoystickPointView,self).paintEvent(event) 89 | 90 | try: 91 | if self._initialized: 92 | pass 93 | except: 94 | self._origPos = self.pos() 95 | self._initialized = True 96 | 97 | qp = QPainter() 98 | qp.begin(self) 99 | 100 | borderWidth = 2 101 | radius = self.height()/2 102 | center = QtCore.QPoint(self.height()/2,self.width()/2) 103 | 104 | # Outer Circle 105 | qp.setRenderHint(QPainter.Antialiasing, True) 106 | qp.setPen(QPen(QtCore.Qt.darkGray, borderWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,QtCore.Qt.RoundJoin)) 107 | qp.setBrush(QBrush(QtCore.Qt.white, QtCore.Qt.SolidPattern)) 108 | qp.drawEllipse(center,radius-borderWidth,radius-borderWidth) 109 | 110 | # Inner Circle 111 | qp.setPen(QPen(QtCore.Qt.lightGray, borderWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,QtCore.Qt.RoundJoin)) 112 | qp.setBrush(QBrush(QtCore.Qt.white, QtCore.Qt.SolidPattern)) 113 | qp.drawEllipse(center,radius-borderWidth-1,radius-borderWidth-1) 114 | 115 | qp.end() 116 | 117 | 118 | def mousePressEvent(self, event): 119 | self.__mousePressPos = None 120 | self.__mouseMovePos = None 121 | if event.button() == QtCore.Qt.LeftButton: 122 | self.setFocus() 123 | self.__mousePressPos = event.globalPos() 124 | self.__mouseMovePos = event.globalPos() 125 | 126 | super(JoystickPointView, self).mousePressEvent(event) 127 | 128 | def mouseMoveEvent(self, event): 129 | if event.buttons() == QtCore.Qt.LeftButton: 130 | if(self.__mouseMovePos == None): 131 | return 132 | 133 | currPos = self.mapToGlobal(self.pos()) 134 | globalPos = event.globalPos() 135 | diff = globalPos - self.__mouseMovePos 136 | newPos = self.mapFromGlobal(currPos + diff) 137 | 138 | 139 | center = self.centerPos(newPos) 140 | origCenter = self.centerPos(self._origPos) 141 | relative = origCenter - center 142 | 143 | limited = self.limitStickMove(relative, self._mode) 144 | 145 | self._moveJoy(limited) 146 | self.__mouseMovePos = globalPos 147 | 148 | super(JoystickPointView, self).mouseMoveEvent(event) 149 | 150 | def mouseReleaseEvent(self, event): 151 | 152 | self._moveJoy(QtCore.QPoint(0,0)) 153 | 154 | if self.__mousePressPos is not None: 155 | moved = event.globalPos() - self.__mousePressPos 156 | if moved.manhattanLength() > 3: 157 | event.ignore() 158 | return 159 | 160 | super(JoystickPointView, self).mouseReleaseEvent(event) 161 | 162 | 163 | def centerPos(self,pos = None): 164 | if pos is None: 165 | pos = self.pos() 166 | x = pos.x() + (self.width() / 2) 167 | y = pos.y() + (self.height() / 2) 168 | return QtCore.QPoint(x,y) 169 | 170 | def revertCenterPos(self, pos = None): 171 | if pos is None: 172 | pos = self.pos() 173 | x = pos.x() - (self.width() / 2) 174 | y = pos.y() - (self.height() / 2) 175 | return QtCore.QPoint(x,y) 176 | 177 | def limitStickMove(self,pos,mode = "square"): 178 | # Give joystick position from (0,0) 179 | x = 0 180 | y = 0 181 | 182 | if mode == "circle": 183 | 184 | norm = math.sqrt(pos.x() ** 2 + pos.y() ** 2) 185 | 186 | if norm > self._range: 187 | ratio = self._range / norm 188 | else: 189 | ratio = 1.0 190 | 191 | x = pos.x() * ratio 192 | y = pos.y() * ratio 193 | 194 | 195 | if mode == "square": 196 | 197 | if abs(pos.x()) > self._range: 198 | sign = pos.x() / abs(pos.x()) 199 | x = sign * self._range 200 | else: 201 | x = pos.x() 202 | 203 | if abs(pos.y()) > self._range: 204 | sign = pos.y() / abs(pos.y()) 205 | y = sign * self._range 206 | else: 207 | y = pos.y() 208 | 209 | return QtCore.QPoint(x,y) 210 | 211 | def setMode(self,mode): 212 | self._mode = mode 213 | 214 | def setRange(self,value): 215 | self._range = value 216 | 217 | def getJoyValue(self): 218 | try: 219 | center = self.centerPos(self.pos()) 220 | origCenter = self.centerPos(self._origPos) 221 | relative = origCenter - center 222 | 223 | x = float(relative.x()) / self._range 224 | y = float(relative.y()) / self._range 225 | 226 | except: 227 | x = float(0.0) 228 | y = float(0.0) 229 | 230 | return {'x': x, 'y': y} 231 | 232 | def _moveJoy(self,relative): 233 | 234 | pastJoyPos = self.getJoyValue() 235 | 236 | origCenter = self.centerPos(self._origPos) 237 | newCenter = origCenter - relative 238 | self.move(self.revertCenterPos(newCenter)) 239 | 240 | newJoyPos = self.getJoyValue() 241 | 242 | if(pastJoyPos['x'] != newJoyPos['x']): 243 | self.xMoved.emit(newJoyPos['x']) 244 | if(pastJoyPos['y'] != newJoyPos['y']): 245 | self.yMoved.emit(newJoyPos['y']) 246 | 247 | 248 | 249 | 250 | 251 | -------------------------------------------------------------------------------- /resource/VirtualJoy.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Form 4 | 5 | 6 | 7 | 0 8 | 0 9 | 510 10 | 433 11 | 12 | 13 | 14 | Virtual Joystick 15 | 16 | 17 | 18 | 19 | 20 | 21 | 0 22 | 0 23 | 24 | 25 | 26 | 27 | 0 28 | 29 | 30 | 2 31 | 32 | 33 | 2 34 | 35 | 36 | 37 | 38 | 39 | 0 40 | 0 41 | 42 | 43 | 44 | 45 | 50 46 | 0 47 | 48 | 49 | 50 | Topic: 51 | 52 | 53 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 0 62 | 0 63 | 64 | 65 | 66 | 67 | 160 68 | 0 69 | 70 | 71 | 72 | 73 | 74 | 75 | false 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 0 87 | 0 88 | 89 | 90 | 91 | 92 | 0 93 | 0 94 | 95 | 96 | 97 | 98 | 2 99 | 100 | 101 | 2 102 | 103 | 104 | 105 | 106 | 107 | 0 108 | 0 109 | 110 | 111 | 112 | 113 | 40 114 | 20 115 | 116 | 117 | 118 | Publish 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 0 127 | 0 128 | 129 | 130 | 131 | 1 132 | 133 | 134 | 1000 135 | 136 | 137 | 10 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 0 146 | 0 147 | 148 | 149 | 150 | 151 | 10 152 | 0 153 | 154 | 155 | 156 | Hz 157 | 158 | 159 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 160 | 161 | 162 | 163 | 164 | 165 | 166 | Qt::Horizontal 167 | 168 | 169 | 170 | 40 171 | 5 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 2 184 | 185 | 186 | 2 187 | 188 | 189 | 190 | 191 | Qt::Horizontal 192 | 193 | 194 | 195 | 40 196 | 20 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 0 206 | 0 207 | 208 | 209 | 210 | 211 | 200 212 | 340 213 | 214 | 215 | 216 | 217 | 218 | 219 | false 220 | 221 | 222 | false 223 | 224 | 225 | 226 | 227 | 110 228 | 220 229 | 81 230 | 21 231 | 232 | 233 | 234 | (0,0) 235 | 236 | 237 | Qt::PlainText 238 | 239 | 240 | Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter 241 | 242 | 243 | 244 | 245 | 246 | 20 247 | 110 248 | 161 249 | 21 250 | 251 | 252 | 253 | Qt::Horizontal 254 | 255 | 256 | 257 | 258 | 259 | 10 260 | 220 261 | 81 262 | 25 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 90 273 | 40 274 | 21 275 | 161 276 | 277 | 278 | 279 | Qt::Vertical 280 | 281 | 282 | 283 | 284 | 285 | -1 286 | 19 287 | 201 288 | 201 289 | 290 | 291 | 292 | 293 | 294 | 295 | 10 296 | 250 297 | 181 298 | 81 299 | 300 | 301 | 302 | 303 | 304 | 305 | 6 306 | 307 | 308 | 309 | 310 | 311 | 312 | 8 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 0 321 | 0 322 | 323 | 324 | 325 | 1 326 | 327 | 328 | 329 | 330 | 331 | 332 | 5 333 | 334 | 335 | 336 | 337 | 338 | 339 | 10 340 | 341 | 342 | 343 | 344 | 345 | 346 | 12 347 | 348 | 349 | 350 | 351 | 352 | 353 | 9 354 | 355 | 356 | 357 | 358 | 359 | 360 | 2 361 | 362 | 363 | 364 | 365 | 366 | 367 | 4 368 | 369 | 370 | 371 | 372 | 373 | 374 | 11 375 | 376 | 377 | 378 | 379 | 380 | 381 | 7 382 | 383 | 384 | 385 | 386 | 387 | 388 | 3 389 | 390 | 391 | 392 | 393 | 394 | 395 | 13 396 | 397 | 398 | 399 | 400 | 401 | 402 | 14 403 | 404 | 405 | 406 | 407 | 408 | 409 | 15 410 | 411 | 412 | 413 | 414 | 415 | 416 | 16 417 | 418 | 419 | 420 | 421 | 422 | 423 | 17 424 | 425 | 426 | 427 | 428 | 429 | 430 | 18 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | Qt::Horizontal 442 | 443 | 444 | 445 | 40 446 | 20 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | JoystickView 459 | QWidget 460 |
rqt_virtual_joy.joystickView
461 | 1 462 |
463 |
464 | 465 | 466 |
467 | --------------------------------------------------------------------------------