kinematic -- Kinematic solver/constraint system¶
This module defines the types and functions for kinematic manimulation and computation.
Kinematics are a conceptual approach of mechanisms. It sort parts in groups called solids (in solids all parts have the same movement), and links the solids to each other using constraints named joints. That way no matter what are the parts, or what are their shape, or how parts interact - solids movements can be deduced only from joints.
This allows designing the mechanisms before designing its parts. This also allows visualizing the mechanism whether it is complete or not.
As parts in the same solid all have the same movement, solids are considered to be undeformable. This allows the to use the Screw theory to represent the force and movement variables (see https://en.wikipedia.org/wiki/Screw_theory).
In this module, screws are called Screw.
This module mainly features:
Joint- the base class for all joints, instances of joints define a kinematicKinematic- the general kinematic solverChain- a joint and kinematic solver dedicated to kinematic chains
joints are defined in madcad.joints
KinematicError
¶
Bases: Exception
raised when a kinematic problem cannot be solved because the constraints cannot be satisfied or the solver cannot satisfy it
Kinematic(joints=[], content=None, ground=None, inputs=None, outputs=None)
¶
This class allows resolving direct and inverse kinematic problems with any complexity.
It is not meant to be a data format for kinematic, since the whole kinematic definition holds in joints. This class builds appropriate internal data structures on instanciation so that calls to solve() are fast and reproducible.
Realtime (meaning fixed time resolution) is not a target, but reliability and convenience to compute any sort of mechanical interactions between solids.
A kinematic is defined by its joints:
- each joint works using position variables, the list of all joint positions is called the
stateof the kinematic - each joint is a link between 2 solids (start, stop)
- each joint can provide a transformation matrix from its start solid to stop solid deduced from the joint position, as well as a gradient of this matrix
A kinematic problem is defined by
- the joints we fix (or solids we fix, but fixing a solid can be done using a joint)
- the joints who stay free, whose positions need to be deduced from the fixed joints
A list of joints can be seen as a graph of links between solids. The complexity of the kinematic probleme depends on
- the number cycles
- the degree of freedom
Examples:
>>> # keep few joints apart, so we can use them as dict keys, for calling `solve`
>>> motor1 = Revolute((0,2), Axis(...))
>>> motor2 = Revolute((0,7), Axis(...))
>>> free = Free((7,4))
>>> # the kinematic solver object
>>> kinematic = Kinematic([
... Revolute((0,1), Axis(...)),
... motor1,
... motor2,
... Planar((7,3), ...),
... Cylindrical((1,3), ...),
... Ball((3,2), ...),
... Prismatic((4,3), ...),
... Planar((1,5), ...),
... Planar((1,6), ...),
... Weld((7,5), mat4(...)),
... Weld((7,6), mat4(...)),
... free,
... ], ground=7)
defines a kinematic with the following graph
one can also define a kinematic with notions of direct and inverse transformations. The notion of direct and inverse is based on an input/output relation that we define as such:
- inputs is selection of joint coordinates
- outputs is a selection of solids poses
>>> kinematic = Kinematic([
... Revolute((0,1), Axis(...)),
... Planar((7,3), ...),
... Cylindrical((1,3), ...),
... Ball((3,2), ...),
... Prismatic((4,3), ...),
... Planar((1,5), ...),
... Planar((1,6), ...),
... Weld((7,5), mat4(...)),
... Weld((7,6), mat4(...)),
... ],
... ground = 7,
... inputs = [motor1, motor2],
... outputs = [4,5,6],
... )
Tip
If your kinematic is a chain of joints, then prefer using Chain to reduce the overhead of the genericity.
Note
A Kinematic doesn't tolerate modifications of the type of its joints once instanciated. joints positions could eventually be modified at the moment it doesn't affect the hash of the joints (See Joint)
Attributes:
| Name | Type | Description |
|---|---|---|
joints |
a list of each joint is a link between 2 solids, which are represented by a hashable object (it is common to designate these solids by integers, strings, or objects hashable by their id) |
|
content |
display object for each solid, this can be anything implementing the display protocol, and will be used only when this kinematic is displayed |
|
ground |
the reference solid, all other solids positions will be relative to it |
|
inputs |
a list of joints to fix when calling |
|
outputs |
a list of solids to fix when calling |
|
default |
the default joint pose of the kinematic |
|
increment |
the maximum change of joint pose before changing monotony of gradient |
|
bounds |
a tuple of (min, max) joint poses |
|
scale |
the reference distance scale used to adimension residuals |
Source code in madcad/kinematic/solver.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 | |
normalize(state)
¶
inplace normalize the joint coordinates in the given state
Source code in madcad/kinematic/solver.py
981 982 983 984 985 | |
direct(parameters, close=None)
¶
shorthand to self.solve(self.inputs) and computation of desired transformation matrices
it only works when direct and inverse constraining joints have been set
Source code in madcad/kinematic/solver.py
1026 1027 1028 1029 1030 1031 1032 1033 | |
inverse(parameters, close=None)
¶
shorthand to self.solve(self.outputs) and extraction of desired joints
it only works when direct and inverse constraining joints have been set
Source code in madcad/kinematic/solver.py
1035 1036 1037 1038 1039 1040 1041 1042 | |
grad(state, freedom=None)
¶
return a gradient of the all the solids poses at the given joints position
Note
this function will ignore any degree of freedom of the kinematic that is not defined in inputs
Source code in madcad/kinematic/solver.py
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | |
cycles()
¶
return a list of minimal cycles decomposing the gkinematic graph
Examples:
>>> len(kinematic.cycles())
5
Source code in madcad/kinematic/solver.py
753 754 755 756 757 758 759 760 761 | |
solve(fixed={}, close=None, precision=1e-06, maxiter=None, strict=0.0)
¶
compute the joint positions for the given fixed solids positions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fixed
|
dict
|
list of |
{}
|
close
|
list
|
the joint positions we want the result the closest to.
If not provided, |
None
|
precision
|
the desired precision tolerance for kinematic loops closing. this function returns once reached |
1e-06
|
|
strict
|
expected error tolerance on the kinematic loops closing, if not reached after |
0.0
|
|
maxiter
|
maximum number of iterations allowed, or None if not limit |
None
|
Return: the joint positions allowing the kinematic to have the fixed solids in the given poses Raise: KinematicError if no joint position can satisfy the fixed positions
Examples:
>>> # solve with no constraints
>>> kinematic.solve()
[...]
>>> # solve by fixing solid 4
>>> kinematic.solve({free: mat4(...)})
[...]
>>> # solve by fixing some joints
>>> kinematic.solve({motor1: radians(90)})
[...]
>>> kinematic.solve({motor1: radians(90), motor2: radians(15)})
[...]
Source code in madcad/kinematic/solver.py
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 | |
freedom(state, precision=1e-06)
¶
list of free movement joint directions. the length of the list is the degree of freedom.
Note
When state is a singular position in the kinematic, the degree of freedom is locally smaller or bigger than in other positions
Source code in madcad/kinematic/solver.py
987 988 989 990 991 992 993 994 | |
parts(state, precision=1e-06)
¶
return the pose of all solids in the kinematic for the given joints positions
The arguments are the same as for self.direct()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
precision
|
error tolerance for kinematic loop closing, if a loop is above this threshold this function will raise |
1e-06
|
Source code in madcad/kinematic/solver.py
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 | |
display(scene)
¶
display allowing manipulation of kinematic
Source code in madcad/kinematic/solver.py
1062 1063 1064 1065 | |
to_chain()
¶
Source code in madcad/kinematic/solver.py
763 764 765 766 | |
Joint(*args, default=0, **kwargs)
¶
A Joint constraints the relative position of two solids.
In this library, relative positioning is provided by transformation matrices which need a start-end convention, so every joint is directed and the order of self.solids matters.
There is two ways of defining the relative positioning of solids
- by a joint position :math:
(q_i) - by a start-end matrix :math:
T_{ab}
we can switch from one representation to the other using the direct and inverse methods.
Attributes:
| Name | Type | Description |
|---|---|---|
solids |
a tuple (start, end) or hashable objects representing the solids the joint is linking |
|
default |
a default joint position |
|
bounds |
a tuple of |
Source code in madcad/kinematic/solver.py
75 76 77 78 79 80 | |
normalize(state)
¶
make the given joint coordinates consistent. For most joint is is a no-op or just coordinates clipping
for complex joints coordinates containing quaternions or directions or so, it may need to perform normalizations or orthogonalizations or so on. This operation may be implemented in-place or not.
Source code in madcad/kinematic/solver.py
89 90 91 92 93 94 95 | |
direct(state)
¶
direct kinematic computation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
the parameters defining the joint state
It can be any type accepted by |
required |
Returns:
| Type | Description |
|---|---|
'mat4'
|
the transfer matrix from solids |
Source code in madcad/kinematic/solver.py
97 98 99 100 101 102 103 104 105 106 107 108 | |
inverse(matrix, close=None, precision=1e-06, maxiter=None, strict=0.0)
¶
inverse kinematic computation
the default implementation is using a newton method to nullify the error between the given and acheived matrices. For precision and efficiency it is better to implement an exact solution whenever possible especially if self.grad is using finite differentiation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
the transfer matrix from solids |
required | |
close
|
a know solution we want the result the closest to.
if not specified, it defaults to |
None
|
|
precision
|
desired error tolerance on the given matrix, this function returns once reached |
1e-06
|
|
strict
|
expected error tolerance on the given matrix, if not reached after |
0.0
|
|
maxiter
|
maximum number of iterations allowed, or None if not limit |
None
|
Returns:
| Type | Description |
|---|---|
list
|
the joint parameters so that |
Source code in madcad/kinematic/solver.py
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 | |
grad(state, delta=1e-06)
¶
compute the gradient of the direct kinematic
The default implementation is using a finite differentiation. For precision it is better to implement an exact solution in every joint
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
anything accepted by |
required | |
delta
|
finite differentiation interval |
1e-06
|
Returns:
| Type | Description |
|---|---|
'[mat4]'
|
a list of the matrix derivatives of |
Source code in madcad/kinematic/solver.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
scheme(size, junc=None)
¶
generate the scheme elements to render the joint
Source code in madcad/kinematic/solver.py
184 185 186 | |
display(scene)
¶
display showing the schematics of this joint, without interaction
Source code in madcad/kinematic/solver.py
188 189 190 191 192 193 | |
Weld(solids, transform=None)
¶
Bases: Joint
joint with no degree of freedom, simply welding a solid to an other with a transformation matrix to place one relatively to the other
It is useful to fix solids between each other without actually making it the same solid in a kinematic.
Source code in madcad/kinematic/solver.py
311 312 313 | |
bounds = ((), ())
class-attribute
instance-attribute
¶
increment = ()
class-attribute
instance-attribute
¶
default = ()
class-attribute
instance-attribute
¶
solids = solids
instance-attribute
¶
transform = transform or mat4()
instance-attribute
¶
direct(parameters)
¶
Source code in madcad/kinematic/solver.py
315 316 317 | |
inverse(matrix, close=None)
¶
Source code in madcad/kinematic/solver.py
319 320 | |
grad(parameters, delta=1e-06)
¶
Source code in madcad/kinematic/solver.py
322 323 | |
__repr__()
¶
Source code in madcad/kinematic/solver.py
325 326 | |
Free(solids)
¶
Bases: Joint
joint of complete freedom. it adds no effective constraint to the start and end solids. its parameter is its transformation matrix.
it is useful to control the explicit relative pose of solids in a kinematic.
Source code in madcad/kinematic/solver.py
335 336 | |
solids = solids
instance-attribute
¶
bounds = (np.array([-2] * 4 + [-inf] * 3, float), np.array([+2] * 4 + [+inf] * 3, float))
class-attribute
instance-attribute
¶
increment = np.array([0.5] * 4 + [inf] * 3, float)
class-attribute
instance-attribute
¶
default = np.array([1, 0, 0, 0, 0, 0, 0], float)
class-attribute
instance-attribute
¶
normalize(parameters)
¶
Source code in madcad/kinematic/solver.py
345 346 | |
direct(parameters)
¶
Source code in madcad/kinematic/solver.py
348 349 350 351 | |
inverse(matrix, close=None)
¶
Source code in madcad/kinematic/solver.py
353 354 | |
grad(parameters)
¶
Source code in madcad/kinematic/solver.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
__repr__()
¶
Source code in madcad/kinematic/solver.py
405 406 | |
Reverse(joint)
¶
Bases: Joint
that joint behaves like its wrapped joint but with swapped start and stop solids
Source code in madcad/kinematic/solver.py
414 415 416 417 418 | |
joint = joint
instance-attribute
¶
solids = joint.solids[::(-1)]
instance-attribute
¶
position = self.joint.position
instance-attribute
¶
default
property
¶
bounds
property
¶
direct(parameters)
¶
Source code in madcad/kinematic/solver.py
428 429 | |
inverse(matrix, close=None)
¶
Source code in madcad/kinematic/solver.py
431 432 | |
grad(parameters)
¶
Source code in madcad/kinematic/solver.py
434 435 436 437 438 439 440 441 | |
__repr__()
¶
Source code in madcad/kinematic/solver.py
443 444 | |
scheme(index, maxsize, attach_start, attach_end)
¶
Source code in madcad/kinematic/solver.py
446 447 | |
Chain(joints, content=None, default=None)
¶
Bases: Joint
Kinematic chain, This chain of joints acts like one only joint The new formed joint has as many degrees of freedom as its enclosing joints.
This class is often used instead of Kinematic when possible, because having more efficient inverse() and direct() methods dedicated to kinematics with one only cycle. It also has simpler in/out parameters since a chain has only two ends where a random kinematic may have many
A Chain doesn't tolerate modifications of the type of its joints once instanciated. a joint placement can be modified as long as it doesn't change its hash.
Attributes:
| Name | Type | Description |
|---|---|---|
joints |
list
|
joints in the chaining order
it must satisfy |
content |
list
|
displayables matching solids, its length must be |
solids |
tuple
|
the (start,end) joints of the chain, as defined in |
default |
the default joints positions |
|
increment |
the maximum change of joint positions before changin monotony of gradient |
|
bounds |
the (min,max) joints positions |
Source code in madcad/kinematic/solver.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
normalize(state)
¶
inplace normalize the joint coordinates in the given state
Source code in madcad/kinematic/solver.py
491 492 493 494 495 | |
direct(state)
¶
Source code in madcad/kinematic/solver.py
497 498 499 500 501 | |
grad(state)
¶
the jacobian of the flattened parameters list
Source code in madcad/kinematic/solver.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
parts(state)
¶
return the pose of each solid in the chain
Source code in madcad/kinematic/solver.py
525 526 527 528 529 530 | |
to_kinematic()
¶
Source code in madcad/kinematic/solver.py
551 552 | |
to_dh()
¶
denavit-hartenberg representation of this kinematic chain.
it also returns the solids base definitions relative to the denavit-hartenberg convention, it the joints already follows the conventions, these should be eye matrices
Source code in madcad/kinematic/solver.py
554 555 556 557 558 559 560 | |
from_dh(dh, transforms=None)
¶
build a kinematic chain from a denavit-hartenberge representation, and eventual base definitions relative to the denavit-hartenberg convention
Source code in madcad/kinematic/solver.py
562 563 564 | |
display(scene)
¶
display allowing manipulation of the chain
Source code in madcad/kinematic/solver.py
569 570 571 572 | |
arcs(conn)
¶
find ars in the given graph
Source code in madcad/kinematic/solver.py
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 | |
depthfirst(conn, starts=())
¶
generate a depth-first traversal of the givne graph
Source code in madcad/kinematic/solver.py
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 | |
cycles(conn)
¶
extract a set of any-length cycles decomposing the graph
Source code in madcad/kinematic/solver.py
1111 1112 1113 | |
shortcycles(conn, costs, branch=True)
¶
extract a set of minimal cycles decompsing the graph
Source code in madcad/kinematic/solver.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 | |
displays
¶
__all__ = ['ChainManip', 'KinematicManip', 'scale_solid', 'world_solid']
module-attribute
¶
index_toolcenter = 10000
module-attribute
¶
kinematic_color_names = ['annotation', 'schematic']
module-attribute
¶
ChainManip(scene, chain, pose=None, toolcenter=None)
¶
Bases: Group
object to display and interact with a robot in the 3d view
Attributes:
| Name | Type | Description |
|---|---|---|
chain |
the kinematic chain this display is rendering |
|
pose |
joints poses in the last rendered frame |
|
parts |
solids poses in the last rendered frame |
|
toolcenter |
vec3
|
current end-solid rotation point in rotation mode, relative to last solid |
Source code in madcad/kinematic/displays.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
min_colinearity = 0.01
class-attribute
instance-attribute
¶
maxiter = 3
class-attribute
instance-attribute
¶
prec = 1e-06
class-attribute
instance-attribute
¶
chain = chain
instance-attribute
¶
toolcenter = toolcenter or chain.joints[-1].position[1]
instance-attribute
¶
pose = pose or chain.default
instance-attribute
¶
parts = self.chain.parts(self.pose)
instance-attribute
¶
defered = DeferedSolving()
instance-attribute
¶
stack(scene)
¶
rendering stack requested by the madcad rendering system
Source code in madcad/kinematic/displays.py
64 65 66 67 | |
control(view, key, sub, evt)
¶
user event manager, optional part of the madcad rendering system
Source code in madcad/kinematic/displays.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
KinematicManip(scene, kinematic, pose=None, toolcenter=None)
¶
Bases: Group
Display that holds a kinematic structure and allows the user to move it
Attributes:
| Name | Type | Description |
|---|---|---|
kinematic |
the kinematic this display is rendering |
|
pose |
joints poses in the last rendered frame |
|
parts |
solids poses in the last rendered frame |
|
toolcenter |
current solid rotation point in rotation mode, relative to kinematic ground |
Source code in madcad/kinematic/displays.py
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 | |
prec = 1e-06
class-attribute
instance-attribute
¶
move_maxiter = 5
class-attribute
instance-attribute
¶
stay_maxiter = 10000
class-attribute
instance-attribute
¶
move_precision = 0.0001
class-attribute
instance-attribute
¶
stay_precision = 1e-06
class-attribute
instance-attribute
¶
tolerated_precision = 0.1
class-attribute
instance-attribute
¶
damping = 0.9
class-attribute
instance-attribute
¶
kinematic = kinematic
instance-attribute
¶
defered = DeferedSolving()
instance-attribute
¶
toolcenter = toolcenter or vec3(0)
instance-attribute
¶
pose = self.kinematic.solve(close=(pose or self.kinematic.default), maxiter=(self.stay_maxiter), precision=(self.move_precision), strict=(self.tolerated_precision))
instance-attribute
¶
parts = self.kinematic.parts(self.pose, precision=(self.tolerated_precision))
instance-attribute
¶
stack(scene)
¶
rendering stack requested by the madcad rendering system
Source code in madcad/kinematic/displays.py
305 306 307 308 309 310 311 312 313 314 | |
control(view, key, sub, evt)
¶
user event manager, optional part of the madcad rendering system
Source code in madcad/kinematic/displays.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
DeferedSolving()
¶
helper executing solver iterations following ticks of a QTimer
Source code in madcad/kinematic/displays.py
595 596 597 | |
timer = None
instance-attribute
¶
problem = None
instance-attribute
¶
set(prepare, solve, iterations)
¶
schedule solving steps starting now for the given count of iterations
Source code in madcad/kinematic/displays.py
598 599 600 601 602 603 604 605 606 | |
stop()
¶
stop iterations
Source code in madcad/kinematic/displays.py
607 608 609 | |
DeferedProblem(prepare, solve, iterations)
dataclass
¶
scale_solid(solid, center, size, pose=fmat4())
dataclass
¶
scheme space scaling around a point in a given solid
solid
instance-attribute
¶
center
instance-attribute
¶
size
instance-attribute
¶
pose = fmat4()
class-attribute
instance-attribute
¶
__call__(view)
¶
Source code in madcad/kinematic/displays.py
643 644 645 646 647 | |
world_solid(solid, pose=fmat4())
dataclass
¶
kinematic_toolcenter(toolcenter)
¶
create a scheme for drawing the toolcenter in kinematic manipulation
Source code in madcad/kinematic/displays.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
kinematic_scheme(joints)
¶
create a kinematic scheme for the given joints
Source code in madcad/kinematic/displays.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
kinematic_color(i)
¶
return the scheme color vector for solid i in a kinematic
Source code in madcad/kinematic/displays.py
716 717 718 | |
qtpos(qtpos, view)
¶
convert qt position in the widget to opengl screen coords in range (-1, 1)
Source code in madcad/kinematic/displays.py
722 723 724 725 726 727 | |
normsq(x)
¶
Source code in madcad/kinematic/displays.py
729 730 731 | |