Jump to content

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


zitronen

Recommended Posts

Ok, just tested with a clean ksp install and the plugin now running (probably another mod on my main install messing things up)

Its not working right though, here is a run-through of how it behaved.

Before loading I had my switches set to "off"

I load the flight scene.

Plugin detects the com port and states that the SAS and RCS are ON (even though the switches are off...)

Now when i move the switches to "ON"... it turns the SAS and RCS off, so controls are reversed.

It also takes between 8 and 16 seconds for it to respond to the command at all each time.

I will run a few more tests after I eat! I'll see what happens if i start with the switches already set to "ON".. or maybe one set to "ON" and one set to "OFF".

I'll get a test done with the Mega later too, happy to beta test everything you can throw at us :)

...definitely wired to ground, not 5v

EDIT - the mod seems to also freeze the game after a while. Did the same thing on my mod install and the clean install.

Edited by Mulbin
Link to comment
Share on other sites

Test 3

This time I start with the switches already in the "ON" position.

I load the flight scene.

Plugin detects the com port ... but doesnt turn RCS or SAS on

Now when i move the switches to "OFF"... it turns the SAS and RCS on! So I think you have definitely got your on/off function reversed.

Still taking between 8 and 16 seconds between moving the switch and KSP actually responding.

...and once agin, after a couple of minutes, the game freezes up completely and has to be CTRL, ALT DEL to get out.

Edited by Mulbin
Link to comment
Share on other sites

Every time you load a scene it will sync with the arduino at the start,

...correct me if I'm wrong but wouldn't this be insufficient when docking or switching between craft close to each other? Or does it also read the switch states when you jump to another ship or dock with one?

Link to comment
Share on other sites

OK so how are you defining what is ON state? I am assuming that when the pins are connected to ground things are OFF, when the pins are not connected to ground things are ON.

If you switch vessels then things will probably not be in sync right now, but all you have to do is flip the switch that is not synced to sync it (it will not continue to be out of sync like a keyboard emulator). If you start at launch or revert then things will be synced.

When you say it takes 8-16 seconds are you talking about the SAS/RCS lights or the debug messages? The debug messages don't update in real time.

Maybe 40Hz is to fast for your computer?

Can you try changing "#define CONTROLREFRESH 25" to "#define CONTROLREFRESH 500" (line 24)

Link to comment
Share on other sites

I'm using a toggle switch... when it is in the on position (completing the circuit) the plugin tells ksp to turn the sas off. When i turn the switch off (breaking the circuit) the plugin tells ksp to turn sas on.

on-off-toggle-switch.jpg

As for the time delay. Its the actual sas, not the debug. I flip the switch... nothing happens. 8-16 seconds later the SAS light turns on.

I'll give the code a try in the morning, but my computer is a 6 core running at 4.2ghz with 8gb of ram...

Link to comment
Share on other sites

I'm using a toggle switch... when it is in the on position (completing the circuit) the plugin tells ksp to turn the sas off. When i turn the switch off (breaking the circuit) the plugin tells ksp to turn sas on.

That is the correct behaviour. When you turn the switch on, the pin is connected to ground, which is a logic LOW, which I am using as OFF. When you turn the switch off, the pin is internally pulled up to 5V, which is a logic HIGH, which turns things ON. If you don't want this, you can just flip the switch around physically or reverse the arduino code.

The arduino code has the same warning LED displays on pins 5 6 7 as the previous one, check and make sure the green yellow red LEDs are working during launch.

Link to comment
Share on other sites

If you don't want this, you can just flip the switch around physically or reverse the arduino code.

I'll have to see if I can change the code, The switches have "ON" and "OFF" written on them! I also use some push buttons and momentary switches for functions that are not suitable to a toggle switch (like firing an action group).... with the current method I would have to sit with a finger holding each push button in until I wanted to use them... then I'd have to release them! Likewise your plugin wont work with a selecter switch... rather than turning on one selection at a time it wil turn on all except the one you want. Just curious, what is the logic of setting it up so all of your commands are backwards? Have I missed something?

Link to comment
Share on other sites

Just swapped the code round, really easy :) Now the switches work the right way round :D

I also tried that change to the code you suggested... now taking 30 seconds to respond to commands!

Anyway, just spotted you have found a problem, I'll leave you to it :)

Link to comment
Share on other sites

I'll have to see if I can change the code, The switches have "ON" and "OFF" written on them!

Can you not just undo the screw and flip the label around? You can do it in hardware or software, whichever is easier.

I also use some push buttons and momentary switches for functions that are not suitable to a toggle switch (like firing an action group).... with the current method I would have to sit with a finger holding each push button in until I wanted to use them... then I'd have to release them! Likewise your plugin wont work with a selecter switch... rather than turning on one selection at a time it wil turn on all except the one you want. Just curious, what is the logic of setting it up so all of your commands are backwards? Have I missed something?

This is just a simple demo, you will need to write your own code for handling your hardware. This will work just fine for buttons, you will also need to flip the code. How to wire selector switches will depend on the switch, you will again have to come up with some code that works with your hardware. Look at some tutorials on how to wire input to the arduino.

Link to comment
Share on other sites

Zitronen, I just wanted to say Thank you for making this. I love KSP and enjoy working with Arduinos and I am so glad there is now an easy way to interface between them that is only going to become better.
this looks great, will definitely be giving it a go at home very soon. I have a Arduino Nano (probably 3rd party)

Thanks guys, if you can help with the testing it will be great!

Link to comment
Share on other sites

This is just a simple demo, you will need to write your own code for handling your hardware. This will work just fine for buttons, you will also need to flip the code. How to wire selector switches will depend on the switch, you will again have to come up with some code that works with your hardware. Look at some tutorials on how to wire input to the arduino.

Yep, switching the code has fixed it and will make push buttons and selectors work properly too :)

Can you not just undo the screw and flip the label around? You can do it in hardware or software, whichever is easier.

No, switches are universally designed to be installed the correct way round. They have a groove on one side so you can't flip things like "ON/OFF" label plates or safety covers (they just won't go on). Its a deliberate design feature of all toggle switches to prevent you wiring them the wrong way.

Link to comment
Share on other sites

Don't bother testing the plugin any more. There were so many bugs, I'm surprised it ran at all! :0.0:

I will upload a new version when I get home.

Ha! Ok :) the important thing is you've got the Arduino talking to KSP... even if its talking very slowly with a phrase book ;)

Link to comment
Share on other sites

Just curious, what is the logic of setting it up so all of your commands are backwards? Have I missed something?

that is standard logic for microprocessors.

@zitronen, I'll also have a play with the new version when it gets uploaded :)

*edit, just to see what would happen I hooked up my Nano, installed your plugin and uploaded the test code to it but changing the Launch LED to use D13 which is the onboard LED.

and yay, it works :) (btw, first ever program with my Arduino...Have had it for a couple of months but never done anything with it, so at-least I know it works :) )

Also ordered a 4x20 LCD panel off ebay, ~$6, which I don't think is too bad... the description said it was compatible with the Arduino.

Edited by pantner
Link to comment
Share on other sites

Try this: https://sites.google.com/site/zitronfiles/KSPSerialIO_013_1.zip

The performance problems were caused by issues with event handlers and threading, the delayed response was caused by the build up of a queue of commands in the serial buffer because I was not reading the data frequently enough. There were also null reference bugs cause by me being stupid.

Link to comment
Share on other sites

That's great :) Still getting a slight delay up to 1 second (sometimes only a fraction of a second though) but it is useable!

How will action groups work - for instance if I have my solar panels on an action group? Whereas SAS has two states On or Off... an action group is just toggle. I'd like to have an on/off switches connected to solar panels, antenna etc.

Now if I use a "toggle panel" action group won't the plugin just check the switch position and fire the action group if the switch is on? ...which is a problem with a toggle command, It will go out of sync on the first quickload. The switch position wont move but the action group will toggle the opposite way each time it checks.

I assume the only way would be to have 1 action group for "extend panels" and one for "retract panels" so when the plugin checks the switch position it either fires one action group or the other? This of course is another massive problem as you're suddenly reduced to 5 switches instead of 10 as each one will need two action groups to function... one for "on" and one for "off".

Or are you able to make the plugin extend and retract all solar panels on the ship without using action groups? Or able to toggle the state of an antenna without action groups?

EDIT - of course this wouldn't be an issue if someone made a mod that actually gives you more action groups! (amazed that one doesn't exist).

Edited by Mulbin
Link to comment
Share on other sites

Try this: https://sites.google.com/site/zitronfiles/KSPSerialIO_013_1.zip

The performance problems were caused by issues with event handlers and threading, the delayed response was caused by the build up of a queue of commands in the serial buffer because I was not reading the data frequently enough. There were also null reference bugs cause by me being stupid.

I honestly know very little about Unity engine or C#, but my first thought seriously was a buffer buildup when Mulbin said there was a large time delay. Glad to hear that it was fixed.

I will create a stock version and try this version out tonight if I get a chance. I assume it has all the same functionality so that the previous demo code still applies for data structures and sending/receiving.

Also, if my Arduino experience carries over to computers and serial data: for the purposes of the computer reading serial data, do you just have it read the most recent chunk (either counting back so many bytes from the end or else searching for a specific identifier) and then just flush all the rest rather than reading every set? Or have you just increased the checking frequency to higher than the Arduino's sending frequency? As I said, I only know the Arduino side, so if it is neither of these, then I probably would not understand, but it is just an idea.

Link to comment
Share on other sites

Control groups will only be momentary button or switch, since you can only turn "ON" a group so it can't be a toggle switch. You would have to assign open solar panel and close solar panel to different groups, or assign toggle panel. Although I guess you can implement whatever you want in the arduino code to make toggle switch work.

Can you test increasing the baud rate to 115200 in both the arduino and plugin? Maybe it will help. There really shouldn't be any noticeable delay.

I will create a stock version and try this version out tonight if I get a chance. I assume it has all the same functionality so that the previous demo code still applies for data structures and sending/receiving.

Yeah it's the same.

Also, if my Arduino experience carries over to computers and serial data: for the purposes of the computer reading serial data, do you just have it read the most recent chunk (either counting back so many bytes from the end or else searching for a specific identifier) and then just flush all the rest rather than reading every set? Or have you just increased the checking frequency to higher than the Arduino's sending frequency? As I said, I only know the Arduino side, so if it is neither of these, then I probably would not understand, but it is just an idea.

The receiving is done with event handlers which is great but hard to debug. It's made more difficult by the fact that I am using someone else's abandoned serial library which is incomplete and poorly documented.

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