55from std_msgs .msg import String , Bool , UInt8MultiArray
66from MecademicRobot import RobotController , RobotFeedback
77
8+
89class MecademicRobot_Driver ():
910 """ROS Mecademic Robot Node Class to make a Node for the Mecademic Robot
1011
1112 Attributes:
1213 subscriber: ROS subscriber to send command to the Mecademic Robot through a topic
13- publisher: ROS publisher to place replies from the Mecademic Robot in a topic
14+ publisher: ROS publisher to place replies from the Mecademic Robot in a topic
1415 MecademicRobot : driver to control the MecademicRobot Robot
1516 """
17+
1618 def __init__ (self , robot , feedback ):
1719 """Constructor for the ROS MecademicRobot Driver
1820 """
1921 rospy .init_node ("MecademicRobot_driver" , anonymous = True )
20- self .joint_subscriber = rospy .Subscriber ("MecademicRobot_joint" , JointState , self .joint_callback )
21- self .pose_subscriber = rospy .Subscriber ("MecademicRobot_pose" , Pose , self .pose_callback )
22+ self .joint_subscriber = rospy .Subscriber ("MecademicRobot_joint" , JointState , self .joint_callback )
23+ self .pose_subscriber = rospy .Subscriber ("MecademicRobot_pose" , Pose , self .pose_callback )
2224 self .command_subscriber = rospy .Subscriber ("MecademicRobot_command" , String , self .command_callback )
2325 self .gripper_subscriber = rospy .Subscriber ("MecademicRobot_gripper" , Bool , self .gripper_callback )
24- self .reply_publisher = rospy .Publisher ("MecademicRobot_reply" , String , queue_size = 1 )
25- self .joint_publisher = rospy .Publisher ("MecademicRobot_joint_fb" , JointState , queue_size = 1 )
26- self .pose_publisher = rospy .Publisher ("MecademicRobot_pose_fb" , Pose , queue_size = 1 )
27- self .status_publisher = rospy .Publisher ("MecademicRobot_status" , UInt8MultiArray , queue_size = 1 )
26+ self .reply_publisher = rospy .Publisher ("MecademicRobot_reply" , String , queue_size = 1 )
27+ self .joint_publisher = rospy .Publisher ("MecademicRobot_joint_fb" , JointState , queue_size = 1 )
28+ self .pose_publisher = rospy .Publisher ("MecademicRobot_pose_fb" , Pose , queue_size = 1 )
29+ self .status_publisher = rospy .Publisher ("MecademicRobot_status" , UInt8MultiArray , queue_size = 1 )
2830
2931 self .robot = robot
3032 self .feedback = feedback
@@ -33,100 +35,111 @@ def __init__(self, robot, feedback):
3335
3436 self .feedbackLoop ()
3537
38+ def __del__ (self ):
39+ """Deconstructor for the Mecademic Robot ROS driver
40+ Deactivates the robot and closes socket connection with the robot
41+ """
42+ self .robot .DeactivateRobot ()
43+ self .robot .disconnect ()
44+ self .feedback .disconnect ()
45+
3646 def command_callback (self , command ):
3747 """Forwards a ascii command to the Mecademic Robot
3848
3949 :param command: ascii command to forward to the Robot
4050 """
41- while ( not self .socket_available ) : # wait for socket to be available
51+ while not self .socket_available : # wait for socket to be available
4252 pass
43- self .socket_available = False # block socket from being used in other processes
44- if ( self .robot .is_in_error () ):
53+ self .socket_available = False # block socket from being used in other processes
54+ if self .robot .is_in_error ():
4555 self .robot .ResetError ()
4656 self .robot .ResumeMotion ()
4757 reply = self .robot .exchange_msg (command .data , decode = False )
48- self .socket_available = True # Release socket so other processes can use it
49- if ( reply is not None ) :
58+ self .socket_available = True # Release socket so other processes can use it
59+ if reply is not None :
5060 self .reply_publisher .publish (reply )
51-
61+
5262 def joint_callback (self , joints ):
5363 """Callback when the MecademicRobot_emit topic receives a message
5464 Forwards message to driver that translate into real command
5565 to the Mecademic Robot
5666
5767 :param joints: message received from topic containing position and velocity information
5868 """
59- while ( not self .socket_available ) : # wait for the socket to be available
69+ while not self .socket_available : # wait for the socket to be available
6070 pass
6171 reply = None
62- self .socket_available = False # Block other processes from using the socket
63- if ( self .robot .is_in_error () ):
72+ self .socket_available = False # Block other processes from using the socket
73+ if self .robot .is_in_error ():
6474 self .robot .ResetError ()
6575 self .robot .ResumeMotion ()
66- if ( len (joints .velocity )> 0 ) :
76+ if len (joints .velocity ) > 0 :
6777 self .robot .SetJointVel (joints .velocity [0 ])
68- if (len (joints .position )== 6 ):
69- reply = self .robot .MoveJoints (joints .position [0 ],joints .position [1 ],joints .position [2 ],joints .position [3 ],joints .position [4 ],joints .position [5 ])
70- elif (len (joints .position )== 4 ):
71- reply = self .robot .MoveJoints (joints .position [0 ],joints .position [1 ],joints .position [2 ],joints .position [3 ])
72- self .socket_available = True #Release the socket so other processes can use it
73- if (reply is not None ):
78+ if len (joints .position ) == 6 :
79+ reply = self .robot .MoveJoints (joints .position [0 ], joints .position [1 ], joints .position [2 ],
80+ joints .position [3 ], joints .position [4 ], joints .position [5 ])
81+ elif len (joints .position ) == 4 :
82+ reply = self .robot .MoveJoints (joints .position [0 ], joints .position [1 ], joints .position [2 ],
83+ joints .position [3 ])
84+ self .socket_available = True # Release the socket so other processes can use it
85+ if reply is not None :
7486 self .reply_publisher .publish (reply )
75-
87+
7688 def pose_callback (self , pose ):
7789 """Callback when the MecademicRobot_emit topic receives a message
7890 Forwards message to driver that translate into real command
7991 to the Mecademic Robot
8092
8193 :param pose: message received from topic containing position and orientation information
8294 """
83- while (not self .socket_available ): # wait for socket to become available
95+ while (not self .socket_available ): # wait for socket to become available
8496 pass
8597 reply = None
86- self .socket_available = False # Block other processes from using the socket while in use
87- if ( self .robot .is_in_error () ):
98+ self .socket_available = False # Block other processes from using the socket while in use
99+ if self .robot .is_in_error ():
88100 self .robot .ResetError ()
89101 self .robot .ResumeMotion ()
90- if (pose .position .z is not None ):
91- reply = self .robot .MovePose (pose .position .x ,pose .position .y ,pose .position .z ,pose .orientation .x ,pose .orientation .y ,pose .orientation .z )
102+ if pose .position .z is not None :
103+ reply = self .robot .MovePose (pose .position .x , pose .position .y , pose .position .z , pose .orientation .x ,
104+ pose .orientation .y , pose .orientation .z )
92105 else :
93- reply = self .robot .MovePose (pose .position .x ,pose .position .y ,pose .orientation .x ,pose .orientation .y )
94- self .socket_available = True # Release socket so other processes can continue
95- if ( reply is not None ) :
96- self .reply_publisher .publish (reply )
106+ reply = self .robot .MovePose (pose .position .x , pose .position .y , pose .orientation .x , pose .orientation .y )
107+ self .socket_available = True # Release socket so other processes can continue
108+ if reply is not None :
109+ self .reply_publisher .publish (reply )
97110
98111 def gripper_callback (self , state ):
99112 """Controls whether to open or close the gripper.
100113 True for open, False for close
101114
102115 :param state: ROS Bool message
103116 """
104- while ( not self .socket_available ) : # wait for socket to be available
117+ while not self .socket_available : # wait for socket to be available
105118 pass
106- self .socket_available = False # Block other processes from using the socket
107- if ( self .robot .is_in_error () ):
119+ self .socket_available = False # Block other processes from using the socket
120+ if self .robot .is_in_error ():
108121 self .robot .ResetError ()
109122 self .robot .ResumeMotion ()
110- if ( state .data ) :
123+ if state .data :
111124 reply = self .robot .GripperOpen ()
112125 else :
113126 reply = self .robot .GripperClose ()
114- self .socket_available = True # Release socket so other processes can use it
115- if ( reply is not None ) :
116- self .reply_publisher .publish (reply )
127+ self .socket_available = True # Release socket so other processes can use it
128+ if reply is not None :
129+ self .reply_publisher .publish (reply )
117130
118131 def feedbackLoop (self ):
119132 """Retrieves live position feedback and publishes the data
120133 to its corresponding topic. (infinite loop)
121134 """
122135 while not rospy .is_shutdown ():
123136 try :
124- #Robot Status Feedback
125- if ( self .socket_available ) :
126- self .socket_available = False # Block other operations from using the socket while in use
137+ # Robot Status Feedback
138+ if self .socket_available :
139+ self .socket_available = False # Block other operations from using the socket while in use
127140 robot_status = self .robot .GetStatusRobot ()
128141 gripper_status = self .robot .GetStatusGripper ()
129- self .socket_available = True # Release the socket so other processes can happen
142+ self .socket_available = True # Release the socket so other processes can happen
130143 status = UInt8MultiArray ()
131144 status .data = [
132145 robot_status ["Activated" ],
@@ -144,33 +157,26 @@ def feedbackLoop(self):
144157 ]
145158 self .status_publisher .publish (status )
146159
147- #Position Feedback
160+ # Position Feedback
148161 self .feedback .get_data ()
149162 joints_fb = JointState ()
150163 joints_fb .position = feedback .joints
151164 pose_fb = Pose ()
152- pose_fb .position .x = feedback .cartesian [0 ]
153- pose_fb .position .y = feedback .cartesian [1 ]
154- if ( len (feedback .cartesian )== 4 ) :
155- pose_fb .orientation .x = feedback .cartesian [2 ]
156- pose_fb .orientation .y = feedback .cartesian [3 ]
165+ pose_fb .position .x = feedback .cartesian [0 ]
166+ pose_fb .position .y = feedback .cartesian [1 ]
167+ if len (feedback .cartesian ) == 4 :
168+ pose_fb .orientation .x = feedback .cartesian [2 ]
169+ pose_fb .orientation .y = feedback .cartesian [3 ]
157170 else :
158- pose_fb .position .z = feedback .cartesian [2 ]
159- pose_fb .orientation .x = feedback .cartesian [3 ]
160- pose_fb .orientation .y = feedback .cartesian [4 ]
161- pose_fb .orientation .z = feedback .cartesian [5 ]
171+ pose_fb .position .z = feedback .cartesian [2 ]
172+ pose_fb .orientation .x = feedback .cartesian [3 ]
173+ pose_fb .orientation .y = feedback .cartesian [4 ]
174+ pose_fb .orientation .z = feedback .cartesian [5 ]
162175 self .joint_publisher .publish (joints_fb )
163176 self .pose_publisher .publish (pose_fb )
164177 except Exception as error :
165178 rospy .logerr (str (error ))
166-
167- def __del__ (self ):
168- """Deconstructor for the Mecademic Robot ROS driver
169- Deactivates the robot and closes socket connection with the robot
170- """
171- self .robot .DeactivateRobot ()
172- self .robot .disconnect ()
173- self .feedback .disconnect ()
179+
174180
175181if __name__ == "__main__" :
176182 robot = RobotController ('192.168.1.9' )
@@ -180,4 +186,4 @@ def __del__(self):
180186 robot .ActivateRobot ()
181187 robot .home ()
182188 driver = MecademicRobot_Driver (robot , feedback )
183- rospy .spin ()
189+ rospy .spin ()
0 commit comments