Jump to content

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


zitronen

Recommended Posts

Lol yeah, its going to take careful planning, I only have 54 in total... and putting any momentary buttons that don't really need the plugin on a separate arduino (keystroke emulating),of course if you were able to make the plugin support multiple arduinos at the same time... ;)

EDIT - of course eventually I might learn how shift registers work... then I have lots of pins :)

There are addons for the Arduinos that allow you to add extra inputs/outputs. I believe I posted a link to one a few pages back

Ok thanks, I'll give that a try. Although I'm now thinking longer term I may put the leds on another pin... I'm planning to have dual active/warning lights that with a bit of coding in arduino should work like this example..

RCS switch off = led off

RCS switch on + monopropellant on board = green RCS led lit.

RCS switch on but out of monopropellant = red RCS led lit.

similar things to tie electric charge to various functions... a basic form of error light really.

You can get Dual colour LEDs which you might like.

Also, you can use the circuit Katamari posted without the LED, and a 10k resistor and have the logic the way you wanted a few pages back.

Switch closed = switch on = Pin high

If that makes sense?

This is how you will want to hook up (sorry about the odd symbols, the software isn't really made for Arduino).

http://i.imgur.com/2bFAPGY.png

V_High is your 5V output from the Arduino, Input_Pin is the pin to read from, and Ground is your standard ground.

What happens is that while the switch is open, the pin should read ground, but when you close the switch, there will be no voltage drop across it, so the pin will read high. Then the 5V will drop across the resistor and the LED then be back to ground, so it will power the LED. Remember, input pins act like voltmeters, so they do not normally really take any current flow through them.

Not that im an expert, would that not make some kind of voltage divider? Or because the Arduino draws so little power it doesn't matter?

Link to comment
Share on other sites

There are addons for the Arduinos that allow you to add extra inputs/outputs. I believe I posted a link to one a few pages back

You can get Dual colour LEDs which you might like.

Also, you can use the circuit Katamari posted without the LED, and a 10k resistor and have the logic the way you wanted a few pages back.

Switch closed = switch on = Pin high

I'll look into the extra pins :) For the on/off issue I just reversed the HIGH/LOW states in the arduino code and it works fine now.

Link to comment
Share on other sites

I'll look into the extra pins :) For the on/off issue I just reversed the HIGH/LOW states in the arduino code and it works fine now.

but if you do use the circuit posted, the logic will be backwards? You're connecting the switch to ground?

Link to comment
Share on other sites

but if you do use the circuit posted, the logic will be backwards? You're connecting the switch to ground?

Oh I see.. so in the posted example I connect the switch to 5V and the pin, instead of the pin and ground? I can always switch the code back around for any switch wired like this.

Link to comment
Share on other sites

Oh I see.. so in the posted example I connect the switch to 5V and the pin, instead of the pin and ground? I can always switch the code back around for any switch wired like this.

yea that's right, but don't forget the 'pull down' resistor. probably a 10K.

That drops the voltage on that pin enough for the arduino to read it as 'low', then when you turn on the switch and close the circuit it goes 'high'.

Link to comment
Share on other sites

Got action groups working! Please test:

Updated plugin

https://sites.google.com/site/zitronfiles/KSPSerialIO_013_4.zip

Arduino

https://sites.google.com/site/zitronfiles/KSPIODemo3.zip

(Pin 10 is used for action group 1)

In the arduino code, to control say action group 10 simply do:


if (digitalRead(CG10PIN))
ControlGroups(10, HIGH);
else
ControlGroups(10, LOW);

I found that if you set your controls to "toggle" in KSP, you can actually use a switch to turn an action group on and off! Brilliant! This should reduce the number of action group required. Try it with a solar panel for example. Control groups are synced at the start of the flight.

Link to comment
Share on other sites

Well.. after 3 hours of rewiring my panel and 10 minutes altering my sketch... it works!

A couple of bugs but they both seem to be ksp, not the plugin. Solar panels don't display correctly if you load a scene with the switch set to deploy... until you switch the solar panel of, then it suddenly appears and plays the folding anim! Also reaction wheels work in reverse to other parts (on is off - off is on) but this is easily fixed by reversing the HIGH/LOW code for any switch assigned to reaction wheels.

But as I say these are KSP bugs, and really not that significant.

Great work! Now almost all of my existing switches are reliable and don't need resetting :) Not many default commands left to add to your plugin now...

Link to comment
Share on other sites

This is how you will want to hook up (sorry about the odd symbols, the software isn't really made for Arduino).

2bFAPGY.png

V_High is your 5V output from the Arduino, Input_Pin is the pin to read from, and Ground is your standard ground.

What happens is that while the switch is open, the pin should read ground, but when you close the switch, there will be no voltage drop across it, so the pin will read high. Then the 5V will drop across the resistor and the LED then be back to ground, so it will power the LED. Remember, input pins act like voltmeters, so they do not normally really take any current flow through them.

don't forget the 'pull down' resistor. probably a 10K.

Ok, I've tried this circuit using a 10k resistor and it doesn't work... SAS is always on and LED is always on... all that happens is the LED gets brighter when I flip the switch.

Triple checked the circuit... what is going wrong?

EDIT - I'm using a modified version of Zitronen's demo... is it because he doesn't use the pinmode command to set the pin as input? Seems like the pin is putting out voltage...

Edited by Mulbin
Link to comment
Share on other sites

Well.. after 3 hours of rewiring my panel and 10 minutes altering my sketch... it works!

A couple of bugs but they both seem to be ksp, not the plugin. Solar panels don't display correctly if you load a scene with the switch set to deploy... until you switch the solar panel of, then it suddenly appears and plays the folding anim! Also reaction wheels work in reverse to other parts (on is off - off is on) but this is easily fixed by reversing the HIGH/LOW code for any switch assigned to reaction wheels.

But as I say these are KSP bugs, and really not that significant.

Great work! Now almost all of my existing switches are reliable and don't need resetting :) Not many default commands left to add to your plugin now...

OK Great. I will update the first page with the new code. I will try to get axes working next week, after that I will go back and take requests for what variables you guys want it to output.

Ok, I've tried this circuit using a 10k resistor and it doesn't work... SAS is always on and LED is always on... all that happens is the LED gets brighter when I flip the switch.

Triple checked the circuit... what is going wrong?

EDIT - I'm using a modified version of Zitronen's demo... is it because he doesn't use the pinmode command to set the pin as input? Seems like the pin is putting out voltage...

Try changing pinMode(SASPIN, INPUT_PULLUP); in the output tab to pinMode(SASPIN, INPUT);

Link to comment
Share on other sites

Try changing pinMode(SASPIN, INPUT_PULLUP); in the output tab to pinMode(SASPIN, INPUT);

Great, that's fixed it :)

Next noob question...

What is the correct arduino code to make in input trigger two actions rather than just one?

For a couple of controls I want to also send a signal to light an LED on another pin

The currect code for the SAS switch is...

if (digitalRead(RCSPIN))

MainControls(RCS, HIGH);

else

MainControls(RCS, LOW);

but I want to add in digitalWrite(RCSLED, HIGH); to be also triggered when the switch is used... what is the correct syntax to add that to the function? My attempts are just throwing up errors :)

Link to comment
Share on other sites

Try changing pinMode(SASPIN, INPUT_PULLUP); in the output tab to pinMode(SASPIN, INPUT);

Thanks for explaining that for him, Zitronen.

Sorry, I forgot that I should probably have explained how to code for it.

Also, Mulbin, if you want a good introductory guide to read through and explain how to code in Arduino and some simple circuits, I would suggest you check out the PDF here: http://ardx.org/src/guide/2/ARDX-EG-ADAF-WEB.pdf

It is a really short guide that starts off with a coding explanation followed by examples of how to build circuits and how to program them. Additionally, it also teaches how to use shift registers in one of the sections if you are still looking at using them.

One thing it does not cover is the pinMode(pin, INPUT_PULLUP) command, but the official explanation of how INPUT_PULLUP works can be found here: http://arduino.cc/en/Tutorial/InputPullupSerial#.UxzOGIVhdFs

Link to comment
Share on other sites

the other thing you can do is run a switch with 2 'sides' and just have the second side switch the LED on and off. Now I always get confused...Think that is a 'dual throw'. ie, DPDT (dual pole, dual throw... it has a connection either side of the 'common' and is basically 2 switches thrown at the same time)

Link to comment
Share on other sites

Let me preface this post by saying that I have read through the entire thread and have been unable to find a solution to the problem issued below.

My problem is the same problem that Mulbin had a while back. I have uploaded the KSPIODemo to my Arduino Leonardo and my Arduino Mega 2560, made sure that they were on a specified serial port (in this case COM9), and then tested the LEDs to make sure they lit up on command (they did) When I ran KSP I saw in the Alt-F12 log they the plugin had started correctly. Upon going into the VAB, selecting a craft, and then clicking the launch button, my green LED would light up a few times while a the RX button flashed on my arduino. When the game was fully loaded the green light turned off and I received this message in the debug window.

SH0HbsX.png

proof that the arduino was connected to the COM port.

thfXykO.png

If someone could provide me with some guidance that would be fantastic. Thank you :D

Edit: Tried with an UNO as well. Set the COM port in the config, tried raising the Handshake Delay. Restarted the game. No luck.

Edited by SirSpaceGoat
adding info
Link to comment
Share on other sites

Let me preface this post by saying that I have read through the entire thread and have been unable to find a solution to the problem issued below.

My problem is the same problem that Mulbin had a while back. I have uploaded the KSPIODemo to my Arduino Leonardo and my Arduino Mega 2560, made sure that they were on a specified serial port (in this case COM9), and then tested the LEDs to make sure they lit up on command (they did) When I ran KSP I saw in the Alt-F12 log they the plugin had started correctly. Upon going into the VAB, selecting a craft, and then clicking the launch button, my green LED would light up a few times while a the RX button flashed on my arduino. When the game was fully loaded the green light turned off and I received this message in the debug window.

http://i.imgur.com/SH0HbsX.png

proof that the arduino was connected to the COM port.

http://i.imgur.com/thfXykO.png

If someone could provide me with some guidance that would be fantastic. Thank you :D

Edit: Tried with an UNO as well. Set the COM port in the config, tried raising the Handshake Delay. Restarted the game. No luck.

Have you tried the version in this post:

http://forum.kerbalspaceprogram.com/threads/66393-Hardware-Plugin-Arduino-based-physical-display-serial-port-io-tutorial-%2825-Feb%29?p=1010819&viewfull=1#post1010819

Edit: are you using the original KSPdemo on the first page? The protocol has changed so the demo no longer works. You need to run the second demo or use the code in above. I need to update the tutorial.

Edited by zitronen
Link to comment
Share on other sites

Have you tried the version in this post:

http://forum.kerbalspaceprogram.com/threads/66393-Hardware-Plugin-Arduino-based-physical-display-serial-port-io-tutorial-%2825-Feb%29?p=1010819&viewfull=1#post1010819

Edit: are you using the original KSPdemo on the first page? The protocol has changed so the demo no longer works. You need to run the second demo or use the code in above. I need to update the tutorial.

Worked! Thank you very much!

Link to comment
Share on other sites

For anyone that is thinking of building more complex simpits like myself, this guy is working on a mod that increases the amount of action groups from 10... to 260!!!

http://forum.kerbalspaceprogram.com/threads/71631-Additional-Action-Groups-Development-Thread

Combined with Zitronen's mod I will now have all the resources I need to build an insanely complicated simpit :)

Link to comment
Share on other sites

Zitronen - to answer your question what I would like to see next... all of the things on the list I posted a few pages back, but for the three I need first, just take a look at the first completed part for my second panel :)

8alByLR.jpg

Link to comment
Share on other sites

Oh wow, ok maybe I would work on output of resources instead of axes. What's "draw"?

Charge will display the amount of power stored in the batteries, Draw will display electrical drain / charge rate... I'm hoping you can isolate it, its already used by several mods like Steam gauges and fusebox. I'll need to put a new face on the dial to swing between positive and negative values.

Link to comment
Share on other sites

I would recommend against having resource draw as an output to the Arduino because that is just making for a very large packet of data to send back and forth. If the Arduino gets the the actual amount (preferably in a float to a high precision), then you can calculate the draw rate really easily. Just store the value from the current data packet, then read in the new data packet. Subtract the old charge from the new charge and divide by the time between when the packets were received. A very quick and dirty way to do this is shown below by only changing the loop() (I assume that the electric charge is VData.charge):

void loop()
{
// Store time and charge from last reading
unsigned long oldTime = currentTime;
float oldCharge = VData.charge;

input();
output();

// Find change in charge and time
unsigned long currentTime = millis();
unsigned long deltaTimeMillis = currentTime - oldTime;
float deltaTSeconds = deltaTimeMillis/1000;
float deltaCharge = VData.charge - oldCharge;
// Calculate draw
float draw = deltaCharge/deltaTSeconds;
}

It could be made slightly better, but the point is that this should be possible from only reading the charge and time of each data packet from the game.

Link to comment
Share on other sites

Subtract the old charge from the new charge and divide by the time between when the packets were received.

You would have to use the in game MET clock to calculate... not real time. Real time would be very inaccurate as KSP speeds up and slows down according to graphics and memory load. Sometimes 1 in game second can last up to 5 real seconds.

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