-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtRandomMotion.py
More file actions
199 lines (161 loc) · 6.85 KB
/
Copy pathqtRandomMotion.py
File metadata and controls
199 lines (161 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from os import times
import random
import numpy as np
import sys
import rospy
from qt_robot_interface.srv import *
from qt_motors_controller.srv import *
from std_msgs.msg import Float64MultiArray
from sensor_msgs.msg import JointState
rospy.init_node('qt_motors_command')
rospy.loginfo("This is when random movements start!")
head_pub = rospy.Publisher('/qt_robot/head_position/command', Float64MultiArray, queue_size=10)
right_pub = rospy.Publisher('/qt_robot/right_arm_position/command', Float64MultiArray, queue_size=10)
left_pub = rospy.Publisher('/qt_robot/left_arm_position/command', Float64MultiArray, queue_size=10)
setVelocity = rospy.ServiceProxy('/qt_robot/motors/setVelocity', set_velocity)
class randomClassAbstract:
def __init__(self, joints, jointParallel, velMin, velMax, angleMin, angleMax, jerkMin, jerkMax, selectZero):
self.joints = joints
self.jointParallel = jointParallel
self.velMin = velMin
self.velMax = velMax
self.angleMin = angleMin
self.angleMax = angleMax
self.jerkMin = jerkMin
self.jerkMax = jerkMax
self.selectZero = selectZero
def create_movement_vel(self):
jointVelocities = dict.fromkeys(self.joints, False)
for joint in jointVelocities:
if joint in self.jointParallel:
if jointVelocities[joint] == False:
jointMove = random.choice([False, True])
if jointMove:
jointVelocities[joint] = random.randint(self.velMin, self.velMax)
jointVelocities[self.jointParallel[joint]] = jointVelocities[joint]
else:
options = [joint, self.jointParallel[joint]]
whichJointMove = random.choice(options)
jointVelocities[whichJointMove] = random.randint(0, self.velMax)
otherJoint = options[1] if whichJointMove == options[0] else options[0]
jointVelocities[otherJoint] = 0
else:
jointVelocities[joint] = random.randint(self.velMin, self.velMax)
print("Velocities:", jointVelocities)
return jointVelocities
def create_movement_angle(self):
jointAngles = dict.fromkeys(self.joints, False)
for joint in self.jointParallel:
if jointAngles[joint] == False:
jointMove = random.choice([False, True])
if jointMove:
jointAngles[joint] = random.uniform(self.angleMin, self.angleMax)
jointAngles[self.jointParallel[joint]] = jointAngles[joint]
else:
options = [joint, self.jointParallel[joint]]
whichJointMove = random.choice(options)
jointAngles[whichJointMove] = random.uniform(self.angleMin, self.angleMax)
otherJoint = options[1] if whichJointMove == options[0] else options[0]
jointAngles[otherJoint] = 0
for joint in self.joints:
if jointAngles[joint] == False:
jointAngles[joint] = random.uniform(self.angleMin, self.angleMax)
print("Angles:", jointAngles)
return jointAngles
def create_movement_jerk(self, initial_velocities=None, duration=5.0, dt=0.05):
if initial_velocities is None:
initial_velocities = self.create_movement_vel()
times_arr = list(np.arange(0, duration + dt, dt))
movement_profiles = {}
for joint in self.joints:
v0 = initial_velocities.get(joint, 0) or 0
jerk = random.uniform(self.jerkMin, self.jerkMax)
velocities = []
v = v0
a = 0.0
for i, t in enumerate(times_arr):
a += jerk * dt
v += a * dt
if v >= self.velMax:
v = self.velMax
a = -abs(a)
jerk = -abs(jerk)
elif v <= 0 and a < 0:
v = 0
a = 0
if i >= len(times_arr) - 1:
v = 0
a = 0
velocities.append(v)
movement_profiles[joint] = {
'times': times_arr,
'velocities': velocities,
'jerk': jerk
}
return movement_profiles
def run_full(self, time, sequence):
v_seq = []
for _ in range(sequence):
v_seq.append(self.create_movement_vel())
return v_seq
def apply_movement(self):
pass
if __name__ == "__main__":
print("hello!")
ALL_JOINTS = [
'RightShoulderPitch', 'RightShoulderRoll', 'RightElbowRoll',
'LeftShoulderPitch', 'LeftShoulderRoll', 'LeftElbowRoll',
'HeadYaw', 'HeadPitch',
]
PARALLEL = {
'RightShoulderPitch': 'LeftShoulderPitch',
'RightShoulderRoll': 'LeftShoulderRoll',
'RightElbowRoll': 'LeftElbowRoll',
}
test1 = randomClassAbstract(
joints = ALL_JOINTS,
jointParallel = PARALLEL,
#might need to update these ranges based on what the robot can actually do - these are just placeholders for now
velMin = random.randint(-100, 50),
velMax = random.randint(50, 100),
angleMin = random.randint(-100, 50),
angleMax = random.randint(50, 100),
jerkMin = 0.5,
jerkMax = 0.8,
selectZero = None
)
jointVelocities = test1.create_movement_vel()
jointAngles = test1.create_movement_angle()
for j in ALL_JOINTS:
print(f" {j:30s} angle={jointAngles.get(j):.2f} vel={jointVelocities.get(j)}")
while not rospy.is_shutdown():
rospy.wait_for_service('/qt_robot/motors/setVelocity')
try:
print('Publishing movement commands...')
rospy.sleep(5)
rref = Float64MultiArray()
rref.data = [
jointAngles['RightShoulderPitch'],
jointAngles['RightShoulderRoll'],
jointAngles['RightElbowRoll'],
]
right_pub.publish(rref)
lref = Float64MultiArray()
lref.data = [
jointAngles['LeftShoulderPitch'],
jointAngles['LeftShoulderRoll'],
jointAngles['LeftElbowRoll'],
]
left_pub.publish(lref)
href = Float64MultiArray()
href.data = [
jointAngles['HeadYaw'],
jointAngles['HeadPitch'],
]
head_pub.publish(href)
jointVelocities = test1.create_movement_vel()
jointAngles = test1.create_movement_angle()
except KeyboardInterrupt:
pass
rospy.loginfo("finished!")
rospy.loginfo("No more random moves!")