help with code plea...
 
Notifications
Clear all

help with code please

27 Posts
8 Users
0 Reactions
9,307 Views
Posts: 164
(@victor-pv)
Estimable Member
Joined: 12 years ago

Anyway the way the header works in most of the simple sketches we see around here (general serial ~!) you don't really lose those values, and using them does not cause any problem.
If we use let's say 16 channels, the sketches only look for those 2 values right before a block of another 16 bytes, and does not check for them again until it reads 16 more bytes and writes them to the channels.
Then it waits to see those 2 values again, and then get the next 16 bytes straight again without checking their value.
In case a byte get's lost in flight, and 15 bytes of payload arrive only, then it will take ~ as the 16th byte of payload.
So the next byte in the buffer is !, but the sketch is looking now for the first byte of the header, ~, so it discards the ! and keeps discarding bytes until it gets ~, and right next !, if it doesn't get those two in sequence, it keeps discarding bytes.
So two conditions may happen when a byte is lost in flight and loses the sync:
1- randomly the sequence actually uses those two values for two consecutive channels, ~ and !. In that case, it will think the next 16 bytes are payload, and may mess up with the lights. That is very unlikely but possible. If that happens, unless your sequence keeps sending those 2 bytes for 2 channels several times, it should resync in the next block of data.
2.- Your sequence does not contain those 2 values for 2 consecutive channels, in which case 1 block of 15 bytes is discarded looking for the header, until it gets to the next good header, and runs in synch from there on.

The way I see it, it is very unlikely that your sequence will contain those 2 bytes exactly at the exact same time that the Arduino got out of sync and is checking every byte in the buffer trying to find the header.
If it actually happens once, no big deal, it should re-synch immediately after as long as your sequence do not keep sending the same 2 values for the same two consecutive channels for a long time.
If you have the bad luck that it happens that your arduino gets out of sync exactly in one part of your sequence is sending those 2 bytes for two consecutive channels for a long time, then your show will look pretty weird for as long as your sequence sends those 2 values in the same exact positions.

But what are the probabilities of so many coincidences happening at once?
I think 2 bytes for a header gives a fairly good margin.


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
victor_pv wrote:Anyway the way the header works in most of the simple sketches we see around here (general serial ~!) you don't really lose those values, and using them does not cause any problem.....

Well, it appears to be that way for the users here. I think sync problems were usually channel count issues, rather than false header confusion in the Arduino sketch.
But, I also got to learn the finesse details of the Renard protocol that I was not aware of before.


Reply
Posts: 3650
(@jhinkle)
Famed Member
Joined: 15 years ago
victor_pv wrote:Anyway the way the header works in most of the simple sketches we see around here (general serial ~!) you don't really lose those values, and using them does not cause any problem.
If we use let's say 16 channels, the sketches only look for those 2 values right before a block of another 16 bytes, and does not check for them again until it reads 16 more bytes and writes them to the channels.
Then it waits to see those 2 values again, and then get the next 16 bytes straight again without checking their value.
In case a byte get's lost in flight, and 15 bytes of payload arrive only, then it will take ~ as the 16th byte of payload.
So the next byte in the buffer is !, but the sketch is looking now for the first byte of the header, ~, so it discards the ! and keeps discarding bytes until it gets ~, and right next !, if it doesn't get those two in sequence, it keeps discarding bytes.
So two conditions may happen when a byte is lost in flight and loses the sync:
1- randomly the sequence actually uses those two values for two consecutive channels, ~ and !. In that case, it will think the next 16 bytes are payload, and may mess up with the lights. That is very unlikely but possible. If that happens, unless your sequence keeps sending those 2 bytes for 2 channels several times, it should resync in the next block of data.
2.- Your sequence does not contain those 2 values for 2 consecutive channels, in which case 1 block of 15 bytes is discarded looking for the header, until it gets to the next good header, and runs in synch from there on.

The way I see it, it is very unlikely that your sequence will contain those 2 bytes exactly at the exact same time that the Arduino got out of sync and is checking every byte in the buffer trying to find the header.
If it actually happens once, no big deal, it should re-synch immediately after as long as your sequence do not keep sending the same 2 values for the same two consecutive channels for a long time.
If you have the bad luck that it happens that your arduino gets out of sync exactly in one part of your sequence is sending those 2 bytes for two consecutive channels for a long time, then your show will look pretty weird for as long as your sequence sends those 2 values in the same exact positions.

But what are the probabilities of so many coincidences happening at once?
I think 2 bytes for a header gives a fairly good margin.

Please don't take my comments personally.

The statements you made suggests that it's ok to develop sloppy code because it's done all the time in the Arduino community.

I usually stay out of this forum because most of what I see is flawed and most appear not to want to change it.

The young man who posted this thread states that he is 19 and self taught.

If he learns from what I usually see in Arundio sketches -- he is using a very poor media for learning.

One should learn how to do it correctly -- covering all boundary conditions -- no matter how much code it takes or how slow it executes.

Once that person understands how to "fully" design - then they can choose to get loosy-goosy and start making things not as robust.

The issue I always see in the Arduino community is someone solves/designs something -- not very robust in that all boundary conditions are accounted for -- and then posts it. Others, who may be trying to learn (as the poster of this thread) -- takes that code and applies it to something else with the belief that it should work --- and has no idea why it may fail. They also learn and consider the BAD implementation as "Good Code" to be used whenever necessary.

The poster asked how to do a header. If someone post a loosy-goosy implementation (because that is how most Ardunio sketches do it) -- it may work today with his data set --- but what about a year from now when he takes that code and applies it elsewhere where the data set set bites him --- he will have no idea of why the issue suddenly popped up -- and may not have developed the skill set to solve it because he was never taught the right way.

I'm sorry if I sound like I'm on a soap box.

I would think the Arduino community (as a DIY community) would want to push and strive for "commercial" grade code instead of "hobby" code. Doing so would help educate those learning to do it right.

There are many "commercial" ways to solve a issue --- there are a million times more "hobby" ways --- most will end up in forums like this one with users asking why their code misbehaves.

Joe


Reply
Posts: 164
(@victor-pv)
Estimable Member
Joined: 12 years ago

Don't worry I don't take it personally.

I was not trying to imply that was the best solution or even a good one, but just explaining how those sketches normally work. I believe it provides a fairly good chance of resynchronizing without messing up for too long.

I don't think it is at a commercial level, I would expect to at least have also a CRC at the end of each block, so any corrupted data can be discarded right away, but so far I haven't seen that used over here, and no idea if there is any Vixen setting or any plugin that can do it.

I agree with you that having better examples to start with helps people learn the correct way of doing things from the start. But on the other hand having things that just work and are not overly complicated will keep them interested. I think Arduino tries to seat in the middle, as an example Jetty was pointing out how extremely slow digitalWrite and analogWrite are in a thread a few days ago. They are not the most efficient commands, but they are simple. For me, as I am learning, they help me get going, but I learn that they are slow, and if I ever have to do something with arduino that needs efficiency, I'll avoid them.


Reply
Posts: 6728
(@p-short)
Illustrious Member
Joined: 20 years ago

Is there an easy way for an arduino to detect a break? If so, that could be used as a frame delimiter.

Another thing that I've done on personal projects is to use 0x00 as a sync byte, and accept that there are only 255 levels of dimming. Although there are times where this isn't the right solution, mostly nobody would notice the difference between 255 and 256 levels.

P.S. I've modified an existing plugin to insert the 0x00 sync character and substitute a 0x01 for the 0x00 in the user data stream.


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
P. Short wrote:Is there an easy way for an arduino to detect a break? If so, that could be used as a frame delimiter.

Another thing that I've done on personal projects is to use 0x00 as a sync byte, and accept that there are only 255 levels of dimming. Although there are times where this isn't the right solution, mostly nobody would notice the difference between 255 and 256 levels.

P.S. I've modified an existing plugin to insert the 0x00 sync character and substitute a 0x01 for the 0x00 in the user data stream.

I think that the better way forward is to not use the "Generic Serial" but to use the Renard protocol, or a similar one, which incorporates the robustness of serial communication. Then we wouldn't need to fiddle with headers, or frame bytes because it would part of the protocol. Vixen already has the plugin for it.

If adopted, we ( the "techie" helpers who agree) would only support proper coding as we learn the stuff and then promote those sketches to newbies with these protocols implemented for their channel counts, and either on-off or dimming needs. We would not assist newbies in "spaghetti" code changes. Perhaps within a year we'll have better "good practises" within our lighting community. 🙂


Reply
Posts: 164
(@victor-pv)
Estimable Member
Joined: 12 years ago
LightUp wrote:I think that the better way forward is to not use the "Generic Serial" but to use the Renard protocol, or a similar one, which incorporates the robustness of serial communication. Then we wouldn't need to fiddle with headers, or frame bytes because it would part of the protocol. Vixen already has the plugin for it.

If adopted, we ( the "techie" helpers who agree) would only support proper coding as we learn the stuff and then promote those sketches to newbies with these protocols implemented for their channel counts, and either on-off or dimming needs. We would not assist newbies in "spaghetti" code changes. Perhaps within a year we'll have better "good practises" within our lighting community. 🙂

I agree that promoting good coding practicing is the best, and I really look forward to learn more from the people that really knows them.
On the other hand for people that opens a thread saying this is the sketch I use but fails to do this and that, what do we do?
Should we try to make a simple universal sketch for driving SSRs directly with an arduino but using proper coding techniques, and Renard, and make it available first, so we can point those people to that sketch? kind of like we did with the mix up I did?
I just don't want to refuse helping someone unless I there is something better to point them too, I'd rather have them running with something imperfect than not running.


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
victor_pv wrote:...On the other hand for people that opens a thread saying this is the sketch I use but fails to do this and that, what do we do?
Should we try to make a simple universal sketch for driving SSRs directly with an arduino but using proper coding techniques, and Renard, and make it available first, so we can point those people to that sketch? kind of like we did with the mix up I did?

I don't want to sound like "its my way or the highway". I would envisage the offer of an alternative code that from experience is more robust. Something akin to what you started. It would be an "improved" offer that has the same functionality. I'm not a professional coder so I don't have all the answers to efficient and robust code. But as we get to know better ways we would offer these alternatives. We could also offer suggestions of why we are going this way - to reduce newbie frustration of not getting the results they were looking for. And to build on a good foundation for expansion as we get to discover improved methods from more experienced programmers.

Since Vixen already has the plugin for the Renard protocol, we could offer an Arduino Renard receiver for the serial communication rather than the "header" version. Newbies still have the option to chose the current code or the suggested newer improved versions.


Reply
Posts: 152
(@jetty)
Estimable Member
Joined: 14 years ago
victor_pv wrote:Don't worry I don't take it personally.
don't think it is at a commercial level, I would expect to at least have also a CRC at the end of each block, so any corrupted data can be discarded right away, but so far I haven't seen that used over here, and no idea if there is any Vixen setting or any plugin that can do it.

Personally I'd say that Renard is a lossy protocol and and is commercial grade but doesn't require a CRC, provided you remember that lossy protocols are designed to be okay when they lose data.

The data is retransmitted every frame with a header. If you lose one frame, or corrupt a frame, it's corrected the next frame.

The only thing you have to watch out for is corruption on the final frame, so it's wise to make sure you have a "switch all lights too off" after a period of non-transmission.


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
jetty wrote:Personally I'd say that Renard is a lossy protocol and and is commercial grade but doesn't require a CRC, provided you remember that lossy protocols are designed to be okay when they lose data.

The data is retransmitted every frame with a header. If you lose one frame, or corrupt a frame, it's corrected the next frame.

The only thing you have to watch out for is corruption on the final frame, so it's wise to make sure you have a "switch all lights too off" after a period of non-transmission.

I'm not sure where this conversation is going.
We need a serial communication line of some kind. If "generic serial" doesn't provide the "commercial grade", Renard is "lossy" and the "header/footer" is too "skoppy" then what alternative can we use tomorrow?

There are places for a CNC machine to cut boards very accurately. But there are times I'm just going to grab my handsaw to get a job done. Is this conversation implying that using a hand saw is too sloppy and shouldn't be used? Most newbies don't have a CNC tool in their toolbox. I may be too pragmatic. 🙂

Edit: Is "best practise" turning into "perfect practise"?


Reply
Page 2 / 3
Share: