Jump to content

My KerbalController


hugopeeters

Recommended Posts

Thanks again @richfiles, your trick worked like a charm! I got the resistors bent out of the way and was able to mount the LED bars flush with the face plate.

RqHwkbp.jpg

 

After soldering in the last 55 wires it was on to some hefty coding! The way I have the LEDs hooked up is efficient, but not very easy to code for. There are 7 shift registers with 8 outputs each. There are 5 LED bars with 10 LEDs each. When I soldered in the wires, I only took into account what would be easiest to route the wires. Everything else can be fixed in the code, I thought. Well, sure, but it takes a lot of testing and debugging.

I first get the fuel levels from the game and calculate the percentage:

//Fuel Gauges
  vSF = 100 * VData.SolidFuel / VData.SolidFuelTot; //percentage of solid fuel remaining
  vLF = 100 * VData.LiquidFuelS / VData.LiquidFuelTotS; //percentage of liquid fuel remaining in current stage
  vOX = 100 * VData.OxidizerS / VData.OxidizerTotS; //percentage of oxidized remaining in current stage
  vEL = 100 * VData.ECharge / VData.EChargeTot; //percentage of electric charge remaining
  vMP = 100 * VData.MonoProp / VData.MonoPropTot; //percentage of monopropellant remaining

Next, I map them to values between 0 and 9. Note I correct for having one LED bar soldered in the wrong way around.

//scale down to 0-9 for binary calculations
  SF = constrain(map(vSF, 100, 0, 0, 9), 0, 9);
  LF = constrain(map(vLF, 100, 0, 0, 9), 0, 9);
  OX = constrain(map(vOX, 100, 0, 0, 9), 0, 9);
  EL = constrain(map(vEL, 0, 100, 0, 9), 0, 9); //EL bar soldered wrong way around
  MP = constrain(map(vMP, 100, 0, 0, 9), 0, 9);

Then, I calculate 2 to the power of those values. The pow() function works with floats, so you get some rounding issues. That's what the 0.1 is for. That doesn't work when the exponents get bigger, but for now it's fine.

  //calculate the power of 2. Now each value in binary is all zeroes an a single 1. we can use that to light one LED in each LED bar (dot mode)
  int powOX = 0.1+pow(2,OX);
  int powEL = 0.1+pow(2,EL);
  int powMP = 0.1+pow(2,MP);
  int powSF = 0.1+pow(2,SF);
  int powLF = 0.1+pow(2,LF);

If you think about these values in binary, each one is a series of zeroes with a single 1 in there. The position of the 1 has a linear correlation to the initial input (% of fuel remaining). All I have to do now is get those bits shifted into the shift registers in the correct order. One issue with that is the shiftOut() function expects bytes (8 bits) at a time. I could write my own function, but I chose to map my 10-bit numbers into the proper 8-bit bytes. In hindsight maybe not the simplest way, but ah well.

//map the 8-bit 595 shift registers to the 10-bit LED bars, specific to the way I wired them
  inputBytes[0] = powSF >> 6;
  inputBytes[1] = (powSF << 2) | (powLF >> 8);
  inputBytes[2] = powLF;
  inputBytes[3] = powEL >> 3;
  bitWrite(inputBytes[3], 0, bitRead(powEL, 4)); //fix the skipped 595 pin
  inputBytes[4] = (powEL << 4) | (powMP >> 6);
  inputBytes[5] = (powMP << 2) | (powOX >> 8);
  inputBytes[6] = powOX;

Finally, those 7 bytes can be shifted out.

  //prepare the shift register
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  digitalWrite(latchPin, LOW);

  //loop through the input bytes
  for (int j=0; j<=6; j++){
    byte inputByte = inputBytes[j];
    Serial.println(inputByte);
    shiftOut(dataPin, clockPin, MSBFIRST, inputByte);
  }
  
  //latch the values in when done shifting
  digitalWrite(latchPin, HIGH); 

 

And here's the result!

giphy.gif

 

Some vanity shots:

lnuemx8.jpg

PqguWAp.jpg

 

All that remains is to find or build an enclosure.

And I'll probably try to compile my own modified version of KSPSerialIO to add some improvements.

Link to comment
Share on other sites

For those interested, here's a list of the main parts I used. 

 

Parts

Cost Used as/for Link
LED bar graph  $   1,95 not used in final build https://www.sparkfun.com/products/9937
Arcade button  $   1,95 not used in final build https://www.sparkfun.com/products/9341
LED bar driver  $   2,45 not used in final build, only works with analog input, not PWM https://www.sparkfun.com/products/12694
Metal pushbutton  $   4,95 Stage button https://www.sparkfun.com/products/11968
LCD  $ 24,95 LCD https://www.sparkfun.com/products/9395
Slide pot knob  $   0,95 Throttle https://www.sparkfun.com/products/9120
Slide pot    $   2,95 Throttle https://www.sparkfun.com/products/9119
Toggle switch  $   1,95 SAS and RCS https://www.sparkfun.com/products/9276
Safety switch  $   2,95 Abort and Stage Lock (Arm) https://www.sparkfun.com/products/11310
Screws and spacers, 4 boxes with different sizes (M2, M2.5, M3)  € 17,00 Throttle, LCD and protoboards with LED bars https://nl.aliexpress.com/item/10pcs-Hex-nut-screw-M3-4-5-6-7-6mm-Hex-head-Brass-Threaded-Pillar-Female/32565388551.html?spm=a2g0s.9042311.0.0.Ri4DZM
Shift registers     0,54 Driving the LED bars (Fuel Gauges) https://nl.aliexpress.com/item/10pcs-free-shipping-SN74HC595N-SN74HC595-74HC595N-74HC595-DIP-16-Counter-Shift-Registers-Tri-State-8-Bit/32429190104.html?spm=a2g0s.9042311.0.0.Ri4DZM
LED bars     0,78 Fuel Gauges https://nl.aliexpress.com/item/2Pcs-10-Segment-LED-Bargraph-Light-Display-Module-Bar-Graph-Ultra-Bright-Red-Yellow-Green-Blue/32815306576.html?spm=a2g0s.9042311.0.0.Ri4DZM
4-axis joysticks  € 14,72 Translation and rotation controls https://nl.aliexpress.com/item/4-axis-potentiometer-Joystick-400-series-Rocker-hall-joystick-dimensional-resistance-5K-sealed-with-button-joystick/32258450509.html?spm=a2g0s.9042311.0.0.Ri4DZM
Buttons with LED (different colors)     3,00 Action Groups https://www.tinytronics.nl/shop/nl/componenten/schakelaars/rode-drukknop-16mm-reset-met-6.3v-led-verlichting?search=AB6Y-M-6.3V-RED
Rocker switch     0,50 Power (not implemented (yet)) https://www.tinytronics.nl/shop/nl/componenten/schakelaars/standaard-inbouw-wipschakelaar-normaal?search=switchn
Rocker switch small     0,45 Debug mode https://www.tinytronics.nl/shop/nl/componenten/schakelaars/standaard-inbouw-wipschakelaar-klein?search=switchs
protoboards     0,80 LED bars and shift registers https://www.tinytronics.nl/shop/nl/prototyping/printplaten/experimenteer-printplaat-5cm*7cm-dubbelzijdig?search=PCB5x7DUB
Toggle switch small     0,60 X/Y/X toggles for LCD mode https://www.tinytronics.nl/shop/nl/componenten/schakelaars/standaard-inbouw-tuimelschakelaar-mts-102?search=MTS-102
Link to comment
Share on other sites

Man, you are kickin' it! :cool:

That build is awesome, and you threw it together SO FAST!!! The BoM is cool too! I figured a total of $80.49 (unless I missed adding a line :P ). That's not bad at all, considering all the stuff you packed into it! I could't even guess what I've spent on mine, mainly cause so much was bought at different times, and I really haven't been keeping track all that well, and a bit of it is salvage... Wow... Organization, drive, and ambition, with a side of actually knowing how to code... It gets stuff done!

I... do not know how to code in C very well. Any variant... I enjoy the hardware work... The physical build, but the software is looming in front of me like a big final test that I've never studied for. That kinda worries me. It is inspirational though to see projects like yours come together so nicely! It gives me hope that there's a light at the end of my own tunnel someday! :wink:

Link to comment
Share on other sites

Some of the prices are per part, so need to be multiplied by 2 or 5 or 10. And the cost of the Arduino and the lasercutting are not listed, nor the shipping cots. But all in all, it's not too crazy.

I surprise myself with the progress I am making. I am usually very good at the first 70% of learning new things or making something. And than I lose interest and it sits in a corner collecting dust. Not with this, though! I can wait to work on it, I sometimes have a hard time getting to sleep because I am thinking about the next steps. I have "written" the code in my head multiple time before I got the time to sit down and type it out.

Your build is taking a bit longer, but it is sooo much more complex! It is going to be amazing once it is done. I'd be happy to help you with the coding.

Link to comment
Share on other sites

First of: WOW. I took half a year before I had something to show, and you have a far nicer result. Having a proper, open Git of source code is another nice touch, people could very well find it useful, more so since it is well documented.

If I recall correctly, pow is rather expensive. May I suggest a function myPow2(int n):

int myPow2(int n) // returns 2^n, calculated in integer math
{
  int result = 1;
  if (n = 0) return result;
  else if ( n < 0) return -1;
  else if (n > 30) return -1; //overflow
  else
  {
    for (int i = 0; i < n; i++) result = result *2;
    return result;
  }
}

Warning: untested

Link to comment
Share on other sites

Thanks! My first basic prototype was probably half a year ago, so the posts are going faster than the actual progress. It also helps that I did similar tinkering before so I didn't have a cold start.

And thanks for the suggestion! I did think about solving it that way, but didn't get I right the first try, so I used a workaround :D. I have saved your suggestion as an issue in my github, so I won't forget to try it out.

Oh by the way, I have decided to get the side panels for the enclosure laser cut also. It's really affordable (for me) and I would never be able to saw something myself so accurately. Also, it gives me the opportunity to get a logo engraved this time around! Stay tuned for the result!

Link to comment
Share on other sites

I was able to get the last two metrics to display (maxoverheat and time to next maneuver node) using dtostrf(). Thanks for the tip guys!

In other news, I have drawn the rest of the panels for the enclosure and am getting a quote from the laser cutter. These should feature the KSP logo as an engraving!

Link to comment
Share on other sites

23 minutes ago, zitronen said:

That's some dedication man! I think everyone who have attempted to make one of these can appreciate just how much work it takes to make one of these (so many **** wires!!!). Well done!

Thanks! And thank you for the great plugin that makes it all work. I am now diving into the details of your work so I can make my own tweaks. I have some coding experience, but I am by no means a software developer.

Link to comment
Share on other sites

I was curious about the exact cost for my controller. I bought plenty of excess parts during the build, as well as invested in some tools, but somebody that might want to recreate my controller could try to limit cost and buy only the following:

Item  USD   EUR  amount  TotalEUR  TotalUSD
Metal pushbutton  $    4,95      4,16 1              4,16  $          4,95
LCD  $  24,95    20,96 1            20,96  $        24,95
Slide pot knob  $    0,95      0,80 1              0,80  $          0,95
Slide pot    $    2,95      2,48 1              2,48  $          2,95
Toggle switch  $    1,95      1,64 2              3,28  $          3,90
Safety switch  $    2,95      2,48 2              4,96  $          5,90
Shift registers  $    0,64      0,54 7              3,78  $          4,50
LED bars  $    0,93      0,78 5              3,90  $          4,64
4-axis joysticks  $  17,52    14,72 2            29,44  $        35,05
Buttons with LED  $    3,57      3,00 10            30,00  $        35,71
Rocker switch  $    0,60      0,50 1              0,50  $          0,60
Rocker switch small  $    0,54      0,45 1              0,45  $          0,54
protoboards  $    0,95      0,80 4              3,20  $          3,81
Toggle switch small  $    0,71      0,60 1              0,60  $          0,71
arduino mega  $  45,95    38,60 1            38,60  $        45,95
lasercutting faceplate  $  48,24    40,52 1            40,52  $        48,24
lasercutting box  $  52,38    44,00 1            44,00  $        52,38
Total        €        231,61  $      275,73

UPDATE: lasercutting designs uploaded here: https://github.com/hugopeeters/KerbalControllerLasercut

Link to comment
Share on other sites

Finally, after another missed-delivery-while-I-was-home-stupid-mailman-grrr, it is time for the final assembly!

fKpQzD0.jpg

 

I'm not great at woodwork and stuff, so lot's of glue, some screws, and hope for the best.

8PxbRRp.jpg

 

I'll let it dry for a while before I fix the front panel to it and show off those vanity shots.

Link to comment
Share on other sites

Assembly complete! Whoohoo!

eYSQ8WG.jpg

Up, up and away!

qb5zpXp.jpg

giphy.gif

X0va9g1.jpg

You can tell from the pics I damages the boards a bit trying to assemble it. The screw holes are not in the correct places and screwing or nailing into the ends of the boards causes them to split. I solved the mounting of the front plate by hammering in two nails slightly further in, so the catch insode the board. Pressure from the cables bending inside, and the USB cable being fixed in the hole, keeps it securely in place.

You can also tell the unpainted MDF becomes darker where you touch it a lot. I'm fine with a worn out look it will get quickly, but you might want to use a clear coat of paint if you want to keep it neat.

Now it is time to play a full campaign with the controller, tweak the code along the way and maybe add some software-based improvements. If anybody has any suggestions for the "power" switch, let me know. It's hooked up to the Arduino, but not used yet. Same goes for the button on the translation joystick.

What's going to be my next project? An add-on module maybe? Sell or give away this unit and rebuild it from scratch (with improvements)? ... Who knows.

 

A little detail I added, just in case it ends up in a goodwill store one day :wink: 

nX4pCHc.jpg

 

Edited by hugopeeters
added gif
Link to comment
Share on other sites

Grats on your build, it turned out really nice. For the top hat I would suggest a toggle to turn down the sensitivity of the joystick by a factor of five or ten, it might come handy when docking. For the power switch, you could use it for toggle between gaming mode and  debug mode, when I twiddle with my code I can switch the LCD to display whatever relevant debug information I see fit for the project.

As for expansion, more displays and gauges are always a plus. A rotary selector for SAS modes maybe?

BTW what do you use for IVA in the mk II cockpit. It does not look like standard RPM?

Edited by Freshmeat
typo
Link to comment
Share on other sites

Hehe... That K-OS blue screen. :confused:

This has turned out beautifully! One tip, regarding mounting holes. If they don't line up perfectly, just drill them out. drilling will create a new hole alignment. If you can't get screws to bite anymore, then epoxy some nuts on the inside, so the screws have something solid to thread into. Sawdust and glue can also be used as a filler, and you can then redrill new holes as required.

But seriously... That build is so awesome! :cool:

Link to comment
Share on other sites

Thanks!

Great suggestions. I already have the smaller black switch as a debug mode though. I am thinking about writing a demo routine that can be toggled with the power switch. That way I can show it off without running the game...

SAS Modes: yes! I would love that.

It's stock on KSP 1.2.2. Only two mods installed are KSPSerialIO and Chatterer. :D 

 

1 minute ago, richfiles said:

Hehe... That K-OS blue screen. :confused:

This has turned out beautifully! One tip, regarding mounting holes. If they don't line up perfectly, just drill them out. drilling will create a new hole alignment. If you can't get screws to bite anymore, then epoxy some nuts on the inside, so the screws have something solid to thread into. Sawdust and glue can also be used as a filler, and you can then redrill new holes as required.

But seriously... That build is so awesome! :cool:

That's also stock. Really funny though!

Thanks richfiles! I can use all the tips and tricks regarding construction... :D 

Is there a blushing kerbal emoji? :o Thanks for the compliments guys!

Link to comment
Share on other sites

48 minutes ago, richfiles said:

Wait, the k-OS screen is stock?!? I assumed it was the scripting mod. :blush:

They put the blue screen in the IVA view in stock as a placeholder for mods that can display stuff on those screens. Pretty cool that they pay tribute to the kOS mod like that.

On seconds thought: maybe the blue screen was there first, and the mod used the name... :blush: <-- hey look there _is_ a blushing kerbal emoji!

Edited by hugopeeters
Link to comment
Share on other sites

  • 6 months later...

Hello, I'm using your project as a base to do mine. I have a few questions at this moment:

-Which values of resistors do you use for each color in led gauges?

-Have you managed the warnings on the code? seems that 'WriteLCD' function uses a deprecated writing method

 

Thank you for sharing the info.

Link to comment
Share on other sites

Hey @Roland108, that's cool!

The LED gauges have 680 Ohm resistors (blue, grey, black, black, brown) for all colours except for the yellow LEDS, those have what appear to be 510 Ohm resistors (green, brown, black, black, brown). It's best to test it out on a breadboard first, so you can change them around until you are happy.

The Arduino code? I don't get any warnings.

Link to comment
Share on other sites

I'm playing with a potentiometer to achieve a nice value where leds don't bright too much, but having a reference is always better. About the code, I get a warning on every line calling 'WriteLCD'. For example

Quote

 

J:\KSP\KerbalController-master\KerbalController\define_vesseldatadisplay.ino:273:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

     writeLCD("Heat: ");

 

 

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