# KOS Beginner Help

## Recommended Posts

Happy Holidays to everyone! Apologies if this is not the correct forum. If so, please let me know where to post my questions on this topic.

I've been playing KSP for a while and love it. I recently started to try to learn KOS, but I have zero coding knowledge, so am trying to learn.

I'm trying to start with a simple script that will make my ship pitch over 1 degree every 1000m starting at 900m. Once the ship reaches about 80000m this will stop.

I got it to work by writing a line of code for each 1 degree change, but it was annoying to have to write that 80 times. I know there must be a way to automate this but have not figured it out yet. This is what I'm trying right now. After the ship lifts off the code is:

{
set X to 90. // Sets pitch
set A to 900. // Sets altitude

when ship:altitude > A then lock steering to heading (90,X-1).

when ship:altitude = A + 1000 then loop.

until ship:altitude > 79000 lock steering to heading (90,10).
}

It's not working the way I'd hoped. It starts to pitch over right away and really fast. It's nose is abot ten degrees above the horizon by the time it reaches a kilometer in altitude.

Thanks in advance for any help.

##### Share on other sites

This:

```until ship:altitude > 79000 lock steering to heading (90,10).
```

is overriding everything else you're doing.  It's continually setting the steering to heading(90,10) regardless of all the work the whens are doing.

Also, don't do that.  Every time you re-lock steering you make the in-build PID controller start over, making it forget the accumulated integral term.

This is better:

```lock steering to heading(90,p).
until ship:altitude > 79000 {
set p to (put something here to decide what pitch you want).
wait 0.001.
}```

##### Share on other sites

haven't had much experience steering a rocket with kOS but this is a nice way for a poor man's gravity turn, just wrote up something simple and similar and worked well. Good start! Other places to ask questions are the kOS thread or reddit

Edited by Gaiiden

##### Share on other sites

Thanks guys.

STEVEN - I feel thick, but still cannot get it to work. Are you saying the rest of the script above

`until ship:altitude > 79000 lock steering to heading (90,10).`

is OK?

If I replace that with your code and set p to 10, it starts pitching over immediately after liftoff. If I set to 90 is stays straight up, but never starts to pitch over 1 degree at a time starting at 900m. Again, this is all really new to me and I appreciate your help.

##### Share on other sites

run this:

```set p to 89.
set alt to 900.
until ship:altitude > 80000 {
// will not drop into this if statement until 1900m
if ship:altitude - alt > 1000 {
// reset alt so it takes another 1000m to trigger
set alt to ship:altitude.
// because steering is locked (not set), we only have to change p to change the steering command
set p to p - 1.
// must allow a physics tick to pass
wait 0.001.
}
}```

##### Share on other sites

GAIDEN

Crash on liftoff!

----

Cannot perform the operation - on structure kOS.

Suffixed.VesselAlt and System.Double

At kopernicuslaunch3 on archive line 69

if ship:altitude - alt > 1000 {

----

Help...

##### Share on other sites

so, seems

`if ship:altitude - alt > 1000`

should be + alt, not - ?

Changed that and at least got liftoff, but the ship pitched over way too fast, and began to randomly make dramatic pitch and yaw adjustments. It was not able to recover and fly straight up.

##### Share on other sites

It should be -, not +.

The idea is that as the altitude keeps increasing, alt remains as last set, so the difference between them will only vary from 0 to 1000.  Once it hits 1000, alt is increased another tick (setting the difference to 0 again), and the cycle repeats.

If you make it +, then it responds to the sum, not the difference, which only increases.  With alt set to 900, that sum would pass 1000 when the ship hit one hundred meters (100 + 900 = 1000), never reset, and then the pitchover code would run continuously, which is apparently what you observed.

I think this line might be a problem:

`set alt to ship:altitude.`

This just makes alt equal to the ship's altitude.  The difference is always zero, so it never runs the pitchover loop again, and you go straight up into space.

`set alt to alt + 1000.`

And see whether that helps.

##### Share on other sites

Wait...should:

if ship:altitude - alt > 1000

be

if ship:altitude = alt + 1000

?

##### Share on other sites
6 hours ago, Zhetaan said:

The idea is that as the altitude keeps increasing, alt remains as last set, so the difference between them will only vary from 0 to 1000.  Once it hits 1000, alt is increased another tick (setting the difference to 0 again), and the cycle repeats.

If you make it +, then it responds to the sum, not the difference, which only increases.  With alt set to 900, that sum would pass 1000 when the ship hit one hundred meters (100 + 900 = 1000), never reset, and then the pitchover code would run continuously, which is apparently what you observed.

I think this line might be a problem:

```
set alt to ship:altitude.```

This just makes alt equal to the ship's altitude.  The difference is always zero, so it never runs the pitchover loop again, and you go straight up into space.

the difference is not always zero, because it is only reset when the difference is greater than 1000. It is not locked to ship:altitude, so the value remains the altitude of the last 1000 pass while the ship's current altitude continues to increase. It's the same way you reset a timer you want to fire every x number of seconds:

```abort off.
set running to true.
set currTime to floor(time:seconds).
on abort { set running to false. }.
until not running {
wait until time:seconds - currTime > 1.
set currTime to floor(time:seconds).
// do stuff once a second
print currTime.
}.```
8 hours ago, OzEtkin said:

GAIDEN

Crash on liftoff!

----

Cannot perform the operation - on structure kOS.

Suffixed.VesselAlt and System.Double

At kopernicuslaunch3 on archive line 69

if ship:altitude - alt > 1000 {

----

Help...

I gave you that code straight from my brain and wasn't able test it in KSP for any errors. I derped and forgot that alt is a special suffix and therefore you can't use it as a variable name. Change it to something else and you should be fine - I just ran it in KSP and it works as advertised.

BTW if you want the ship to start pitching over at 900m instead of 1900m just set your initial value for whatever you call alt to -100

Edited by Gaiiden

##### Share on other sites

Whoops, my mistake.  I blame too much Christmas cheer.

##### Share on other sites

Thanks for all the help! The script is running perfectly now.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.