AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
keyboardCommand_C.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 #!/usr/bin/env python
5 
6 from geometry_msgs.msg import TwistStamped
7 from sensor_msgs.msg import JointState
8 import termios, fcntl, sys, os
9 import rospy
10 
11 #import service library
12 from std_srvs.srv import Empty
13 
14 #topic to command
15 # Twist better than odometry TODO ask why
16 twist_topic="/uwsim/g500_C/twist_command"
17 #base velocity for the teleoperation
18 baseVelocity=0.8
19 
20 #Console input variables to teleop it from the console
21 fd = sys.stdin.fileno()
22 oldterm = termios.tcgetattr(fd)
23 newattr = termios.tcgetattr(fd)
24 newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
25 termios.tcsetattr(fd, termios.TCSANOW, newattr)
26 oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
27 fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
28 
29 ##create the publishers
30 pubTwist = rospy.Publisher(twist_topic, TwistStamped,queue_size=1)
31 rospy.init_node('keyboardCommand_C')
32 
33 #The try is necessary for the console input!
34 try:
35  while not rospy.is_shutdown():
36  msgTwist = TwistStamped()
37  try:
38  c = sys.stdin.read(1)
39  ##Depending on the character set the proper speeds
40  if c=='\x1b': ##This means we are pressing an arrow!
41  c2= sys.stdin.read(1)
42  c2= sys.stdin.read(1)
43  if c2=='A':
44  msgTwist.twist.angular.y=-baseVelocity
45  elif c2=='B':
46  msgTwist.twist.angular.y=baseVelocity
47  elif c2 == 'C':
48  msgTwist.twist.angular.z=baseVelocity
49  elif c2 == 'D':
50  msgTwist.twist.angular.z=-baseVelocity
51  elif c == 'w':
52  msgTwist.twist.linear.x = baseVelocity
53  elif c == 's':
54  msgTwist.twist.linear.x = -baseVelocity
55  elif c == 'd':
56  msgTwist.twist.linear.y = baseVelocity
57  elif c == 'a':
58  msgTwist.twist.linear.y = -baseVelocity
59  elif c == 'v':
60  msgTwist.twist.linear.z = baseVelocity
61  elif c == 'f':
62  msgTwist.twist.linear.z = -baseVelocity
63  elif c == 'q':
64  msgTwist.twist.angular.x = -baseVelocity
65  elif c == 'e':
66  msgTwist.twist.angular.x = baseVelocity
67 
68  #Increase Velocity send command
69  elif c == 'y':
70  baseVelocity+=0.1;
71  print "baseVelocity: " , baseVelocity
72  elif c == 'x':
73  if baseVelocity > 0.1:
74  baseVelocity-=0.1
75  else:
76  baseVelocity = 0
77  print "baseVelocity: " , baseVelocity
78  else:
79  print 'wrong key pressed'
80  while c!='':
81  c = sys.stdin.read(1)
82  except IOError: pass
83 
84  ##publish the message
85  pubTwist.publish(msgTwist)
86  rospy.sleep(0.1)
87 
88 ##Other input stuff
89 finally:
90  termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
91  fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)