Jump to content

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


zitronen

Recommended Posts

MechJeb source code says

float atmDv = (stats.atmoStats.Length == 0) ? 0 : stats.atmoStats[stats.atmoStats.Length - 1].deltaV;
float vacDv = (stats.vacStats.Length == 0) ? 0 : stats.vacStats[stats.vacStats.Length - 1].deltaV;

returns atmospheric and vaccum stage delta-V.


float atmDv = stats.atmoStats.Sum(s => s.deltaV);
float vacDv = stats.vacStats.Sum(s => s.deltaV);

are the total delta-V stats.


FlightGlobals.currentMainBody.maxAtmosphereAltitude
FlightGlobals.currentMainBody.Radius

are from Unity

I must admit I have not yet started work on extracting data from MechJeb or controlling it. My coding skills are not that good. But I'll have to look into those issues for my project, so why not share it with you? Or would you prefer writing that code yourself?

Edited by cm2227
Link to comment
Share on other sites

Would you consider a mechjeb-version and a non-mechjeb version, maybe a fork? I don't understand enough of the game architecture and c# to calculate these values in code instead of grabbing them from a plugin that actually does it. See for example https://github.com/CYBUTEK/KerbalEngineer/blob/a596fcfbd50ab08b828401c04c2e1ca8b941ae69/KerbalEngineer/Simulation/Simulation.cs, it is the source code of how Kerbal Engineer simulates all stages and comes up with stage and total dV. If you don't like the idea of accessing another plugin's data we should find another way.

Link to comment
Share on other sites

Yeah that would help if you have performance problems. If you are only displaying 4 values to a LCD you really shouldn't be. I am doing much more with my display and have no problems at all. You are using the lcd library right?

Yes, i am using the LCD library, and not having performance issues now that i am using the 8-bit bus for the LCD.

Actually going to use it to read groups of digital inputs (and possibly do outputs aswell, ie, LEDs) and then Case statements to do specific things as opposed to using an if statement for each input.

For example, i too am going to use a rotary switch to switch between different displays but i don't want 6 if statements for it. From my understanding if statements are not very efficient. Reading all pins as a single value and then using a case statement would definitely be much better.

was just an idea.

Would you consider a mechjeb-version and a non-mechjeb version, maybe a fork? I don't understand enough of the game architecture and c# to calculate these values in code instead of grabbing them from a plugin that actually does it. See for example https://github.com/CYBUTEK/KerbalEngineer/blob/a596fcfbd50ab08b828401c04c2e1ca8b941ae69/KerbalEngineer/Simulation/Simulation.cs, it is the source code of how Kerbal Engineer simulates all stages and comes up with stage and total dV. If you don't like the idea of accessing another plugin's data we should find another way.

maybe a value in the config file for the plugin to turn mechjeb support on and off?

*EDIT - Also, looking through the plugin more...seems you have defined the same pin to 2 different functions? 4-5-6-7 are seem to be 2 'things'???

Edited by pantner
Link to comment
Share on other sites

I think I found another solution to the mechjeb question. A routine to check wether mechjeb is present, and all mechjeb specific functions are only active when mj is present. In the VesselData packet all values calculated by mj would be 0, and mj commands in the control packet would be simply ignored. How about that?

Link to comment
Share on other sites

*EDIT - Also, looking through the plugin more...seems you have defined the same pin to 2 different functions? 4-5-6-7 are seem to be 2 'things'???

Sorry I don't understand what you are saying? Which line of code are you talking about?

I think I found another solution to the mechjeb question. A routine to check wether mechjeb is present, and all mechjeb specific functions are only active when mj is present. In the VesselData packet all values calculated by mj would be 0, and mj commands in the control packet would be simply ignored. How about that?

Yeah that could work. We have tons of space in the control packet.

Link to comment
Share on other sites

Sorry I don't understand what you are saying? Which line of code are you talking about?

Yeah that could work. We have tons of space in the control packet.

in the main sketch you have

#define GLED 5
#define YLED 6
#define RLED 7

and then further down you have

#define SAS 7
#define RCS 6
#define LIGHTS 5

aren't they referring to the same pins?

Link to comment
Share on other sites

#define GLED 5
#define YLED 6
#define RLED 7

These are LED pins.

#define SAS 7
#define RCS 6
#define LIGHTS 5

These have nothing to do with pins.

There are still questions remaining for me. Do you want to work on your code together with me? If so, who writes which part? And how do we exchange code? Github?

If the code is short/simple, you can just post it and I can add it.

Link to comment
Share on other sites

#define GLED 5
#define YLED 6
#define RLED 7

These are LED pins.

#define SAS 7
#define RCS 6
#define LIGHTS 5

These have nothing to do with pins.

hmm, ok then, what do they do then?

Just trying to get my head around what I need to change in the code to start when I start adding all my own inputs and leds, etc.

Link to comment
Share on other sites

Dear panter,

have a look at void controls() and void MainControls(byte n, boolean s). controls() reads pin input and calls MainControls to write the input value to the nth bit of the controls byte. So the defines you list,

#define SAS 7

for example, don't apply to pins but to which bit in the control byte stores which binary information about a system (SAS in that case) to be turned on or off.

Link to comment
Share on other sites

If the code is short/simple, you can just post it and I can add it.

As I have to write a mechjeb interface, it'll neither be short nor simple. I forked a copy of KSP to my git at https://github.com/cm2227/KSPSerialIO/blob/master/KSPSerialIO/KSPIO.cs. I will tell you about what I do and you can decide what to add to your code. I'll write you my mail adress by private message.

Link to comment
Share on other sites

OK, from your PM is seem that your requirements are fairly specific to your hardware. In that case I will just direct people who want additional mechjeb functionality to you. You can be in charge of your own plugin fork, and feel free to use the code however you want. I'll put a link in the first post to your thread. I might steal some code from you if you add other output/input from the vanilla game! Also thanks for explaining it to panter.

Link to comment
Share on other sites

You can be in charge of your own plugin fork, and feel free to use the code however you want.

Sounds fair and okay to me, thank you for your work. I might continue asking stupid questions when I don't understand how your code works. :D

Link to comment
Share on other sites

I am starting to dabble a bit myself, as I was able to scrounge up a few parts. My plan is to use an Arduino mainly for controlling LEDs and a thruster. An old keyboard chip will control buttons, which I have experience with already. Looking through the vessel parameters, it seems to me that you are unable to read the current status of the main controls, only set them. Is that so?

Link to comment
Share on other sites

That's correct. Depends on what you mean by "main controls". If you are talking about things like throttle, pitch, roll etc., it would be possible to output them. If you are talking about RCS, SAS, etc., I don't know how to get the status from the game.

Link to comment
Share on other sites

That's correct. Depends on what you mean by "main controls". If you are talking about things like throttle, pitch, roll etc., it would be possible to output them. If you are talking about RCS, SAS, etc., I don't know how to get the status from the game.

Thanks for the fast answer. I referred to the controls defined in your MainControls byte. The original plan was to control these by keystrokes on a keyboard. However, while writing I realize that if I use a pin for a LED, I could just as well use it for a switch and activate a LED with the switch as well.

Link to comment
Share on other sites

Zitronen we've had a problem. I start the simulation, KSPSerialIO states it opened the com port, and the green LED indication connection is on. I can read true numbers on my displays (like AP and PE), and then the connection LED goes off but still the RX blinks. It never stops blinking, but the numbers remain static until at a random point in the next minutes the green light goes back on and the numbers are updated, but only once. It seems the connection is somehow erratic. From your experience, do you know possible causes for this behaviour? Thanks alot for your help!

Strange behaviour just observed: The arduino did four resets between Space Center an the loaded scene, and then KSPSerialIO said there was no display found. Very strange.

Edited by cm2227
Link to comment
Share on other sites

If your arduino is crashing randomly and resetting itself that usually means you have a serious bug in your arduino code... I think it may be related to your other problems.

Can you confirm that the demo code in the first post is working or not?

Link to comment
Share on other sites

I think I have to look into how your code determines the COM port it is using. I set up my arduino on a USB TTL converter adressed COM6 and changed the default com port in config.xml to COM6. Now when I start the game scene KSPSerialIO claims to have established a connection on COM3 (which is not listed in device manager) and prints [Log]: KSPSerialIO: Handshake received - 123, but the arduino (of course) does nothing.

Okay, just take this as some sort of bug report.

I now tried another configuration. Regarding what I posted yesterday about the reboots I suspected a power supply issue and added external DC 7.5 Volts. Now the arduino still reboots several times during the loading screen, but IT WORKS INGAME as desired, with my modified code for LCDs. Now I have to raise baud rate and update interval, which I lowered when I suspected a bad connection issue. Zitronen, you are still very helpful and responsive considering you are doing it for fun and free. Thanks alot, man!

Edited by cm2227
Link to comment
Share on other sites

Zitronen I have two simple followup-questions.

1) How many bytes does the VesselData packet usually have? I'm trying to find out the minimal baud rate I need, as higher baud rates and update rates seem to be unstable on my arduino code, probably because I have several modules for display on LCDs and LEDs which result in a significantly lower loop rate than in your example, I guess.

2) At which point is the config file read by your plugin? I don't think it is possible to change for example the baud rate without restarting KSP. And you might consider an option in your plugin to not scan for the correct com port but use the default provided in the config file and just retry that connection every other second until it works. Problem is somehow your plugin considers the internal modem of my laptop a serial display and continues to use the wrong COM port until I restart KSP.

Forget Question 1), it's 165 bytes. It seems that 9600 Baud and 0.5 seconds is working fine, 9600 Baud and 0.2 seconds interval is too much. Damn. Math says 165 bytes times 5 would still be well below 9600 baud, but at those settings KSPSerialIO tries to connect at the wrong COM port (3 instead of 2) which I consider a handshake issue.

But don't forget: I achieved a huge milstone today, which I owe to you and your work.

Actually a third question came up. VData.MissionTime seems to contain unreadable Data, as my LCD turns the raw value into a single chinese character. Any suggestions on troubleshooting?

By the way, I managed to make one arduino handle all data and handshake and simply write all data received to Serial1 for the second arduino. So having multiple serial connections on your plugin is no more an issue for me.

Edited by cm2227
Link to comment
Share on other sites

Ok that is weird, if it says KSPSerialIO: Handshake received - 123, that means the plugins had sent the packet to the arduino, and the arduino has replied correctly. So the arduino must be connected to that port! Can you post the debug log?

You need to add 4 bytes to 165 for overhead. 9600 Baud is 9600 bits/s maximum, which is only 1200 Bytes/s, that's pretty close to the 850 bytes you are sending. Remember Baud rate is not a guarantee, it's the maximum possible.

Config files are read at the main menu screen, you can try switching back from launch to menu.

Mission time is a unsigned 32 bit integer with a unit of seconds, are you printing it as a number?

Edited by zitronen
Link to comment
Share on other sites

Hold on a second. The correct response should be KSPSerialIO: Handshake received - 314, not 123. It seems like whatever is on your "COM3" is just returning the packet send from the plug-in straight back to the plug-in! Are you sure you don't have some kind of hidden loop-back device on your computer?

Link to comment
Share on other sites

Afaik COM3 is an internal modem on the laptop I use for the dev work. My gaming pc should have no devices except for the hardware COM1 and the arduino. The point is: I would prefer if KSPSerialIO would try no other COM port than the one listed in config.xml. But I should be able to change that myself in the config file or even add an option wether your code should scan the COM ports or use nothing but the default.

Okay, the Mission time issue is something different. I forgot to specify the base of the unsigned long integer. Now it works. Somehow. Not really. I'll look into it, but it doesn't seem to be your fault in any way.

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