AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
old_keyboardCommandOdom_A.py
1 #!/usr/bin/env python
2 
3 from nav_msgs.msg import Odometry
4 import termios, fcntl, sys, os
5 import rospy
6 
7 #import service library
8 from std_srvs.srv import Empty
9 
10 #topic to command
11 # Twist better than odometry? TODO ask why
12 twist_topic="/uwsim/g500_A/dataNavigator_A"
13 #base velocity for the teleoperation (0.5 m/s) / (0.5rad/s)
14 baseVelocity=0.5
15 
16 #Console input variables to teleop it from the console
17 fd = sys.stdin.fileno()
18 oldterm = termios.tcgetattr(fd)
19 newattr = termios.tcgetattr(fd)
20 newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
21 termios.tcsetattr(fd, termios.TCSANOW, newattr)
22 oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
23 fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
24 
25 ##create the publisher
26 pub = rospy.Publisher(twist_topic, Odometry,queue_size=1)
27 rospy.init_node('keyboardCommandOdom_A')
28 
29 #The try is necessary for the console input!
30 try:
31  while not rospy.is_shutdown():
32  msg = Odometry()
33  msg.pose.pose.position.x=0.0
34  msg.pose.pose.position.y=0.0
35  msg.pose.pose.position.z=0.0
36  msg.pose.pose.orientation.x=0.0
37  msg.pose.pose.orientation.y=0.0
38  msg.pose.pose.orientation.z=0.0
39  msg.pose.pose.orientation.w=1
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  msg.twist.twist.angular.y=-baseVelocity
49  elif c2=='B':
50  msg.twist.twist.angular.y=baseVelocity
51  elif c2 == 'C':
52  msg.twist.twist.angular.z=baseVelocity
53  elif c2 == 'D':
54  msg.twist.twist.angular.z=-baseVelocity
55  elif c == 'w':
56  msg.twist.twist.linear.x = baseVelocity
57  elif c == 's':
58  msg.twist.twist.linear.x = -baseVelocity
59  elif c == 'd':
60  msg.twist.twist.linear.y = baseVelocity
61  elif c == 'a':
62  msg.twist.twist.linear.y = -baseVelocity
63  elif c == 'v':
64  msg.twist.twist.linear.z = baseVelocity
65  elif c == 'f':
66  msg.twist.twist.linear.z = -baseVelocity
67  else:
68  print 'wrong key pressed'
69  while c!='':
70  c = sys.stdin.read(1)
71  except IOError: pass
72 
73  ##publish the message
74  pub.publish(msg)
75  rospy.sleep(0.1)
76 
77 ##Other input stuff
78 finally:
79  termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
80  fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)