Skip to content

Commit 0fde4a8

Browse files
Merge pull request AtsushiSakai#1 from AtsushiSakai/master
Update
2 parents 79ccc76 + 03811e6 commit 0fde4a8

File tree

106 files changed

+7431
-845
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+7431
-845
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
__pycache__/
55
*.py[cod]
66
*$py.class
7+
_build/
8+
.idea/
79

810
# C extensions
911
*.so

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "doc/PythonRoboticsPaper"]
2+
path = doc/PythonRoboticsPaper
3+
url = https://github.com/AtsushiSakai/PythonRoboticsPaper
4+

.travis.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ python:
33
- 3.6
44

55
before_install:
6+
- sudo apt-get update
67
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
78
- chmod +x miniconda.sh
89
- bash miniconda.sh -b -p $HOME/miniconda
@@ -14,13 +15,15 @@ before_install:
1415
- conda info -a
1516

1617
install:
17-
- pip install scipy
18-
- pip install numpy
19-
- pip install matplotlib
20-
- pip install pandas
21-
- pip install cvxpy
22-
- conda install -y -c cvxgrp cvxpy
23-
- conda install -y -c anaconda cvxopt
18+
- conda install numpy
19+
- conda install scipy
20+
- conda install matplotlib
21+
- conda install pandas
22+
- conda install -c conda-forge lapack
23+
- conda install -c cvxgrp cvxpy
24+
#- pip install cvxpy
25+
#- conda install -y -c cvxgrp cvxpy
26+
#- conda install -y -c anaconda cvxopt
2427

2528
script:
2629
- python --version

AerialNavigation/Quadrotor.py renamed to AerialNavigation/drone_3d_trajectory_following/Quadrotor.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@
99
import matplotlib.pyplot as plt
1010
from mpl_toolkits.mplot3d import Axes3D
1111

12+
1213
class Quadrotor():
13-
def __init__(self, x=0, y=0, z=0, roll=0, pitch=0, yaw=0, size=0.25):
14-
self.p1 = np.array([size/2, 0, 0, 1]).T
15-
self.p2 = np.array([-size/2, 0, 0, 1]).T
16-
self.p3 = np.array([0, size/2, 0, 1]).T
17-
self.p4 = np.array([0, -size/2, 0, 1]).T
14+
def __init__(self, x=0, y=0, z=0, roll=0, pitch=0, yaw=0, size=0.25, show_animation=True):
15+
self.p1 = np.array([size / 2, 0, 0, 1]).T
16+
self.p2 = np.array([-size / 2, 0, 0, 1]).T
17+
self.p3 = np.array([0, size / 2, 0, 1]).T
18+
self.p4 = np.array([0, -size / 2, 0, 1]).T
1819

1920
self.x_data = []
2021
self.y_data = []
2122
self.z_data = []
22-
23-
plt.ion()
24-
25-
fig = plt.figure()
26-
self.ax = fig.add_subplot(111, projection='3d')
23+
self.show_animation = show_animation
2724

2825
self.update_pose(x, y, z, roll, pitch, yaw)
2926

27+
if self.show_animation:
28+
plt.ion()
29+
fig = plt.figure()
30+
self.ax = fig.add_subplot(111, projection='3d')
31+
3032
def update_pose(self, x, y, z, roll, pitch, yaw):
3133
self.x = x
3234
self.y = y
@@ -37,7 +39,9 @@ def update_pose(self, x, y, z, roll, pitch, yaw):
3739
self.x_data.append(x)
3840
self.y_data.append(y)
3941
self.z_data.append(z)
40-
self.plot()
42+
43+
if self.show_animation:
44+
self.plot()
4145

4246
def transformation_matrix(self):
4347
x = self.x
@@ -47,10 +51,11 @@ def transformation_matrix(self):
4751
pitch = self.pitch
4852
yaw = self.yaw
4953
return np.array(
50-
[[cos(yaw)*cos(pitch), -sin(yaw)*cos(roll)+cos(yaw)*sin(pitch)*sin(roll), sin(yaw)*sin(roll)+cos(yaw)*sin(pitch)*cos(roll), x],
51-
[sin(yaw)*cos(pitch), cos(yaw)*cos(roll)+sin(yaw)*sin(pitch)*sin(roll), -cos(yaw)*sin(roll)+sin(yaw)*sin(pitch)*cos(roll), y],
52-
[-sin(pitch), cos(pitch)*sin(roll), cos(pitch)*cos(yaw), z]
53-
])
54+
[[cos(yaw) * cos(pitch), -sin(yaw) * cos(roll) + cos(yaw) * sin(pitch) * sin(roll), sin(yaw) * sin(roll) + cos(yaw) * sin(pitch) * cos(roll), x],
55+
[sin(yaw) * cos(pitch), cos(yaw) * cos(roll) + sin(yaw) * sin(pitch) *
56+
sin(roll), -cos(yaw) * sin(roll) + sin(yaw) * sin(pitch) * cos(roll), y],
57+
[-sin(pitch), cos(pitch) * sin(roll), cos(pitch) * cos(yaw), z]
58+
])
5459

5560
def plot(self):
5661
T = self.transformation_matrix()
@@ -66,14 +71,15 @@ def plot(self):
6671
[p1_t[1], p2_t[1], p3_t[1], p4_t[1]],
6772
[p1_t[2], p2_t[2], p3_t[2], p4_t[2]], 'k.')
6873

69-
self.ax.plot([p1_t[0], p2_t[0]], [p1_t[1], p2_t[1]], [p1_t[2], p2_t[2]], 'r-')
70-
self.ax.plot([p3_t[0], p4_t[0]], [p3_t[1], p4_t[1]], [p3_t[2], p4_t[2]], 'r-')
74+
self.ax.plot([p1_t[0], p2_t[0]], [p1_t[1], p2_t[1]],
75+
[p1_t[2], p2_t[2]], 'r-')
76+
self.ax.plot([p3_t[0], p4_t[0]], [p3_t[1], p4_t[1]],
77+
[p3_t[2], p4_t[2]], 'r-')
7178

7279
self.ax.plot(self.x_data, self.y_data, self.z_data, 'b:')
7380

7481
plt.xlim(-5, 5)
7582
plt.ylim(-5, 5)
7683
self.ax.set_zlim(0, 10)
7784

78-
plt.show()
7985
plt.pause(0.001)

AerialNavigation/TrajectoryGenerator.py renamed to AerialNavigation/drone_3d_trajectory_following/TrajectoryGenerator.py

File renamed without changes.
12.1 MB
Loading

AerialNavigation/quad_sim.py renamed to AerialNavigation/drone_3d_trajectory_following/drone_3d_trajectory_following.py

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,30 @@
99
from Quadrotor import Quadrotor
1010
from TrajectoryGenerator import TrajectoryGenerator
1111

12-
#Simulation parameters
12+
show_animation = True
13+
14+
# Simulation parameters
1315
g = 9.81
1416
m = 0.2
1517
Ixx = 1
1618
Iyy = 1
1719
Izz = 1
1820
T = 5
1921

20-
#Proportional coefficients
22+
# Proportional coefficients
2123
Kp_x = 1
2224
Kp_y = 1
2325
Kp_z = 1
2426
Kp_roll = 25
2527
Kp_pitch = 25
2628
Kp_yaw = 25
2729

28-
#Derivative coefficients
30+
# Derivative coefficients
2931
Kd_x = 10
3032
Kd_y = 10
3133
Kd_z = 1
3234

35+
3336
def quad_sim(x_c, y_c, z_c):
3437
"""
3538
Calculates the necessary thrust and torques for the quadrotor to
@@ -57,9 +60,12 @@ def quad_sim(x_c, y_c, z_c):
5760
dt = 0.1
5861
t = 0
5962

60-
q = Quadrotor(x=x_pos, y=y_pos, z=z_pos, roll=roll, pitch=pitch, yaw=yaw, size=1)
63+
q = Quadrotor(x=x_pos, y=y_pos, z=z_pos, roll=roll,
64+
pitch=pitch, yaw=yaw, size=1, show_animation=show_animation)
6165

6266
i = 0
67+
n_run = 8
68+
irun = 0
6369

6470
while True:
6571
while t <= T:
@@ -73,38 +79,48 @@ def quad_sim(x_c, y_c, z_c):
7379
des_y_acc = calculate_acceleration(y_c[i], t)
7480
des_z_acc = calculate_acceleration(z_c[i], t)
7581

76-
thrust = m*(g + des_z_acc + Kp_z*(des_z_pos - z_pos) + Kd_z*(des_z_vel - z_vel))
82+
thrust = m * (g + des_z_acc + Kp_z * (des_z_pos -
83+
z_pos) + Kd_z * (des_z_vel - z_vel))
7784

78-
roll_torque = Kp_roll*(((des_x_acc*sin(des_yaw) - des_y_acc*cos(des_yaw))/g) - roll)
79-
pitch_torque = Kp_pitch*(((des_x_acc*cos(des_yaw) - des_y_acc*sin(des_yaw))/g) - pitch)
80-
yaw_torque = Kp_yaw*(des_yaw - yaw)
85+
roll_torque = Kp_roll * \
86+
(((des_x_acc * sin(des_yaw) - des_y_acc * cos(des_yaw)) / g) - roll)
87+
pitch_torque = Kp_pitch * \
88+
(((des_x_acc * cos(des_yaw) - des_y_acc * sin(des_yaw)) / g) - pitch)
89+
yaw_torque = Kp_yaw * (des_yaw - yaw)
8190

82-
roll_vel += roll_torque*dt/Ixx
83-
pitch_vel += pitch_torque*dt/Iyy
84-
yaw_vel += yaw_torque*dt/Izz
91+
roll_vel += roll_torque * dt / Ixx
92+
pitch_vel += pitch_torque * dt / Iyy
93+
yaw_vel += yaw_torque * dt / Izz
8594

86-
roll += roll_vel*dt
87-
pitch += pitch_vel*dt
88-
yaw += yaw_vel*dt
95+
roll += roll_vel * dt
96+
pitch += pitch_vel * dt
97+
yaw += yaw_vel * dt
8998

9099
R = rotation_matrix(roll, pitch, yaw)
91-
acc = (np.matmul(R, np.array([0, 0, thrust]).T) - np.array([0, 0, m*g]).T)/m
100+
acc = (np.matmul(R, np.array(
101+
[0, 0, thrust]).T) - np.array([0, 0, m * g]).T) / m
92102
x_acc = acc[0]
93103
y_acc = acc[1]
94104
z_acc = acc[2]
95-
x_vel += x_acc*dt
96-
y_vel += y_acc*dt
97-
z_vel += z_acc*dt
98-
x_pos += x_vel*dt
99-
y_pos += y_vel*dt
100-
z_pos += z_vel*dt
105+
x_vel += x_acc * dt
106+
y_vel += y_acc * dt
107+
z_vel += z_acc * dt
108+
x_pos += x_vel * dt
109+
y_pos += y_vel * dt
110+
z_pos += z_vel * dt
101111

102112
q.update_pose(x_pos, y_pos, z_pos, roll, pitch, yaw)
103113

104114
t += dt
105115

106116
t = 0
107-
i = (i+1)%4
117+
i = (i + 1) % 4
118+
irun += 1
119+
if irun >= n_run:
120+
break
121+
122+
print("Done")
123+
108124

109125
def calculate_position(c, t):
110126
"""
@@ -118,7 +134,8 @@ def calculate_position(c, t):
118134
Returns
119135
Position
120136
"""
121-
return c[0]*t**5 + c[1]*t**4 + c[2]*t**3 + c[3]*t**2 + c[4]*t + c[5]
137+
return c[0] * t**5 + c[1] * t**4 + c[2] * t**3 + c[3] * t**2 + c[4] * t + c[5]
138+
122139

123140
def calculate_velocity(c, t):
124141
"""
@@ -132,7 +149,8 @@ def calculate_velocity(c, t):
132149
Returns
133150
Velocity
134151
"""
135-
return 5*c[0]*t**4 + 4*c[1]*t**3 + 3*c[2]*t**2 + 2*c[3]*t + c[4]
152+
return 5 * c[0] * t**4 + 4 * c[1] * t**3 + 3 * c[2] * t**2 + 2 * c[3] * t + c[4]
153+
136154

137155
def calculate_acceleration(c, t):
138156
"""
@@ -146,7 +164,8 @@ def calculate_acceleration(c, t):
146164
Returns
147165
Acceleration
148166
"""
149-
return 20*c[0]*t**3 + 12*c[1]*t**2 + 6*c[2]*t + 2*c[3]
167+
return 20 * c[0] * t**3 + 12 * c[1] * t**2 + 6 * c[2] * t + 2 * c[3]
168+
150169

151170
def rotation_matrix(roll, pitch, yaw):
152171
"""
@@ -161,10 +180,12 @@ def rotation_matrix(roll, pitch, yaw):
161180
3x3 rotation matrix as NumPy array
162181
"""
163182
return np.array(
164-
[[cos(yaw)*cos(pitch), -sin(yaw)*cos(roll)+cos(yaw)*sin(pitch)*sin(roll), sin(yaw)*sin(roll)+cos(yaw)*sin(pitch)*cos(roll)],
165-
[sin(yaw)*cos(pitch), cos(yaw)*cos(roll)+sin(yaw)*sin(pitch)*sin(roll), -cos(yaw)*sin(roll)+sin(yaw)*sin(pitch)*cos(roll)],
166-
[-sin(pitch), cos(pitch)*sin(roll), cos(pitch)*cos(yaw)]
167-
])
183+
[[cos(yaw) * cos(pitch), -sin(yaw) * cos(roll) + cos(yaw) * sin(pitch) * sin(roll), sin(yaw) * sin(roll) + cos(yaw) * sin(pitch) * cos(roll)],
184+
[sin(yaw) * cos(pitch), cos(yaw) * cos(roll) + sin(yaw) * sin(pitch) *
185+
sin(roll), -cos(yaw) * sin(roll) + sin(yaw) * sin(pitch) * cos(roll)],
186+
[-sin(pitch), cos(pitch) * sin(roll), cos(pitch) * cos(yaw)]
187+
])
188+
168189

169190
def main():
170191
"""
@@ -177,13 +198,14 @@ def main():
177198
waypoints = [[-5, -5, 5], [5, -5, 5], [5, 5, 5], [-5, 5, 5]]
178199

179200
for i in range(4):
180-
traj = TrajectoryGenerator(waypoints[i], waypoints[(i+1)%4], T)
201+
traj = TrajectoryGenerator(waypoints[i], waypoints[(i + 1) % 4], T)
181202
traj.solve()
182203
x_coeffs[i] = traj.x_c
183204
y_coeffs[i] = traj.y_c
184205
z_coeffs[i] = traj.z_c
185206

186207
quad_sim(x_coeffs, y_coeffs, z_coeffs)
187208

209+
188210
if __name__ == "__main__":
189211
main()

RoboticArm/NLinkArm.py renamed to ArmNavigation/n_joint_arm_to_point_control/NLinkArm.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,61 @@
77
import numpy as np
88
import matplotlib.pyplot as plt
99

10+
1011
class NLinkArm(object):
11-
def __init__(self, link_lengths, joint_angles, goal):
12+
def __init__(self, link_lengths, joint_angles, goal, show_animation):
13+
self.show_animation = show_animation
1214
self.n_links = len(link_lengths)
1315
if self.n_links is not len(joint_angles):
1416
raise ValueError()
1517

1618
self.link_lengths = np.array(link_lengths)
1719
self.joint_angles = np.array(joint_angles)
18-
self.points = [[0, 0] for _ in range(self.n_links+1)]
20+
self.points = [[0, 0] for _ in range(self.n_links + 1)]
1921

2022
self.lim = sum(link_lengths)
2123
self.goal = np.array(goal).T
2224

23-
self.fig = plt.figure()
24-
self.fig.canvas.mpl_connect('button_press_event', self.click)
25+
if show_animation:
26+
self.fig = plt.figure()
27+
self.fig.canvas.mpl_connect('button_press_event', self.click)
2528

26-
plt.ion()
27-
plt.show()
29+
plt.ion()
30+
plt.show()
2831

2932
self.update_points()
3033

3134
def update_joints(self, joint_angles):
3235
self.joint_angles = joint_angles
36+
3337
self.update_points()
3438

3539
def update_points(self):
36-
for i in range(1, self.n_links+1):
37-
self.points[i][0] = self.points[i-1][0] + self.link_lengths[i-1]*np.cos(np.sum(self.joint_angles[:i]))
38-
self.points[i][1] = self.points[i-1][1] + self.link_lengths[i-1]*np.sin(np.sum(self.joint_angles[:i]))
40+
for i in range(1, self.n_links + 1):
41+
self.points[i][0] = self.points[i - 1][0] + \
42+
self.link_lengths[i - 1] * \
43+
np.cos(np.sum(self.joint_angles[:i]))
44+
self.points[i][1] = self.points[i - 1][1] + \
45+
self.link_lengths[i - 1] * \
46+
np.sin(np.sum(self.joint_angles[:i]))
3947

4048
self.end_effector = np.array(self.points[self.n_links]).T
41-
self.plot()
49+
if self.show_animation:
50+
self.plot()
4251

4352
def plot(self):
4453
plt.cla()
4554

46-
for i in range(self.n_links+1):
55+
for i in range(self.n_links + 1):
4756
if i is not self.n_links:
48-
plt.plot([self.points[i][0], self.points[i+1][0]], [self.points[i][1], self.points[i+1][1]], 'r-')
57+
plt.plot([self.points[i][0], self.points[i + 1][0]],
58+
[self.points[i][1], self.points[i + 1][1]], 'r-')
4959
plt.plot(self.points[i][0], self.points[i][1], 'ko')
5060

5161
plt.plot(self.goal[0], self.goal[1], 'gx')
5262

53-
plt.plot([self.end_effector[0], self.goal[0]], [self.end_effector[1], self.goal[1]], 'g--')
63+
plt.plot([self.end_effector[0], self.goal[0]], [
64+
self.end_effector[1], self.goal[1]], 'g--')
5465

5566
plt.xlim([-self.lim, self.lim])
5667
plt.ylim([-self.lim, self.lim])
1.49 MB
Loading

0 commit comments

Comments
 (0)