Skip to content
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Robotics_with_Python
Learning Robotics with Python
将基础的线性代数与Python结合,练习机器人中的基础概念。本套教程适合有一定编程基础的学习者。
建议学习者边学习边观看3Blue1Brown中有关线性代数的视频教程。
322 changes: 322 additions & 0 deletions lesson001_刚体状态描述与绘制箭头.ipynb

Large diffs are not rendered by default.

259 changes: 259 additions & 0 deletions lesson002_刚体状态描述与坐标系绘制.ipynb

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions lesson003python封装/Lesson003 part1.ipynb

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions lesson003python封装/Lesson003 part2.ipynb

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions lesson003python封装/Lesson003 part3.ipynb

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions lesson003python封装/framelib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import numpy as np
import matplotlib.pyplot as plt


def frame_show(label, Point_A, Point_B, ax, size):
R90 = np.array([[0, -1], [1, 0]])
Line_AB = Point_B - Point_A
norm_Line_AB = np.linalg.norm(Line_AB)
if norm_Line_AB != 1:
Line_AB = Line_AB / norm_Line_AB * size
ax.arrow(Point_A[0], Point_A[1], Line_AB[0], Line_AB[1], head_width=0.5, length_includes_head=True, color="red")
R90 = np.array([[0, -1], [1, 0]]) # 旋转公式
Line_AB_Rot90 = R90.dot(Line_AB) # 点乘
ax.arrow(Point_A[0], Point_A[1], Line_AB_Rot90[0], Line_AB_Rot90[1], head_width=0.5, length_includes_head=True,
color="green")
plt.scatter(Point_A[0], Point_A[1])
label = "{" + str(label) + "}"
plt.text(Point_A[0], Point_A[1], label)


def Rot_func(angle):
angle = np.deg2rad(angle)
R90 = np.array([[0, -1], [1, 0]])
elem_sim = lambda elem: np.around(elem, 0) if (elem - np.around(elem, 0)) < 1e-10 else elem
A11 = elem_sim(np.cos(angle))
A12 = elem_sim(-np.sin(angle))
A21 = elem_sim(np.sin(angle))
A22 = A11
Rot = np.array([[A11, A12], [A21, A22]])
return Rot


def frame_show_angle_center(label, center, angle, ax, size):
Rot = Rot_func(angle)
x = np.array([1, 0])
x = Rot.dot(x)
frame_show(label, center, x + center, ax, size)
print("center" + str(center))
print("center+edge" + str(x + center))
11 changes: 11 additions & 0 deletions lesson003python封装/frameworks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import framelib
import numpy as np
import matplotlib.pyplot as plt
class frame2D:
def __init__(self):
self.center=np.array([0,0])
self.x_axis=np.array([1,0])
self.y_axis=np.array([0,1])
self.angle=0
def show(self,ax,label,size,color):
framelib.frame_show_angle_center(label,self.center,self.angle,ax,size)
6 changes: 6 additions & 0 deletions lesson003python封装/lesson003part000总览
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
本节主要是通过代码封装中的类方法进行的。
试着通过本节了解类的使用方法。
part 1 主要完成类的定义
part 2 主要完成通过文件导入函数
part 3 将类也封装到文件当中
如果有丰富的编程经验建议直接看part 3
37 changes: 37 additions & 0 deletions lesson004_动画/Lesson004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import frameworks as fm
import time
# 进一步将类的封装定义在frameworks当中
import numpy as np
import matplotlib.pyplot as plt

prev = fm.frame2D()
tmp_list = []
step = 1
for i in range(0, 90, step):
tmp_list.append(fm.frame2D())
tmp_list[i].angle = i
# run_time_max = 0
# run_time_max_index = 0
for i in range(0, 90, step):
plt.clf()
ax = plt.axes()
xlimit = 10
ylimit = 10
plt.axis('equal') # 等轴

plt.xlim([-xlimit, xlimit])
plt.ylim([-ylimit, ylimit])
# start_time = time.time()
tmp_list[i].show(ax, "A", 2, "red")
# end_time = time.time()
# run_time = end_time - start_time
# print(str(i) + ":" + str(run_time))
# debug
# if run_time_max < run_time:
# run_time_max = run_time
# run_time_max_index = i

plt.pause(0.1)
# print(run_time_max)
# print(run_time_max_index)
# plt.show()
4 changes: 4 additions & 0 deletions lesson004_动画/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- 该节中主要利用了matplotlib.pyplot中的clf()函数进行图像清除,利用pause()函数进行暂停设值进行的。
- 其中主要练习过程当中的debug能力。需要通过测试擦看代码中的错误具体的位置。
- 相似问题可以通过相同的方法对数学公式或者建模进行检测。
- 由于jupyterlab中显示动画较为繁琐,请将所有文件放到同一文件夹下。这里我用的是PyCharm打开。
48 changes: 48 additions & 0 deletions lesson004_动画/framelib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np
import matplotlib.pyplot as plt
def frame_show(label, Point_A, Point_B, ax, size):
R90 = np.array([[0, -1], [1, 0]])
Line_AB = Point_B - Point_A
norm_Line_AB = np.linalg.norm(Line_AB)
if norm_Line_AB != 1:
Line_AB = Line_AB / norm_Line_AB * size
ax.arrow(Point_A[0], Point_A[1], Line_AB[0], Line_AB[1], head_width=0.5, length_includes_head=True, color="red")
R90 = np.array([[0, -1], [1, 0]]) # 旋转公式
Line_AB_Rot90 = R90.dot(Line_AB) # 点乘
ax.arrow(Point_A[0], Point_A[1], Line_AB_Rot90[0], Line_AB_Rot90[1], head_width=0.5, length_includes_head=True,
color="green")
plt.scatter(Point_A[0], Point_A[1])
label = "{" + str(label) + "}"
plt.text(Point_A[0], Point_A[1], label)


def Rot_func(angle):
angle = np.deg2rad(angle)
R90 = np.array([[0, -1], [1, 0]])
# elem_sim = lambda elem: np.around(elem, 0) if (elem - np.around(elem, 0)) < 1e-16 else elem
# A11 = elem_sim(np.cos(angle)).copy()
# A12 = elem_sim(-np.sin(angle)).copy()
# A21 = elem_sim(np.sin(angle)).copy()
# A22 = A11.copy()
A11=np.cos(angle).copy()
A12=-np.sin(angle).copy()
A21=np.sin(angle).copy()
A22=A11.copy()
Rot = np.array([[A11, A12], [A21, A22]]).copy()
print(np.rad2deg(angle))
print(np.cos(angle))
print(np.sin(angle))
return Rot


def frame_show_angle_center(label, center, angle, ax, size):
Rot = Rot_func(angle)
x = np.array([1, 0])
x = Rot.dot(x)
x_norm=np.linalg.norm(x)
if x_norm!=1:
x=x.copy()/x_norm
frame_show(label, center, x + center, ax, size)
print("angle:"+str(angle))
print("center" + str(center))
print("center+edge" + str(x + center))
11 changes: 11 additions & 0 deletions lesson004_动画/frameworks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import framelib
import numpy as np
import matplotlib.pyplot as plt
class frame2D:
def __init__(self):
self.center=np.array([0,0])
self.x_axis=np.array([1,0])
self.y_axis=np.array([0,1])
self.angle=0
def show(self,ax,label,size,color):
framelib.frame_show_angle_center(label,self.center,self.angle,ax,size)