johnwhall / kos-scripts

Other
10 stars 2 forks source link

`lambertsProblem` returning burns that do not intercept target at all. #2

Open npyoung opened 8 years ago

npyoung commented 8 years ago

I'm trying to use your kerbalscript Lambert solver implementation to emulate Mechjeb's "intercept target at time". But I'm getting burns that don't even come close to intercepting my target, even when I start from a close encounter. I debugged as well as I could, but I can't rule out that it's just my use of the function. See for yourself:

run once liblambert.

// Choose burn and arrival times.
set t0 to time:seconds + 60.     // burn time
set t1 to time:seconds + 300.   // arrival time
set dt to t1 - t0.

// Get SOI-centered positions of me and my target at burn and arrival respectively.
set me_at_t0 to positionat(ship, t0) - ship:body:position.
set target_at_t1 to positionat(target, t1) - ship:body:position.

// Use the Lambert solver
set result to lambertsProblem(ship:body:mu, me_at_t0, target_at_t1, dt, 0, 1).
if result:length > 0 {
    set result0 to result[0][0].

    // Solver result is in absolute velocity. Figure out how much you need to burn to adjust velocity.
    set dv to result0 - velocityat(ship, t0):orbit.
}

add node(t0, dv:x, dv:y, dv:z). 

When starting in a situation where I already have a close encounter in 300 seconds, this gives me burns that cause me to completely miss my target and that are 100s of m/s greater than what MechJeb suggests.

If you have an example of how you used lambertsProblem pre-mainframe, or a test case where the resulting burn dV is known, that would help a lot. Thanks.

johnwhall commented 7 years ago

I know it's been a long time since you asked this, but just in case you are still around or someone else has the same question, here are some things to look at:

  1. I wouldn't trust positionat(...) - ship:body:position to give vectors that are suitable for the Lambert solver. It's probably giving you vectors with a weird rotation. Check out lib/liblambertoptimizer.ks for an example of how to get the correct vectors.
  2. Same thing for the resulting velocity.
  3. I should get rid of the kerboscript Lambert solver because it's too slow and I never used it enough to know if it has bugs.
  4. Use the mainframe instead :) That way, you can optimize the burn and arrival times to minimize the delta-v needed. You can do this with the kerboscript solver, but it'll be much slower.