Upgrade to Pro — share decks privately, control downloads, hide ads and more …

kerbal_space_program.rb

listrophy
October 27, 2017

 kerbal_space_program.rb

So there's this game, Kerbal Space Program. You basically create your own space program from scratch. Well, you can control it via TCP, and—using Ruby—we're gonna launch, orbit, and crash our brave Kerbins. You might even learn a thing or two about orbital mechanics and control theory along the way.

listrophy

October 27, 2017
Tweet

More Decks by listrophy

Other Decks in Programming

Transcript

  1. > kerbal_space_program.rb
    ~brad_grzesiak/keep_ruby_weird | 2017-10-27 14:30
    ~brad_grzesiak/keep_ruby_weird | 2017-10-27 14:29
    > groups
    brad_grzesiak wheel bendyworks twitter.listrophy

    View full-size slide

  2. Whenever Brad is
    drinking, you wanna
    make sure you're sitting
    next to him, because
    instead of being
    obnoxious, he teaches
    you orbital mechanics.
    Ashe Dryden

    View full-size slide

  3. Whenever Brad is
    drinking, you wanna
    make sure you're sitting
    next to him, because
    instead of being
    obnoxious, he teaches
    you orbital mechanics.
    Ashe Dryden
    GET YOUR
    ALTERCONF
    TICKETS!

    View full-size slide

  4. PREFACE
    PREREQUISITES

    View full-size slide

  5. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)

    View full-size slide

  6. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    $24

    View full-size slide

  7. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    ▸ Install ckan
    $24

    View full-size slide

  8. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    ▸ Install ckan
    ▸ Install kRPC on ckan
    $24

    View full-size slide

  9. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    ▸ Install ckan
    ▸ Install kRPC on ckan
    ▸ EMA 550: Astrodynamics
    $24

    View full-size slide

  10. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    ▸ Install ckan
    ▸ Install kRPC on ckan
    ▸ EMA 550: Astrodynamics
    ▸ ME 446: Automatic Controls
    $24

    View full-size slide

  11. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    ▸ Install ckan
    ▸ Install kRPC on ckan
    ▸ EMA 550: Astrodynamics
    ▸ ME 446: Automatic Controls
    ▸ Install ruby correctly #%&!
    $24

    View full-size slide

  12. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    ▸ Install ckan
    ▸ Install kRPC on ckan
    ▸ EMA 550: Astrodynamics
    ▸ ME 446: Automatic Controls
    ▸ Install ruby correctly #%&!
    ▸ gem install krpc
    $24

    View full-size slide

  13. PREFACE
    PREREQUISITES
    ▸ Buy KSP ($40 on Steam)
    ▸ Install ckan
    ▸ Install kRPC on ckan
    ▸ EMA 550: Astrodynamics
    ▸ ME 446: Automatic Controls
    ▸ Install ruby correctly #%&!
    ▸ gem install krpc
    ▸ Write some ruby
    $24

    View full-size slide

  14. SPACE.FAIL:3000

    View full-size slide

  15. PROJECT 1:
    MERCURY
    HOVER

    View full-size slide

  16. MAKE A ROCKET
    HOVER
    PROBLEM STATEMENT:

    View full-size slide

  17. MAKE A ROCKET
    HOVER
    PROBLEM STATEMENT:
    MAKE ALTITUDE CONSTANT
    (AND SUFFICIENTLY >0)

    View full-size slide

  18. MAKE A ROCKET
    HOVER
    PROBLEM STATEMENT:
    MAKE ALTITUDE CONSTANT
    (AND SUFFICIENTLY >0)
    1 CONTROL: THROTTLE
    (ALSO, VARIABLE MASS)

    View full-size slide

  19. PROJECT 1: MERCURY
    SOME MATHS
    F = m⋅a

    T − m⋅ g = m⋅a
    T − m⋅ g = m⋅ d
    dt
    v
    T − m⋅ g = m⋅ d2
    dt2
    x
    T(t) − m(t)⋅ g = m(t)⋅ d2
    dt2
    x(t)

    View full-size slide

  20. PROJECT 1: MERCURY
    SOME MATHS
    F = m⋅a

    T − m⋅ g = m⋅a
    T − m⋅ g = m⋅ d
    dt
    v
    T − m⋅ g = m⋅ d2
    dt2
    x
    T(t) − m(t)⋅ g = m(t)⋅ d2
    dt2
    x(t)
    Nonlinear 2nd Order

    Differential Equation

    View full-size slide

  21. PROJECT 1: MERCURY
    SOME MATHS
    F = m⋅a

    T − m⋅ g = m⋅a
    T − m⋅ g = m⋅ d
    dt
    v
    T − m⋅ g = m⋅ d2
    dt2
    x
    T(t) − m(t)⋅ g = m(t)⋅ d2
    dt2
    x(t)
    Nonlinear 2nd Order

    Differential Equation
    ಠ_ಠ

    View full-size slide

  22. PID
    CONTROLLER

    View full-size slide

  23. P
    I
    D
    CONTROLLER
    roportional
    ntegral
    erivative

    View full-size slide

  24. P
    I
    D
    CONTROLLER
    roportional
    ntegral
    erivative

    View full-size slide

  25. PROJECT 1: MERCURY
    NEGATIVE FEEDBACK
    THROTTLE

    View full-size slide

  26. PROJECT 1: MERCURY
    NEGATIVE FEEDBACK
    THROTTLE ROCKET

    View full-size slide

  27. PROJECT 1: MERCURY
    NEGATIVE FEEDBACK
    ACTUAL
    ALTITUDE
    THROTTLE ROCKET

    View full-size slide

  28. PROJECT 1: MERCURY
    NEGATIVE FEEDBACK
    ACTUAL
    ALTITUDE
    THROTTLE ROCKET
    PID
    CONTROLLER
    DESIRED
    ALTITUDE

    View full-size slide

  29. PROJECT 1: MERCURY
    NEGATIVE FEEDBACK
    ACTUAL
    ALTITUDE
    THROTTLE ROCKET
    PID
    CONTROLLER
    DESIRED
    ALTITUDE

    View full-size slide

  30. PROJECT 1: MERCURY
    PID CONTROLLER
    prev_time = Time.now
    dt = 0.1
    prev_error = 0
    accumulated_error = 0
    while true do
    sleep dt
    curr_time = Time.now
    error = DESIRED_ALTITUDE - current_altitude()
    accumulated_error += error * dt
    derivative = (error - prev_error) / dt
    set_throttle(
    P * error +
    I * accumulated_error +
    D * derivative
    )
    prev_error, prev_time = error, curr_time
    end

    View full-size slide

  31. PROJECT 2:
    GEMINI
    ORBIT
    Photo courtesy of: SpaceX

    View full-size slide

  32. MAKE A ROCKET
    ORBIT
    PROBLEM STATEMENT:

    View full-size slide

  33. MAKE A ROCKET
    ORBIT
    PROBLEM STATEMENT:
    GO REAL HIGH,
    THEN ON THE WAY BACK DOWN,
    MISS

    View full-size slide

  34. First Stage
    OUR ROCKET

    View full-size slide

  35. OUR ROCKET
    Second Stage
    }

    View full-size slide

  36. OUR ROCKET
    Third Stage
    }

    View full-size slide

  37. ORBIT?
    1 foot
    Austin
    Houston

    View full-size slide

  38. ORBIT?
    1 foot
    Austin
    Houston
    d = 237km

    View full-size slide

  39. ORBIT?
    1 foot
    Austin
    Houston
    d = 237km
    h = 4.41km

    View full-size slide

  40. ORBIT?
    1 foot
    Austin
    Houston
    d = 237km
    h = 4.41km
    t = 30s

    View full-size slide

  41. ORBIT?
    1 foot
    Austin
    Houston
    d = 237km
    h = 4.41km
    t = 30s
    v = 7.9km/s

    View full-size slide

  42. ENERGIES
    Get up there
    m g h = E
    g h = E/m
    (9.81/1000) 200
    1.96 MJ

    View full-size slide

  43. ENERGIES
    Get up there
    m g h = E
    g h = E/m
    (9.81/1000) 200
    1.96 MJ
    Go real fast
    E = ½ m v2
    E/m = ½ v2
    ½ (7.8)2
    30.4 MJ

    View full-size slide

  44. PROJECT 2: GEMINI
    OK, BUT HOW?
    ▸ Checklist!
    ▸ 2 state machines: staging & control
    ▸ On each tick, use case-when for each state machine
    ▸ On each state-transition, maybe do a single thing
    ▸ "Pitch over while managing stages"

    View full-size slide

  45. PROJECT 3:
    APOLLO
    TO THE MUN!

    View full-size slide

  46. LAND A ROCKET
    ON THE MUN
    PROBLEM STATEMENT:

    View full-size slide

  47. LAND A ROCKET
    ON THE MUN
    PROBLEM STATEMENT:
    LANDING SPEED UNSPECIFIED;
    LEFT TO DEVELOPER

    View full-size slide

  48. ORBIT TRANSFERS

    View full-size slide

  49. ORBIT TRANSFERS

    View full-size slide

  50. ORBIT TRANSFERS

    View full-size slide

  51. ORBIT TRANSFERS

    View full-size slide

  52. PROJECT 3: APOLLO
    STATES
    1. determining insertion burn delta v
    2. determining insertion burn location
    3. waiting for insertion burn far
    4. waiting for insertion burn mid
    5. waiting for insertion burn near
    6. insertion burning
    7. finalizing insertion burn
    8. transmunar orbit
    9. outer munar orbit
    10.mid munar orbit
    11.final approach

    View full-size slide

  53. THANK YOU!
    Brad Grzesiak - @listrophy
    CEO, Bendyworks

    View full-size slide