-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Buggy time handling code (doesn't consider timer rollover) #2832
Comments
I don't think the rollover is a real issue (printer powered on for more than 50 days). |
The issue could easily be solved by adding a simple function:
In many cases just rearranging the guard in the correct way would solve the issue, like:
should be changed into:
Anyway, I Agree, It's only an issue if your printers are always on, and for print farms etc. The issue can just be added to the known bugs list and we can recommend to restart the TFT once a month to prevent the bug. BTW: Do you understand what is happening with the timestamps in uint8_t Touch_Enc_ReadPos(void)? |
What do you mean? |
Here is the function (from Touch_Encoder.c)
In line 10 there is a guard: This is "always" true because nowTime is equal to 0 or equal to a previous OS_GetTimeMs(), and OS_GetTimeMs() is always >= 0. |
yes, it is useless (I probably made some testing using timer but then I forgot that useless piece of code in the past, so I will remove it now). Touch_Enc_ReadPos() function is used in Marlin menu to handle the scroll Up/Down (touch, hold and move the finger on right part of the screen). |
I saw you updated
Do you believe this code has any purpose? I believe it does nothing except taking up time and making a small error in time calculation every 50 days because it skips a count. |
? It is no more present on my push (it has been removed from the code) |
Sorry, you are right, it's removed, well done, the TFT is getting faster and faster :D BTW: I fixed all the Timer rollover bugs, it only took me an hour or so. It's really trivial if you understand 32-bit modulus calculations. |
Eventually post the files but I would provide the changes in a specific PR (not urgent) |
Sure, I will do some extensive testing first, also for the DMA serial writing, which seems to be working very well, it causes a significant increase in the scan frequency. Unfortunately I only have the STM32F2_4 implementation, I don't have any other hardware, I only have the BTT TFT35 V3.0 and MKS TFT35 V1.0. |
Beware that not all ARM Cortex-M MCUs have a dedicated hardware divider. (ex Cortex-M3) |
Line 10 should be
so the touch coordinate is not read faster than every 1 ms. Altough it is a very small time, it is for debouncing purposes. (As you move your fingers on the TFT there can be fractions of a time where there's no physical touch detected. It is better understood if the output is monitored with an oscilloscope. I use debounce always on physical switches/contacts, otherwise you think you pressed the button once but the MCU can see actually hundreds of ON/OFF toggles.) To better serve its purpose I would change it to:
where ...or as per your anti-rollover method:
:) |
I made optimizations on the I might make a PR out of it but do not expect any tangible gain out of it. |
This is another example of a Timer rollover bug, please stop doing this. Here the correct version:
How about showing your improvements here? Let everyone review and discuss... I don't see any obvious relevant improvement anymore in TIM7/updatePrintTime, so please surprise me. @digant73 could add it to #2824 and I'm sure you would be credited. |
I already gave literally the same example.
It can be discussed in a PR also.
I do not have such goals (to surprise anyone here), but sure, I'll make a PR.
He has no problem to copy changes from an existing PR and whenever he did, usually no credits were given.
I was well aware of this, I never stated that TFT speed increase is not beneficial (especially if it is for free), I just stated that print speed will not benefit from it, a conclusion that I came to by tests, not by theory.
I did it out of curiosity. |
Yes, you did, you provide 2 wrong lines of code, and corrected one. Better teach everyone here to use the correct version, it's not an option or irrelevant thing. Please note, the correct way of handling timestamps doesn't prevent rollovers, it's not a "anti-rollover method", it just handles rollovers correctly so you don't have a glitch at rollover.
Of course you can, but it slows down progress here and decreases your chances of getting your proposals merged.
"He" is @digant73 I guess. I think you real pain is here... getting credit.
That's quite surprising to me. I can imagine gaining a ms left and right, which could add up to a few seconds maybe if you don't use ADVANCED_OK, but tell me more... tell me more... any numbers? |
I am sorry, I made a typo. What I meant to say is that " I just stated that print speed will NOT benefit from it" |
Normally it would speed it up and increase the chances of getting it merged. The maintainers of this repository nicely and explicitly asked us to provide clean pull requests with separate topics, not cluttered ones with many unrelated changes. Pull request Standards
It can be found here: https://github.com/bigtreetech/BIGTREETECH-TouchScreenFirmware/blob/master/coding_standard.md
So? He is not the only one! For example @guruathwal did a ton of heavy lifting for this FW to be a better one. You can check it here if you're not familiar with his work amount.
So?
The issue is solved for more than a month now. The path you two have taken is one where I've already been. If you remember, I already implemented ADVANCED_OK a few months ago. Implementing it was an eye opener for the fact (for me at least) that ADVANCED_OK is not the answer, it just mitigates the outcome of an issue and it is not universally applicable, it serves for a few setup only. So I really do not see how can I be any help in a matter that I already believe it's not the answer for the issue. |
@kisslorand Where's your hesitation bugfix? Do you agree that the current bugfix solves the hesitation issue? Or are there other issues you solved in your closed source solution? Of course a PRs should not cover too many things, but @digant73 has already added an (very low risk) improvements to #2824, it would make sense to add your additional improvements now too. Otherwise you risk that your improvement is too "lightweight" to justify a merge. It's up to you... |
On my PC.
I found out it creates more problems, it is not universally viable for all setups, it mitigates the outcome of the hesitation problem not fixes it.
Here: #2761 Later edit: sorry I remembered it wrong, I didn't share any findings there, only my concerns regarding ADVANCED_OK.
I highly doubt it's me who's holding up this firmware. I have numerous bugfixes not merged. However the next level firmware is already available for more than a month now in my repository.
It's implemented for more than a month now in my repository.
No. I already stated numerous time it just mitigates the outcome, doesn't fix anything. A painkiller is not fixing a rotten teeth, you need to go to the dentist to fix the teeth itself. For me the ADVANCED_OK is in the past. I always wanted to implement it, I eventually did it and than ditched it. End of story! I totally understand your enthusiasm towards ADVANCED_OK. I was in your shoes once. I wanted it also really badly and waited and waited for someone to implement it. Garry from PrintEngineering implemented it 4 or 5 years ago for the Artillery printers but he didn't make the source code available. I was really frustrated. Anyway there was no visible change with or without his ADVANCED_OK, we were printing at low speeds back in the time (80mm/s).
I am not sure what you're asking here. Of course I solved other issues too. The changelog can be read in my repository.
I do not risk anything, my printers are working just fine. ;) |
Good news! Some code has appeared (#2833), that's what I like to discuss here: @kisslorand Brilliant improvement for updatePrintTime, but why did you make void loopTouchScreen 2 to 3 times slower than before? Sabotage? |
@rondlh |
I'm only joking about the sabotage part, your implementation of loopTouchScreen is about 2-3 times slower than the old one, so I'm wondering why you did that? Code readability? UPDATE: @kisslorand My tests show that your loopTouchScreen is only 25% slower than the original code (STM32F207), not the 2-3 times I theoretically expected. Only code readability has decreased by a factor of 2-3. Here an easy way to make loopTouchScreen a tiny bit faster. Drop the guard inversion and swap the "normal" and "else" case. Original code:
Improved code:
If you want to make it 25% slower and obfuscate the code then replace it with this (Credit to @kisslorand):
|
OK. Let's keep the discussion in one place: #2833 |
What do you mean "end of story"!? This is about a problem in Cura 4.7.1 (too many small segments) in combination with low-powered devices. And ADVANCED_OK is reported to improve the situation significantly, but cannot fully solve the problem (buffer underruns). To fully address the problem you need to upgrade Cura or/and new/faster hardware. I guess you will never wear a raincoat or use an umbrella, because you will still get wet, right? |
I will use a raincoat and/or umbrella because I cannot fix the root of the problem.
I mean that I'm done with believing that ADVANCED_OK is a magic wand. I think we can put an end to the debate about ADVANCED_OK. As I said, I fully understand your enthusiasm towards it. For the moment it's the best fix you two could come up with. I've already been there, I get it. The tone of the discussion starts to deviate from a friendly one so with your permission I will excuse myself from it to save what's left and keep it from degenerate further. |
@digant73 Your monitoring update is really nice (#2824), I've abused it to do some benchmarks lately, potentially you could add a few blank (commented out) lines for general purpose use. I would for example be interested to see how much free memory is available, and perhaps MCU loading during printing would be interesting too. |
that menu was meant to be extended in the future adding any KPI that can provide useful info. I didn't add other info at the moment simply because the menu was initially (and enabled at compile time) focused only to monitor resources for "advanced ok" feature. |
Sure, I understood from your previous comments on ADVANCED_OK. I like it, it's very useful and easy to use and abuse :D |
Not sure about the sentences from kisslorand about extra resources on Marlin side to support ADVANCED_OK. In my printers I even reduced BUFSIZE to 8 (from 32) while maintaining even RX_BUFFER_SIZE disabled. |
That user seems to have quite outdated and incorrect insights and information, his arguments do not seem objective, very little code but lots of talk. I like to talk about code or at least strategy, just saying things are bad is useless if you don't give more pointers afterwards, or even better, show some draft sample code to discuss. |
I have tested what actually happens when a timer rollover happens (by using a time machine moving 49 days into the future). |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
While looking through the TFT source code I noticed some issues, the time handling code does not handle timer rollovers correctly. This happens in many places in the code (just search for "OS_GetTimeMs"), luckily rollovers are not frequent (it takes 50 days), but this issue will probably cause a hiccup of the TFT, perhaps even a lockup.
Here an example (common.c):
The problem here is that "lastTime + duration" does rollover before curTime does, causing the
if
statement guard to become true immediately, which will happen again and again until curTime also rolls over.The correct code would be this:
if (curTime - lastTime > duration)
This code is rollover safe.
Another example (menu.c)
This should be:
So this makes me think that timer rollovers were not considered, but then I came across this piece of code in timer.c
The last lines seem very odd to me, it looks like someone tried to prevent a rollover from happening.
This code is executed 1000x per second, waiting until 1ms before a rollover, then prevents a rollover. If you just let the rollover happen, then everything (timing) will be correct again. The code seems to only slows down the TFT, or am I missing something?
Also the updatePrintTime is quite odd if you consider what happens in updatePrintTime:
The following will be much more efficient (remove odd rollover code, don't call updatePrintTime when not needed):
UPDATE: I did a quick scan to find the bugs, here the results
Bug is present in:
Correct timer handling in:
The text was updated successfully, but these errors were encountered: