zitronen

[Hardware, Plugin] Arduino based physical display + serial port io+ tutorial (24-11-19)

Recommended Posts

I will share it, first i have to understand how github works and how my new IDE works with Github. :blush:

Can you try this : http://www.filedropper.com/kspserialio

You will find :

KSPSerialIO.dll_work

KSPSerialIO.dll_dontwork

Just rename one of thoose in KSPSerialIO.dll and test.

Could you test both ? The working one is without event, the don't working one is the event. (Both use PsimaxSerial)

Use this demo : https://sites.google.com/site/zitronfiles/KSPIODemo13.zip

Just check the Baudrate in the config file and the Arduino program before use, it might be different.

Because like zitronen said, it's better to use event.

Hello all; It's been a while.

Zalta -- thanks for your hard work!

I've been having troubles with the latest plugin since it was released.

I would have intermittent connects. Like, you'd go to launch, and it could be 1 min, it could be 15 until the plugin started talking to my controller / Arduino.

The previous version worked immediately, with no issues.

You could jump between ships, go from VAB to ships, etc -- always seemed to work.

I'm running Windows 7, 64bit

I've included the video so you can see the 'normally' pre-load process, and what kind of activity should be working when the plugin is communicating with my arduino.

* *

I just tried Zalta's plugin. Here is what I found -

Launching a vehicle that's already on the pad: connects within 15 seconds or so.

Launching a vehicle from VAB - long delays; doesn't seem to connect.

Reverting to launch whilst mid-flight; when you're back at the pad, it doesn't connect.

I found I'd have to go to 'Space Center', unplug the USB, reconnect, then go and launch the vehicle sitting on the pad. That worked.

When going to the 'Tracking Station', there was a whole host of weirdness (see video below).

I would get music; but nothing on screen. Then, I would go back to space center - and I'd just get stars.

I'd shut down the game, and start it up again. Then, the vehicle I had tried to 'switch-to' using the tracking station will have completely disappeared. I lost two 'deep-space probes' testing this :P

Your plugin is eating my vessels!!! ;.;:confused:

[it's ok, I don't hold you accountable :wink:]

So, it's definitely an improvement... but with it comes another series of problems.

(Jump to 1:15 to see the tracking station weirdness)

Share this post


Link to post
Share on other sites

Okay, have you try the version "dontwork" on Windows 7 it May be better. Have you tried with other baudrate ?

Edit : I fork with my code here : https://github.com/zaltanar/KSPSerialIO

Edit2 : I didn't reproduce your disparition bug.

The only problem i had with the Tracking Station was i had to attempt twice before control the ship.

PS : i'm not using mods when i test

Edited by Zalta

Share this post


Link to post
Share on other sites

I've been having troubles with the latest plugin since it was released.

I would have intermittent connects. Like, you'd go to launch, and it could be 1 min, it could be 15 until the plugin started talking to my controller / Arduino.

The previous version worked immediately, with no issues.

You could jump between ships, go from VAB to ships, etc -- always seemed to work.

I'm running Windows 7, 64bit

I've included the video so you can see the 'normally' pre-load process, and what kind of activity should be working when the plugin is communicating with my arduino.

Hey have you tried increasing the arduino serial buffer size? See posts #1201-1204. Seems to have fixed the problem for bork_bork.

Share this post


Link to post
Share on other sites
Okay, have you try the version "dontwork" on Windows 7 it May be better. Have you tried with other baudrate ?

Edit : I fork with my code here : https://github.com/zaltanar/KSPSerialIO

Edit2 : I didn't reproduce your disparition bug.

The only problem i had with the Tracking Station was i had to attempt twice before control the ship.

PS : i'm not using mods when i test

Funnily enough, I thought that I shouldn't because it wouldn't work :P

I'm not using any mods either. Very Vanilla. Installed KSP Engineer Redux last night, but I'll remove for testing.

And no, I've been using 115200 as my Baud rate, because it seemed to fix the issues I had with v12.

So I will do some testing with that too.

Hey have you tried increasing the arduino serial buffer size? See posts #1201-1204. Seems to have fixed the problem for bork_bork.

I forgot about that thing :blush: Thanks for the reminder :)

I'll try to increase the arduino serial buffer today and I'll report back here with my findings :)

[And I'll try using your plugins, Zitronen, before trying Zalta's]

Thank you :D

***

UPDATE

***

That linked worked. Increasing serial size seems to have done it. I'll do some more testing, but hopefully this should be the solution! :)

Thanks for the reminder, Zitronen! :D

I floated that suggestion to Sputnix as well, and they said they'd give it a crack this week. Should hopefully have some confirmation soon. :)

Yep. Confirmation here -- that's a go! :D Thanks again!

Edited by Sputnix

Share this post


Link to post
Share on other sites
No that's software serial, I think this is the correct one: http://www.hobbytronics.co.uk/arduino-serial-buffer-size

I can't find such thing in my arduino install. The hardware folder contains only the tools folder and one .exe inside. nothing else.

I use Arduino MEGA, could you help me with it? I'm having problems with the communication freezing for 2~3 seconds every now and then.

Share this post


Link to post
Share on other sites
I can't find such thing in my arduino install. The hardware folder contains only the tools folder and one .exe inside. nothing else.

I use Arduino MEGA, could you help me with it? I'm having problems with the communication freezing for 2~3 seconds every now and then.

Are you using the latest IDE version 1.6.5?

The link is a bit out of date, but any way you need to find HardwareSerial.h, for me it's in Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino, and change #define SERIAL_RX_BUFFER_SIZE 64 to #define SERIAL_RX_BUFFER_SIZE 256

Bear in mind this will increase RAM usage for all your programs unless you change it back.

Share this post


Link to post
Share on other sites
Are you using the latest IDE version 1.6.5?

The link is a bit out of date, but any way you need to find HardwareSerial.h, for me it's in Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino, and change #define SERIAL_RX_BUFFER_SIZE 64 to #define SERIAL_RX_BUFFER_SIZE 256

Bear in mind this will increase RAM usage for all your programs unless you change it back.

I'm using IDE 1.6.5

Nope, nothing, the hardware folder has only "Tools" folder inside. I reached for some info in the web and looks like the boards information have to be downloaded from the web using the boards manager, the info is not stored where it was before in 1.0 :\

I tried to manually add the docs from the Arduino github with no success... I'll try this week to install the 1.0 arduino and run again with this modification.

Edited by Scoppio

Share this post


Link to post
Share on other sites

I've got IDE 1.6.3; the path Zitronen refers to is accurate for this version.

I might have missed this - what version windows do you have?

Have you got hidden files / folders showing? (on the off chances it's hidden);

Share this post


Link to post
Share on other sites

I checked on my laptop with 1.6.2, and can confirm Scoppio. My main computer, with 1.6.4, has the path that zitronen indicate (and it worked like a charm). That one was an upgrade from 1.6.1, though. I am mystified.

EDIT:

Did you upgrade from 1.6.2? I found this in the changelog for 1.6.3:

* AVR bundle files (core files and toolchain) is again inside IDE "hardware" folder, instead of unpacked into user's home folder at first IDE startup

Try to check

<userdir>\AppData\Roaming\Arduino15\packages\arduino\hardware\avr\<version>\cores\arduino

if on Windows, something like that if on Mac/Linux. It might be an artifact from an upgrade.

Edited by Freshmeat

Share this post


Link to post
Share on other sites
Hey have you tried increasing the arduino serial buffer size? See posts #1201-1204. Seems to have fixed the problem for bork_bork.

Note that both adjusting the refresh rate and increasing the buffer did the trick for me. I'm on the Mac version of the plug-in, though. I don't know how the refresh rate works on the windows version.

/BORK!

- - - Updated - - -

Would it be possible to add some error tracking to the arduino code to watch for serial buffer overflows? Putting in a bit of basic error handling really helped me track down the source of my connection drops.

Share this post


Link to post
Share on other sites

Hello all,

First off I want to say that I think this is one of the coolest addons available for KSP. Thanks to everyone who works on this for making something really interesting that has encouraged me to get back into arduino and has the potential to make my KSP experience much more fun!:D ...if I can get it working. :(

I just started building an Arduino controlled display/interface, but reading the last couple of pages it looks like I've picked a bad time.

Unfortunately, my computer has only Windows 10 on it. I'm testing using a clean KSP 1.0.4 install running at 32bit, along with version 13 of the demo sketch running on an Arduino Due. I'm also running the latest version f the Arduino IDE.

I have tried the following versions of the plugin:

0.17.3

0.17.4

Zalta's Dontwork fork

Zalta's Work fork

For the first three, my GLED communication light turns on briefly while the scene is loading, before turning off and remaining dark. Onscreen it tells me it hasn't found a display, and in my KSP log it finds the correct COM port for my Arduino, but then says "KSP Display not found":


[LOG 15:25:23.901] AddonLoader: Instantiating addon 'KSPSerialPort' from assembly 'KSPSerialIO'
[LOG 15:25:23.910] KSPSerialIO: Version 0.17.4
[LOG 15:25:23.912] KSPSerialIO: Getting serial ports...
[LOG 15:25:23.914] KSPSerialIO: Output packet size: 191/255
[LOG 15:25:23.924] KSPSerialIO: Found 2 serial ports
[LOG 15:25:23.926] KSPSerialIO: trying default port COM8
[LOG 15:25:23.934] Error opening serial port COM8: The port `COM8' does not exist.
[LOG 15:25:23.938] KSPSerialIO: trying port \Device\USBSER000 - COM5
[LOG 15:25:27.999] KSPSerialIO: KSP Display not found
[LOG 15:25:28.000] AddonLoader: Instantiating addon 'KSPSerialIO' from assembly 'KSPSerialIO'

Zalta's Work fork does some interesting stuff, however. My GLED sometimes briefly lights up as with the other three versions, but usually doesn't. Either way, once the scene is loaded all my leds remain blank and hardware controls are unresponsive. Nevertheless, the onscreen notification and the KSP log file both tell me I'm getting a connection:


[LOG 14:09:08.801] AddonLoader: Instantiating addon 'KSPSerialPort' from assembly 'KSPSerialIO'
[LOG 14:09:08.812] KSPSerialIO: Version 0.17.4
[LOG 14:09:08.814] KSPSerialIO: Getting serial ports...
[LOG 14:09:08.816] KSPSerialIO: Output packet size: 191/255
[LOG 14:09:08.822] KSPSerialIO: Found 2 serial ports
[LOG 14:09:08.824] KSPSerialIO: trying default port COM8
[LOG 14:09:08.832] Error opening serial port COM8: The port `COM8' does not exist.
[LOG 14:09:08.836] KSPSerialIO: trying port \Device\USBSER000 - COM5
[LOG 14:09:11.450] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:11.451] 164
[LOG 14:09:11.453] KSPSerialIO : Calling succefull ...
[LOG 14:09:11.456] KSPSerialIO : Calling succefull ...
[LOG 14:09:11.458] KSPSerialIO : Calling succefull ...
[LOG 14:09:11.459] KSPSerialIO : Calling succefull ...
[LOG 14:09:11.461] KSPSerialIO : Calling succefull ...
[LOG 14:09:11.463] KSPSerialIO : Calling succefull ...
[LOG 14:09:11.464] KSPSerialIO : Calling succefull ...
[LOG 14:09:11.497] KSPSerialIO: found KSP Display at COM5
[LOG 14:09:11.499] AddonLoader: Instantiating addon 'KSPSerialIO' from assembly 'KSPSerialIO'

Unfortunately, the game runs at an abysmal framerate with this version, and the plugin spams the log fiercely:


[LOG 14:09:21.570] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:21.572] 0
[LOG 14:09:21.668] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:21.670] 0
[LOG 14:09:21.775] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:21.779] 0
[LOG 14:09:21.784] [PlanetariumCamera]: Focus: Kerbal X
[LOG 14:09:21.964] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:21.967] 0
[WRN 14:09:22.060] [CurrencyWidgetsApp]: No live widgets to Despawn!
[LOG 14:09:22.065] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.068] 0
[LOG 14:09:22.126] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.128] 0
[LOG 14:09:22.228] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.231] 0
[LOG 14:09:22.310] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.312] 0
[LOG 14:09:22.382] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.385] 0
[LOG 14:09:22.455] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.458] 0
[LOG 14:09:22.535] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.538] 0
[LOG 14:09:22.605] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.607] 0
[LOG 14:09:22.681] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.683] 0
[LOG 14:09:22.757] KSPSerialIO : Calling our event, trying to read ...
[LOG 14:09:22.760] 0

In each case, I made sure the baud rate matched, tried increasing the Handshake Delay, etc.

If you want to take a look at the full logs, you can get them HERE.

I'm guessing this is an ongoing problem with Windows 10, but I figured I'd post my test results here for posterity, and to see if anyone has suggestions for how I might try to get the plugin working.

Thanks!

Share this post


Link to post
Share on other sites

I just started building an Arduino controlled display/interface, but reading the last couple of pages it looks like I've picked a bad time.

Unfortunately, my computer has only Windows 10 on it. I'm testing using a clean KSP 1.0.4 install running at 32bit, along with version 13 of the demo sketch running on an Arduino Due. I'm also running the latest version f the Arduino IDE.

I'm running Windows 7 and it works fine. If you can get your hands on a copy of 7 it would probably be the best way on getting on with your project. Another alternative would be to run Linux and get proper 64 bit support too.

Share this post


Link to post
Share on other sites

Would it be possible to add some error tracking to the arduino code to watch for serial buffer overflows? Putting in a bit of basic error handling really helped me track down the source of my connection drops.

Share this post


Link to post
Share on other sites
Hello all,

I'm guessing this is an ongoing problem with Windows 10, but I figured I'd post my test results here for posterity, and to see if anyone has suggestions for how I might try to get the plugin working.

Thanks!

I don't have framerate issues, but my PC is verry powerfull.

As said Zitronen, removing the event is not a pretty solution, because we force the plugin to work everytime, and it's bad.

I have delays problem with my code, the response time is something like .1 or .2 seconds.

I think I'm going to give up, and install Windows 7 in dual-boot.

Share this post


Link to post
Share on other sites
I don't have framerate issues, but my PC is verry powerfull.

As said Zitronen, removing the event is not a pretty solution, because we force the plugin to work everytime, and it's bad.

I have delays problem with my code, the response time is something like .1 or .2 seconds.

I think I'm going to give up, and install Windows 7 in dual-boot.

Have you tried contacting Marzubus? I think he had the same event issue on Mac/Linux. Maybe he knows the cause of it.

Share this post


Link to post
Share on other sites
I checked on my laptop with 1.6.2, and can confirm Scoppio. My main computer, with 1.6.4, has the path that zitronen indicate (and it worked like a charm). That one was an upgrade from 1.6.1, though. I am mystified.

EDIT:

Did you upgrade from 1.6.2? I found this in the changelog for 1.6.3:

* AVR bundle files (core files and toolchain) is again inside IDE "hardware" folder, instead of unpacked into user's home folder at first IDE startup

Try to check

<userdir>\AppData\Roaming\Arduino15\packages\arduino\hardware\avr\<version>\cores\arduino

if on Windows, something like that if on Mac/Linux. It might be an artifact from an upgrade.

Perfect! I totally forgot to check AppData/Roaming! I'll try the change today at night!

Share this post


Link to post
Share on other sites

I need some help to understand how works the Action Groups and buttons.

This is my code at my arduino that controls the action groups.

if (digitalRead(BT_B)) {

switch (pointer){

case 0:

break;

case 1:

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

case 10:

ControlGroups( pointer, HIGH);//~bitRead(VData.ActionGroups,pointer+5));

break;

case 11:

MainControls(SAS, HIGH);//~bitRead(VData.ActionGroups,AGSAS));

break;

case 12:

MainControls(RCS, HIGH);//~bitRead(VData.ActionGroups,AGRCS));

break;

case 13:

MainControls(LIGHTS, HIGH);//~bitRead(VData.ActionGroups,AGLight));

break;

case 14:

MainControls(GEAR, HIGH);//~bitRead(VData.ActionGroups,AGGear));

break;

case 15:

MainControls(BRAKES, HIGH);// ~bitRead(VData.ActionGroups,AGBrakes));

break;

case 16:

MainControls(PRECISION, HIGH);//~precision);

//precision = ~precision;

break;

case 17:

MainControls(ABORT, HIGH);//~bitRead(VData.ActionGroups,AGAbort));

break;

case 18:

MainControls(STAGE, HIGH);

/*

if (Connected) {

KSPBoardSendData(details(CPacket));

}

while(digitalRead(BT_B));

MainControls(STAGE, LOW);

/*/

break;

}

if (Connected) {

KSPBoardSendData(details(CPacket));

}

while(digitalRead(BT_B));

}

The signal that I must send to turn ON action group 1 is a pulse or it has to stay on all the time I want ACG-1 to be on?

For STAGE, I need a pulse? How long it have to be?

And the PRECISION do not seen to work.

Share this post


Link to post
Share on other sites

Also, with the bigger buffer size the project is running really smoth! It is AWESOME to see the RT-update of my artificial horizon, Now I CAN pilot using instrumentations :D

Share this post


Link to post
Share on other sites

I'd like to start by thanking you for making this mod, Zitronen. I've been working with it off and on the last week and I'm having a ton of fun (and learning lots!). I'm finally going to be able to build the Kerbal Kontrol Panel I've been wanting!

So things were going well. I've had the brakes, gear, lights, RCS, SAS, Abort, Stage, and throttle all working great on my breadboard circuit. Last night I tried adding an LCD screen with a serial backpack and things went south. When I have any code in for the LCD I can't use anything else. While in KSP I can see it finds the right port and all, but it says "No display found." Also, the lights stay off and none of the buttons work. I have the throttle set so it only comes from the controller, but when the LCD is programmed in, I can fully control from the keyboard. The controller doesn't even try. Also, the LCD screen says "0.00" but I'm guessing thats just from the "Setup" function?

I've dabbled in programming for a long time, but I took a few years off due to school. I've also never messed with this sort of thing, especially the hardware end. Dealing with serial is totally new to me, so this is probably a dummy error. :sticktongue:

Specs:

I have an Arduino Uno (The mega is on its way! :D)

The LCD is a 20x4 on pin 2 (I'll post the code too)

I have THIS serial backpack on the LCD

NOTE: I'm running out of pins and wire for my breadboard, so I don't have the LEDs functioning anymore.

I've only altered the Output and the KSPIODemo12 tab for the LCD, so I'll include those two.

MY CODE:


#include <SoftwareSerial.h>


SoftwareSerial lcd(3, 2); //LCD on pin 2
int k = 0; //Variable for the LCD update cycle


//pins for LEDs
#define GLED 5
//#define YLED 6
//#define RLED 7
//#define SASLED 11
//#define RCSLED 12
//#define CG1LED 13


//pins for input
#define SASPIN 8
#define RCSPIN 9
//#define CG1PIN 10
#define THROTTLEPIN 0
#define STAGEPIN 7
#define ABORTPIN 10
#define LIGHTPIN 11
#define BRAKEPIN 12
#define GEARPIN 13




#define THROTTLEDB 4 //Throttle axis deadband


//Input enums
#define SAS 7
#define RCS 6
#define LIGHTS 5
#define GEAR 4
#define BRAKES 3
#define PRECISION 2
#define ABORT 1
#define STAGE 0


//Action group statuses
#define AGSAS 0
#define AGRCS 1
#define AGLight 2
#define AGGear 3
#define AGBrakes 4
#define AGAbort 5
#define AGCustom01 6
#define AGCustom02 7
#define AGCustom03 8
#define AGCustom04 9
#define AGCustom05 10
#define AGCustom06 11
#define AGCustom07 12
#define AGCustom08 13
#define AGCustom09 14
#define AGCustom10 15


//macro
#define details(name) (uint8_t*)&name,sizeof(name)


//if no message received from KSP for more than 2s, go idle
#define IDLETIMER 2000
#define CONTROLREFRESH 25


//warnings
#define GWARN 9 //9G Warning
#define GCAUTION 5 //5G Caution
#define FUELCAUTION 10.0 //10% Fuel Caution
#define FUELWARN 5.0 //5% Fuel warning


unsigned long deadtime, deadtimeOld, controlTime, controlTimeOld;
unsigned long now;


boolean Connected = false;


byte caution = 0, warning = 0, id;


struct VesselData
{
byte id; //1
float AP; //2
float PE; //3
float SemiMajorAxis; //4
float SemiMinorAxis; //5
float VVI; //6
float e; //7
float inc; //8
float G; //9
long TAp; //10
long TPe; //11
float TrueAnomaly; //12
float Density; //13
long period; //14
float RAlt; //15
float Alt; //16
float Vsurf; //17
float Lat; //18
float Lon; //19
float LiquidFuelTot; //20
float LiquidFuel; //21
float OxidizerTot; //22
float Oxidizer; //23
float EChargeTot; //24
float ECharge; //25
float MonoPropTot; //26
float MonoProp; //27
float IntakeAirTot; //28
float IntakeAir; //29
float SolidFuelTot; //30
float SolidFuel; //31
float XenonGasTot; //32
float XenonGas; //33
float LiquidFuelTotS; //34
float LiquidFuelS; //35
float OxidizerTotS; //36
float OxidizerS; //37
uint32_t MissionTime; //38
float deltaTime; //39
float VOrbit; //40
uint32_t MNTime; //41
float MNDeltaV; //42
float Pitch; //43
float Roll; //44
float Heading; //45
uint16_t ActionGroups; //46 status bit order:SAS, RCS, Light, Gear, Brakes, Abort, Custom01 - 10
byte SOINumber; //47 SOI Number (decimal format: sun-planet-moon e.g. 130 = kerbin, 131 = mun)
byte MaxOverHeat; //48 Max part overheat (% percent)
float MachNumber; //49
float IAS; //50 Indicated Air Speed
};


struct HandShakePacket
{
byte id;
byte M1;
byte M2;
byte M3;
};


struct ControlPacket {
byte id;
byte MainControls; //SAS RCS Lights Gear Brakes Precision Abort Stage
byte Mode; //0 = stage, 1 = docking, 2 = map
unsigned int ControlGroup; //control groups 1-10 in 2 bytes
byte AdditionalControlByte1; //other stuff
byte AdditionalControlByte2;
int Pitch; //-1000 -> 1000
int Roll; //-1000 -> 1000
int Yaw; //-1000 -> 1000
int TX; //-1000 -> 1000
int TY; //-1000 -> 1000
int TZ; //-1000 -> 1000
int WheelSteer; //-1000 -> 1000
int Throttle; // 0 -> 1000
int WheelThrottle; // 0 -> 1000
};


HandShakePacket HPacket;
VesselData VData;
ControlPacket CPacket;


void setup() {
Serial.begin(38400);


//LCD SETUP
Serial.begin(9600); //Starts the serial for the LCD
lcd.begin(9600); //Starts LCD
lcd.write(254); //Calls special command
lcd.write(0x10); //Sets BAUD to 38400
delay(500); //Waits 500
lcd.write(254); //Calls special command
lcd.write(1); //Clears the LCD
lcd.print(VData.G); //Displays the Gee forces (Only to start - output tab contains update function)





initLEDS();
InitTxPackets();
controlsInit();


LEDSAllOff();
}


void loop()
{
input();
output();
}


void output() {
now = millis();
controlTime = now - controlTimeOld;
if (controlTime > CONTROLREFRESH){
controlTimeOld = now;
controls();
}
}


void controls() {
if (Connected) {


if (digitalRead(SASPIN)) //--------- This is how you do main controls
MainControls(SAS, HIGH);
else
MainControls(SAS, LOW);


if (digitalRead(RCSPIN))
MainControls(RCS, HIGH);
else
MainControls(RCS, LOW);

if (digitalRead(STAGEPIN))
MainControls(STAGE, HIGH);
else
MainControls(STAGE, LOW);


if (digitalRead(ABORTPIN))
MainControls(ABORT, HIGH);
else
MainControls(ABORT, LOW);


if (digitalRead(LIGHTPIN))
MainControls(LIGHTS, HIGH);
else
MainControls(LIGHTS, LOW);


if (digitalRead(BRAKEPIN))
MainControls(BRAKES, HIGH);
else
MainControls(BRAKES, LOW);


if (digitalRead(GEARPIN))
MainControls(GEAR, HIGH);
else
MainControls(GEAR, LOW);


// if (digitalRead(CG1PIN)) //--------- This is how you do control groups
// ControlGroups(1, HIGH);
// else
// ControlGroups(1, LOW);


//This is an example of reading analog inputs to an axis, with deadband and limits
CPacket.Throttle = constrain(map(analogRead(THROTTLEPIN),THROTTLEDB,1024-THROTTLEDB,0,1000),0, 1000);


// This is so the LCD only gets every third packet (fixes refresh problem)
if (k == 3){
UPLCD();
k=0;
} else {
k++;
}


KSPBoardSendData(details(CPacket));
}
}


void UPLCD() {
lcd.write(254); //Calls the special command
lcd.write(1); //Clears the screen
lcd.print(VData.G); //Displays the Gee forces on screen

}


void controlsInit() {
pinMode(SASPIN, INPUT_PULLUP);
pinMode(RCSPIN, INPUT_PULLUP);
// pinMode(CG1PIN, INPUT_PULLUP);
pinMode(STAGEPIN, INPUT_PULLUP);
pinMode(ABORTPIN, INPUT_PULLUP);
pinMode(LIGHTPIN, INPUT_PULLUP);
pinMode(BRAKEPIN, INPUT_PULLUP);
pinMode(GEARPIN, INPUT_PULLUP);
}


void MainControls(byte n, boolean s) {
if (s)
CPacket.MainControls |= (1 << n); // forces nth bit of x to be 1. all other bits left alone.
else
CPacket.MainControls &= ~(1 << n); // forces nth bit of x to be 0. all other bits left alone.
}


void ControlGroups(byte n, boolean s) {
if (s)
CPacket.ControlGroup |= (1 << n); // forces nth bit of x to be 1. all other bits left alone.
else
CPacket.ControlGroup &= ~(1 << n); // forces nth bit of x to be 0. all other bits left alone.
}


The code probably looks messy, I'm going to clean it up once I get everything working and the Mega hooked up.

Any ideas on what I'm doing wrong? I can send the logs too if you'd like, but they just say "Found on COM3" and then "No display".

Thanks in advance for your help! And thanks for this awesome mod! :)

Share this post


Link to post
Share on other sites
I'd like to start by thanking you for making this mod, Zitronen. I've been working with it off and on the last week and I'm having a ton of fun (and learning lots!). I'm finally going to be able to build the Kerbal Kontrol Panel I've been wanting!

So things were going well. I've had the brakes, gear, lights, RCS, SAS, Abort, Stage, and throttle all working great on my breadboard circuit. Last night I tried adding an LCD screen with a serial backpack and things went south. When I have any code in for the LCD I can't use anything else. While in KSP I can see it finds the right port and all, but it says "No display found." Also, the lights stay off and none of the buttons work. I have the throttle set so it only comes from the controller, but when the LCD is programmed in, I can fully control from the keyboard. The controller doesn't even try. Also, the LCD screen says "0.00" but I'm guessing thats just from the "Setup" function?

I've dabbled in programming for a long time, but I took a few years off due to school. I've also never messed with this sort of thing, especially the hardware end. Dealing with serial is totally new to me, so this is probably a dummy error. :sticktongue:

Specs:

I have an Arduino Uno (The mega is on its way! :D)

The LCD is a 20x4 on pin 2 (I'll post the code too)

I have THIS serial backpack on the LCD

NOTE: I'm running out of pins and wire for my breadboard, so I don't have the LEDs functioning anymore.

I've only altered the Output and the KSPIODemo12 tab for the LCD, so I'll include those two.

MY CODE:


#include <SoftwareSerial.h>


SoftwareSerial lcd(3, 2); //LCD on pin 2
int k = 0; //Variable for the LCD update cycle


//pins for LEDs
#define GLED 5
//#define YLED 6
//#define RLED 7
//#define SASLED 11
//#define RCSLED 12
//#define CG1LED 13


//pins for input
#define SASPIN 8
#define RCSPIN 9
//#define CG1PIN 10
#define THROTTLEPIN 0
#define STAGEPIN 7
#define ABORTPIN 10
#define LIGHTPIN 11
#define BRAKEPIN 12
#define GEARPIN 13




#define THROTTLEDB 4 //Throttle axis deadband


//Input enums
#define SAS 7
#define RCS 6
#define LIGHTS 5
#define GEAR 4
#define BRAKES 3
#define PRECISION 2
#define ABORT 1
#define STAGE 0


//Action group statuses
#define AGSAS 0
#define AGRCS 1
#define AGLight 2
#define AGGear 3
#define AGBrakes 4
#define AGAbort 5
#define AGCustom01 6
#define AGCustom02 7
#define AGCustom03 8
#define AGCustom04 9
#define AGCustom05 10
#define AGCustom06 11
#define AGCustom07 12
#define AGCustom08 13
#define AGCustom09 14
#define AGCustom10 15


//macro
#define details(name) (uint8_t*)&name,sizeof(name)


//if no message received from KSP for more than 2s, go idle
#define IDLETIMER 2000
#define CONTROLREFRESH 25


//warnings
#define GWARN 9 //9G Warning
#define GCAUTION 5 //5G Caution
#define FUELCAUTION 10.0 //10% Fuel Caution
#define FUELWARN 5.0 //5% Fuel warning


unsigned long deadtime, deadtimeOld, controlTime, controlTimeOld;
unsigned long now;


boolean Connected = false;


byte caution = 0, warning = 0, id;


struct VesselData
{
byte id; //1
float AP; //2
float PE; //3
float SemiMajorAxis; //4
float SemiMinorAxis; //5
float VVI; //6
float e; //7
float inc; //8
float G; //9
long TAp; //10
long TPe; //11
float TrueAnomaly; //12
float Density; //13
long period; //14
float RAlt; //15
float Alt; //16
float Vsurf; //17
float Lat; //18
float Lon; //19
float LiquidFuelTot; //20
float LiquidFuel; //21
float OxidizerTot; //22
float Oxidizer; //23
float EChargeTot; //24
float ECharge; //25
float MonoPropTot; //26
float MonoProp; //27
float IntakeAirTot; //28
float IntakeAir; //29
float SolidFuelTot; //30
float SolidFuel; //31
float XenonGasTot; //32
float XenonGas; //33
float LiquidFuelTotS; //34
float LiquidFuelS; //35
float OxidizerTotS; //36
float OxidizerS; //37
uint32_t MissionTime; //38
float deltaTime; //39
float VOrbit; //40
uint32_t MNTime; //41
float MNDeltaV; //42
float Pitch; //43
float Roll; //44
float Heading; //45
uint16_t ActionGroups; //46 status bit order:SAS, RCS, Light, Gear, Brakes, Abort, Custom01 - 10
byte SOINumber; //47 SOI Number (decimal format: sun-planet-moon e.g. 130 = kerbin, 131 = mun)
byte MaxOverHeat; //48 Max part overheat (% percent)
float MachNumber; //49
float IAS; //50 Indicated Air Speed
};


struct HandShakePacket
{
byte id;
byte M1;
byte M2;
byte M3;
};


struct ControlPacket {
byte id;
byte MainControls; //SAS RCS Lights Gear Brakes Precision Abort Stage
byte Mode; //0 = stage, 1 = docking, 2 = map
unsigned int ControlGroup; //control groups 1-10 in 2 bytes
byte AdditionalControlByte1; //other stuff
byte AdditionalControlByte2;
int Pitch; //-1000 -> 1000
int Roll; //-1000 -> 1000
int Yaw; //-1000 -> 1000
int TX; //-1000 -> 1000
int TY; //-1000 -> 1000
int TZ; //-1000 -> 1000
int WheelSteer; //-1000 -> 1000
int Throttle; // 0 -> 1000
int WheelThrottle; // 0 -> 1000
};


HandShakePacket HPacket;
VesselData VData;
ControlPacket CPacket;


void setup() {
Serial.begin(38400);


//LCD SETUP
Serial.begin(9600); //Starts the serial for the LCD
lcd.begin(9600); //Starts LCD
lcd.write(254); //Calls special command
lcd.write(0x10); //Sets BAUD to 38400
delay(500); //Waits 500
lcd.write(254); //Calls special command
lcd.write(1); //Clears the LCD
lcd.print(VData.G); //Displays the Gee forces (Only to start - output tab contains update function)





initLEDS();
InitTxPackets();
controlsInit();


LEDSAllOff();
}


void loop()
{
input();
output();
}


void output() {
now = millis();
controlTime = now - controlTimeOld;
if (controlTime > CONTROLREFRESH){
controlTimeOld = now;
controls();
}
}


void controls() {
if (Connected) {


if (digitalRead(SASPIN)) //--------- This is how you do main controls
MainControls(SAS, HIGH);
else
MainControls(SAS, LOW);


if (digitalRead(RCSPIN))
MainControls(RCS, HIGH);
else
MainControls(RCS, LOW);

if (digitalRead(STAGEPIN))
MainControls(STAGE, HIGH);
else
MainControls(STAGE, LOW);


if (digitalRead(ABORTPIN))
MainControls(ABORT, HIGH);
else
MainControls(ABORT, LOW);


if (digitalRead(LIGHTPIN))
MainControls(LIGHTS, HIGH);
else
MainControls(LIGHTS, LOW);


if (digitalRead(BRAKEPIN))
MainControls(BRAKES, HIGH);
else
MainControls(BRAKES, LOW);


if (digitalRead(GEARPIN))
MainControls(GEAR, HIGH);
else
MainControls(GEAR, LOW);


// if (digitalRead(CG1PIN)) //--------- This is how you do control groups
// ControlGroups(1, HIGH);
// else
// ControlGroups(1, LOW);


//This is an example of reading analog inputs to an axis, with deadband and limits
CPacket.Throttle = constrain(map(analogRead(THROTTLEPIN),THROTTLEDB,1024-THROTTLEDB,0,1000),0, 1000);


// This is so the LCD only gets every third packet (fixes refresh problem)
if (k == 3){
UPLCD();
k=0;
} else {
k++;
}


KSPBoardSendData(details(CPacket));
}
}


void UPLCD() {
lcd.write(254); //Calls the special command
lcd.write(1); //Clears the screen
lcd.print(VData.G); //Displays the Gee forces on screen

}


void controlsInit() {
pinMode(SASPIN, INPUT_PULLUP);
pinMode(RCSPIN, INPUT_PULLUP);
// pinMode(CG1PIN, INPUT_PULLUP);
pinMode(STAGEPIN, INPUT_PULLUP);
pinMode(ABORTPIN, INPUT_PULLUP);
pinMode(LIGHTPIN, INPUT_PULLUP);
pinMode(BRAKEPIN, INPUT_PULLUP);
pinMode(GEARPIN, INPUT_PULLUP);
}


void MainControls(byte n, boolean s) {
if (s)
CPacket.MainControls |= (1 << n); // forces nth bit of x to be 1. all other bits left alone.
else
CPacket.MainControls &= ~(1 << n); // forces nth bit of x to be 0. all other bits left alone.
}


void ControlGroups(byte n, boolean s) {
if (s)
CPacket.ControlGroup |= (1 << n); // forces nth bit of x to be 1. all other bits left alone.
else
CPacket.ControlGroup &= ~(1 << n); // forces nth bit of x to be 0. all other bits left alone.
}


The code probably looks messy, I'm going to clean it up once I get everything working and the Mega hooked up.

Any ideas on what I'm doing wrong? I can send the logs too if you'd like, but they just say "Found on COM3" and then "No display".

Thanks in advance for your help! And thanks for this awesome mod! :)

Easy fix!

You forgot to tell that the LCD is using a different COM ;)

Make sure you code FIRST the KSP-ARDU comunication and only then you setup the LCD COM.

void setup() {

Serial.begin(38400);

InitTxPackets();

controlsInit();

//finished the setup of the KSP-ARDU COM

//start and set other gizmos below this line

Monitor.begin(TVSYSTEM, SCREENWIDTH, SCREENHEIGHT);

Monitor.select_font(font6x8);

Monitor.clear_screen();

Monitor.println(10, 10, "KERBAL CONSOLE");

Monitor.print("v 0.2");

Monitor.delay_frame(120);

Monitor.clear_screen();

}

Share this post


Link to post
Share on other sites
The signal that I must send to turn ON action group 1 is a pulse or it has to stay on all the time I want ACG-1 to be on?

For STAGE, I need a pulse? How long it have to be?

Action groups are designed to work with toggle inputs. They're only on while the appropriate bit in the ControlGroup is high, they can't be pulsed. If you want to use momentary buttons or similar you'll need to latch your inputs. I talked about some code I wrote to do that back in this post.

Stage does just have to be a pulse. Pretty sure everybody does it by just sending the current state of the stage button, so their pulse is as long as they hold the button. But I strongly suspect pulsing it for a single packet will work too.

And the PRECISION do not seen to work.

There's been a bit for precision in MainControls since forever, but it's never been hooked up. I vaguely recall zitronen complaining about his attempts at it being too unreliable to ship. I haven't had much time to play with this plugin for a little while, but I wouldn't mind trying out the ides from this thread for that though.

- - - Updated - - -


void setup() {
Serial.begin(38400);


//LCD SETUP
Serial.begin(9600); //Starts the serial for the LCD
...

You initially set the serial port speed to 38400, but then immediately set it again to 9600. I guess that's left over from previous LCD test code? :) Remove that second Serial.begin and you're probably good to go. But I'd check the rest of the LCD code you added to make sure there's no other debug stuff being printed to the serial port.

Edited by stibbons

Share this post


Link to post
Share on other sites
You initially set the serial port speed to 38400, but then immediately set it again to 9600. I guess that's left over from previous LCD test code? :) Remove that second Serial.begin and you're probably good to go. But I'd check the rest of the LCD code you added to make sure there's no other debug stuff being printed to the serial port.

That fixed it. *facepalm* I knew when I asked here that it would be something obvious! Thank you! It was left over, like you said. It was late when I was at that stage and I over looked it.

Easy fix!

You forgot to tell that the LCD is using a different COM :wink:

Make sure you code FIRST the KSP-ARDU comunication and only then you setup the LCD COM.

void setup() {

Serial.begin(38400);

InitTxPackets();

controlsInit();

//finished the setup of the KSP-ARDU COM

//start and set other gizmos below this line

Monitor.begin(TVSYSTEM, SCREENWIDTH, SCREENHEIGHT);

Monitor.select_font(font6x8);

Monitor.clear_screen();

Monitor.println(10, 10, "KERBAL CONSOLE");

Monitor.print("v 0.2");

Monitor.delay_frame(120);

Monitor.clear_screen();

}

I'm new to this LCD and serial stuff. What do you mean use a different COM? It sounds important. Does it matter that this is a character based LCD?

That being said, my LCD is updating now, but it cuts out at random times. All the controls will cut out and the LCD will quit updating. It'll start up randomly, sometimes it won't come up at all. Is this related to that?

Thanks again for your help guys!

Edited by Rapidwolf

Share this post


Link to post
Share on other sites

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.