Author Topic: [arduino] can't subtract two time stamps  (Read 4418 times)

0 Members and 1 Guest are viewing this topic.

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
[arduino] can't subtract two time stamps
« on: October 20, 2021, 08:00:12 am »
I am trying to calculate a speed based on how many pulse's a proximity sensor next to a toothed wheel has generated in an amount of time.

Code: [Select]
uint32_t varLastSpeedTime;
volatile int16_t vTachoSpeedCounts;

void lcdSpeedPrint() {
  uint32_t tempvarTime =  millis();
  uint32_t vtSpeed = (vTachoSpeedCounts * tachoStep * 600) / (millis() - varLastSpeedTime);
  int32_t tvTimeDifference = ((int32_t)tempvarTime - (int32_t)varLastSpeedTime);
  char text[6] = {0, 0, 0, 0, 0, 0};
  char counter = 0;
  char temp;
 
  lcd.print(millis());
  lcd.setCursor(0, 2);
  lcd.print(vTachoSpeedCounts);
  lcd.setCursor(0, 3);
  lcd.print(tvTimeDifference, DEC);

  varLastSpeedTime = millis();
  vTachoSpeedCounts = 0;

The difference between the current time and the last time always results in some randomly low number lik 6 or 9 or 7.

Initially "vTachoSpeedCounts" was called "varTachoSpeedCounts" and it failed to update. I don't know if anything starting with "var" has some other meaning in Arduino but the colour of the text never changed. Unfortunately colour highlighting on the arduino IDE is very limited and you can't tell the difference between things like variables and defined text.
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2191
  • Country: us
Re: [arduino] can't subtract two time stamps
« Reply #1 on: October 20, 2021, 08:58:46 am »
First thing I'd do is replace all but the first millis() call with tempvarTime. I guarantee the last millis() call isn't returning the same number as the first.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #2 on: October 20, 2021, 09:24:18 am »
Nope, not that the calculation the only other call to millis is in is being used. I gave up displaying the speed and just wanted to see the time difference come out right. As the function is called around every 2s I should get something just over 2000.

vTachoSpeedCounts will count up if I don't reset it, but when I do and put a signal in at 20Hz I get 0,

this is absurd and annoying because it's not like I can't write a program to do this, I'm trying to get a hitachi controller based display to work on a SAMD so that I can for now ditch this arduino crap but converting peter fleury's library from AVR to ARM is a good bit harder than converting from one AVR to another.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 2566
  • Country: nz
  • Formerly SiFive, Samsung R&D
Re: [arduino] can't subtract two time stamps
« Reply #3 on: October 20, 2021, 09:33:29 am »
Of course it doesn't matter what the variable names are.

And it is evident that your function is being called about every 10 ms not every 2 seconds as you state. Which is why the count is zero for a 20 Hz signal.

That's your problem.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #4 on: October 20, 2021, 09:37:58 am »
why is it evident that it is being called every 10ms? This is just one function in a program. As you can see I am putting the current millis() on the display, this does indeed every 2s update (very visibly) by another 2000 and a bit).

If I increase my signal to 1kHz I get "8" as an answer.

Code: [Select]
void tachoUpdate() {
  vTachoSpeedCounts ++;
  tachoDistanceCounts ++; 
}

And yes so long as the name started with var it never updated but will sort of now. It's crazy
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #5 on: October 20, 2021, 10:12:29 am »
Complete project attached. I have attempted to use multiple files in the same way you would in any C/C++ IDE.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 4802
  • Country: fi
Re: [arduino] can't subtract two time stamps
« Reply #6 on: October 20, 2021, 10:22:14 am »
Signed int wraparound is implementation defined behavior and may or may not work with luck.

Always cast timestamp to unsigned types, do unsigned subtraction. You can then cast the result to signed type.

Didn't look the code further, there may be other issues as well.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 2566
  • Country: nz
  • Formerly SiFive, Samsung R&D
Re: [arduino] can't subtract two time stamps
« Reply #7 on: October 20, 2021, 10:51:28 am »
And yes so long as the name started with var it never updated but will sort of now. It's crazy

Impossible. Just simply impossible.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #8 on: October 20, 2021, 10:52:07 am »
It makes no difference. The value is always positive so the issue will only occur if I exceed 32s which I do not plan to. The lcd.print() fucntion apparently will want signed integers. I guess that providing I do not exceed half of the storage limit of the variable type it will work with signed or unsigned as it does in their own example of printing millis() to the screen.

I have tried with various variable types but the result is always the same. Just the same as the variable counts on the interrupt routine never come out right, but the distance travelled calculation works fine.

Distance travelled has a variable that is updated on every interrupt and speed calculation also has a variable of the same type that is updated in the same interrupt function. So one variable will update every time but the other not? if I just update both it works, as soon as I zero the speed calculation variable on the exit of the function called every loop it suddenly stops working.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #9 on: October 20, 2021, 12:13:26 pm »
I dunno how that lcd library works, but if it does hard delays to get the LCD timing right it will cause you major problems because you are disabling interrupts before any LCD stuff starts then re-enabling interrupts after the LCD stuff is finished. 

Think of CPU execution time inside void loop() as a pie chart showing the percentage of cpu cycles with interrupts on vs off.

Hard delays uses many 10000 of cpu cycles.  So if the total cycles for each loop of void main() is 10000 of pointless delay cycles + 100 cycles running your code that means your interrupts are off for like 99% of the time which would make you miss lots of tacho edge interrupts.

I cant see why you should need to disable interrupts to write to the LCD.
Except maybe if the library is using hard delays and doesn't want anything else messing with the timing.  But if that's the case that is also the problem.
Try take out your interrupt enable and disable lines and see what happens.
Any LCD library that uses hard delays is badly written.
« Last Edit: October 20, 2021, 12:19:42 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #10 on: October 20, 2021, 12:44:29 pm »
I put the disable and enable interrupts in to make sure nothing was happening in the middle, turned out it's the same as with interrupts on all the time. Something is going really screwy. I am about to look up the math stuff like subtraction. Every time I do a subtraction the result is near 0.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #11 on: October 20, 2021, 01:28:55 pm »
Go back to basics. Check output of the mills() function first

Display it on your LCD and confirm it makes sense.

Sure it will be changing super fast and the lower digits will jump and miss some values, but you can see if it appears to be changing at the right speed.
Maybe your clock or maybe the freq #define is not right and mills function thinks the cpu is running slower/faster than it really is.

basic add/sub calcs not working is usually an indication you are doing something wrong with signed vs unsigned variables.  Or that something is not set 'volatile' and it should be.
« Last Edit: October 20, 2021, 01:30:47 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #12 on: October 20, 2021, 01:40:57 pm »
Go back to basics. Check output of the mills() function first

Display it on your LCD and confirm it makes sense.

Sure it will be changing super fast and the lower digits will jump and miss some values, but you can see if it appears to be changing at the right speed.
Maybe your clock or maybe the freq #define is not right and mills function thinks the cpu is running slower/faster than it really is.

basic add/sub calcs not working is usually an indication you are doing something wrong with signed vs unsigned variables.  Or that something is not set 'volatile' and it should be.

Yes I have done that already, if I:

lcd.print(millis());

I see the time on the dispaly, in this case it increments by a little over 2000 at each update as the function is called every 2s. It's the moment I try to subtract 2 numbers.

I have the sketch in multiple files, if a lack of a volatile is the problem then every damn variable needs to be volatile. When I call a variable not declared in the function what is supposed to happen? I have written this as I would any C project and never had all this before when I am bare metalling it.

I am currently investigating ripping the libraries I need out of the arduino and rewriting all the low level stuff myself like pinMode and digitalWrihe ect. Yes the LCD library is full of delays. Actually doing delays of a few µs with interrups I guess does not leave much time for anything else to happen anyway and if they deem that having the exact delay is more important they have made a choice to make a mess of things. If an interrupt occurs during a delay surely the interrupt is serviced?

If I am using a delay I would happily accept that the delay may be longer if I know something useful is being done.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #13 on: October 20, 2021, 09:39:47 pm »
I don't think your issue is a volatile issue, i had a look and it seemed to be set correctly. Only the two variables in the interrupt need to be volatile and yours both are.

« Last Edit: October 21, 2021, 02:13:25 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #14 on: October 20, 2021, 09:43:46 pm »
I am currently investigating ripping the libraries I need out of the arduino and rewriting all the low level stuff myself like pinMode and digitalWrihe ect. Yes the LCD library is full of delays. Actually doing delays of a few µs with interrups

I wouldn't bother looking at pinMode /digitalWrite.
Those are core functionality and will be fine.

The LCD module could be part of the issue as that is not a core module.
Maybe add a single LED you can flash and comment out all the LCD stuff temporarily.
Just to see if things seem to work with it not there.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #15 on: October 20, 2021, 09:47:18 pm »
If an interrupt occurs during a delay surely the interrupt is serviced?

Only if you have interrupts enabled, which your original code didn't. It had them off for all the LCD stuff.  but you said you removing that, so that's all good now. Unless of cause there are calls to turn interrupts off hidden inside the LCD module. There should not be, but you can never be sure with Arduino libraries.

One thing, I see in some places you have a space between the variable name and ++
That might work, but i'm not sure, ive never seen anyone put a space there before. Obviously it does compile but maybe remove those spaces just to be sure.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 2566
  • Country: nz
  • Formerly SiFive, Samsung R&D
Re: [arduino] can't subtract two time stamps
« Reply #16 on: October 21, 2021, 12:11:51 am »
One thing, I see in some places you have a space between the variable name and ++
That might work, but i'm not sure, ive never seen anyone put a space there before. Obviously it does compile but maybe remove those spaces just to be sure.

ffs

Whitespace changes nothing in C, except (obviously) between two adjacent identifiers such as a type and variable name in a declaration e.g. "int foo" but not "intfoo". Or inside a string literal. Or the newline at the end of a // comment or #define #if etc (which are all preprocessor, not C)

There is a scary amount of uninformed superstition in this thread.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #17 on: October 21, 2021, 02:09:19 am »
One thing, I see in some places you have a space between the variable name and ++
That might work, but i'm not sure, ive never seen anyone put a space there before. Obviously it does compile but maybe remove those spaces just to be sure.

ffs

Whitespace changes nothing in C, except (obviously) between two adjacent identifiers such as a type and variable name in a declaration e.g. "int foo" but not "intfoo". Or inside a string literal. Or the newline at the end of a // comment or #define #if etc (which are all preprocessor, not C)

There is a scary amount of uninformed superstition in this thread.

Whitespace is  "supposed" to change nothing. Doesn't mean it always does, for all we know he is using some experimental compiler with a bug in it.  Or he copied that line from a webpage and it put some unicode blank char in there, maybe it's not a space char at all. etc..

When searching for code problems and you see something that's very unlikely to be the cause of the problem but it does look a bit odd, you might as well fix it if doing so takes 2 seconds.

There is always a tradeoff between likeliness to be the cause of the problem verses how easy it is to fix.

And before you start going on about me saying "only variables inside the interrupt need to be volatile"  Yes I know that is not strictly true and they only need to be volatile if accessed both inside and outside the interrupt. There is no point making things overly complicated when trying to explain basic things.
« Last Edit: October 21, 2021, 02:17:14 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 2566
  • Country: nz
  • Formerly SiFive, Samsung R&D
Re: [arduino] can't subtract two time stamps
« Reply #18 on: October 21, 2021, 02:56:32 am »
for all we know he is using some experimental compiler with a bug in it.

He's using the Arduino IDE with AVR. That means gcc, which is the most heavily used and debugged compiler that has ever existed.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #19 on: October 21, 2021, 05:45:10 am »
No it is a SAD21G18A (MKR zero) The spaces are typed spaces by me. The liquidcrystal files don't have any disabling of interrupts in them. If there is a better place to get libraries from I'd happily give those a go short of writing my own in C for the hassle that these are to work out.

So this is a lashup for the customer, it just needs the display to work counting the pulses. Further down the line I need to work with an SD card, RTC and Bluetooth module so I guess that if I can make the arduino LCD library work without the arduino I have half a chance of adding all of the other functionality.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #20 on: October 21, 2021, 01:33:05 pm »
I have zero experience running Arduino on a ATSAMD21 processor.

An arduino compatibility issue is possible,  how stable is arduino considered with that core?
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #21 on: October 21, 2021, 05:39:08 pm »
no idea, I don't consider the arduino stable full stop, not the first time I have an issue, the last was on the UNO of all boards!!!
 

Offline boffin

  • Supporter
  • ****
  • Posts: 1019
  • Country: ca
Re: [arduino] can't subtract two time stamps
« Reply #22 on: October 21, 2021, 07:54:07 pm »
I did some tach work with a pin change interrupt just a month ago and had no issues, I can post the code if you want later today..


 

Online Simon

  • Global Moderator
  • *****
  • Posts: 16298
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [arduino] can't subtract two time stamps
« Reply #23 on: October 21, 2021, 08:54:31 pm »
Sure, but my problem here is that this should work, if I can display one time or the other on their own why is it that the moment I subtract them the answer is less than 9? I am sick of the arduino. Why can I make that function run every 2s reliably by doing newmillis-oldmillis  and then have the exact same thing fail in another instance. How can I have two identical type variables that should both update in the interrupt but only one works? I'm done with this shit!
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 8267
  • Country: nz
Re: [arduino] can't subtract two time stamps
« Reply #24 on: October 21, 2021, 11:24:25 pm »
Make a copy of your project, then start pulling out everything except lines needed to demonstrates the bug.
Greek letter 'Psi' (not Pounds per Square Inch)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf