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

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

7.8k Downloads

Arduino flight data

EricKerman opened this issue · 8 comments

commented

Anytime I pull flight data like altitude or g’s the game locks for about 1/2 second.

commented

Can you please provide some example code that demonstrates the issue?

commented
commented

Here is a Cleaner version. That has the issue after the rocket gets above 1000m and stops when it goes back below 1000m. It's weird because it gets called the same way, but if it's in a while loop its ok.

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

HardwareSerial * conn;

#ifdef KRPC_ERROR_CHECK_FN
#include <krpc/error.h>

// Error handler function called if an RPC fails
void error_handler(krpc_error_t err) {
while (true) {
for (int i = 0; i < -(int)err; i++) {
//digitalWrite(LED_BUILTIN, HIGH);
delay(200);
//digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
delay(1200);
}
}
#endif

void setup() {

conn = &Serial; // Open the serial port connection
krpc_open(&conn, NULL);

}

void loop() {

prog();
delay(2);
}

void prog() {
krpc_connect(conn, "Sub-orbital flight");

krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);

krpc_SpaceCenter_AutoPilot_t auto_pilot;
krpc_SpaceCenter_Vessel_AutoPilot(conn, &auto_pilot, vessel);

krpc_SpaceCenter_AutoPilot_t control;
krpc_SpaceCenter_Vessel_Control(conn, &control, vessel);

krpc_SpaceCenter_AutoPilot_TargetPitchAndHeading(conn, auto_pilot, 90, 90);
krpc_SpaceCenter_AutoPilot_Engage(conn, auto_pilot);
krpc_SpaceCenter_Control_set_Throttle(conn, control, 1);
delay(1000);

krpc_SpaceCenter_Control_ActivateNextStage(conn, NULL, control);

krpc_SpaceCenter_Resources_t resources;
krpc_SpaceCenter_Vessel_Resources(conn, &resources, vessel);

krpc_SpaceCenter_Flight_t flight;
krpc_SpaceCenter_Vessel_Flight(conn, &flight, vessel, KRPC_NULL);
double mean_altitude;
while (true) {
krpc_SpaceCenter_Flight_MeanAltitude(conn, &mean_altitude, flight);
if (mean_altitude > 1000)
break;
}
krpc_SpaceCenter_Flight_MeanAltitude(conn, &mean_altitude, flight);
//krpc_SpaceCenter_Control_ActivateNextStage(conn, NULL, control);

}

commented

Been having a look at this and I can't quite figure out what this is wrong, and I don't quite follow what you mean by it having the issue above 1000m and fine again below 1000m?

Also I notice you repeatedly run prog from loop. Is this really the intended behaviour? Maybe make it so that prog only runs once to completion and then stops. For example:

bool ran = false;
void loop() {
    if (!ran)
        prog();
    ran = true;
}

In the first code you posted, you never write to the flight variable -- so its value could be anything. This would cause issues when trying to get flight data, and the server is probably returning an error when you do so. The waiting you see could just be the error detection loop in your code? Also worth checking the game's output log to see any possible errors the server is sending (just in case the error detection code isn't working as expected).

commented

Use the 2nd for the easier example. The call to prog() is the way I wanted it. This was built off a simple sounding rocket. It would detach the engine stage at 1K and also demonstrated the pause does not happen while in the loop. When I knew that worked I commented it out the staging deliberately and made it a call for altitude to demonstrate the pause is there every time it is called. So as soon as the craft gets to 1000 meters the bug pauses\delays\locks the game.

After the craft goes falls back to Kerbin below 1000 meters the program gets trapped back in the while loop and the bug goes away because the call is not being made repetitively.

There is nothing interesting in the LOG. I will try to post it soon.

Because the stream is not an option I want to call the flight data every loop or every other loop-ish, but if it pauses the game it unusable. Thanks for looking into this.

commented

I'm pretty sure I have the same Issue: When requesting RCS and SAS status the server locks up the game for a few hundred ms. The game locks up when the rx-LED on the Arduino lits up and continues when the LED goes low. In the code example below the game locks up twice per cycle.

The logfile does not display anything out of the ordinary.

KSP: 1.4.3
Client: C-Nano, v0.4.5
Server: v0.4.6 (also tried 0.4.5, no difference)
Other installed mods: Also checked without any, no difference

Arduino sketch:

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

HardwareSerial * conn;
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_Control_t control;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  conn = &Serial;
  // Open the serial port connection
  krpc_open(&conn, NULL);
  // Set up communication with the server
  krpc_connect(conn, "Arduino Example");

  // Indicate succesful connection by lighting the on-board LED
  digitalWrite(LED_BUILTIN, HIGH);

  krpc_SpaceCenter_ActiveVessel(conn, &vessel);
  krpc_SpaceCenter_Vessel_Control(conn, &control, vessel);
}

void loop() {
  boolean rcs;
  boolean sas;
  
  krpc_SpaceCenter_Control_RCS(conn, &rcs, control);
  krpc_SpaceCenter_Control_SAS(conn, &sas, control);
  
  delay(1000);
}

kRPC Settings:
image

Screenshot during lockup (Exec time goes down to .01 after the lockup and then up again):
image

commented

Sorry I've been a bit busy recently and didn't have time to follow up on this.

Thanks for the nice simple test case. Unfortunately I don't see the same behaviour on my system, running under Linux. Are you on Windows?

The kRPC server is implement as a single thread that runs whenever the game updates, and this relies on RPCs executing reasonably fast. My guess is that the serial communication is taking a longer time than it should, for whatever reason, which would cause this behaviour.

commented

Yep, I'm on Win10.