AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
keyboardCommand_B.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_B/twist_command"
17 joint_topic="/uwsim/g500_B/joint_command"
18 #base velocity for the teleoperation
19 baseVelocity=0.8
20 baseJoint=0.1
21 
22 #Console input variables to teleop it from the console
23 fd = sys.stdin.fileno()
24 oldterm = termios.tcgetattr(fd)
25 newattr = termios.tcgetattr(fd)
26 newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
27 termios.tcsetattr(fd, termios.TCSANOW, newattr)
28 oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
29 fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
30 
31 ##create the publishers
32 pubTwist = rospy.Publisher(twist_topic, TwistStamped,queue_size=1)
33 pubJoint = rospy.Publisher(joint_topic, JointState,queue_size=1)
34 rospy.init_node('keyboardCommand_B')
35 
36 #The try is necessary for the console input!
37 try:
38  while not rospy.is_shutdown():
39  msgTwist = TwistStamped()
40  msgJoint = JointState()
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  #JointsCommands
72  elif c == 't':
73  msgJoint.name.append("Slew")
74  msgJoint.velocity.append(baseJoint)
75  elif c == 'z':
76  msgJoint.name.append("Shoulder")
77  msgJoint.velocity.append(baseJoint)
78  elif c == 'u':
79  msgJoint.name.append("Elbow")
80  msgJoint.velocity.append(baseJoint)
81  elif c == 'i':
82  msgJoint.name.append("JawRotate")
83  msgJoint.velocity.append(baseJoint)
84  elif c == 'o':
85  msgJoint.name.append("JawOpening")
86  msgJoint.velocity.append(baseJoint)
87  elif c == 'g':
88  msgJoint.name.append("Slew")
89  msgJoint.velocity.append(-baseJoint)
90  elif c == 'h':
91  msgJoint.name.append("Shoulder")
92  msgJoint.velocity.append(-baseJoint)
93  elif c == 'j':
94  msgJoint.name.append("Elbow")
95  msgJoint.velocity.append(-baseJoint)
96  elif c == 'k':
97  msgJoint.name.append("JawRotate")
98  msgJoint.velocity.append(-baseJoint)
99  elif c == 'l':
100  msgJoint.name.append("JawOpening")
101  msgJoint.velocity.append(-baseJoint)
102  #Increase Velocity send command
103  elif c == 'y':
104  baseVelocity+=0.1;
105  print "baseVelocity: " , baseVelocity
106  elif c == 'x':
107  if baseVelocity > 0.2:
108  baseVelocity-=0.1
109  elif baseVelocity > 0:
110  baseVelocity -= 0.01
111  else:
112  baseVelocity = 0
113  print "baseVelocity: " , baseVelocity
114  elif c == 'n':
115  baseJoint+=0.1;
116  print "jointVelocity: " , baseJoint
117  elif c == 'm':
118  if baseJoint > 0.2:
119  baseJoint -= 0.1
120  elif baseJoint > 0:
121  baseJoint -= 0.01
122  else:
123  baseJoint = 0
124  print "jointVelocity: ", baseJoint
125  else:
126  print 'wrong key pressed'
127  while c!='':
128  c = sys.stdin.read(1)
129  except IOError: pass
130 
131  ##publish the message
132  pubTwist.publish(msgTwist)
133  pubJoint.publish(msgJoint)
134  rospy.sleep(0.1)
135 
136 ##Other input stuff
137 finally:
138  termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
139  fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)