Jump to content

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


zitronen

Recommended Posts

You can simply print some blank spaces where your number is before you print the number every time, or you can use a function such as "dtostrf" to print formatted strings.

I'll add orbital velocity this weekend. Anything else you want added?

I found dtostrf, I don't like it because when the string gets short it gets right justified so for velocity I could end up with "V= 12m/s".

Not sure if that can be changed?

Does the blank spaces make it flicker? I tried using lcd.clear() and that made it flicker horribly.

umm, time/distance til maneuver node would be nice?

Also, how do you format your times? Have you written code or is there a function that will convert seconds into minutes, hours, days, etc?

Link to comment
Share on other sites

I found dtostrf, I don't like it because when the string gets short it gets right justified so for velocity I could end up with "V= 12m/s".

Not sure if that can be changed?

Does the blank spaces make it flicker? I tried using lcd.clear() and that made it flicker horribly.

umm, time/distance til maneuver node would be nice?

Also, how do you format your times? Have you written code or is there a function that will convert seconds into minutes, hours, days, etc?

Blank space shouldn't flicker, it's a lot faster to print a couple spaces than clearing the entire screen. Right justifying is how numbers are usually displayed, if you want left justify use negative values for width in dtostrf.

I'll have a look at how to get manoeuvre node stuff, target info would be nice too, but can't promise anything.

I don't have the code with me right now, but I'm sure you can find some code on teh intarwebs to convert seconds to HH:MM:SS in C.

Link to comment
Share on other sites

Blank space shouldn't flicker, it's a lot faster to print a couple spaces than clearing the entire screen. Right justifying is how numbers are usually displayed, if you want left justify use negative values for width in dtostrf.

I'll have a look at how to get manoeuvre node stuff, target info would be nice too, but can't promise anything.

I don't have the code with me right now, but I'm sure you can find some code on teh intarwebs to convert seconds to HH:MM:SS in C.

Think I worked out a good way. Covert to string using dtostrf with a negative (as you suggest), however, im still left with trash characters. I then pass it to a function along with the max length of that string which compares the length in a loop and adds spaces to the end as necessary.

I also appear to be having trouble with my float calculations, seems to be stalling the unit and stopping the read out.

but getting there slowly! almost got AP, PE, Alt and velocity reading out with unit changes as the numbers get larger.

***EDIT, hmm... I have AP distance, PE distance, Altitude and Speed all working...but I can only have 3 at any one time. If I do all 4 of them the system seems to only update once every minute or so.

***EDIT #2 - I think it might be related to the 'VData.Vsurf' float that I am using. will change it to orbit speed when it is put in the plugin. Oh, and I think I have a library for the time conversions :)

Edited by pantner
Link to comment
Share on other sites

Like many people here, I wanted to make an arduino physical display then quickly realized unity does not have the required serial IO name space. So the solution is to borrow someone else's code (http://sourceforge.net/projects/serialportnet/), which turns out is kind of incomplete but works. This plugin sends/receives data packets over the serial port, which should make it much easier for people wanting to build hardware for KSP but can't be bothered with making plugins as well........

I am trying to do something simular to what you are doing, but I am having issues connecting to the COM port for the Arduino. I am using OpenNETCF.IO.Ports like you are but I am getting an error CreateFile Failed: 3. Here is the basic code below:


private SerialPort spKSPCPanel;
spKSPCPanel = new SerialPort(@"\Device\VCP0", 9600, Parity.None, 8, StopBits.One);
spKSPCPanel.Open();

Any ideas?

Link to comment
Share on other sites

Not so weekly "Weekly" update 10

Added:

Orbital speed

Time to next node

Delta V of next node

The data structure has changed, so be sure to update yours.

Plugin:

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

Arduino Code

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

Edited by zitronen
Link to comment
Share on other sites

Not so weekly "Weekly" update 10

Added:

Orbital speed

Time to next node

Delta V of next node

The data structure has changed, so be sure to update yours.

Plugin:

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

Arduino Code

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

that's awesome, thank you very much for that.

I think I have my code sorted now...started again and while I did use my old code for reference, I think I've improved it.

Just had a thought, would a string variable of the body you are orbiting be possible to add it?

Link to comment
Share on other sites

string no, but maybe just a byte where 1 = Kerbol, 2 = Moho, 3 = Eve, etc.

that would be cool :D

Also seems im still having issues with my code. Got my display with AP, PE and orbit velocity showing. When I add orbit inclination or the escape value (can't remember the name) it just seems to die. Is it because I am doing everything as a separate print command? Would it help If i used an 8-bit connection? (using 4bit at the moment)

Link to comment
Share on other sites

I use 4bit as well, I'm pretty sure no one uses all 8 wires. Try just making a simple test code and see how long your print commands are taking (add some debug lines to make it flash a LED or something). You must have bug somewhere

Link to comment
Share on other sites

I use 4bit as well, I'm pretty sure no one uses all 8 wires. Try just making a simple test code and see how long your print commands are taking (add some debug lines to make it flash a LED or something). You must have bug somewhere

Hmm, very odd. Everything seems to work on it's own, it's when I have over 3 values being displayed that I have issues. I am converting each value to a string if that makes a difference?

Will try and add some debugging in.

Link to comment
Share on other sites

Try SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); ?

That was it! I feel stupid after that.

One other question, for input, do you recommend me using something like the Teensy++ so I can have a USB HID device (true joystick) as input or should I use the arduino and manipulate the flight data via the plugin?

I'm thinking a Teensy with an I/O expander, but just wanted your opinion before I started to tackle it.

Link to comment
Share on other sites

That was it! I feel stupid after that.

One other question, for input, do you recommend me using something like the Teensy++ so I can have a USB HID device (true joystick) as input or should I use the arduino and manipulate the flight data via the plugin?

I'm thinking a Teensy with an I/O expander, but just wanted your opinion before I started to tackle it.

the problem you may have is that if you leave your controls switched on/off, then if it doesn't check the settings on initialisation then certain controls could be inverted.

ie, you make a flight and leave SAS turned on. You quickload/revert to a time when you had SAS turned off but your physical switch is still turned on.

In order to turn SAS on you must switch your physical switch of 'off'. (or use the keyboard to return it to the correct setting according to your physical switch)

The plugin checks all of that for you.

and the Teensy just looks like a variation of an Arduino, how would that be any different?? or am I missing something?

Link to comment
Share on other sites

the problem you may have is that if you leave your controls switched on/off, then if it doesn't check the settings on initialisation then certain controls could be inverted.

ie, you make a flight and leave SAS turned on. You quickload/revert to a time when you had SAS turned off but your physical switch is still turned on.

In order to turn SAS on you must switch your physical switch of 'off'. (or use the keyboard to return it to the correct setting according to your physical switch)

The plugin checks all of that for you.

and the Teensy just looks like a variation of an Arduino, how would that be any different?? or am I missing something?

The Teensy is very similar to the Arduino, but one of the benefits is it lets you change how the device is presented to the OS. For example, you can make it present as a USB HID Gamepad/Joystick, keyboard, mouse, etc. instead of just a COM device.

Here is how to configure a joystick with the Teensy: http://www.pjrc.com/teensy/td_joystick.html I have done this with another customer controller I was making for the Raspberry Pi and it worked perfectly, just didn't know how KSP would interact with a joystick, never tried it.

Link to comment
Share on other sites

The Teensy is very similar to the Arduino, but one of the benefits is it lets you change how the device is presented to the OS. For example, you can make it present as a USB HID Gamepad/Joystick, keyboard, mouse, etc. instead of just a COM device.

Here is how to configure a joystick with the Teensy: http://www.pjrc.com/teensy/td_joystick.html I have done this with another customer controller I was making for the Raspberry Pi and it worked perfectly, just didn't know how KSP would interact with a joystick, never tried it.

It's just a clone of an arduino leonardo, all use the ATMEGA32U. http://arduino.cc/en/Reference/MouseKeyboard

Link to comment
Share on other sites

Got my issue sorted!

My code was fine, seems it was the 4-bit bus for the LCD Panel.

setup the 8-bit bus for the LCD display and it is now working well :)

Already had the wires connected so decided to give it a go and it appears to have fixed my issue.

I had AP and PE 'height', orbit velocity, orbit inclination and orbit escape (Whatever VData.e is).

I have purchased a 6-way rotary switch, so will try and setup a similar system to you with different displays. ie, a take-off/landing display, normal space, transfers (ie, manoeuvre node stuff).

One other thing I just thought of, is it possible to have the manoeuvre node delta-v value passed through?

Edited by pantner
Link to comment
Share on other sites

Huh ok I guess whatever works!

e is orbit eccentricity, 0 if orbit is circular, 0-1 if elliptical >1 if hyperbola (escape).

Next node delta V is already included, is it not working?

Link to comment
Share on other sites

Huh ok I guess whatever works!

e is orbit eccentricity, 0 if orbit is circular, 0-1 if elliptical >1 if hyperbola (escape).

Next node delta V is already included, is it not working?

yea, my LCD was really cheap ($6 on ebay) so it might have a slower controller or something...I dunno :P

right, gotcha :)

doh, I see that now, sorry, I missed it!

Now I can start working on my different 'screens' for the LCD :)

Thanks again for all your help!

Link to comment
Share on other sites

Hey guys,

I'm using the code on my own project. Do you think it is possible with reasonable effort to connect the plug-in on two seperate serial connections to two different arduinos?

Link to comment
Share on other sites

I was just thinking about that. One way would be to have one connection to the plugin and have one arduino forward the packet to the next one. The other way is to have the plugin instantiate multiple COM ports. The first one is easy since I wouldn't have to do anything, but you will need to add some C code to your display and will require multiple serial ports (like arduino mega). The second method will require some work from me.

Link to comment
Share on other sites

Of course I appreciate you doing any work on your project which I can use. But I have already found a few points where I have to add code to your plug-in to make it work with my project. I would add to VesselData dVtotal, dVstage, planet radius and atmospheric height. And the ControlPacket needs MechJeb commands for my purpose. So what I'm trying to say is I could write this for you to incorporate into your official release. Same applies to the multiple serial connections.

Link to comment
Share on other sites

something I just came across that is definitely going to be a big help for me

http://tronixstuff.com/2011/10/22/tutorial-arduino-port-manipulation/

allows you to read or write to pins as a group instead of individually.

Should increase the speed of your code and possibly reduce the size.

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?

Of course I appreciate you doing any work on your project which I can use. But I have already found a few points where I have to add code to your plug-in to make it work with my project. I would add to VesselData dVtotal, dVstage, planet radius and atmospheric height. And the ControlPacket needs MechJeb commands for my purpose. So what I'm trying to say is I could write this for you to incorporate into your official release. Same applies to the multiple serial connections.

Yeah I am happy to include any code (less work for me :D!!). We would think about how to support other mods like mechjeb, to make sure it will work with people not using those mods.

Link to comment
Share on other sites

Well that sounds great to me. As I'm constantly changing my development goals, you might regret working with me. Anyway, here is a preliminary list of additions that I deem necessary for my project:

(I call the Arduino Mega with keypad for issuing commands to MechJeb "colossus" and the one managing all displays "luminous".)


[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct VesselData
{
float dVTot; //43 Total delta-V remaining for current vessel
float dV; //44 Delta-V remaining in current stage
float Atmo; //45 Atmospheric height of body currently orbited
float R;//46 Radius of body currently orbited
byte state; //47 Used to tell colossus wether MechJeb is active or not or wether the plugin can accept orders

}

struct ControlPacket {
String opcode; //command packet from colossus to MechJeb, needs interpretation in plug-in
};

The opcode command packet is what I intend to use for controlling MechJeb from the colossus unit. The packet looks like program#argument1#argument2#, for example 12#85#4 would read Program 12 (ascent guidance) to 85km orbit with autostage to stage #4. Of course this requires not only functions to call MechJeb members, it also requires a string parser to decode that information. If you have suggestions for improvement, feel free to tell me.

To solve the multiple serial question I'm pretty much committed to using a single connection between the PC and colossus, and copy the VesselData to luminous as luminous does not have any functions requiring a connection to the PC, it is just a passive (but complex) information display.

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