Jump to content
The simFlight Network Forums

How to initiate values sent to arduino through lua script


Recommended Posts

 Hello,

I am working on a panel, which is based on an Arduino which is connected to some input and output elements.

When I start the lua script, it takes some time to connect to / boot the arduino. (The arduino has an LCD display attached to it acting as a cockpit watch and the display always goes black for a couple of seconds before lighting up again.) Through ipc.display, I can see, that all the parameters sent to the Arduino are initiated. But somehow the Arduino isn't ready yet, when they are send. As an effect, it isn't until the values change, that they are shown (resent) to the Arduino.

Is there a way to initiate the values correctly?

Many Thanks,

 Frank

Link to comment
Share on other sites

4 hours ago, FrankP said:

Through ipc.display, I can see, that all the parameters sent to the Arduino are initiated. But somehow the Arduino isn't ready yet, when they are send. As an effect, it isn't until the values change, that they are shown (resent) to the Arduino.

Is there a way to initiate the values correctly?

I'm afraid I don't know much about the Arduino. you'll need to find out what it needs. Wouldn't just sending it some dummy (not-real) values work?

Isn't the Arduino something you program yourself? If it is your Arduino program, wouldn't you know best what it is doing and what to do about it?

Pete

 

Link to comment
Share on other sites

Hi Frank.

 

From what you describe, it sound like the same problem I encountered.

What I noticed when connecting via a LUA script was that all of the events that I was monitoring would be called on startup and the resulting values were sent from LUA to Arduino.

I was only receiving a half dozen or so of these messages on the Arduino side. The rest were either lost or corrupt. After startup and things settled down, everything worked fine.

I send all of my com writes via my own function and log what I send to a text file. I could then see exactly what was sent to the Arduino card. The bottom line was, I was loosing a lot of messages at startup. (Buffer overflow.)

I resolved this by doing two things.

First, Increased the buffers size on the Arduino card.

Second, I introduced a delay in my ComWrite function of 300 ms for the first 20 seconds after startup. I use a delay of 50 ms thereafter.

This allowed the Arduino card to keep pace with LUA on startup.

Hope this helps,

Chris.

 

Link to comment
Share on other sites

8 hours ago, ccb777 said:

What I noticed when connecting via a LUA script was that all of the events that I was monitoring would be called on startup

Yes, for event.offset, the initial values make events. That is so that you get changes from then on. Else what are "changes" from? 

This is documented in the section on event.offset, thus:

 

"The function is also executed initially, when the plugin is first run, in order to initialise things. This saves using an explicit call to do the same"

If only subsequent changes are of interest (which would be unusual) you could use a flag to indicate when the channel to the Arduino is open, and open it in an event.timer function a few seconds or so after the plug-in is started.

But it would be better, of course, for the Arduino to simply keep up. I'm surprised it is so much slower -- perhaps a lower speed should be set at the PC end? But your solution of a larger buffer and a deley between each transmission also works, of course.

Pete

 

Link to comment
Share on other sites

 Hello Pete, hello Chris,

many thanks for both your replies. They helped me a lot to understand which assumed unknown unknowns don't exist and pointed me into the right direction.

I've thus been able to find a simple solution. I've simply added 2500ms delay after the serial communication has been initiated. That alone does the trick. The arduino needs around 2200 ms to boot. 300ms later the data from sent via the lua script kicks in and everythink works like a charm.

Here the relevant portion of the code in case anyone has the same problem.

Arduino_Com_Port = com.open("COM"..port_number, speed, handshake)
if Arduino_Com_Port == 0 then
    ipc.display("Could not connect to Switch Panel at COM"..port_number,10)
    ipc.exit()
else
    ipc.display("Switch Panel connected at COM"..port_number, 1, 3)
    ipc.sleep(2500)
end

Best regards,

 Frank

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.