Jump to content

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


zitronen

Recommended Posts

So, I'm newish to Arduino and I'm pretty confused about the Input for displays. I have a 16x2 LCD I'd like to hook up. How do I get the values (ie, Fuel, speed, Alt, etc.) and feed them to the display?

Hmm well without knowing what LCD you have it's guesswork, honestly. You should have gotten a datasheet with the LCD, consult that to find out what the LCD can do and how (something you'll have to get used to, no matter what project).

But if you're using a "real" Arduino and the Arduino IDE to write your code someone else will probably already have solved that problem for you. You might want to take a look into this library: http://arduino.cc/en/Reference/LiquidCrystal?from=Tutorial.LCDLibrary

Link to comment
Share on other sites

Thanks for the reply.

The issue is not getting the data onto the LCD or what it is capable of, I've done some of the LiquidCrystal libraries for it recently. I should clarify that I specifically mean how to call the values from the plugin as variables. So I want to go roughly:

lcd.write(Radar Altitude)

or something along those lines. so the result would look like (with more coding and all)

rALT 123 ALT 456

vSPD 789 SPD 246

with those randoms numbers changing as it received data from the plugin. I don't know how the plugin sends out the numbers so I can feed them into the display.

Link to comment
Share on other sites

Oh wow I completely misread your other post then. Sorry :D

Take a look at the "demo8" from Zitronen, it doesn't really get much easier than that.


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
};


VesselData VData;

void loop()
{
input();
// from here on you can read KSP data such as VData.RAlt

output();
}

Please note the comment near the end.

Link to comment
Share on other sites

Oh, thanks! I missed that comment in the code, I was confused and instead was looking at the numbers that were commented out. So if I want to sub in the rALT data I use the variable "VDATA.RALT", and for the compass heading I'd use "VDATA.HEADING". I think I have this resolved now.

Thanks!

Link to comment
Share on other sites

Hi everyone. I am new to the Arduino and an avid KSP player. I really love this game. As I started to mess around with the Arduino I started to get a lot of good ideas and I started to look around for other great projects. This is by far the best project to work on. What I want to do is what Swonely is talking about, have external LCDs to give me data on Altitude and Radar Altitude things like that. I pretty much spent the last 2 days reading up all the posts here, but I am getting very confused on a lot of things. The biggest thing I am confused on is the Arduino code. I downloaded DEMO 4 and looked though the code, but there are several sketches. I thought (and pretty sure) the Arduino can only use one sketch at a time. Also I notice there is not void setup code in most, if not all the sketches. I want to test the Radar Alt function by having the Arduino println command display the value in the COM viewer, but I don't know how to fetch that data. If someone could please guide me in the right direction, it would be greatly appreciated.

Link to comment
Share on other sites

Hi everyone. I am new to the Arduino and an avid KSP player. I really love this game. As I started to mess around with the Arduino I started to get a lot of good ideas and I started to look around for other great projects. This is by far the best project to work on. What I want to do is what Swonely is talking about, have external LCDs to give me data on Altitude and Radar Altitude things like that. I pretty much spent the last 2 days reading up all the posts here, but I am getting very confused on a lot of things. The biggest thing I am confused on is the Arduino code. I downloaded DEMO 4 and looked though the code, but there are several sketches. I thought (and pretty sure) the Arduino can only use one sketch at a time. Also I notice there is not void setup code in most, if not all the sketches. I want to test the Radar Alt function by having the Arduino println command display the value in the COM viewer, but I don't know how to fetch that data. If someone could please guide me in the right direction, it would be greatly appreciated.

You probably won't be able to use the com viewer (it's a serial port terminal, actually) since the connection between KSP and the Arduino is already using that. Try it out though, I'm not using Arduinos much (only the Atmel chips on the Arduino). However if you get a "port is busy" message or similar while KSP and Arduino are working together my suspicion is confirmed.

Your confusion about the several sketches is actually quite simple: Only the KSPIODemo8 is the actual sketch, the other .ino files are includes. Zitronen separated the code into several files to keep things organized. If you're familiar with C, the KSPIODemo8.ino is the main.c and the other files are refered to with #include statments.

This is actually one of the main issues why I don't like the Arduino IDE much - too much abstraction. It's great to get started but as soon as projects get a bit more complex I favor control over ease of use. But that's just me, YMMV

Link to comment
Share on other sites

Hi everyone. I am new to the Arduino and an avid KSP player. I really love this game. As I started to mess around with the Arduino I started to get a lot of good ideas and I started to look around for other great projects. This is by far the best project to work on. What I want to do is what Swonely is talking about, have external LCDs to give me data on Altitude and Radar Altitude things like that. I pretty much spent the last 2 days reading up all the posts here, but I am getting very confused on a lot of things. The biggest thing I am confused on is the Arduino code. I downloaded DEMO 4 and looked though the code, but there are several sketches. I thought (and pretty sure) the Arduino can only use one sketch at a time. Also I notice there is not void setup code in most, if not all the sketches. I want to test the Radar Alt function by having the Arduino println command display the value in the COM viewer, but I don't know how to fetch that data. If someone could please guide me in the right direction, it would be greatly appreciated.

If you are new with arduino I would start by doing some simple arduino tutorials. I made the plugin for people who would already know how to use arduino but didn't have a way talk to KSP like myself. So unfortunately the arduino code is not very well explained.

What MrOnak said is correct, if you download the demo8 zip for tutorial 4, and put it in your arduino folder in my documents, it should be listed under "sketches" when you start your arduino IDE. Wire up some LEDs to the correct pins (explained in tutorial 1), and make sure it's working. Then you can try implementing your own stuff.

Edited by zitronen
Link to comment
Share on other sites

Thanks I will try that this weekend to see if it works then start fiddling with it. Thanks for the clarification.

I've been fiddling with the LCDs a bit in my free time. I can share some of my code once it's finished. I'm in midterms right now so I can't promise anything soon. The big thing is to look at all of the LiquidCyrstal libraries, and know what each command does. (See http://arduino.cc/en/Reference/LiquidCrystal). Then using that, make a smaller project, such as a simple clock or thermometer using a 16x2, so that you become familiar with variables and the code. A tutorial I've been looking at a lot is the adafruit one (see https://learn.adafruit.com/character-lcds/wiring-a-character-lcd). It explains the wiring and how some of the commands work.

Link to comment
Share on other sites

And I'm lost. Would anyone who has gotten a character display working please share the code? A working example would help a lot, I can figure the rest out after that.

Hmm I haven't worked with LCDs yet but what exactly is the problem? If you've gotten it to work in some other test project it should work with KSP as well?

Edited by MrOnak
Link to comment
Share on other sites

Hmm I haven't worked with LCDs yet but what exactly is the problem? If you've gotten it to work in some other test project it should work with KSP as well?

In theory, yes. In application, no. My problem is putting the LCD code I've written into the demo8 code. I tried copy and pasting it into a new tab, but that won't work because you can only run "Void Setup" once. So I defined the character "16x2" in that new tab, and the listed that variable in the demo8 tab's "Void Setup". I then called back to the tab which had all my display code, and that's when it fell apart. At this time, I'm just trying to write anything on the screen and then I'll start adding units, numbers, and other fun stuff. I may scrap it and start fresh.

Link to comment
Share on other sites

Err, well. Call whatever initialization routines you need for the LCD from within the setup() function in the KSPIODemo8 file. That'll set the LCD up just fine.

From there you can push data onto the LCD from within the loop() function in the KSPIODemo8 file, just make sure it's placed after the calls to input() or you're going to be 1 cycle late.

For example, I've made a test script using 7-segment displays. My stuff is in a separate file but my KSPIODemo8 file's functions have been altered to this:


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

InitTxPackets();
//controlsInit();
sevenSegInit();
}

void loop()
{
input();
// output();
sevenSegUpdateDisplay(VData.Alt);
}

The sevenSegInit() called at the end of setup() initializes all pins I need for my display and sevenSegUpdateDisplay(VData.Alt) inside loop() updates my display with the latest altimeter readout. Note I've disabled all control/output calls since I'm using this purely for testing and I don't need to send control information back to KSP.

Link to comment
Share on other sites

In theory, yes. In application, no. My problem is putting the LCD code I've written into the demo8 code. I tried copy and pasting it into a new tab, but that won't work because you can only run "Void Setup" once. So I defined the character "16x2" in that new tab, and the listed that variable in the demo8 tab's "Void Setup". I then called back to the tab which had all my display code, and that's when it fell apart. At this time, I'm just trying to write anything on the screen and then I'll start adding units, numbers, and other fun stuff. I may scrap it and start fresh.

OK so what you need to do is put all the stuff in setup() of your original code into a function, name it void LCDsetup() or something and call LCDsetup(); in the demo 8 setup();

Alternatively you can just copy your setup() into the demo 8 setup, and just paste it after init packets.

Do the same thing for your code for writing to the LCD, but put it in the loop(); function.

Edited by zitronen
Link to comment
Share on other sites

Okay, I may have got it working. I went back and looked at some code from another project, and lcd.print and lcd.write are two different things. Important note, semicolons and colons are two different things, semicolons are your friends. I'll have to hook up my Arduino once I get home. flight is the variable I added to the Void Setup. Here:

void loop()

{

input();

output();

flight();

}

And then for flight itself.

void flight () {

lcd.begin(16, 2);

lcd.print("ALT");

lcd.setCursor(1, 0);

lcd.write(VData.Alt);

delay(250);

}

If this doesn't work, I suspect it will be because lcd.begin it running over and over again. So I'll make a 16x2 variable to run in the setup, or when each menu is called.

Link to comment
Share on other sites

OK so what you need to do is put all the stuff in setup() of your original code into a function, name it void LCDsetup() or something and call LCDsetup(); in the demo 8 setup();

Alternatively you can just copy your setup() into the demo 9 setup, and just paste it after init packets.

Do the same thing for your code for writing to the LCD, but put it in the loop(); function.

I saw you post after I made my most recent one. I made the changes you suggested. My code is now verified finally, and I'll test it when I get the chance. Thanks for the help.

EDIT: Got a fully working display now, thanks guys!

Edited by Swonely
Update
Link to comment
Share on other sites

  • 2 weeks later...

So I've installed this and started playing around, and am busy building an autopilot that I can point to in the real world (as well as get practice with microcontrollers)! I have noticed, however, that I cannot control any part of the game that I have okay'd my UNO to control. Because I'm testing a sort of drag'n'drop mission fileset, I was hoping to install one module at a time and test behaviors without exiting all of the way out of the game, then going all the way back in just to give myself throttle control or pitch/roll control for a mission.

So! What I was hoping for was some sort of plugin that goes to Blizzy's toolbar or something that allows me to check/uncheck the controls I want the arduino to control, or perhaps some tribal knowledge of how to control this myself!

Thank you all, and keep up the great work!

Link to comment
Share on other sites

@postalbyke: From what you wrote I gather that you're rather new to the world of Arduino and microcontrollers in general? If you want to implement things like automated ascend / landing guidance, autopilots etc. on an Arduino and you're indeed new to microcontrollers, one word of advise:

Check the kOS plugin for KSP and implement your modules there first. Once they work, port them over to the Arduino and try again.

Why? Well a few reasons:

- Writing code for microcontrollers isn't the easiest type of software development since you're dealing with very basic (to say the least) debugging possibilities and the messiness of unclean electronic signals in addition to your software.

- kOS is a simple enough language and is editable in any text editor (Notepad++ is a good basic choice) while KSP is running. You have infinite chances to change your code while KSP runs, can simply revert to launch / quickload to try again. Changing the code on the microcontroller is more complicated (among other things you'll end up replugging the USB cable a LOT).

- kOS has an in-game console which is great for debugging. Since KSP and the Arduino use the serial connection to exchange data, the serial terminal console (Arduino "debugging console") is not available so you'd either have to attach an LCD display to the Arduino for debugging or resort to blinking LEDs or the like.

[edit to add]- like Zitronen said, you'll very likely need PID controllers. While easy to write (documentation on the interwebs is good) it's very painful to tune (trial and error unless you're really good with math) - kOS makes it easier simply because you can iterate over your tuning parameters faster.[/edit]

I write software for a living and I consider myself reasonably good at it. It took me several long days to come up with a decent ascend guidance / automatic lander code in kOS. I wouldn't want to go through those pains on an Arduino without having many options to get a clue what goes wrong where and why.

All that said, I wish you luck :)

Edited by MrOnak
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...