Jump to content

[1.8.x to 1.12.x] kRPC: Control the game using C#, C++, Java, Lua, Python, Ruby, Haskell, C (Arduino)... (v0.5.2, 18th March 2023)


djungelorm

Recommended Posts

33 minutes ago, FumbeiNumbie said:

Hi! Not sure if I can help you with your question but I wonder what you mean by normal gravity turn. Especially compared to actual gravity turn.

My script gradually turns to 45 degrees before 12 km, then it continues turning and flattens out right at apogee. This approach is far from real gravity turn. If only I could find an equation that would describe ships trajectory. Then locking autopilot's pitch and heading to alpha and 90 (where alpha is defined by the equation) would be a relatively easy thing. Meh, if only...

well what i do is when the rocket reaches 100m/s~ i tip it to about 80~ degrees and disable controls, gravity then pulls the vessel down in such a way i get a nice turn into space, which starts at 100km with low earth orbits at 200km~ since i use "real space"

Edited by logaritm
Link to comment
Share on other sites

3 minutes ago, logaritm said:

well what i do is when the rocket reaches 100m/s~ i tip it to about 80~ degrees and disable controls controls, gravity then pulls the vessel down in such a way i get a nice turn into space, which starts at 100km with low earth orbits at 200km~ since i use "real space"

But you still want autopilot to check if you are on optimal trajectory, right? If that's the case I would imagine something that sets the course much like my script does, measures the angle between your ship's velocity vector and desired vector and takes action if the angle is too big. But if you do that, it's just easier to let the autopilot fly the ship from the beginning. So we go back to the best trajectory. Something tells me it is a brachistochrone curve. But I won't go further.

Link to comment
Share on other sites

19 minutes ago, FumbeiNumbie said:

But you still want autopilot to check if you are on optimal trajectory, right? If that's the case I would imagine something that sets the course much like my script does, measures the angle between your ship's velocity vector and desired vector and takes action if the angle is too big. But if you do that, it's just easier to let the autopilot fly the ship from the beginning. So we go back to the best trajectory. Something tells me it is a brachistochrone curve. But I won't go further.

when i was reading around about optimal accent paths pretty much all i could find said that they are very vessel dependent, i don't know how true it is but somewhere it said that NASA calculate the optimal accent path by pretty much just brute forcing it, anyway the conclusion i drew from it all was that doing the "basic" definition of the gravity turn was a good start

Edited by logaritm
Link to comment
Share on other sites

1 minute ago, logaritm said:

when i was reading around about optimal accent paths pretty much all i could find said that they are very vessel dependent, i don't know how true it is but somewhere it said that NASA calculate the optimal accent path by pretty much just brute forcing it, anyway the conclusion i drew from it all was that doing the "true" definition of the gravity turn was a good start

I used gravity turn in kOS at first. But I got inconsistent results with different ships so I started using hardwired solutions. 

On another topic: has anyone tried to make a windows forms app for KRPC in C#? Got any decent results?

Link to comment
Share on other sites

I was going to suggest the same - you could still let autopilot check and maintain pitch.   If your rocket 'wants' to fly the curve then it should take very little control input to stay on course, but guidance checks wouldn't be a bad thing either way!  

But you'd have to mathematically describe your ideal curve.   Which is the sort of thing KRPC is great at!   You could log pitch, altitude and velocity for several typical launcher configurations flying without guidance and then try to derive an equation to describe them!    For my demo script here https://github.com/krpc/krpc-library/tree/master/Art_Whaleys_KRPC_Demos  I just used a quadratic shape... and it's not terrible... but I'd love to upgrade to something more efficient!  

If you really do want to just let pitch to it's thing while AP controls yaw and roll, then you can just set the pitch PID's to zero so there's no control authority.   Here's a super quick and dirty example of that I hacked together.  Things that aren't totally obvious -  The last 10 lines or so are just there to re-enable autotune every 10 seconds and then disable it again.  That lets the autopilot keep itself tuned correctly as mass and control torque change over time.  I don't know that it's totally necessary, but it seemed like a good idea.

#Configure ascent parameters
HEADING = 90  
ROLL = 0
ALT = 100000

import time
import krpc

#Setup KRPC
conn = krpc.connect()
sc = conn.space_center
v = sc.active_vessel

#Configure initial Autopilot values
ap = v.auto_pilot
ap.target_pitch_and_heading(90, HEADING)
ap.target_roll = ROLL
ap.engage()

#Launch
v.control.activate_next_stage()  # Engine Startup

#Vertical Ascent
while(v.flight(v.orbit.body.reference_frame).vertical_speed < 100):
    time.sleep(.1)
    
#Pitch Over
ap.target_pitch_and_heading(80, HEADING)
time.sleep(10)  #ap.wait() ought to work here... but it was never unblocking so I cheated for this quick test

#Disable pitch guidance
ap.auto_tune = False
ap.pitch_pid_gains = (0,0,0)

#Retune Yaw and Roll AP every 10 seconds
nextcalibration = time.time() + 10
while(v.orbit.apoapsis_altitude < ALT):
    if(time.time() > nextcalibration):
        ap.target_pitch = v.flight(v.surface_reference_frame).pitch #Lock AP to current Pitch
        ap.auto_tune = True  # Auto tone all controllers
        time.sleep (.1)
        ap.auto_tune = False
        ap.pitch_pid_gains = (0,0,0) #Release Pitch control again
        nextcalibration = time.time() + 10
    time.sleep(.1)

v.control.throttle = 0.0  #MECO

 

Link to comment
Share on other sites

22 hours ago, artwhaley said:

 

i currently have something like that aside from the tuning part, anywho i'm currently working more towards actually getting something into orbit and the optimization will come later, right now I'm wondering about what exactly the method "current_requirement" returns, i hope it's something like the amount of fuel burnt per time unit.

Link to comment
Share on other sites

To manually set tuning parameters you do have to turn off the autotune feature!  

As for just getting into orbit - it's not RSS optimized but certainly could be with some tweaking, but you can feel free to rob from my example at - https://github.com/krpc/krpc-library/blob/master/Art_Whaleys_KRPC_Demos/Simple_Launch_Script.py   Someone math-y-er than me might be able to figure out a more efficient curve shape...  and of course if you have limited engine lights you may want to do something smarter with the logic towards the end of ascent that tries to precisely tune the apoapsis.

 

Link to comment
Share on other sites

11 hours ago, logaritm said:

I'm wondering about what exactly the method "current_requirement" returns, i hope it's something like the amount of fuel burnt per time unit.

I think, there's an easier way. It is described in launch tutorial. It looks like this:

double F = vessel.AvailableThrust;
double Isp = vessel.SpecificImpulse * 9.82;
double m0 = vessel.Mass;
double m1 = m0 / Math.Exp(node.DeltaV / Isp); //mass after burn
double flowRate = F / Isp;
double burnTime = (m0 - m1) / flowRate;

 

Link to comment
Share on other sites

5 hours ago, FumbeiNumbie said:

I think, there's an easier way. It is described in launch tutorial. It looks like this:


double F = vessel.AvailableThrust;
double Isp = vessel.SpecificImpulse * 9.82;
double m0 = vessel.Mass;
double m1 = m0 / Math.Exp(node.DeltaV / Isp); //mass after burn
double flowRate = F / Isp;
double burnTime = (m0 - m1) / flowRate;

 

nice, skimmed trough the examples but must have missed it

12 hours ago, artwhaley said:

To manually set tuning parameters you do have to turn off the autotune feature!  

As for just getting into orbit - it's not RSS optimized but certainly could be with some tweaking, but you can feel free to rob from my example at - https://github.com/krpc/krpc-library/blob/master/Art_Whaleys_KRPC_Demos/Simple_Launch_Script.py   Someone math-y-er than me might be able to figure out a more efficient curve shape...  and of course if you have limited engine lights you may want to do something smarter with the logic towards the end of ascent that tries to precisely tune the apoapsis.

 

uh yah, when i mean get into orbit i mean orbit with no grace or finesse at all since i'm playing RP-0 career mode and my current vessel have like 500~ delta-V to spare lol

Edited by logaritm
Link to comment
Share on other sites

Is this a bug or am I misunderstanding what simulate_aerodynamic_force_at does?

>>> kerbin = space_center.bodies['Kerbin']
>>> jool = space_center.bodies['Jool']

>>> print 'Kerbin rotating:', vessel.flight(kerbin.reference_frame             ).simulate_aerodynamic_force_at(kerbin, (kerbin.equatorial_radius+10000,0,0), (0,0,0))
>>> print 'Kerbin inertial:', vessel.flight(kerbin.non_rotating_reference_frame).simulate_aerodynamic_force_at(kerbin, (kerbin.equatorial_radius+10000,0,0), (0,0,0))
>>> print '  Jool rotating:', vessel.flight(jool  .reference_frame             ).simulate_aerodynamic_force_at(jool  , (jool  .equatorial_radius+50000,0,0), (0,0,0))
>>> print '  Jool inertial:', vessel.flight(jool  .non_rotating_reference_frame).simulate_aerodynamic_force_at(jool  , (jool  .equatorial_radius+50000,0,0), (0,0,0))

Kerbin rotating: (7015.528009767944, -41.65580368041992, -37885.88657339831)
Kerbin inertial: (0.0, 0.0, -0.0)
  Jool rotating: (196773485.3791723, -1886926.75, -93153130.4682703)
  Jool inertial: (136595783.37232614, -2246031.5, 220058997.8491733)

If my vessel were 10km above Kerbin's equator and rotating with Kerbin, I'd expect to get zeros.

If my vessel were 10km above Kerbin's equator and not rotating with Kerbin, I'd expect to get some huge values.

If my vessel were 50km above Jool's equator and rotating with Jool, I'd expect to get zeros.

If my vessel were 50km above Jool's equator and not rotating with Jool, I'd expect to get some huge values.

----

Flight.SimulateAerodynamicForce converts the coordinates to World Space, and calls StockAerodynamics.SimAeroForce (I don't have FAR installed). Then SimAeroForce treats this world velocity as if it were relative to the atmosphere (e.g. mach = v_wrld_vel.magnitude / soundSpeed). Shouldn't it convert the velocity to body.ReferenceFrame first?

Link to comment
Share on other sites

On 3/29/2018 at 4:01 PM, Ericwi said:

Can some post a link or examples of KRPC and Arduino interaction? I can get connected but lack reference to interaction with objects in Arduino. 

You inspired me to try to help...  but... I can't get my Arduino to connect at all.   

I tried the example from the documentation, and the ones that were posted earlier in this thread.  It hangs on the   krpc_open line.   I downloaded the library through the arduino library manager...  copied the example code in...  uploaded... 

I went to KSP and started the server with the default protobuf over serial settings (9600, 8n1), reset my arduino...  and...  nothing.   

If I can figure it out (Or if someone else can tell me what I'm doing wrong!) I'll try to hack together a couple of examples of doing stuff...  

my sketch -  

#include <krpc.h>
#include <krpc/services/krpc.h>

void setup() {
  krpc_connection_t conn;
  krpc_open(&conn, &Serial);
  krpc_connect(conn, "Basic example");
  krpc_schema_Status status;
  krpc_KRPC_GetStatus(conn, &status);
  printf("Connected to kRPC server version %s\n", status.version);
}

void loop() {
}

 

Link to comment
Share on other sites

On 3/30/2018 at 4:09 PM, artwhaley said:

You inspired me to try to help...  but... I can't get my Arduino to connect at all.   

I tried the example from the documentation, and the ones that were posted earlier in this thread.  It hangs on the   krpc_open line.   I downloaded the library through the arduino library manager...  copied the example code in...  uploaded... 

I went to KSP and started the server with the default protobuf over serial settings (9600, 8n1), reset my arduino...  and...  nothing.   

If I can figure it out (Or if someone else can tell me what I'm doing wrong!) I'll try to hack together a couple of examples of doing stuff...  

my sketch -  

 

 

4

#include <krpc.h>
#include <krpc/services/krpc.h>
HardwareSerial * conn;  //I think you are mising this

void setup() {
  conn = &Serial;
  krpc_open(&conn, NULL);  
  krpc_connect(conn, "Arduino Example");   
}

void loop() {
  // put your main code here, to run repeatedly:

}

 

I am running KSP1.4.2 and KRPC 0.4.5  and IDE 1.8.5 and same serial setting. The server will not start if you have Serial Monitor open.

Edited by Ericwi
Link to comment
Share on other sites

Someone help me please

Im having issues to install the mod, the Beginners Guide instructions are not clear for me, it says: "Download and Install the mod". I downloaded it, when i open the zip file there's three main folders, GameData, Schema and Client, i know what to do with GameData, but where i need to put the other two folders, inside the KSP main folder ?

Another question is about the kRPC Python module, it says that i need to type the command in the "command prompt", you mean, in the windows CMD ?

Thank you guys.

Edited by HardZiin03
Link to comment
Share on other sites

Sorry for the slow responses guys - I was away on holiday and then swamped with other things last week. Got a bit more time to work on this now.

@Ericwi here's the suborbital flight tutorial program written for Arduino: https://gist.github.com/djungelorm/a4b9d2e76a7828efc600257f8f4598db I will add Arduino examples to all the other tutorials in the docs soon.

@Kerbart looks like kRPC runs just fine in 1.4.2. They caught me out by releasing this bug fix update while I was away - I'll get the warning removed in the next release.

@MrZacbot I'm afraid their aren't. I don't have a copy of VS (or any experience with it). If someone else wants to provide binaries though I'd be happy to link them. If you can't get it to work on Windows, maybe try out Python instead - it's dead easy to install on Windows.

@HardZiin03 Ignore the Schema and Client folders - you only need the GameData folder copied over your KSP install. You need to run "pip.exe install krpc" from windows CMD to install the client.

Link to comment
Share on other sites

10 hours ago, djungelorm said:

Sorry for the slow responses guys - I was away on holiday and then swamped with other things last week. Got a bit more time to work on this now.

@Ericwi here's the suborbital flight tutorial program written for Arduino: https://gist.github.com/djungelorm/a4b9d2e76a7828efc600257f8f4598db I will add Arduino examples to all the other tutorials in the docs soon.

@Kerbart looks like kRPC runs just fine in 1.4.2. They caught me out by releasing this bug fix update while I was away - I'll get the warning removed in the next release.

@MrZacbot I'm afraid their aren't. I don't have a copy of VS (or any experience with it). If someone else wants to provide binaries though I'd be happy to link them. If you can't get it to work on Windows, maybe try out Python instead - it's dead easy to install on Windows.

@HardZiin03 Ignore the Schema and Client folders - you only need the GameData folder copied over your KSP install. You need to run "pip.exe install krpc" from windows CMD to install the client.

Hey, im really noob in this thinks so, i can't install the client, i tried three ways. In the first one i typed "pip.exe install krpc" in the CMD and it returns an error: 'pip.exe' is not recognized as an internal or external command,
operable program or batch file.

In the second try i typed: "C:\Python36-32\Scripts pip.exe install krpc" and it returns the same error.

And in the thirty way, i go to the python folder (C:\Python36-32\Scripts) and shift+right click, and clicked in the "Open powershell window here". And typed "pip.exe install krpc" and it returned a similar error saying that ''pip.exe'' is not a command.

How i can't do this, im really dumb. Help me

Link to comment
Share on other sites

4 hours ago, HardZiin03 said:

Hey, im really noob in this thinks so, i can't install the client, i tried three ways. In the first one i typed "pip.exe install krpc" in the CMD and it returns an error: 'pip.exe' is not recognized as an internal or external command,
operable program or batch file.

In the second try i typed: "C:\Python36-32\Scripts pip.exe install krpc" and it returns the same error.

And in the thirty way, i go to the python folder (C:\Python36-32\Scripts) and shift+right click, and clicked in the "Open powershell window here". And typed "pip.exe install krpc" and it returned a similar error saying that ''pip.exe'' is not a command.

How i can't do this, im really dumb. Help me

Can you open the folder C:\Python36-32\Scripts in file explorer and verify that pip.exe exists?

Also, the command you posted is missing a backslash. The command should probably be "C:\Python36-32\Scripts\pip.exe install krpc" (note the backslash between Scripts and pip.exe)

Link to comment
Share on other sites

When all else fails you can always try [path where python is]\python.exe -m pip install krpc

Edited by Kerbart
Updated formatting to something that's better than you can ever achieve using a mobile device on this ### forum.
Link to comment
Share on other sites

15 hours ago, djungelorm said:

Can you open the folder C:\Python36-32\Scripts in file explorer and verify that pip.exe exists?

Also, the command you posted is missing a backslash. The command should probably be "C:\Python36-32\Scripts\pip.exe install krpc" (note the backslash between Scripts and pip.exe)

I got it, i just did what Kerbart told me, and it works 100% fine here. Maybe you can put his hint in the Beguinners Guide.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

×
×
  • Create New...