summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/robotarm/robotarm.sty
blob: 5fb7f730aa4959dc98c25cb6291fee8194b52d47 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
%%
%% This is file `robotarm.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% robotarm.dtx  (with options: `robotarm-package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2021 by M.J.W. Snippe
%% -----------------------------------
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3 of this license or (at your option) any later
%% version. The latest version of this license is in:
%% 
%%     http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%% 
%% This work has the LPPL maintenance status
%% `author-maintained'.
%% 
%% This work consists of the files found at github.com/max-sn/robotarm.
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{robotarm}
  [2022/03/08 v0.1 Tikz commands to draw planar robot arms]

\RequirePackage{tikz}

\usetikzlibrary{patterns}

\makeatletter

\newif\ifRA@robotarm@drawannotations

\pgfkeys{
  /robotarm/base link/.cd,
    width/.code={\pgfmathsetmacro\RA@baselink@width{#1}},
    width=0.6,
    height/.code={\pgfmathsetmacro\RA@baselink@height{#1}},
    height=0.3,
    world width/.code={\pgfmathsetmacro\RA@baselink@worldwidth{#1}},
    world width=1.0,
    world height/.code={\pgfmathsetmacro\RA@baselink@worldheight{#1}},
    world height=0.3,
    draw base link/.code={%
      \path[link style]
        (-1/2*\RA@baselink@width,0)
        arc (180:0:1/2*\RA@baselink@width)
        -- ++ (0, -\RA@baselink@height)
        -- ++ (-\RA@baselink@width, 0)
        -- cycle;
    },
    draw world/.code={%
      \path[world style]
        (-1/2*\RA@baselink@worldwidth,-\RA@baselink@height)
        arc (180:360:{1/2*\RA@baselink@worldwidth}
          and {\RA@baselink@worldheight}) -- cycle;
      \path[draw, world style]
        (-1/2*\RA@baselink@worldwidth,-\RA@baselink@height)
        -- ++(\RA@baselink@worldwidth,0);
    },
  /robotarm/link/.cd,
    width/.code={\pgfmathsetmacro\RA@link@width{#1}},
    width=0.4,
    length/.code={\pgfmathsetmacro\RA@link@length{#1}},
    length=2.0,
    joint radius/.code={\pgfmathsetmacro\RA@link@jointradius{#1}},
    joint radius=0.25,
    draw link/.code={%
      \path[link style]
        (0,1/2*\RA@link@width)
        -- ++ ( \RA@link@length, 0)
        arc (90:-90:1/2*\RA@link@width)
        -- ++ (-\RA@link@length, 0)
        arc (270:90:1/2*\RA@link@width)
        -- cycle;
    },
    draw joint/.code={%
      \path[link style]
        (0,0) circle (\RA@link@jointradius);
      \path[link style]
        (0,0) circle (1/3*\RA@link@jointradius);
    },
  /robotarm/end effector/.cd,
    width/.code={\pgfmathsetmacro\RA@endeff@width{#1}},
    width=0.4,
    length/.code={\pgfmathsetmacro\RA@endeff@length{#1}},
    length=2,
    joint radius/.code={\pgfmathsetmacro\RA@endeff@jointradius{#1}},
    joint radius=0.25,
    gripper radius/.code={\pgfmathsetmacro\RA@endeff@gripperradius{#1}},
    gripper radius=0.3,
    gripper opening angle/.code={%
      \pgfmathsetmacro\RA@endeff@gripperopeningangle{#1}},
    gripper opening angle=60,
    draw joint/.code={%
      \path[link style]
        (0,0) circle (\RA@endeff@jointradius);
      \path[link style]
        (0,0) circle (1/3*\RA@endeff@jointradius);
    },
    draw link/.code={%
      \pgfmathsetmacro{\link@startangle}{%
        180-asin(1/2*\RA@endeff@width/\RA@endeff@gripperradius)}
      \pgfmathsetmacro{\link@endangle}{%
        180+asin(1/2*\RA@endeff@width/\RA@endeff@gripperradius)}

      \path[link style]
        (\RA@endeff@length, 0)
        ++ (\link@startangle:\RA@endeff@gripperradius)
        arc (\link@startangle:\link@endangle:\RA@endeff@gripperradius)
        -- (0,0|-0,-1/2*\RA@endeff@width)
        arc (-90:90:1/2*\RA@endeff@width)
        -- cycle;
    },
    draw end effector/.code={
      \draw[link style]
        (\RA@endeff@length, 0)
        ++ (-1/2*\RA@endeff@gripperopeningangle:%
            \RA@endeff@gripperradius)
        arc [start angle=-1/2*\RA@endeff@gripperopeningangle,
             delta angle=-360+\RA@endeff@gripperopeningangle,
             radius=\RA@endeff@gripperradius]
        -- ++(180+1/2*\RA@endeff@gripperopeningangle:%
              0.4*\RA@endeff@gripperradius)
        arc [start angle=1/2*\RA@endeff@gripperopeningangle,
             delta angle=360-\RA@endeff@gripperopeningangle,
             radius=0.6*\RA@endeff@gripperradius]
        -- ++(-1/2*\RA@endeff@gripperopeningangle:%
              0.4*\RA@endeff@gripperradius)
        -- cycle;
    },
  /robotarm/.cd,
    draw annotations/.is if=RA@robotarm@drawannotations,
    draw annotations=true,
    every annotation/.style={},
    every length annotation/.style={},
    every length annotation arrow/.style={draw,->},
    every length annotation node/.style={circle,inner sep=0.5pt},
    every length annotation help line/.style={draw,help lines},
    every angle annotation/.style={},
    every angle annotation arrow/.style={draw,->},
    every angle annotation node/.style={},
    every angle annotation help line/.style={draw,help lines},
    base link/.code=\pgfkeys{/robotarm/base link/.cd,#1},
    link/.code=\pgfkeys{/robotarm/link/.cd,#1},
    end effector/.code=\pgfkeys{/robotarm/end effector/.cd,#1},
    geometry/.code=\pgfkeys{/robotarm/geometry/.cd,#1},
    config/.code=\pgfkeys{/robotarm/config/.cd,#1},
    spacing/.code=\pgfkeys{/robotarm/annotations/spacing/.cd,#1},
    labels/.code=\pgfkeys{/robotarm/annotations/labels/.cd,#1},
    styles/.code=\pgfkeys{/robotarm/styles/.cd,#1},
  /robotarm/geometry/.cd,
    a0/.initial=0,
    a/.initial=2,
    r/.initial=0.25,
    w/.initial=0.4,
  /robotarm/config/q/.initial=0,
  /robotarm/frames/.cd,
    in link 0/.style={},
    in end effector/.style={
      /robotarm/frames/in link \RA@robotarm@numlinks,
      shift={%
        (\pgfkeysvalueof{/robotarm/geometry/a\RA@robotarm@numlinks},0)},
    },
    in world/.style={
      shift={(0,-\RA@baselink@height)}
    },
  /robotarm/styles/.cd,
    world/.style={pattern=north west lines},
    link/.style={
      draw,
      fill=lightgray,
    },
    link 0/.style={/robotarm/styles/link},
  /robotarm/annotations/.cd,
    spacing/.cd,
      a/.initial=3,
      q/.initial=1/2,
  /robotarm/annotations/.cd,
    labels/.cd,
      a/.initial=a,
      q/.initial=q,
}
\tikzset{
  link style/.style={/robotarm/styles/link},
  world style/.style={/robotarm/styles/world},
}
\newcommand\robotarmset[1]{%
  \pgfkeys{/robotarm/.cd,#1}%
}
\newcommand\robotArmLink[1][]{
  \begingroup
    \pgfkeys{/robotarm/link/.cd,#1}

    \pgfkeys{/robotarm/link/draw link}
    \pgfkeys{/robotarm/link/draw joint}
  \endgroup
}
\newcommand\robotArmEndEffector[1][]{
  \begingroup
    \pgfkeys{/robotarm/end effector/.cd,#1}

    \pgfkeys{/robotarm/end effector/draw link}
    \pgfkeys{/robotarm/end effector/draw joint}
    \pgfkeys{/robotarm/end effector/draw end effector}

  \endgroup
}
\newcommand\robotArmBaseLink[1][]{
  \begingroup
    \pgfkeys{/robotarm/base link/.cd,#1}

    \pgfkeys{/robotarm/base link/draw world}
    \pgfkeys{/robotarm/base link/draw base link}
  \endgroup
}
\newcommand\robotArm[2][]{
  \pgfmathtruncatemacro\RA@robotarm@numlinks{#2}
  \def\@tmpkeys{}
  \foreach \@link [remember=\@link as \@prevlink (initially 0)] in %
    {1,...,\RA@robotarm@numlinks}{
    \xdef\@tmpkeys{\@tmpkeys%
      /robotarm/geometry/a\@link/.initial=%
        \pgfkeysvalueof{/robotarm/geometry/a},%
      /robotarm/geometry/r\@link/.initial=%
        \pgfkeysvalueof{/robotarm/geometry/r},%
      /robotarm/geometry/w\@link/.initial=%
        \pgfkeysvalueof{/robotarm/geometry/w},%
      /robotarm/config/q\@link/.initial=%
        \pgfkeysvalueof{/robotarm/config/q},%
      /robotarm/styles/link \@link/.style={/robotarm/styles/link},%
      /robotarm/annotations/labels/a\@link/.initial={%
        $\pgfkeysvalueof{/robotarm/annotations/labels/a}_{\@link}$},%
      /robotarm/annotations/labels/q\@link/.initial={%
        $\pgfkeysvalueof{/robotarm/annotations/labels/q}_{\@link}$},%
      /robotarm/annotations/spacing/a\@link/.initial={%
        \pgfkeysvalueof{/robotarm/annotations/spacing/a}},%
      /robotarm/annotations/spacing/q\@link/.initial={%
        \pgfkeysvalueof{/robotarm/annotations/spacing/q}},%
    }
  }
  \expandafter\pgfkeys\expandafter{\@tmpkeys}
  \pgfkeys{/robotarm/.cd,#1}
  \def\@tmpkeys{}
  \foreach \@link [remember=\@link as \@prevlink (initially 0)] in %
    {1,...,\RA@robotarm@numlinks}{
    \xdef\@tmpkeys{\@tmpkeys%
      /robotarm/frames/in link \@link/.style={%
        /robotarm/frames/in link \@prevlink,
        /tikz/shift={%
          (\pgfkeysvalueof{/robotarm/geometry/a\@prevlink},0)},
        /tikz/rotate={\pgfkeysvalueof{/robotarm/config/q\@link}},
      },
    }
  }
  \expandafter\pgfkeys\expandafter{\@tmpkeys}

  \begin{scope}[/robotarm/frames/in link 0,
                link style/.style={/robotarm/styles/link 0}]
    \robotArmBaseLink
  \end{scope}

  \foreach\link@num in {1,...,\RA@robotarm@numlinks}{
    \begin{scope}[/robotarm/frames/in link \link@num,
                  link style/.style={/robotarm/styles/link \link@num}]

      \ifnum\link@num<\RA@robotarm@numlinks
        \robotArmLink[
          joint radius=\pgfkeysvalueof{/robotarm/geometry/r\link@num},
          length=\pgfkeysvalueof{/robotarm/geometry/a\link@num},
          width=\pgfkeysvalueof{/robotarm/geometry/w\link@num},
        ]
      \else
        \robotArmEndEffector[
          joint radius=\pgfkeysvalueof{/robotarm/geometry/r\link@num},
          length=\pgfkeysvalueof{/robotarm/geometry/a\link@num},
          width=\pgfkeysvalueof{/robotarm/geometry/w\link@num},
        ]
      \fi
    \end{scope}
  }
  \foreach\link@num in {1,...,\RA@robotarm@numlinks}{
    \begin{scope}[/robotarm/frames/in link \link@num]
      \pgfmathsetmacro\link@length{\pgfkeysvalueof{%
        /robotarm/geometry/a\link@num}}
      \pgfmathsetmacro\link@angle{\pgfkeysvalueof{%
        /robotarm/config/q\link@num}}

      \ifRA@robotarm@drawannotations
        \pgfmathsetmacro\link@lengthannotspacing{%
          \pgfkeysvalueof{/robotarm/annotations/spacing/a\link@num}*
            \pgfkeysvalueof{/robotarm/geometry/r\link@num}}
        \pgfmathsetmacro\link@angleannotspacing{%
          \pgfkeysvalueof{/robotarm/annotations/spacing/q\link@num}*
            \link@length}

        % Length annotation help lines
        \path[/robotarm/every annotation,
              /robotarm/every length annotation,
              /robotarm/every length annotation help line]
          (0,0) -- (\link@length,0);
        \path[/robotarm/every annotation,
              /robotarm/every length annotation,
              /robotarm/every length annotation help line]
          (0,0) -- ++ (0,{\link@lengthannotspacing +
            0.1*sign(\link@lengthannotspacing)});
        \path[/robotarm/every annotation,
              /robotarm/every length annotation,
              /robotarm/every length annotation help line]
          (\link@length,0) -- ++ (0,{\link@lengthannotspacing +
            0.1*sign(\link@lengthannotspacing)});
        \path (0,\link@lengthannotspacing)
          -- coordinate[pos=0.5] (coor) ++ (\link@length,0);
        \node[/robotarm/every annotation,
              /robotarm/every length annotation,
              /robotarm/every length annotation node]
          at (coor) (tag)
          {\pgfkeysvalueof{/robotarm/annotations/labels/a\link@num}};
        \path[/robotarm/every annotation,
              /robotarm/every length annotation,
              /robotarm/every length annotation arrow]
          (tag) -- (0,\link@lengthannotspacing);
        \path[/robotarm/every annotation,
              /robotarm/every length annotation,
              /robotarm/every length annotation arrow]
          (tag) -- (\link@length,\link@lengthannotspacing);

        \pgfmathsetmacro\angleannotationcase{%
          ifthenelse(\link@angle==0.0, 0, 1)}
        \ifnum\angleannotationcase>0
          % Angle annotation help lines
          \path[/robotarm/every annotation,
                /robotarm/every angle annotation,
                /robotarm/every angle annotation help line]
            (0,0) -- ++(-\link@angle:{\link@angleannotspacing+0.1});
          \path[/robotarm/every annotation,
                /robotarm/every angle annotation,
                /robotarm/every angle annotation help line]
            (0,0) -- ++(0:{\link@angleannotspacing+0.1});

          % Angle annotation arrow
          \path[/robotarm/every annotation,
                /robotarm/every angle annotation,
                /robotarm/every angle annotation arrow]
            (0,0) ++ (-\link@angle:\link@angleannotspacing)
            arc (-\link@angle:0:\link@angleannotspacing);

          % Angle annotation node
          \node[/robotarm/every annotation,
                /robotarm/every angle annotation,
                /robotarm/every angle annotation node]
            at (-\link@angle/2:\link@angleannotspacing+0.3)
            {\pgfkeysvalueof{/robotarm/annotations/labels/q\link@num}};
        \fi
      \fi
    \end{scope}
  }
  \tikzset{
    in link/.style={/robotarm/frames/in link #1},
    in base link/.style={/robotarm/frames/in link 0},
    in end effector/.style={/robotarm/frames/in end effector},
    in world/.style={/robotarm/frames/in world},
  }
}
\makeatother
\endinput
%%
%% End of file `robotarm.sty'.