⇠ Back to the gallery

Pogo point masses

This example shows how to use templates and the event system in codyn to implement simple hybrid dynamical systems. In this case, a template is defined named "pogopoint", which defines the dynamics of a bouncing point mass. In the air, the point mass is subject only to gravity, while on the ground it acts as if compressing a spring.

Events are used to determine when to transition between air and ground states. Differential equations can be specified to only be active in a certain state, as shown on line 31.

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
integrator {
  method = "runge-kutta"
}

templates {
  node "pogopoint" {
    initial-state "air"

    pogolength = 0.1
    bounced = 0

    y = 1
    m = 1
    K = 1000
    D = 1

    # Spring force of the ball when it is being
    # compressed
    fspring = "K * (pogolength - y)"

    # Damping force of the ball when it is being
    # compressed
    fdamping = "-D * y'"

    # Acceleration of y due to gravity
    y'' = "-g"

    # Acceleration of y due to the spring and damping
    # force. This term is only active when the pogo
    # stick is in contact with the ground
    y'' = "(fspring + fdamping) / m" state "ground"

    # Transfer from the air to the ground when y becomes
    # smaller than the pogo stick length
    event "air" to   "ground"
                when "y < pogolength" within 0.001 {
      # Keep track of the number of times we bounced
      set bounced = "bounced + 1"
    }

    # Transfer from ground to air when y becomes larger
    # than the pogo stick length
    event "ground" to   "air"
                   when "y > pogolength" within 0.001 {}
  }
}

g = 9.81

node "p{1:3}" : pogopoint {
  y = "rand(1, 3)"
  m = [0.6, 0.3, 0.4]
  K = [1200, 500, 600]
  D = [1, 1.5, 1.8]
}

⌦ Open in playground