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