├── Joyteleop_turtlebot2.py ├── Joyteleop_turtlebot3.py ├── LICENSE └── README.md /Joyteleop_turtlebot2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Sep 30 20:02:45 2018 4 | 5 | @author: mason 6 | """ 7 | 8 | import numpy as np 9 | import rospy 10 | import roslib 11 | import subprocess 12 | import time 13 | from geometry_msgs.msg import Twist 14 | from sensor_msgs.msg import Joy 15 | import sys 16 | import signal 17 | 18 | def signal_handler(signal, frame): # ctrl + c -> exit program 19 | print('You pressed Ctrl+C!') 20 | sys.exit(0) 21 | signal.signal(signal.SIGINT, signal_handler) 22 | ''' class ''' 23 | class robot(): 24 | def __init__(self): 25 | rospy.init_node('robot_controller', anonymous=True) 26 | self.velocity_publisher = rospy.Publisher('/mobile_base/commands/velocity', Twist, queue_size=1) 27 | self.pose_subscriber2 = rospy.Subscriber('/joy',Joy,self.callback) 28 | self.rate = rospy.Rate(20) 29 | 30 | def callback(self, data): 31 | global inn 32 | inn=0 33 | self.joy = data.buttons 34 | self.joy2= data.axes 35 | if np.shape(self.joy)[0]>0: 36 | inn=1 37 | self.nemo=self.joy[0] 38 | self.semo=self.joy[3] 39 | self.one=self.joy[2] 40 | self.x=self.joy[1] 41 | if np.shape(self.joy2)[0]>0: 42 | inn=1 43 | self.linear=self.joy2[1] 44 | self.angular=self.joy2[0] 45 | if inn==1: 46 | if self.joy[0]==0 and self.joy[1]==0 and self.joy[2]==0 and self.joy[3]==0 and self.joy2[0]==0 and self.joy2[1]==0: 47 | inn=0 48 | else: 49 | pass 50 | def moving(self,vel_msg): 51 | self.velocity_publisher.publish(vel_msg) 52 | 53 | data=Joy() 54 | vel_msg=Twist() 55 | 56 | ''' robot position ''' 57 | turtle = robot() 58 | turtle.callback(data) #without this, getting error 59 | global inn 60 | inn=0 61 | 62 | ''' main ''' 63 | if __name__ == '__main__': 64 | while 1: 65 | if inn==1: 66 | if turtle.nemo==1: 67 | vel_msg.linear.x=turtle.linear*0.4 68 | vel_msg.angular.z=turtle.angular*1.2 69 | elif turtle.semo==1: 70 | #subprocess.call('',shell=True) 71 | p=subprocess.Popen('rostopic pub /mobile_base/commands/reset_odometry std_msgs/Empty "{}"',shell=True) 72 | time.sleep(2) 73 | p.terminate() 74 | elif turtle.one==1: 75 | vel_msg.linear.x=turtle.linear*0.7 76 | vel_msg.angular.z=turtle.angular*2 77 | elif turtle.x==1: 78 | vel_msg.linear.x=0 79 | vel_msg.angular.z=0 80 | turtle.moving(vel_msg) 81 | else: 82 | print('no data in') 83 | turtle.rate.sleep() -------------------------------------------------------------------------------- /Joyteleop_turtlebot3.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Sep 30 20:02:45 2018 4 | 5 | @author: mason 6 | """ 7 | 8 | import numpy as np 9 | import rospy 10 | import roslib 11 | import subprocess 12 | import time 13 | from geometry_msgs.msg import Twist 14 | from sensor_msgs.msg import Joy 15 | import sys 16 | import signal 17 | 18 | def signal_handler(signal, frame): # ctrl + c -> exit program 19 | print('You pressed Ctrl+C!') 20 | sys.exit(0) 21 | signal.signal(signal.SIGINT, signal_handler) 22 | ''' class ''' 23 | class robot(): 24 | def __init__(self): 25 | rospy.init_node('robot_controller', anonymous=True) 26 | self.velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=1) 27 | self.pose_subscriber2 = rospy.Subscriber('/joy',Joy,self.callback) 28 | self.rate = rospy.Rate(20) 29 | 30 | def callback(self, data): 31 | global inn 32 | inn=0 33 | self.joy = data.buttons 34 | self.joy2= data.axes 35 | if np.shape(self.joy)[0]>0: 36 | inn=1 37 | self.nemo=self.joy[0] 38 | self.semo=self.joy[3] 39 | self.one=self.joy[2] 40 | self.x=self.joy[1] 41 | if np.shape(self.joy2)[0]>0: 42 | inn=1 43 | self.linear=self.joy2[1] 44 | self.angular=self.joy2[0] 45 | if inn==1: 46 | if self.joy[0]==0 and self.joy[1]==0 and self.joy[2]==0 and self.joy[3]==0 and self.joy2[0]==0 and self.joy2[1]==0: 47 | inn=0 48 | else: 49 | pass 50 | def moving(self,vel_msg): 51 | self.velocity_publisher.publish(vel_msg) 52 | 53 | data=Joy() 54 | vel_msg=Twist() 55 | 56 | ''' robot position ''' 57 | turtle = robot() 58 | turtle.callback(data) #without this, getting error 59 | global inn 60 | inn=0 61 | 62 | ''' main ''' 63 | if __name__ == '__main__': 64 | while 1: 65 | if inn==1: 66 | if turtle.nemo==1: 67 | vel_msg.linear.x=turtle.linear*0.1 68 | vel_msg.angular.z=turtle.angular*1.2 69 | elif turtle.semo==1: 70 | #subprocess.call('',shell=True) 71 | p=subprocess.Popen('rostopic pub /reset std_msgs/Empty "{}"',shell=True) 72 | time.sleep(2) 73 | p.terminate() 74 | elif turtle.one==1: 75 | vel_msg.linear.x=turtle.linear*0.22 76 | vel_msg.angular.z=turtle.angular*2 77 | elif turtle.x==1: 78 | vel_msg.linear.x=0 79 | vel_msg.angular.z=0 80 | turtle.moving(vel_msg) 81 | else: 82 | print('no data in') 83 | turtle.rate.sleep() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2018, EungChang-Mason-Lee 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Joystick teleoperation for Mobile Robots using ROS, Python 2 | + Simple Teleoperation using [PS4 from SONY](https://asia.playstation.com/ko-kr/accessories/dualshock4/) 3 |
4 |
5 |
24 |
25 |
42 |
43 |
186 |
187 |
189 |
190 |