Jump to content

kRPC2: Control the game using Python, C#, C++, Java, Lua...


djungelorm

Recommended Posts

kRPC2 allows you to control Kerbal Space Program 2 from scripts running outside of the game, and comes with client libraries for many popular languages. This is a continuation of the kRPC mod for Kerbal Space Program 1

The mod is a very early work in progress, so don't expect much!

----

In this first proof-of-concept release v0.1.0, there is a single "service", called SpaceCenter2, with a few properties for basic telemetry.

It works with the existing kRPC client libraries (as it speaks the same protocol). However we don't yet provide generated stubs for clients like C++, C# and Java. It has only been tested using the python client, which does not require generated stubs (it can auto-generate them on connection).

When the game starts, a server is created with RPC port 50000 and Stream port 50001. There is no configuration for this yet - it is hard coded in.

Here's an example in Python, with all the currently available telemetry:

import krpc
conn = krpc.connect()
print(conn.space_center2.horizontal_surface_speed)
print(conn.space_center2.vertical_surface_speed)
print(conn.space_center2.terrain_altitude)
print(conn.space_center2.sealevel_altitude)
 
Edited by djungelorm
Fix formatting
Link to comment
Share on other sites

3 hours ago, LeroyJenkins said:

how does this compare/relate to KSPSerialIO?

KSPSerialIO does two things: sends you a telemetry stream over serial (using a custom serialisation format) and can receive a similar stream of control data. It's very light weight (so great for things like Arduino). It's also very low level - providing you no abstraction over the interface. And the interface is fixed.

kRPC is a more complex ecosystem of servers and clients, allowing you to interact with the game from many different languages  (including Arduino using the Cnano client). It runs a server in game that can speak various protocols (including protobuf over TCP/IP, websockets and serial io). Clients call "remote procedures" using these protocols.

The various client libraries provide abstractions over this so, as Kerbart points out, you get an object orientated view. These libraries also provide high level abstractions like events, and streams.

The server is also extensible. Other mods can add RPCs to the server to extend the functionality.

Link to comment
Share on other sites

@djungelorm - Been reading up on the kRPC documentation (krpc.github.io/krpc/index.html), specifically the SpaceCenter APIs. Before i get into the full control console hardware design, I want to make a GUI interface representation first to be a stepping stone as I learn this stuff. I'll start with receiving telemetry from KSP2 and displaying it before creating buttons to send commands to KSP2. Kind of like the Telemetry Health and Status Displays we use in Satellite Mission Control Centers (See image at bottom of post). I have a few questions about the APIs capabilities. I'll be referencing C++, since that is what I know

1) I see how I can get the orbital parameters of a vessel by calling Vessel::orbit() and its sub functions. Does this return the parameter values for the active_vessel() only? I ask, cause I'd like to be able to display all the current vessels (in flight) orbital parameters at once. Would I have to cycle through all the vessels in std::vector<Vessel>vessels(), by setting each one in the list as the active vessel (using set_active_vessel(Vessel Value)), getting its orbit parameter and then repeating for each vessel in the array?

2) Are Vessel::Orbit() telemetry only available while KSP2 is in Flight View? Or can I continue to poll the data while in VAB, Map and Tracking Station Views? If there are current missions flying, while I am building a new rocket, I'd like the external Telemetry GUI to keep updating.

3) Under Alarms API. The current alarms are based on time, time offsets, or time to Pe, Ap or Planned Maneuver Node Points. I'd like to see if I can create Alarms for other things. For example: If a spacecraft (s/c 1) is on a long flight mission, and I am working on another spacecraft (s/c 2), I'd like to be alerted if s/c 1's EC goes below 10% of capacity. That way I don't have to keep swapping active views and checking on s/c 1's health and status, while working on s/c 2. If this not in the scope of kRPC2, I could make alarms as part of the external GUI, but then I'd have to make a DB and inference engine to track the each new current value to the set alarm limits. When we fly spacecraft we usually have four limits the system tracks for telemetry. Red High, Yellow High, Yellow Low and Red Low. Also I'd have to create a persistence and hysteresis check to ensure that if the the value is toggling back and forth over a limit, it doesn't consistently repeat the alarm every sample.

Thanks for the help. This information well help me plan the architecture.

 

This is a very basic Telemetry screen (old school style). The newer ones are more graphic and color coded.

oDY5gUq.jpg

Edited by LeroyJenkins
Link to comment
Share on other sites

Cool project! Here's my feedback, hope it helps: 

Quote

I'll start with receiving telemetry from KSP2 and displaying it before creating buttons to send commands to KSP2.

Note that we haven't implemented the SpaceCenter API for KSP2 yet. What we have released so far for KSP2 (called kRPC2) is just a proof-of-concept to see if the server communication works (which it does!) We are actively working on implementing this part, and will likely start with the basic telemetry stuff you'll need.

The SpaceCenter API for KSP1 and KSP2 is planned to be (almost) identical. So if you want to get coding, you could prototype your interface using kRPC1+KSP1. It should work with minor modification in kRPC2+KSP2 once we've implemented the required APIs.

Quote

1) I see how I can get the orbital parameters of a vessel by calling Vessel::orbit() and its sub functions. Does this return the parameter values for the active_vessel() only? I ask, cause I'd like to be able to display all the current vessels (in flight) orbital parameters at once. Would I have to cycle through all the vessels in std::vector<Vessel>vessels(), by setting each one in the list as the active vessel (using set_active_vessel(Vessel Value)), getting its orbit parameter and then repeating for each vessel in the array?

You can do this, and no you don't need to keep switching active vessel. The API is object oriented (instances of objects in the client refer to objects in the game via a unique id number). You can obtain a Vessel object for whatever vessel you like, and then an Orbit object for that vessel, and call the various methods to get orbital parameters for that specific vessel. (it's abstracted like this, as Orbit objects can also refer to things like planets). The SpaceCenter::vessels() function gives you an std::vector of Vessel objects for all the vessels in the game, and SpaceCenter::active_vessel() gives you a Vessel object for just the active vessel. You can treat these Vessel objects just like a normal C++ object, and call the orbit methods they contain.

Quote

2) Are Vessel::Orbit() telemetry only available while KSP2 is in Flight View? Or can I continue to poll the data while in VAB, Map and Tracking Station Views? If there are current missions flying, while I am building a new rocket, I'd like the external Telemetry GUI to keep updating.

You can do this (in KSP1). I can't really comment on this regarding KSP2 as it's not implemented yet, but in KSP1 this telemetry is available in any view (and we aim to do the same for KSP2). In the documentation, it says which game scenes you can use the class/method/function in (and if none are specified you can assume it's always available). For example SpaceCenter::vessels() and all the orbit methods are available in all game scenes. SpaceCenter::active_vessel() is only available from the flight scene.

Quote

3) Under Alarms API. The current alarms are based on time, time offsets, or time to Pe, Ap or Planned Maneuver Node Points. I'd like to see if I can create Alarms for other things. For example: If a spacecraft (s/c 1) is on a long flight mission, and I am working on another spacecraft (s/c 2), I'd like to be alerted if s/c 1's EC goes below 10% of capacity. That way I don't have to keep swapping active views and checking on s/c 1's health and status, while working on s/c 2. If this not in the scope of kRPC2, I could make alarms as part of the external GUI, but then I'd have to make a DB and inference engine to track the each new current value to the set alarm limits. When we fly spacecraft we usually have four limits the system tracks for telemetry. Red High, Yellow High, Yellow Low and Red Low. Also I'd have to create a persistence and hysteresis check to ensure that if the the value is toggling back and forth over a limit, it doesn't consistently repeat the alarm every sample.

The alarm API is for creating/managing KSP1 alarms, just like you would do manually using the KSP1 in-game UI. I don't know enough about KSP2 yet to know if these same alarms exist in KSP2.

As for the kind of alarm you describe, this is the sort of thing we want to support and is in scope for the mod. One approach that could work for you are events. In kRPC, you could create a server-side expression for "EC < 10%", create a stream that wraps it, and then wrap an event around it. In the C++ client this would give you an std::condition_variable that gets notified when the EC drops below the threshold. There is an example in the docs that does something very similar. It triggers an event when the altitude of the vessel exceeds 1km: https://krpc.github.io/krpc/cpp/client.html#custom-events

One thing that could cause problems for this, is whether the game is actually running the EC simulation for s/c1 in the background while you are working on s/c 2. I don't know enough about KSP2 yet to know if that's the case.

Edit: you can also set the rate for a stream. So you could configure it to only check "EC < 10%" once per minute to save on game performance.

Edited by djungelorm
Add comment on stream rate control
Link to comment
Share on other sites

@djungelorm That was a HUGE help. I understand that how this works in KSP2 is TBD as IG devs evolve it. Understanding how it works in KSP1 and your Vision for kRPC2 helps me map out my plan.

1) Got it. Create a Vessel::object for each item in SpaceCenter::Vessels(), then access the methods I want. Perfect.

2) Understood

3) I'll have to investigate how to do stream wrapped expressions events. But thanks for pointing me in that direction. I'll look into it.

Awesome that we can set the rate for the stream. No need to poll something every cycle if it doesn't change much. We do this a lot for S/C telemetry. Somethings that are binary and don't change often, we'll only transmit its data 1-10sec (1-0.1Hz) , where others that are floats and change rapidly we would send up to 60 Hz rates. No sense sending everything at 60Hz and taking up bandwidth.

Edited by LeroyJenkins
Link to comment
Share on other sites

21 hours ago, LeroyJenkins said:

We do this a lot for S/C telemetry. Somethings that are binary and don't change often, we'll only transmit its data 1-10sec (1-0.1Hz) , where others that are floats and change rapidly we would send up to 60 Hz rates. No sense sending everything at 60Hz and taking up bandwidth.

I've written telemetry software for aircraft flight test and we do the same thing, as you might expect. But the highest rates we were sending was 400 Hz.

Also I'm working on a similar project and will be using kRPC and eventually kRPC2 to connect physical displays and controls and MFDs to KSP/KSP2.

Link to comment
Share on other sites

On 3/20/2023 at 1:23 PM, Computerizer said:

Also I'm working on a similar project and will be using kRPC and eventually kRPC2 to connect physical displays and controls and MFDs to KSP/KSP2.

@Computerizer 400Hz!! wow that's fast! how many data points? Most we did was 60,000 @ 60Hz.

I never thought about MFDs, but those would be so cool. I'm a private pilot and have flown with the G1000 glass cockpit, that would be interesting as a spaceship control system. My efforts are more focused on Mission Control views for multiple missions. The below image. I would like to base my web GUIs off of AstroUX, but not sure on the licensing or international use.

I am looking at kRPC, though not sure if the Kerbal Web Program Mod (https://spacedock.info/mod/3275/Kerbal Web Program) might be better for my project since it will come with a front end architecture already built. Just don't know what limitations it has compared to kRPC2 yet.

I'd be happy to collaborate across projects, I am sure we'll run into similar issues. Also, if there are others out there that would like to help with the project, I'd welcome it.

8tbDCoR.jpg

Edited by LeroyJenkins
Link to comment
Share on other sites

3 hours ago, LeroyJenkins said:

@Computerizer 400Hz!! wow that's fast! how many data points? Most we did was 60,000 @ 60Hz.

I never thought about MFDs, but those would be so cool. I'm a private pilot and have flown with the G1000 glass cockpit, that would be interesting as a spaceship control system. My efforts are more focused on Mission Control views for multiple missions. The below image. I would like to base my web GUIs off of AstroUX, but not sure on the licensing or international use.

I am looking at kRPC, though not sure if the Kerbal Web Program Mod (https://spacedock.info/mod/3275/Kerbal Web Program) might be better for my project since it will come with a front end architecture already built. Just don't know what limitations it has compared to kRPC2 yet.

I'd be happy to collaborate across projects, I am sure we'll run into similar issues. Also, if there are others out there that would like to help with the project, I'd welcome it.

Very nice.

Work stuff: If I recall we have about 20 sensors recording at 1200 Hz and transmitting at 400 Hz. Those were accelerometers for detecting flutter. In total I have it recording about 66,000 measurements per second and transmitting ... I can't recall off the top of my head. Maybe a third overall? We're also transmitting video and audio over the same link to our telemetry room. I wrote the software (with the help of my small team) that records and transmits the data, video, and audio. We use IADS for our displays.

Personal stuff: I agree it's possible we may have stuff to share. I'm doing a lot of design & requirements level stuff right now, but once I start building and writing code I plan to have it open source and I'll share a link.

Link to comment
Share on other sites

7 hours ago, LeroyJenkins said:

I am looking at kRPC, though not sure if the Kerbal Web Program Mod (https://spacedock.info/mod/3275/Kerbal Web Program) might be better for my project since it will come with a front end architecture already built. Just don't know what limitations it has compared to kRPC2 yet.

I wasn't aware of Kerbal Web Program, thanks for bringing it to my attention! Looks like it's in very early development so take the following with a giant pinch of salt...

I had a quick look over their code on github. It provides an HTTP server (version 1.1 I think?) that listens for HTTP requests, and sends HTTP responses. I guess you'd call this a "REST API"? I can't find any form of structured serialization - looks like everthing is passed around as strings. There is some mention of websockts in the client code, but doesn't appear to be implemented in the server.

Not sure what you meant by "front end architecture already built"? AFAIK making REST API querys from JS is essentially built into the language...

Based on its internal architecture, I wonder if it is a good fit for your use case, as you want to stream lots of telemetry for multiple vessels at the same time. The overhead of doing that with a request/response REST API would be pretty high, and might limit how much data you can get out of the game per-frame without tanking the game performance. You would also need to do active polling for your alarms. kRPCs streams don't require this back and forth between client and server, once set up, the server just acts like a hosepipe sending you the data.

If you want to go the JS route for your frontend, kRPC does speak websockets, and there is a JS client made by someone else https://github.com/eXigentCoder/krpc-node (although not sure how up to date it is). You also don't have to use a client to interace with kRPC. You could send protobuf messages to it yourself - it's a fair bit of work to set this up though.

Link to comment
Share on other sites

@djungelorm KWP I think is more developed for KSP1, the spacedock link I provided is the Modders start to migrate it to KSP2, so less developed.

That was my same concern about KWP was the simultaneous multiple streams of data bogging performance down using the REST APIs. Good to know that kRPC can do websockets, but the standard kRPC server stream seems to be the best. Its more like what I am used to at work for telemetry displays. Thanks!

@Computerizer Great, I'll share a link to my work as well. Yeah, in am in the high level requirement and architecture of the backend stuff, so no flashy GUIs yet. kRPC is looking more and more the best for my objective. So I will be watching its development for KSP2 closely.

Link to comment
Share on other sites

12 minutes ago, LeroyJenkins said:

@djungelorm KWP I think is more developed for KSP1, the spacedock link I provided is the Modders start to migrate it to KSP2, so less developed.

Oh I didn't realise KWP was a KSP1 mod too. I can't seem to find any trace of it though, do you have a link?

Link to comment
Share on other sites

50 minutes ago, djungelorm said:

do you have a link?

I "think" it was a mod for KSP1. I never used it since I was on xbox for KSP1. I thought I saw it referenced in a thread, but I am not finding it either. I may have misunderstood and misspoken. Sorry. @ShadowDev would know for sure, he seems to be leading the KWP for KSP2.

Though I did find this for KSP1, which is very very similar to the project I am envisioning. https://github.com/yagiziskirik/Kerbal-Telemetry. It a Server-client setup (similar to kRPC?), but is in JS.

Link to comment
Share on other sites

4 minutes ago, djungelorm said:

HTTP web server

Thanks, then its more like KWP, so I wonder how Kerbal-Telemetry does on bandwidth as well. kRPC still seems to be the best for performance for multiple streams of data. I am thinking ahead for Colonies & Multiplier. One may have multiple colonies spread across Kerbal Space that they want to monitor concurrently at Mission Control.

Link to comment
Share on other sites

I guess if you are batching lots of telemetry data into a single web request, you mitigate the overhead of making the request. So if you can do that performance should be ok.

Not sure if that approach is very extensible though. You'd still need some mechanism for the client to tell the server what it wants included in the response, and doing that as an HTTP request rather than ahead of time would add overhead.

I guess in "technical speak" this is the difference between a stateless RESTful API (like KWP/Kerbal Telemetry) and kRPCs stateful approach (where client connection to the server is persistent, and has associated state that persists between requests).

Link to comment
Share on other sites

17 hours ago, LeroyJenkins said:

I "think" it was a mod for KSP1. I never used it since I was on xbox for KSP1. I thought I saw it referenced in a thread, but I am not finding it either. I may have misunderstood and misspoken. Sorry. @ShadowDev would know for sure, he seems to be leading the KWP for KSP2.

Though I did find this for KSP1, which is very very similar to the project I am envisioning. https://github.com/yagiziskirik/Kerbal-Telemetry. It a Server-client setup (similar to kRPC?), but is in JS.

KWP is a ksp 2 mod not ksp 1.
 

On 3/22/2023 at 9:35 AM, djungelorm said:

I wasn't aware of Kerbal Web Program, thanks for bringing it to my attention! Looks like it's in very early development so take the following with a giant pinch of salt...

I had a quick look over their code on github. It provides an HTTP server (version 1.1 I think?) that listens for HTTP requests, and sends HTTP responses. I guess you'd call this a "REST API"? I can't find any form of structured serialization - looks like everthing is passed around as strings. There is some mention of websockts in the client code, but doesn't appear to be implemented in the server.

Not sure what you meant by "front end architecture already built"? AFAIK making REST API querys from JS is essentially built into the language...

Based on its internal architecture, I wonder if it is a good fit for your use case, as you want to stream lots of telemetry for multiple vessels at the same time. The overhead of doing that with a request/response REST API would be pretty high, and might limit how much data you can get out of the game per-frame without tanking the game performance. You would also need to do active polling for your alarms. kRPCs streams don't require this back and forth between client and server, once set up, the server just acts like a hosepipe sending you the data.

If you want to go the JS route for your frontend, kRPC does speak websockets, and there is a JS client made by someone else https://github.com/eXigentCoder/krpc-node (although not sure how up to date it is). You also don't have to use a client to interace with kRPC. You could send protobuf messages to it yourself - it's a fair bit of work to set this up though.

Current version of KWP has the REST system implemented, next update will include the WebSocket stuff.
It provides the user with a web server that they can use if they want, also supports any language (being that its just doing network requests)

The current rest version is really for periodic data (Like changing the Autopilot mode) the future WebSocket system will be for the Realtime stuff.

Current feature set of KWP (0.1.0):
Expandable API - api template on github, read the wiki for more info
Standard API
Web server - simple to use, no third party software required
Autogenerated docs for provided api and user expanded

future versions will include stuff like (WebSocket, Premade Dashboard, Camera part, Tracking part, uncompiled CS api support, In game web browser)

Link to comment
Share on other sites

@ShadowDev Thank you for confirming that and the future outlook of KWP.

@djungelorm I installed the kRPC2 mod to KSP2-0.1.1.0 and installed the kRPC python libraries from KSP1 and successfully got a connection and the SpaceCenter2 data out to a Terminal Window.

First Success!

Though when starting a game in KSP2, the kRPC2 Server Setup Window never came up. I see the kRPC2 in my mods list. Though it must be running regardless cause I was able to get the below output on the client side.

afOf183.jpg

Now that I know how and that I can get the data out of KSP2, I'll work on figuring out how to get it to a formatted webpage UI.  One small step......

Link to comment
Share on other sites

  • 3 weeks later...

Hello Djungelorm, 

i cant believe that i didn´t know about this mod for KSP1 and now i cant wait to see the development of it for KSP2 :O 

When i saw this post , i immediately started to write a program for KSP1 that will launch my vessel and brings it into a stable orbit. I never had so much fun learning coding as i had with this mod. 
Please keep up the nice work, you definitely got a fan :D

 

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...