Skip to content

Commit f2eadee

Browse files
committed
fix error and reorder 02-direct-kin notebook for clarity
1 parent 1831c35 commit f2eadee

File tree

1 file changed

+98
-91
lines changed

1 file changed

+98
-91
lines changed

3-robot-manipulation/1-kinematics/02-direct-kinematics.ipynb

Lines changed: 98 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,6 @@
7676
"[![DH Diagram Wikipedia](https://upload.wikimedia.org/wikipedia/commons/3/3f/Sample_Denavit-Hartenberg_Diagram.png)](https://en.wikipedia.org/wiki/Denavit%E2%80%93Hartenberg_parameters#)"
7777
]
7878
},
79-
{
80-
"cell_type": "code",
81-
"execution_count": null,
82-
"metadata": {},
83-
"outputs": [],
84-
"source": [
85-
"import math\n",
86-
"import numpy as np\n",
87-
"\n",
88-
"# The function dh(theta, d, a, alpha)\n",
89-
"def dh(theta: float, d: float, a: float, alpha: float) -> np.array:\n",
90-
" \"\"\"\n",
91-
" This function returns the denavit hartenberg parameter matrix\n",
92-
" from a given theta, d, a, alpha\n",
93-
"\n",
94-
" Args:\n",
95-
" theta (float): the angle in degrees\n",
96-
" d (float): the offset in mm\n",
97-
" a (float): the length in mm\n",
98-
" alpha (float): the angle in degrees\n",
99-
"\n",
100-
" Returns:\n",
101-
" numpy array: the denavit hartenberg parameter matrix\n",
102-
" \"\"\"\n",
103-
"\n",
104-
" # Convert degrees to radians\n",
105-
" theta = math.radians(theta)\n",
106-
" alpha = math.radians(alpha)\n",
107-
"\n",
108-
" # return the denavit hartenberg parameter matrix\n",
109-
" return np.array([\n",
110-
" [np.cos(theta), -1*np.sin(theta)*np.cos(alpha), np.sin(theta)*np.sin(alpha), a*np.cos(theta)],\n",
111-
" [np.sin(theta), np.cos(theta)*np.cos(alpha), -1*np.cos(theta)*np.sin(alpha), a*np.sin(theta)],\n",
112-
" [ 0, np.sin(alpha), np.cos(alpha), d],\n",
113-
" [ 0, 0, 0, 1]\n",
114-
" ])"
115-
]
116-
},
11779
{
11880
"cell_type": "markdown",
11981
"metadata": {},
@@ -139,24 +101,16 @@
139101
"Suppose a robot manipulator has three joints with DH parameters as follows:\n",
140102
"\n",
141103
"- Joint 1: theta=40, d=1000, a=0, alpha=0\n",
142-
"- Joint 2: theta=10, d=1000, a=0, alpha=0\n",
143-
"\n",
144-
"The robot would look like this:\n",
145-
"\n",
146-
"> ***Run the below cells to see the 'computer model' of the robot***"
104+
"- Joint 2: theta=10, d=1000, a=0, alpha=0"
147105
]
148106
},
149107
{
150-
"cell_type": "code",
151-
"execution_count": null,
108+
"cell_type": "markdown",
152109
"metadata": {},
153-
"outputs": [],
154110
"source": [
155-
"# install the robotics toolbox for python\n",
156-
"# this library is helpful for kinematics and dynamics of robot manipulators\n",
157-
"# more info at:\n",
158-
"# https://petercorke.github.io/robotics-toolbox-python/index.html\n",
159-
"%pip install roboticstoolbox-python"
111+
"#### Model Function for Direct Kinematics\n",
112+
"\n",
113+
"The function `dk` models the two joint manipulator example. The function returns the end effector pose given input joint angles. This model computes direct kinematics. "
160114
]
161115
},
162116
{
@@ -166,47 +120,35 @@
166120
"outputs": [],
167121
"source": [
168122
"import math\n",
169-
"import roboticstoolbox as rtb # load the robotics toolbox\n",
123+
"import numpy as np\n",
170124
"\n",
171-
"# the robot model can be defined using the the robotics toolbox\n",
172-
"# The DHRobot object stores the joint information for the robot\n",
173-
"# RevoluteDH objects can be used to represent each revolute joint in the robot model\n",
174-
"# DH parameters are used to define the robot joint relationships\n",
175-
"robot_2dof = rtb.DHRobot(\n",
176-
" [\n",
177-
" # the RevoluteDH class represents a revolute joint using DH parameters\n",
178-
" # this is a list of 2 joints representing 2 degrees of freedom (DOF)\n",
179-
" rtb.RevoluteDH(\n",
180-
" d = 0,\n",
181-
" alpha = 0,\n",
182-
" a = 1, # note that the dimensions are in m instead of mm\n",
183-
" # offset = math.radians(0)\n",
184-
" ),\n",
185-
" rtb.RevoluteDH(\n",
186-
" d = 0,\n",
187-
" alpha = 0,\n",
188-
" a = 1,\n",
189-
" # offset = math.radians(0)\n",
190-
" )\n",
191-
" ],\n",
192-
" name = \"Two-Link Manipulator\"\n",
193-
")\n",
125+
"# The function dh(theta, d, a, alpha)\n",
126+
"def dh(theta: float, d: float, a: float, alpha: float) -> np.array:\n",
127+
" \"\"\"\n",
128+
" This function returns the denavit hartenberg parameter matrix\n",
129+
" from a given theta, d, a, alpha\n",
194130
"\n",
195-
"# this will print the robot model details\n",
196-
"print(robot_2dof)\n",
131+
" Args:\n",
132+
" theta (float): the angle in degrees\n",
133+
" d (float): the offset in mm\n",
134+
" a (float): the length in mm\n",
135+
" alpha (float): the angle in degrees\n",
197136
"\n",
198-
"# this will plot the robot model graphically\n",
199-
"q = [40.0, 10.0]\n",
200-
"robot_2dof.plot(q, limits=[-2.0, 2.0, -2.0, 2.0, 0.0, 0.5])"
201-
]
202-
},
203-
{
204-
"cell_type": "markdown",
205-
"metadata": {},
206-
"source": [
207-
"#### Model Function for Direct Kinematics\n",
137+
" Returns:\n",
138+
" numpy array: the denavit hartenberg parameter matrix\n",
139+
" \"\"\"\n",
208140
"\n",
209-
"The function `dk` models the two joint manipulator example. The function returns the end effector pose given input joint angles. This model computes direct kinematics. "
141+
" # Convert degrees to radians\n",
142+
" theta = math.radians(theta)\n",
143+
" alpha = math.radians(alpha)\n",
144+
"\n",
145+
" # return the denavit hartenberg parameter matrix\n",
146+
" return np.array([\n",
147+
" [np.cos(theta), -1*np.sin(theta)*np.cos(alpha), np.sin(theta)*np.sin(alpha), a*np.cos(theta)],\n",
148+
" [np.sin(theta), np.cos(theta)*np.cos(alpha), -1*np.cos(theta)*np.sin(alpha), a*np.sin(theta)],\n",
149+
" [ 0, np.sin(alpha), np.cos(alpha), d],\n",
150+
" [ 0, 0, 0, 1]\n",
151+
" ])"
210152
]
211153
},
212154
{
@@ -234,10 +176,10 @@
234176
"\n",
235177
" # Denavit-Hartenberg parameters for the 3 DOF robot\n",
236178
" # end effector: H10 * H21\n",
237-
" H10 = dh(t1 + 40, 1000, 0, 0)\n",
238-
" H21 = dh(t2 + 10, 1000, 0, 0)\n",
179+
" H10 = dh(t1, 0, 1000, 0)\n",
180+
" H21 = dh(t2, 0, 1000, 0)\n",
239181
" # the tool has a length of 100 mm\n",
240-
" Htool3 = dh(0, 100, 0, 0)\n",
182+
" Htool3 = dh(0, 0, 100, 0)\n",
241183
"\n",
242184
" # matrix multiplication to get the pose\n",
243185
" # uses numpy's matrix multiplication\n",
@@ -287,6 +229,71 @@
287229
"# Try with several values to validate your calculations."
288230
]
289231
},
232+
{
233+
"cell_type": "markdown",
234+
"metadata": {},
235+
"source": [
236+
"Using the robotics toolbox, we can visualize the robot manipulator.\n",
237+
"\n",
238+
"The robot would look like this:\n",
239+
"\n",
240+
"> ***Run the below cells to see the 'computer model' of the robot***"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": null,
246+
"metadata": {},
247+
"outputs": [],
248+
"source": [
249+
"# install the robotics toolbox for python\n",
250+
"# this library is helpful for kinematics and dynamics of robot manipulators\n",
251+
"# more info at:\n",
252+
"# https://petercorke.github.io/robotics-toolbox-python/index.html\n",
253+
"%pip install roboticstoolbox-python"
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": null,
259+
"metadata": {},
260+
"outputs": [],
261+
"source": [
262+
"import math\n",
263+
"import roboticstoolbox as rtb # load the robotics toolbox\n",
264+
"\n",
265+
"# the robot model can be defined using the the robotics toolbox\n",
266+
"# The DHRobot object stores the joint information for the robot\n",
267+
"# RevoluteDH objects can be used to represent each revolute joint in the robot model\n",
268+
"# DH parameters are used to define the robot joint relationships\n",
269+
"robot_2dof = rtb.DHRobot(\n",
270+
" [\n",
271+
" # the RevoluteDH class represents a revolute joint using DH parameters\n",
272+
" # this is a list of 2 joints representing 2 degrees of freedom (DOF)\n",
273+
" rtb.RevoluteDH(\n",
274+
" d = 0,\n",
275+
" alpha = 0,\n",
276+
" a = 1, # note that the dimensions are in m instead of mm\n",
277+
" # offset = math.radians(0)\n",
278+
" ),\n",
279+
" rtb.RevoluteDH(\n",
280+
" d = 0,\n",
281+
" alpha = 0,\n",
282+
" a = 1,\n",
283+
" # offset = math.radians(0)\n",
284+
" )\n",
285+
" ],\n",
286+
" name = \"Two-Link Manipulator\"\n",
287+
")\n",
288+
"\n",
289+
"# this will print the robot model details\n",
290+
"print(robot_2dof)\n",
291+
"\n",
292+
"# this will plot the robot model graphically\n",
293+
"q = [40.0, 10.0]\n",
294+
"robot_2dof.plot(q, limits=[-2.0, 2.0, -2.0, 2.0, 0.0, 0.5])"
295+
]
296+
},
290297
{
291298
"cell_type": "markdown",
292299
"metadata": {},

0 commit comments

Comments
 (0)