zitronen

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

Recommended Posts

4 hours ago, BamBam said:

ok, so i have belatedly come to realize that i cannot simply copy somebody's code without having their exact setup.  So, I will be starting from the Demo17 code.  That Demo17 code works just as it should.

Here is my rough plan...  I have a USB-HID interface board that I will be using for the actual input into the game.  that lets me keep the input functionality separate and simple.  I plan to use serialIO to drive my indicator LEDs and a set of 7 segment / 8 digit displays to show altitude, apoapsis, periapsis, velocity.  and some analog guages to show fuel levels, and atmosphere density.  It means that i will have to have two USB cables connected but that is no big deal.

So, instead of somebody to solve my problem for me, what i need is a really basic tutorial on how to make the starter code in Demo17 to do what i tell it.  My programming skills are all from 1994 when we did Turbo Pascal in High School.  Since then i have not done anything more than write batch files and a few really simple if/then operations.  So I have basically zero experience with Arduino.  My 3D printer ran briefly on Marlin but even that code was already setup and you just had to define a few variables.

Dipping into some major wishful thinking here, I saw on this or maybe on the subreddit /r/kerbalcontrollers that somebody had made a sort of DSKY display that used a small LCD panel.  That was beautiful, and if I could figure out how that was implemented that would be a major upgrade to my sort of simplistic plan at the moment.

Taking the demo code and building on it is exactly what I'd recommend, that's how I've gotten started.  Take a look at my starter controller a few posts up, I've gotten something very similar to what you're describing.  7-seg displays for the Ap/Pe/Alt/Velocity, some analog gauges, and nice big buttons for staging.

As for learning the Arduino environment, I wholeheartedly recommend the Sparkfun Inventor's Kit as well as the matching Guidebook.  It's exactly how I got started into the world of micro-electronics, and will give the building-blocks to start learning the Arduino language (which is really just C++ with some additional functions/subroutines built in).  Sparkfun (while they are expensive) also has a bunch of add-ons and products that will make your life much easier, such as their Serial 7-Segment Display and the matching Hookup Guide which makes hooking up a lot of 7-segment displays vastly simpler. 

Their support and guides on how to hook things up have basically saved my ass, almost all of my controller is built off of chunks of their demo code that I've modified to work with my own variable names.

Edited by tsaven

Share this post


Link to post
Share on other sites

ok.  so this function under the output section.

void controls() {
  if (Connected) {

    if (digitalRead(SASPIN)) { //--------- This is how you do main controls
      MainControls(SAS, HIGH);
      setSASMode(SMSAS); //setting SAS mode
      //setNavballMode(NAVBallSURFACE); //setting navball mode
    }
    else {
      //setNavballMode(NAVBallTARGET);
      MainControls(SAS, LOW);
    }

    if (digitalRead(RCSPIN))
      MainControls(RCS, HIGH);
    else
      MainControls(RCS, LOW);

    if (digitalRead(CG1PIN))   //--------- This is how you do control groups
      ControlGroups(1, HIGH);
    else
      ControlGroups(1, LOW);
      
    /*
       if (getSASMode() == SMPrograde) { //--------- This is how you read SAS modes
         //Blink LED, do stuff, etc.
       }

       if (getNavballMode() == NAVBallTARGET) { //--------- This is how you read navball modes
         //Blink LED, do stuff, etc.
       }
    */


    //This is an example of reading analog inputs to an axis, with deadband and limits
    CPacket.Throttle = constrain(map(analogRead(THROTTLEPIN), THROTTLEDB, 1024 - THROTTLEDB, 0, 1000), 0, 1000);

    //This is an example of reading analog inputs to an axis, with deadband and limits
    //CPacket.Pitch = constrain(map(analogRead(THROTTLEPIN),0,1024,-1000,1000),-1000, 1000);

    KSPBoardSendData(details(CPacket));
  }
}

Where it calls 'if digitalread rcspin'

that means :

  if the digital pin defined as 'whateverpin' is connected to ground (button pressed), the do the following

make 'whatevercommand' have a 1 as its bit 

else

make 'whatevercommand' have a 0 as its bit

 

and then the "maincontrols" function enforces that its binary and not some analog value, and ads it to the packet.  

Have I got that right?  so each button press command you want to send thru serialio follows that syntax?

Share this post


Link to post
Share on other sites
20 hours ago, Freshmeat said:

It is a bit hard for me to understand what your problem is, but if I understand correctly, the demo code runs, but not your own code. To help, we will need to see you own code. Please, do not paste it in a post, but upload it to somewhere we can download and look. I do recall wurmis name, but not any particulars of his project. If it is old, you might have to update the VesselData and ControlPacket structs with the new entries. Also, a sketch is keyed to a specific hardware setup, so unless you have the exact equivalent setup and wiring the sketch will not do anything useful.

By the way, the other guys code I am looking into using is this.

https://github.com/Richi0D/Kerbalcontroller

 

My hardware goals would align with this for the most part.  I quite like those displays, but i dont want to order them unless i can figure out the programming.

 

EUREKA!!!!     The buad rate in this batch of code for the serial connection was different than the one in Demo17.  It was set way higher.  I changed it down, and now it talks!. The tx and rx lights on the Arduino go active when loading a rocket.   Thats enough tinkering for today.  I can go to sleep feeling all accomplished and stuff.

Edited by BamBam

Share this post


Link to post
Share on other sites
18 hours ago, BamBam said:

Diipping into some major wishful thinking here, I saw on this or maybe on the subreddit /r/kerbalcontrollers that somebody had made a sort of DSKY display that used a small LCD panel.  That was beautiful, and if I could figure out how that was implemented that would be a major upgrade to my sort of simplistic at the moment.

As has been said, start small and get the simple little things under your belt first. That DSKY was a fair bit of work, but looking back, it was just a *lot* of simple things all combined together. I did have to learn how to code a WPF form to do it though. Simple is all relative to where you are on the learning curve. Something like taking the entire RPM/MAS variable list and pushing it to the Arduino/Teensy, should not be anywhere near a first step.

It sounds like your making progress though, and that's all that's required for this hobby - "small progress" It just gets built upon each time you add to it. <- There's a Kerbal saying if I've every heard one. :D

Congrats on the baud rate, that's tripped me up a few times too.

I can relate to your plan with doing a software  focus first. I've tried combining the hardware builds at the same time and just burn out for a month or two due to no progress as there wasn't software planned out to run the hardware. Other people can do it the other way around, so there is a bit of personal preference there.

Edited by cyberKerb

Share this post


Link to post
Share on other sites
On 9/16/2019 at 10:20 AM, tsaven said:

@zitronen, I have to give mad props to you for creating and maintaining a mod that has such capabilities.  I'm farther along in my controller than I ever expected to be and I'd never have been able to do it if your demo code hadn't spelled things out so clearly and easily.  Thank you, thank you for documenting and commenting the code out as well as you have.

I'm integrating more and more functions into my controller every day!

 

 

Well done! When you guys get stuff working, it makes me happy. I would suggest adding time to AP, it's really useful to have to know if you are going to slow or to fast to orbit efficiently.

 

12 hours ago, BamBam said:

ok.  so this function under the output section.


void controls() {
  if (Connected) {

    if (digitalRead(SASPIN)) { //--------- This is how you do main controls
      MainControls(SAS, HIGH);
      setSASMode(SMSAS); //setting SAS mode
      //setNavballMode(NAVBallSURFACE); //setting navball mode
    }
    else {
      //setNavballMode(NAVBallTARGET);
      MainControls(SAS, LOW);
    }

    if (digitalRead(RCSPIN))
      MainControls(RCS, HIGH);
    else
      MainControls(RCS, LOW);

    if (digitalRead(CG1PIN))   //--------- This is how you do control groups
      ControlGroups(1, HIGH);
    else
      ControlGroups(1, LOW);
      
    /*
       if (getSASMode() == SMPrograde) { //--------- This is how you read SAS modes
         //Blink LED, do stuff, etc.
       }

       if (getNavballMode() == NAVBallTARGET) { //--------- This is how you read navball modes
         //Blink LED, do stuff, etc.
       }
    */


    //This is an example of reading analog inputs to an axis, with deadband and limits
    CPacket.Throttle = constrain(map(analogRead(THROTTLEPIN), THROTTLEDB, 1024 - THROTTLEDB, 0, 1000), 0, 1000);

    //This is an example of reading analog inputs to an axis, with deadband and limits
    //CPacket.Pitch = constrain(map(analogRead(THROTTLEPIN),0,1024,-1000,1000),-1000, 1000);

    KSPBoardSendData(details(CPacket));
  }
}

Where it calls 'if digitalread rcspin'

that means :

  if the digital pin defined as 'whateverpin' is connected to ground (button pressed), the do the following

make 'whatevercommand' have a 1 as its bit 

else

make 'whatevercommand' have a 0 as its bit

 

and then the "maincontrols" function enforces that its binary and not some analog value, and ads it to the packet.  

Have I got that right?  so each button press command you want to send thru serialio follows that syntax?

More or less. The way it's written the command goes on when pin is connected to Vcc (3.3 or 5V) not ground, but it's the same principle. Also this is set up for a switch, not a button, unless the button is latching. In order to turn on RCS, a specific bit in the controls byte of the data packet needs to be set. The maincontrols() function just makes the code easier for people to read. Rather than having to remember which bit number is RCS, and doing the bit maths do set it, you just do maincontrols(RCS, HIGH).

 

Share this post


Link to post
Share on other sites
3 hours ago, zitronen said:

Well done! When you guys get stuff working, it makes me happy. I would suggest adding time to AP, it's really useful to have to know if you are going to slow or to fast to orbit efficiently.

I plan to, but right now I'm still having a lot of problems with the timing calculations.  The time displays work fine part of the time, but the seconds counter will randomly get stuck for exactly 59 seconds (until the minute number needs to change) at which point it will work for another 15 seconds before getting stuck again.

I've got a 4x20 LCD that I need to hook up so I can spit variables to it from different parts of the calculations to figure out exactly where it's messing up, but I wanted to make sure I got all of the more basic controls functioning first before I move on to troubleshooting the more ancillary bits.

Share this post


Link to post
Share on other sites

Hey Zitronen, my controller with fastleds / LCD / slider-pot is now working on  IO Vers. 0.19.1 and a reset-up from code base 0.17.   

I altered the RX buffer to 256 and Serial rate to 115200 as i had a connection problem.
but on my arduino i had to write-unlock the hardware.h in the core section, because there was no .h file in the preferences folder. 

 

 

Edited by sec3

Share this post


Link to post
Share on other sites

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.