joycontrol.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import rospy
00003 from std_msgs.msg import String
00004 from std_msgs.msg import Int8
00005 from sensor_msgs.msg import Joy
00006 from geometry_msgs.msg import Twist
00007 
00008 rospy.set_param('mode','0')
00009 cmd_vel_publisher = rospy.Publisher('/cmd_vel', Twist)
00010 ModeCommand = Int8()
00011 ModeCommand = 0
00012 
00013 def joy_callback(data):
00014     global TwistCommand, YawDesired, ModeCommand
00015     
00016     TwistCommand = Twist()
00017     TwistCommand.linear.x = data.axes[1]*0.25
00018     TwistCommand.linear.y = data.axes[0]*0.25
00019 
00020 
00021 
00022     # Up - down
00023     stepZ = 0.01
00024     stepZHigh = 0.1
00025     stepYaw = 0.01
00026     if data.buttons[2] == 1.0:
00027         TwistCommand.linear.z = stepZ
00028     if data.buttons[1] == 1.0: # button 2
00029         TwistCommand.linear.z = -stepZ
00030 
00031     if data.buttons[10] == 1.0: # button11 
00032         TwistCommand.linear.z = stepZHigh
00033     if data.buttons[9] == 1.0: # button10
00034         TwistCommand.linear.z = -stepZHigh
00035     print TwistCommand.linear.z
00036     # Yaw left-right
00037     if data.buttons[3] == 1.0:
00038         YawDesired = stepYaw
00039 
00040     if data.buttons[4] == 1.0:
00041         YawDesired = -stepYaw
00042 
00043     # Mode
00044     if data.buttons[6] == 1.0:
00045         ModeCommand = ModeCommand + 1
00046         if ModeCommand > 2:
00047            ModeCommand = 0
00048         rospy.set_param('/mode',str(ModeCommand))
00049 
00050     TwistCommand.angular.z = YawDesired
00051 
00052     print "Mode", ModeCommand
00053     print TwistCommand
00054     cmd_vel_publisher.publish(TwistCommand)  
00055     TwistCommand.angular.z = 0
00056     YawDesired = 0
00057     #mode_publisher.publish(ModeCommand)
00058 
00059 if __name__ == '__main__':
00060     global TwistCommand, YawDesired
00061     YawDesired = 0;
00062    
00063     TwistCommand = Twist()
00064     rospy.init_node('joy_listener', anonymous=True)
00065  
00066     # Create a ROS subcriber
00067     rospy.Subscriber("joy", Joy, joy_callback)
00068     rospy.set_param('/mode','0')
00069     #
00070     # Create a ROS publisher
00071     rospy.spin()    
00072 


ardrone2islab
Author(s): Trung Nguyen , Oscar De Silva
autogenerated on Thu Jun 6 2019 20:53:51