-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelperFunctions.py
More file actions
187 lines (138 loc) · 6.66 KB
/
Copy pathhelperFunctions.py
File metadata and controls
187 lines (138 loc) · 6.66 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
import pybullet as p
import time
import pybullet_data
import numpy as np
from numpy import loadtxt
import helperFunctions as hf
import math
# Various helper functions for physics calculations
def rotY(angle):
mat = np.array([[math.cos(angle), 0, math.sin(angle)],
[0 , 1, 0 ],
[-math.sin(angle), 0,math.cos(angle)]])
return mat
def readAeroData(fileName):
lines = loadtxt(fileName, unpack=False, skiprows=11)
alphaRTable = lines[:,0]*np.pi/180
cLTable = lines[:,1]
cDTable = lines[:,2]
cMTable = lines[:,4]
return alphaRTable, cLTable, cDTable, cMTable
def computeCenterOfMass(robotId):
allLinkPositions=[] #TODO: Refactor this.
allLinkPositions.append((p.getBasePositionAndOrientation(robotId))[0])
for i in range(0, 3):
allLinkPositions.append((p.getLinkState(robotId, i, 1))[0])
centerOfMass = np.sum(allLinkPositions, axis = 0)/4 #Average x, y, z, of all 4 link CoMs
centerOfMass[2] = centerOfMass[2] -.01 # Z intertial offset used in the urdf file
return centerOfMass
def setHingePosition(hingePosition):
hingeForce = 100
p.setJointMotorControl2(robotId,
jointIndex=0,
controlMode=p.POSITION_CONTROL,
targetPosition=hingePosition,
force=hingeForce)
p.setJointMotorControl2(robotId,
jointIndex=1,
controlMode=p.POSITION_CONTROL,
targetPosition=hingePosition,
force=hingeForce)
p.setJointMotorControl2(robotId,
jointIndex=2,
controlMode=p.POSITION_CONTROL,
targetPosition=hingePosition,
force=hingeForce)
def cycleEverything(i, simTime):
hingePosition = 1.57*np.sin((i*300/simTime))
for hingeNum in hingeIds:
p.setJointMotorControl2(bodyUniqueId=robotId,
jointIndex=hingeNum,
controlMode=p.POSITION_CONTROL,
targetPosition=hingePosition,
force=1000)
for ctrlSurfNum in ctrlSurfIds:
p.setJointMotorControl2(bodyUniqueId=robotId,
jointIndex=ctrlSurfNum,
controlMode=p.POSITION_CONTROL,
targetPosition=-(hingePosition),
force=1000)
for propNum in propIds:
p.setJointMotorControl2(bodyUniqueId=robotId,
jointIndex=propNum,
controlMode=p.VELOCITY_CONTROL,
targetVelocity=100,
force=1000)
def getUAVState(robotId):
a, b, c, d, e, f, g, h = p.getLinkState(robotId, 0, 1)
position = e # x,y,z
orientation = f #Quaternion
velocity = g
angular_velocity = h
return position, orientation, velocity, angular_velocity
def step():
p.stepSimulation()
# time.sleep(0.001)
def set_to_pos_and_q(pos, q):
p.resetBasePositionAndOrientation(robotId, pos, q)
def visualizeThrottle(m0, m1, m2, m3):
p.addUserDebugLine([0,0,0], [0, 0, m0/10], [1.0,1.0,1.0], parentObjectUniqueId = 1, parentLinkIndex = -1, lifeTime = .1)
p.addUserDebugLine([0,0,0], [0, 0, m1/10], [1.0,0.0,0.0], parentObjectUniqueId = 1, parentLinkIndex = 0, lifeTime = .1)
p.addUserDebugLine([0,0,0], [0, 0, m2/10], [0.0,1.0,0.0], parentObjectUniqueId = 1, parentLinkIndex = 1, lifeTime = .1)
p.addUserDebugLine([0,0,0], [0, 0, m3/10], [0.0,0.0,1.0], parentObjectUniqueId = 1, parentLinkIndex = 2, lifeTime = .1)
def visualizeLinkFrame(link):
p.addUserDebugLine([0,0,0], [10, 0, 0], [1.0,0.0,0.0], parentObjectUniqueId = 1, parentLinkIndex = link, lifeTime = .1)
p.addUserDebugLine([0,0,0], [0, 10, 0], [0.0,1.0,0.0], parentObjectUniqueId = 1, parentLinkIndex = link, lifeTime = .1)
p.addUserDebugLine([0,0,0], [0, 0, 10], [0.0,0.0,1.0], parentObjectUniqueId = 1, parentLinkIndex = link, lifeTime = .1)
def visualizeCenterOfMass():
p.addUserDebugLine([0,0,0], computeCenterOfMass(), [1.0,1.0,1.0], lifeTime = .05)
def visualizeZoom(robotId, i, startIter, duration, startZoom, endZoom, yaw, pitch):
rateZoom = (endZoom-startZoom)
if i>(startIter+duration):
zoom = startZoom + rateZoom * (1)
else:
zoom = startZoom + rateZoom * math.sin(1.57*(i-startIter)/duration)
p.resetDebugVisualizerCamera(zoom, yaw, pitch, hf.computeCenterOfMass(robotId)) # Camera position (distance, yaw, pitch, focuspoint)
# Checks if a matrix is a valid rotation matrix.
def isRotationMatrix(R) :
Rt = np.transpose(R)
shouldBeIdentity = np.dot(Rt, R)
I = np.identity(3, dtype = R.dtype)
n = np.linalg.norm(I - shouldBeIdentity)
return n < 1e-6
def rotationMatrixToEulerAngles(R) : # Copied from
assert(isRotationMatrix(R))
sy = math.sqrt(R[0,0] * R[0,0] + R[1,0] * R[1,0])
singular = sy < 1e-6
if not singular :
x = math.atan2(R[2,1] , R[2,2])
y = math.atan2(-R[2,0], sy)
z = math.atan2(R[1,0], R[0,0])
else :
x = math.atan2(-R[1,2], R[1,1])
y = math.atan2(-R[2,0], sy)
z = 0
return np.array([x, y, z])
def eulerAnglesToRotationMatrix(theta) :
R_x = np.array([[1, 0, 0 ],
[0, math.cos(theta[0]), -math.sin(theta[0]) ],
[0, math.sin(theta[0]), math.cos(theta[0]) ]
])
R_y = np.array([[math.cos(theta[1]), 0, math.sin(theta[1]) ],
[0, 1, 0 ],
[-math.sin(theta[1]), 0, math.cos(theta[1]) ]
])
R_z = np.array([[math.cos(theta[2]), -math.sin(theta[2]), 0],
[math.sin(theta[2]), math.cos(theta[2]), 0],
[0, 0, 1]
])
R = np.dot(R_z, np.dot( R_y, R_x ))
return R
def power_required_mt2814(rpm):
# mt2814_power = [0, 71.04, 118.4, 187.96, 250.12, 309.32]
# mt2814_mass = [0, 680, 980, 1330, 1610, 1810]
mt2814_770KV_12_power = [0, 88.8, 146.52, 193.88, 260.48, 313.76]
mt2814_770KV_12_rpm = [0, 5720,6800,7540,8350, 8800]
# thrust_power = 2*np.interp(mass/2, mt2814_mass, mt2814_power)
rpm_power = np.interp(rpm, mt2814_770KV_12_rpm, mt2814_770KV_12_power)
return rpm_power