Jump to content

[1.12.x] kRPC: Control the game using C#, C++, Java, Lua, Python, Ruby, Haskell, C (Arduino)... (v0.5.4)


djungelorm

Recommended Posts

JUYhVrt.png

kRPC allows you to control Kerbal Space Program from scripts running outside of the game. It has client libraries for many popular languages including C#, C++, C (for embedded systems)Java, Lua, Python and Ruby.

For example, you could write programs to:

The possibilities are (almost) endless...

The mod exposes most of KSPs API and includes support for Kerbal Alarm Clock and Infernal Robotics. This functionality is provided to client programs via a Remote Procedure Call server, which can communicate using several different protocols including TCP/IP, websockets (for communication with web browsers) and over a serial port. The server is also extensible. Additional remote procedures (grouped into "services") can be added to the server using the  "Service API".

Download Server Plugin (v0.5.4)

GithubSpaceDock | Curse | or via CKAN

Download Client Libraries

C# | C++ | C-nano | Java | Lua | Python | Ruby (by TeWu) | Haskell (by Kahuette) | node.js (by eXigentCoder)

Documentation and Tutorials

Find us on reddit at /r/krpc and on Discord at https://discord.gg/c8c36UM

Source code on github (Several licenses including LGPL, GPL and MIT. See here for details)

The development forum thread can be found here

sXYTMJw.png

Edited by djungelorm
v0.5.4
Link to comment
Share on other sites

  • 2 weeks later...

I've released version 0.2.1 :) A few minor bug fixes, plus the following more major changes:

Link to comment
Share on other sites

  • 2 weeks later...

Hello!

this solution sounds incredible. I'm trying to display some KSP data on a home made controller I want to build. 

Would I be able to read this data over USB on an arduino so I could display it to any LCDs or OLED displays I would have hooked up? 

Kinda new to the programming world so any thoughts/advice helps!

thanks!!!

Link to comment
Share on other sites

5 hours ago, mtpatane said:

Hello!

this solution sounds incredible. I'm trying to display some KSP data on a home made controller I want to build. 

Would I be able to read this data over USB on an arduino so I could display it to any LCDs or OLED displays I would have hooked up? 

Kinda new to the programming world so any thoughts/advice helps!

thanks!!!

I have no experience with Arduino, but here are my thoughts on this....

kRPC communicates over a TCP/IP connection. So you'd need to somehow emulate that through the USB connection. There are existing protocols to do this (wikipedia). Once you get your Arduino talking on the LAN, kRPC can be configured to communicate over the same LAN, by setting it's listen address to the PC's local IP. On the Arduino, you could then use one of the kRPC client libraries to connect to the LAN IP of the computer running KSP. I guess the C++ library would be the best one to use on the arduino platform?

Link to comment
Share on other sites

I just found this and I'm beginning to dig in.

I'm making a little simpit (simulator with physical switches for RCS etc).

Do you think this will interface well with KSPIO?

I want to be able to receive ship data such as Apogee, and send data such as buttons to control RCS, Autopilot modes, etc, maybe even some throttle values etc.

Do you think it would be possible to control the ship with Python code written on a Raspberry Pi?

Sorry for so many questions. Starting to read the documentation etc right now.

THANKS! This looks awesome!

Edited by Keith Young
Found the answer for one of the questions
Link to comment
Share on other sites

9 minutes ago, Keith Young said:

Do you think this will interface well with KSPIO?

kRPC speaks TCP/IP, and KSPIO speaks over serial IO - so they won't communicate directly without some extra coding effort. One way of doing it would be to write a script that translates between the two protocols. If you just want to stream data to an Arduino, I guess you could write a python script to run kRPC commands and send the results over serial IO. Longer term, we could potentially add serial IO support to kRPC!

7 minutes ago, Keith Young said:

Is it possible to have KSP installed on a Desktop, and within the same Desktop write a Python program? Or do you need 2 network devices?

Yes - that's exactly how I run scripts! Just set the server address to "localhost" and then connect to "localhost" from your script.

Link to comment
Share on other sites

10 hours ago, djungelorm said:

kRPC speaks TCP/IP, and KSPIO speaks over serial IO - so they won't communicate directly without some extra coding effort. One way of doing it would be to write a script that translates between the two protocols. If you just want to stream data to an Arduino, I guess you could write a python script to run kRPC commands and send the results over serial IO. Longer term, we could potentially add serial IO support to kRPC!

Something like adding support to communicate over serial IO would be fantastic. We could then run localhost Python script on the desktop and package the outputs, send them to an arduino over serial USB, then display them to some hardware. 

that could have a lot of potential. If you're planning on adding that feature I'd love to help test it, just might need a little guidance, but I'm getting the hang of it. 

Link to comment
Share on other sites

It's on the todo list! I've been planning on making the server protocol extensible for a while but haven't gotten around to it yet. The plan is to make it so that you can drop your own custom DLL into the gamedata folder to add additional server protocols, in much the same way that you can drop custom "service DLLs" in to add new remote procedures to the server. Then we can support more than just protobuf over TCP/IP -- including things like serial IO, or SOAP over HTTP which would be really useful for web clients.

Link to comment
Share on other sites

4 hours ago, djungelorm said:

It's on the todo list! I've been planning on making the server protocol extensible for a while but haven't gotten around to it yet. The plan is to make it so that you can drop your own custom DLL into the gamedata folder to add additional server protocols, in much the same way that you can drop custom "service DLLs" in to add new remote procedures to the server. Then we can support more than just protobuf over TCP/IP -- including things like serial IO, or SOAP over HTTP which would be really useful for web clients.

Awesome!

i was breifly looking into this and am probably way underestimating the complexity but using pyserial it seemed like it would be fairly straightforward to add to a script written to monitor/control a ship. Like a few lines to open the port and make sure you're talking to an arduino in the begining then just a series of repeated actions to grab data from the game package it and send it over a COM port. 

Like I said, way underwear imaging the complexity, but was looking into it:

http://playground.arduino.cc/Interfacing/Python

http://m.instructables.com/id/Arduino-Python-Communication-via-USB/

Link to comment
Share on other sites

Hey djungelorm,

it's me from the velocity issue again. I don't want to pollute the issue on Github so I'd like to ask my question here:

I now have (partially) usable results from Vessel.Velocity(Vessel.Orbit.Body.ReferenceFrame). Item1 is my West/East velocity, Item2 is North/South. Item3 is a bit confusing, as it does not seem to match Flight(Vessel.Orbit.Body.ReferenceFrame).VerticalSpeed - but this could be my mistake.

Now I'm trying to convert this to the speed from the viewpoint of my vessel (Forward/Backward, Left/Right, Up/Down). I tried

var referenceFrame = vessel.Orbit.Body.ReferenceFrame;
var vesselSpeed = _connection.SpaceCenter().TransformVelocity(vessel.Position(referenceFrame), vessel.Velocity(referenceFrame), referenceFrame, vessel.SurfaceVelocityReferenceFrame);

(And any other reference frame I found on "vessel" ;-) ) but I could not produce the expected result. Can you help me with this?

 

On a side note: The way you support this project is awesome. Your friendliness and openness to suggestions paired with the good documentation and fantastic API make this one of the best, if not the best, modding project I have ever worked with :-D

Link to comment
Share on other sites

Vessel.Velocity(Vessel.Orbit.Body.ReferenceFrame) should return a vector in this reference frame (red line is the prime meridian, blue is the equator):

celestial-body.png

So Item1 is in the direction of x, Item2 y, and Item3 z. If you have observed that Item1 is east/west and Item2 is north/south your vessel must be near the equator, and 90 degrees away from the prime meridian. Item3 should then be away from and towards the surface of the planet. Aren't reference frames fun :P

I haven't run your code, but I'm guessing you are getting zeroes? If you have your velocity vector in Kerbin's reference frame (orbit.body.reference_frame) and you convert it to a velocity vector in any of the vessels reference frames, it will become zero. Because you're converting to a reference frame that moves with the vessel.

I guess what you are wanting is the velocity vector in Kerbin's reference frame, but orientated in the surface velocity reference frame. If that's the case, try using TransformDIrection instead of TransformVelocity. That will rotate the velocity vector without changing its magnitude.

On a side note: my pleasure :) I've had a ton of fun making this mod and it's very exciting that people are enjoying using it!

Edited by djungelorm
Link to comment
Share on other sites

This looks really cool! I cant wait to use it. But I have a problem.

I already installed the python client (3.5) but when I run "C:\Python27\Scripts\pip.exe install krpc" The command im supposed to run to install the module according to the documentation I get this error:

7qmS2z7.png

Is there something im doing wrong? If so, how do I fix it?

Link to comment
Share on other sites

19 minutes ago, SrKomodo said:

This looks really cool! I cant wait to use it. But I have a problem.

I already installed the python client (3.5) but when I run "C:\Python27\Scripts\pip.exe install krpc" The command im supposed to run to install the module according to the documentation I get this error:

7qmS2z7.png

Is there something im doing wrong? If so, how do I fix it?

You are trying to execute that command from the interactive Python shell, which is a mistake.
You should run pip install krpc straight from the command line. 

Also, don't run 'C:\Python27\...' (which is obviously wrong, seeing that you have Python 3.5 installed and not 2.7). Simply run 'pip install krpc', it should work.

Link to comment
Share on other sites

21 minutes ago, DeepSpace said:

You are trying to execute that command from the interactive Python shell, which is a mistake.
You should run pip install krpc straight from the command line. 

Also, don't run 'C:\Python27\...' (which is obviously wrong, seeing that you have Python 3.5 installed and not 2.7). Simply run 'pip install krpc', it should work.

First of all, Thank you for the help.

I used the command "pip install krpc" on the comand line (cmd) as you said, but now the error is that "pip" is not recognized as a command, program, or file.

After some searching, I finally made it work by instead of using "pip" using the whole adress to the file, then using the "install krpc"

I´ll leave this here in case someone has the same problem.

Thank you!

(Sorry for my english, its not my first language)

Link to comment
Share on other sites

I'm having some trouble getting this to work.

I installed both Python 3.5 and 2.7. I can start a client and I can get KSP to recognize a program is trying to access KSP.

When I try your Sub Orbital tutorial I get a bunch of red text in Shell, including:

UnicideDecodeError: 'utf8' codec can't decode byte 0xbe in potion 97: 'utf8' codec can't decode byte 0xbe in potion 97:: invalid start byte in field: krpc.schema.Response.error

I'm using 1.0.4 and I downloaded that version from Curse.

Am I correct that I can just write a script, save it as a .py, and double click it while I have a vehicle on the launch pad? Or I can open it in IDLE and run>run module?

Thanks a ton for making this. I can't wait to get it to work.

Link to comment
Share on other sites

It should work with either Python 2.7 or 3.5. And yes you should be able to just double click the .py file.

kRPC version 1.0.4 doesn't exist! Do you mean *KSP* version 1.0.4?

Is there a stack trace that goes with that error message?

Also, can you run the following from a python interactive terminal to verify what versions python has installed:

import sys
import krpc
import google.protobuf
print(sys.version_info)
print(krpc.__version__)
print(google.protobuf.__version__)

Then try running this simple script, with a vessel on the launchpad. This is a bit simpler than the full blown tutorial script, so should be easier to narrow things down ;)

import krpc
conn = krpc.connect()
print(conn.space_center.active_vessel.name)

 

Link to comment
Share on other sites

sys.version_info 2.7.11 final serial=0
krpc 0.2.2

google 3.0.0b2

I'm using KSP 1.04 currently.

When running that last script it asks if I should allow or deny, I say allow. It then briefly says <unknown> before saying No clients connected.

How do I find out if there is a stack trace?

Link to comment
Share on other sites

KSP 1.0.4 isn't supported. Would you be able to try with KSP 1.0.5? A warning should have appeared when you loaded the game saying that it's not supported?

When the client encounters the error, it should print out a stack trace showing the location in the code that caused the error, along with the description of the error.

Another thing you could try is setting verboseErrors to True and logLevel to Debug in GameData/kRPC/settings.cfg and checking for any error messages in KSP_Data/output_log.txt

Edited by djungelorm
Link to comment
Share on other sites

I was using Kerbal EDU. I got a new full license ( not EDU) and it worked flawlessly.

Do you have any serial features on your future timeline? Such as making this somehow work with KSPIO?

Thanks again for such an amazing mod! My wife rolled her eyes when I cheered at getting a Kerbal X to orbit with this.

Link to comment
Share on other sites

Ah glad you got it working!

Yeah serial stuff is definately planned. Giving an eta of some sort is obviously futile, but I plan to look into making the server protocol extensible (including adding serial io) after we've got the autopilot stuff working a bit better (which a few of us are currently working on) and after fixing up the inevitable breakages when 1.1 drops.

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