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