Skip to content

Commit bd9f2a1

Browse files
authored
Merge pull request #4 from dash-robotics/feature/integrate_state_machine
Add logic for hand over
2 parents 1c0e7d2 + 42c2176 commit bd9f2a1

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

scripts/controller.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import numpy as np
22
import rospy
3+
import time
4+
from collections import deque
35

46
from std_msgs.msg import Float64, Empty
57
from trac_ik_python.trac_ik import IK
@@ -18,6 +20,8 @@ def __init__(self):
1820
rospy.Subscriber('/joint_states', JointState, self.get_joint_state)
1921
self.tf_listener = tf.TransformListener()
2022

23+
self.history = {'timestamp': deque(),
24+
'joint_feedback': deque()}
2125
# global variables
2226
self.current_joint_state = None
2327
self.current_gripper_state = None
@@ -56,6 +60,19 @@ def set_arm_joint_angles(self, joint_target):
5660
self.arm_pub.publish(joint_state)
5761

5862
def get_joint_state(self, data):
63+
# TODO: Change this when required
64+
moveable_joints = [1,2,3]
65+
# Add timestamp
66+
self.history['timestamp'].append(time.time())
67+
if(len(self.history['timestamp']) > 20):
68+
self.history['timestamp'].popleft()
69+
70+
# Add Joint Feedback
71+
joint_angles = np.array(data.position)[moveable_joints]
72+
self.history['joint_feedback'].append(joint_angles)
73+
if(len(self.history['joint_feedback']) > 20):
74+
self.history['joint_feedback'].popleft()
75+
5976
self.current_joint_state = data.position[0:5]
6077
self.current_gripper_state = data.position[7:9]
6178

scripts/machine.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import rospy
55
import smach
66
import smach_ros
7+
import numpy as np
78

89
from controller import Controller
910
from geometry_msgs.msg import PoseStamped
1011
from dynamixel_workbench_msgs.srv import SetPID
1112

1213
# Initialize controller
1314
ctrl = Controller()
15+
MIN_JOINT_MOTION_FOR_HAND_OVER = 0.1
1416
# define state Idle
1517
class Idle(smach.State):
1618
def __init__(self):
@@ -155,9 +157,17 @@ def __init__(self):
155157

156158
def execute(self, userdata):
157159
## Detect when to Open gripper
158-
self.open_gripper()
159160
rospy.loginfo('Executing state OpenGripper')
160-
return 'opened'
161+
while(True):
162+
joint_len = len(ctrl.history['joint_feedback'][0])
163+
joint_sum = np.zeros(ctrl.history['joint_feedback'][0].shape)
164+
for joint_feedback in ctrl.history['joint_feedback']:
165+
joint_sum += joint_feedback
166+
avg_joint_sum = joint_sum/joint_len
167+
if(np.sum(avg_joint_sum) < MIN_JOINT_MOTION_FOR_HAND_OVER):
168+
ctrl.open_gripper()
169+
return 'opened'
170+
161171

162172
def main():
163173
rospy.init_node('attention_bot')

0 commit comments

Comments
 (0)