Jump to content

KSP and Arduino - again.


Fillipuster

Recommended Posts

Hey everyone.

I wasn't entirely sure where to post this, but here it is.

I am - like many others - making a physical KSP controller. In my case, with my uncle on the "physical" part (soldering, connecting, wires, etc.).

I'm doing all the "digital" parts. I'm using kRPC with python, connected to an Arduino mega, all using lawnmowerlatte's python to Arduino framwork, using a serial connection. Our first prototype is working wonderfully, and we're planning out the full thing (it's going to be big). The aim is to be able to fly a mission with one person being mission control - having the map view. And the other being the pilot - only having an IVA view of a window.

Our current prototype: Prototype (how do you embed pictures?)

This post serves not only to show you people what we've been working on (as we think it's very cool), but also as a few questions:

1. We'd love to upgrade with another Arduino at one point, running a little screen with a rendering of a navball, similar to this. How should we go about this? Are there any resources available?

2. We're worried that our endless Python loop will be running so many button checks and kRPC calls, that the delay on things like single button presses and 7-segment screen updates will be too much. Will this be a problem and/or how would one work around it?

Thank you for reading :D

-FP

Link to comment
Share on other sites

1. You could use kRPC for this. Combine it with a python module that renders graphics (pygame/kivy or whatever) based on the streaming telemetry data updates from kRPC. I used kivy to provide a 2d view of the orbit around a body based on apoapsis & periapsis state - perhaps you could do similar with the heading data?

2. You could use a separate thread to deal with the UI side of things and another thread to deal with the kRPC telemetry. I made a wrapper around the kRPC stuff using asyncio to do this using coroutines with my kivy interface. It is all stuff I wrote for fun and uncommented/unprofessional but I'd be happy to share - perhaps via the kRPC examples github.

Tom.

Link to comment
Share on other sites

This looks fun!  I've got a start on a ksp simpit myself that is going to be 'big' too!   But mine gets stalled as i get distracted.   My daughter's only a year old though, so it'll be up and running before she's old enough to enjoy it with me! :)  

 

I'll look into lawnmowerlatte's python to arduino bits - I am working on an example repository for KRPC scripts, and was going to do a KRPC to Arduino example using pyserial to get the data out via my own protocol, but seeing that someone's already done a KSP specific version here, I will do some research to see if I should be using it instead for the example.   My criteria for deciding that, and the criteria I'd recommend you use for deciding if you're using it is the overhead.  I think more than the python loop slowing you down, the baudrate of the serial connection to the arduino could be your bottleneck.   

There's no reason that pyserial couldn't be talking to two different arduinos, but without looking at lawnmowerlatte's code I don't know if his system is set up to make that easy or not? You can absolutely connect multiple python (or other language) clients to one KRPC server instance at the same time, so hypothetically you could have two different python scripts, each pointed at a different serial port and each grabbing or passing different data via KRPC, which would be one way to make your application 'multithreaded,' and tighten up each of the loops to minimize lag from the serial connection or python interpreter execution?  

Having the most efficient protocol possible and putting the processing jobs spread out between the various processors is going to minimize your lag the most.  Instead of polling buttons in python, it would be better to have the Arduino do that and have it send them as efficiently as possible to the python script... for example as a bit mask, so it would only take 2 bytes of serial data to pass in 16 button states?  Plus the libraries and techniques for debouncing and such are well established for Arduino.  

 I've never played with true multi-threading in python, but it exists-  so it would also be possible for you to move communication to a 'background thread' and use what you'd consider the 'main or foreground' loop to do higher level processing?

There's also the possibility of hitting a KRPC bottleneck too...  if you're trying to pull A LOT of data out at a high rate.   I know that setting a refresh rate for streams is something that's on the long term plan for KRPC but I don't think that feature is actively being developed.   I think compiling a list of all the data you want out of KRPC is a good first step, then assigning a realistic 'minimum polling rate' for each of those fields - you may need orientation data as fast as you can get it, but you probably DON'T need lattitude and longitude any faster than once a second-  from this 'prioritized list' we can figure out the most efficient way to communicate with KRPC - what items should be retrieved via a stream and what should be retrieved via repeated polling. 

  My advice on pretty much ALL of this is  keep soldiering on without it...  until the lag becomes a problem, and then let's figure out where it's coming from and how to open up that bottleneck.  Glad to see you working on this!  

 

Link to comment
Share on other sites

  • 1 month later...
On 7/10/2017 at 11:40 PM, ThomasD said:

1. You could use kRPC for this. Combine it with a python module that renders graphics (pygame/kivy or whatever) based on the streaming telemetry data updates from kRPC. I used kivy to provide a 2d view of the orbit around a body based on apoapsis & periapsis state - perhaps you could do similar with the heading data?

2. You could use a separate thread to deal with the UI side of things and another thread to deal with the kRPC telemetry. I made a wrapper around the kRPC stuff using asyncio to do this using coroutines with my kivy interface. It is all stuff I wrote for fun and uncommented/unprofessional but I'd be happy to share - perhaps via the kRPC examples github.

Tom.

Sorry for the (way way waaaay too) late reply. I'd love to see how you've done this if you'd want to share your code :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...