Multibody Dynamics with
SymPy and PyDy
Jim Crist
(Or what I did this summer in 30 slides or less)
Slide 2
Slide 2 text
What is SymPy???
• A Computer Algebra System (CAS) written in pure Python
• Allows you to create expressions, and do things with them
• SymPy knows about all the math needed
Slide 3
Slide 3 text
Create Expressions
>>> from sympy import *
# Create Symbols
>>> a, b, c = symbols('a, b, c')
# Combine Symbols into expressions
>>> expr = sin(a) + cos(b**2)*c
>>> expr
sin(a) + cos(b**2)*c
Slide 4
Slide 4 text
Manipulate Expressions
# subs performs substitutions on expressions
>>> expr = expr.subs(b, a**(1/2))
>>> expr
sin(a) + sin(a)*c
# simplify reduces the size of expressions
>>> expr = expr.simplify()
>>> expr
sin(a)*(1 + c)
Slide 5
Slide 5 text
Solve Expressions
# Create another symbol x
>>> x = symbols('x')
# Quadratic equation
>>> lhs = a*x**2 + b*x + c
>>> expr = Eq(lhs, 0)
>>> expr
a*x**2 + b*x + c == 0
# Solve returns solutions to expressions
>>> solve(expr, x)
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
Slide 6
Slide 6 text
Submodules (just a few…)
• Calculus
• Linear Algebra
• Set Theory
• Combinatorics
• Differential Geometry
• Tensors
• Classical Mechanics
• etc…
Slide 7
Slide 7 text
Submodules (just a few…)
• Calculus
• Linear Algebra
• Set Theory
• Combinatorics
• Differential Geometry
• Tensors
• Classical Mechanics
• etc…
What this talk
is about
Slide 8
Slide 8 text
Classical Mechanics
• Describes the motion of macroscopic objects
• Concerned with solving
= = −1 ∙
• Many ways to do so:
• Newton
• Lagrange
• Kane
Slide 9
Slide 9 text
A VERY BRIEF OVERVIEW OF
MULTIBODY
DYNAMICS
Slide 10
Slide 10 text
Vectors & Reference Frames
Slide 11
Slide 11 text
Vectors & Reference Frames
Slide 12
Slide 12 text
Generalized Coordinates & Speeds
Slide 13
Slide 13 text
Constraints
• If coordinates and speeds are nonminimal, need constraints
• Holonomic
• Relations between coordinates
• 1
,2
, 3
, … ,
, = 0
• Nonholonomic
• “Not-holonomic” (i.e. cannot be put in form above)
Slide 14
Slide 14 text
Constraints
• Pendulum has 1 DOF, but 2
coordinates
• Length is always
•
1
, 2
, = 1
2 + 2
2 − 2 = 0
Slide 15
Slide 15 text
Formulations
• Lagrange
• Enforces constraints with Lagrange Multipliers ()
• Energy based formulation
• Kane
• Avoids need to compute energy equations
• Uses generalized forces (
and
∗)
• Can be simpler to compute for large systems
Equations of Motion
Can be rearranged to form:
(, )
= (, , , ,)
or
= , −1(, , ,, )
Slide 18
Slide 18 text
Classical Mechanics
• Inverted Pendulum, 1 Link
Slide 19
Slide 19 text
Classical Mechanics
• Inverted Pendulum, 3 Links: Rendered in size 2.5 font
Slide 20
Slide 20 text
DERIVE
EQUATIONS
IN SYMPY
Solution:
Slide 21
Slide 21 text
github.com/pydy/pydy
Slide 22
Slide 22 text
Stuff I Did…
Slide 23
Slide 23 text
Linearization
• For systems with constraints, need to be careful
• Can’t just take jacobian, as states are interdependent
• i.e. 1
, , instead of 1
()
Slide 24
Slide 24 text
Example – Single Link Pendulum
• Derived EOM for minimal, and nonminimal formulations
Slide 25
Slide 25 text
Example – Single Link Pendulum
Response for small angle deviation - simple jacobian linearization
Slide 26
Slide 26 text
Example – Single Link Pendulum
Response for small angle deviation - simple jacobian linearization
Wrong…
Slide 27
Slide 27 text
Linearization
• Derived and implemented algorithm to robustly linearize all cases
• Works for formulations from Kane or Lagrange
• Handles holonomic and nonholonomic constraints
• Applied to Whipple bicycle model [1], matches to 14 digits the
benchmark eigenvalues
[1] Meijaard, J.P., Papadopoulos, J.M., Ruina, A., Schwab, A.L.: Linearized dynamics
equations for the balance and steer of a bicycle: a benchmark and review.
Proceedings of the Royal Society A: Mathematical, Physical and Engineering
Sciences 463(2084), 1955–1982(2007)
Other Stuff
• Rewrote solver – 100x speed boost for (some) benchmarks
• Improvements to substitution routines
• Now () not (2)
• Bug fixes for lots of things
• Documentation
Slide 30
Slide 30 text
Demo
(try it at github.com/jcrist/talks/tree/master/pydy_talk)
Slide 31
Slide 31 text
Why?
• Open Source
• Other symbolic solvers (AutoLev, SD/FAST) are proprietary
• Fast
• Slower generation than numeric methods
• Faster simulation
• Correct
• No math errors for hand-calculated EOM
• Automatic translation to C/Fortran -> no typing errors
Slide 32
Slide 32 text
Future Work
• O(n) articulated methods
• Featherstone/Jain
• Finish work on LAPACK/BLAS/ODEPACK integration
• Will remove python layer bottleneck for simulation
• Integration with CasADi