Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 20 of 20

Thread: Any recommendation for formula expression parsing?

  1. #1

    Any recommendation for formula expression parsing?

    The project i've been working on has been converting some old code I wrote for Windows.

    Teensy 4.1 is being used.

    I had a nice package for formula parsing that made life easy.

    All the formulas are mostly "easy".

    "((N1 << 8) +N2) *0.25"

    "((N1 << 8) + N2) / 2.048"

    "((N0 >> 3) & 1)",

    "N0-40"

    "((N0 >> 3) & 1)"

    Simple math. + - / * and bitwise shift left << , shift right >>, &, ect...

    No more than 4 variables ... N0, N1, N2, N3

    Currently around 200+ i'll have available, and can add more later on. So no hard coding these.

    Tried power of Google, but mostly find people on Arduino sites trying to make their own for some silly reason

    Speed is key, so hopefully one that is "fast" as can do upwards of 40-50 of these A few times A second hopefully.

    Looking for recommendations!

    This is my final stretch on getting this project "done" and actually usable.
    Last edited by jdredd; 11-13-2021 at 05:19 AM.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,866
    Not noted what Teensy is to be used ... speed should be good with any

    Post left me a bit short of understanding the goal ...

    Bing Search led to : arduino.cc/t/expression-parser-project

    That is 6 years old but @el_supremo was there in T_3.0 time frame and made it work.

    Links to this: 2012/06/mathematical-expression-parser-in-c.html

    That shows use of the 8 year old : github.com/jamesgregson/expression_parser

    More doc in expression_parser.h than the ReadMe

  3. #3
    Quote Originally Posted by defragster View Post
    Not noted what Teensy is to be used ... speed should be good with any

    Post left me a bit short of understanding the goal ...

    Bing Search led to : arduino.cc/t/expression-parser-project

    That is 6 years old but @el_supremo was there in T_3.0 time frame and made it work.

    Links to this: 2012/06/mathematical-expression-parser-in-c.html

    That shows use of the 8 year old : github.com/jamesgregson/expression_parser

    More doc in expression_parser.h than the ReadMe
    Sorry, updated post.

    Teensy 4.1 being used.

    The goal is to let end user put type in a formula string. Anything that fits the bounds of the parser. Nothing big. Formulas for converting incoming data from a GM OBD2 PCM on the fly.
    Calculate RPM, MPH, Coolant temps, Current Gear, Knock, ect...

    Ill check those links out. Thanks!

  4. #4
    http://jamesgregson.blogspot.com/201...rser-in-c.html

    This one "works" I think... I had to modify the source to add >> << support so could do shift left and shift right.

    Only issue now is to figure out call backs I think. To handle bad formuals... otherwise it crashes and reboots.

    Click image for larger version. 

Name:	cf2406102a7d20960258062c77382ad7.png 
Views:	16 
Size:	3.1 KB 
ID:	26501
    Last edited by jdredd; 11-13-2021 at 06:45 AM.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,866
    Yay for BING search!

    If not already in use >> Add the CrashReport code - something like this:
    Code:
      while ( !Serial ) ;
      if ( CrashReport) Serial.print( CrashReport);
    It will give an idea of where in the code the Crash started - lookup should provide address that resolves to a line of code.

  6. #6
    Quote Originally Posted by defragster View Post
    Yay for BING search!

    If not already in use >> Add the CrashReport code - something like this:
    Code:
      while ( !Serial ) ;
      if ( CrashReport) Serial.print( CrashReport);
    It will give an idea of where in the code the Crash started - lookup should provide address that resolves to a line of code.
    Thanks... all ready been trying to figure that out. CrashReport gives only A little info.

    Click image for larger version. 

Name:	abee579369bcd96f799e65aed1a1f5af.png 
Views:	16 
Size:	8.6 KB 
ID:	26503

    Click image for larger version. 

Name:	bd203fee778760bc74c8c1d4b09f8040.png 
Views:	16 
Size:	7.0 KB 
ID:	26504

    Click image for larger version. 

Name:	a0b2bd9240992914817b8eec647b9ae0.png 
Views:	18 
Size:	51.0 KB 
ID:	26505
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	cf2406102a7d20960258062c77382ad7.png 
Views:	18 
Size:	3.1 KB 
ID:	26502  

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,866
    Looks like a typo in my p#5 ... where in the code the Crash Ended

    Was it the -14 >> 2 eval that caused the Crash - or the one after that ?

  8. #8
    Doing say 3+3 works

    3+tacos ... well. Does not. And when it does, it causes the crash report we see.

    Click image for larger version. 

Name:	bf0085fe7f0ed5dce185de45d794c135.jpg 
Views:	16 
Size:	70.0 KB 
ID:	26506

    It's most def in the parser code.

    I just can't follow at what exact point it is failing.

    It is telling me Print.cpp

    there is printf calls inside this.

    No print or println.

  9. #9
    Click image for larger version. 

Name:	eab2fd770160b9801cdf83721475147c.png 
Views:	12 
Size:	31.0 KB 
ID:	26507

    was the printf call. Commented them out and got NAN back.

    Only two print type calls.

    Both printf calls are an issue of sorts.

    I guess pd.error and expr are dead for whatever reasons I can't figure out why.

    I can probably live with them commented out... get nan back... cool.. all i really care about.

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,866
    IIRC >> @el_supremo noted on the linked Arduino page that 'printf' needs to be replaced with Serial.printf()

  11. #11
    Quote Originally Posted by defragster View Post
    IIRC >> @el_supremo noted on the linked Arduino page that 'printf' needs to be replaced with Serial.printf()
    Yah makes total since. printf by itself, i guess has no where to go on output.

    Renamed it from .C file to .CPP

    Added Arduino.h so can have access to Serial and changed those to Serial.printf and now they output fine.

    Click image for larger version. 

Name:	bb5adbb8703cdc0d3a4404f3be291af8.jpg 
Views:	14 
Size:	91.4 KB 
ID:	26511

    So now this seems to work well!

    Even have my << >> for shifting added and those work too!

    Thanks for being available to bounce this off of.

    I am A Pascal/Delphi developer for A living. Also been using Pascal pretty much for past 30 years. So once it comes to using C, sometimes the obvious right in front of my face isn't so obvious.

    So pretty much in general, rename file, add 1 line, change 2 lines, and now its Arduino compatible.

    FYI, that crashreport was from the last crash that happened. No more crash reports after turning it off and back on.

  12. #12
    I made A fork of this for Arduino life.

    https://github.com/jdredd87/expression_parser

  13. #13
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,866
    Good work - glad to help - Any feedback on the speed?

    Yes, persistent CrashReport is recorded until removed by reading or power loss.

    Tip: Use BING search
    > Just selected text " formula expression parsing" and right click search and added 'Arduino' in front and that brought p#2 links to top search result.

    Nice you made sense of the c>>cpp and Arduino world. IDE may be despicable and cringe worthy by some standards ... but it is a low barrier common denominator : easy to install and use.

  14. #14
    Quote Originally Posted by defragster View Post
    Good work - glad to help - Any feedback on the speed?

    Yes, persistent CrashReport is recorded until removed by reading or power loss.

    Tip: Use BING search
    > Just selected text " formula expression parsing" and right click search and added 'Arduino' in front and that brought p#2 links to top search result.

    Nice you made sense of the c>>cpp and Arduino world. IDE may be despicable and cringe worthy by some standards ... but it is a low barrier common denominator : easy to install and use.
    Speed wise seems very good. Thinking of adding a 2nd sketch showing A number of the simple formulas of mine running one after another and timing it.

    I should use BING more. Use it from time to time. As I literally did "Arduino formula expression parsing" on Google. And its mostly stack over flow fart sniffers or Arduno.cc forum of people in A tizzy.

    .... C / C++ in general and the Arduino IDE spin on things is A head spinner for me that is for sure. Since C / C++ not my thing to begin with.

  15. #15
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,866
    No search is perfect ... too much out there ... and needing just the right context words. Since g00gle is despicable BING is the default here.

    Odd the TOP result was that from a 2015 thread ... and odder still it was the perfect answer!

    The 600 MHz 1062 on the T_4.1 with Double FPU should be fast and give good results. Significantly faster than any AVR or even lesser 32 bit ARM's with 512KB of full speed RAM for CODE and DATA and bumps of dual instruction execution when possible.

    The IDE does dynamic 'make' inclusion (and some hand holding prototyping in INO file) - just needs some tips in the code presented (INO's get Arduino.h and .cpp's don't - as you found) - then it just uses standard 'gcc' build process for c/cpp code into executable.

  16. #16
    It pretty much instantly blasts through 78 formulas.

    147ms to process 78 formulas in A row, 100 times. So for 147ms to calculate 7800 simple formulas.

    I think it is safe to say that this runs fast enough

  17. #17
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    710
    53,000 simple formulas per second.

  18. #18
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,866
    Quote Originally Posted by BriComp View Post
    53,000 simple formulas per second.
    Exceeds the p#1 spec :
    Code:
    Speed is key, so hopefully one that is "fast" as can do upwards of 40-50 of these A few times A second hopefully.

  19. #19
    Quote Originally Posted by defragster View Post
    Exceeds the p#1 spec :
    Code:
    Speed is key, so hopefully one that is "fast" as can do upwards of 40-50 of these A few times A second hopefully.
    For me its fast enough... 50 items, 4 times a second, for 200 a second....

    So being able to process 53,000+ A second is now over kill

    I am sure this is slow on A 16mhz Arduino... but on the Teensy 4.1, I am very happy with the results.

    On Arduino devices, people recommend RPN, Reverse Polish Notation.... and I am glad I got to stay away from RPN.

  20. #20
    Using fixed variable values... 16,32,64,128,256 to fill in variables of N0, N1, N2, N3, N4
    So data not real. But It looks good.

    Start
    Formula for ENGINE SPEED
    Original Formula : ((N1 << 8) +N2) *0.25
    Variables filled in Formula : ((16 << 8) +16) *0.25
    Result = 1028.00
    -----------------------------------------
    Formula for Injector PW
    Original Formula : (((N1<<8)+N2)/65.535)
    Variables filled in Formula : (((16<<8)+16)/65.535)
    Result = 62.75
    -----------------------------------------
    Formula for ECT (ENGINE COOLANT TEMPERATURE)
    Original Formula : N0-40
    Variables filled in Formula : 16-40
    Result = -24.00
    -----------------------------------------
    Formula for MASS AIR FLOW RAW INPUT FREQUENCY
    Original Formula : ((N1 << 8) + N2) / 2.048
    Variables filled in Formula : ((16 << 8) + 16) / 2.048
    Result = 2007.81
    -----------------------------------------
    Formula for SHORT TERM FUEL TRIM BANK 1
    Original Formula : (N0-128)/1.28
    Variables filled in Formula : (16-128)/1.28
    Result = -87.50
    -----------------------------------------
    Formula for LONG TERM FUEL TRIM BANK 1
    Original Formula : (N0-128)/1.28
    Variables filled in Formula : (16-128)/1.28
    Result = -87.50
    -----------------------------------------
    Formula for MAP (kPa)
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for VEHICLE SPEED
    Original Formula : N0/1.609
    Variables filled in Formula : 16/1.609
    Result = 9.94
    -----------------------------------------
    Formula for SPARK ADVANCE
    Original Formula : ((N0/2) - 64)
    Variables filled in Formula : ((16/2) - 64)
    Result = -56.00
    -----------------------------------------
    Formula for INTAKE AIR TEMPERATURE
    Original Formula : N0-40
    Variables filled in Formula : 16-40
    Result = -24.00
    -----------------------------------------
    Formula for TP Angle %
    Original Formula : N0/2.55
    Variables filled in Formula : 16/2.55
    Result = 6.27
    -----------------------------------------
    Formula for Ignition 1 Voltage
    Original Formula : N0/10
    Variables filled in Formula : 16/10
    Result = 1.60
    -----------------------------------------
    Formula for H02S Sensor 1
    Original Formula : 0.434*N0*10
    Variables filled in Formula : 0.434*16*10
    Result = 69.44
    -----------------------------------------
    Formula for Knock Retard
    Original Formula : 22.5*N0/256
    Variables filled in Formula : 22.5*16/256
    Result = 1.41
    -----------------------------------------
    Formula for Current Gear
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Misfire Cyl. 1 History
    Original Formula : (N1 << 8) + N2
    Variables filled in Formula : (16 << 8) + 16
    Result = 4112.00
    -----------------------------------------
    Formula for Misfire Cyl. 1 Current
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Misfire Cyl. 2 History
    Original Formula : (N1 << 8) + N2
    Variables filled in Formula : (16 << 8) + 16
    Result = 4112.00
    -----------------------------------------
    Formula for Misfire Cyl. 2 Current
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Misfire Cyl. 3 History
    Original Formula : (N1 << 8) + N2
    Variables filled in Formula : (16 << 8) + 16
    Result = 4112.00
    -----------------------------------------
    Formula for Misfire Cyl. 3 Current
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Misfire Cyl. 4 History
    Original Formula : (N1 << 8) + N2
    Variables filled in Formula : (16 << 8) + 16
    Result = 4112.00
    -----------------------------------------
    Formula for Misfire Cyl. 4 Current
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Misfire Cyl. 5 History
    Original Formula : (N1 << 8) + N2
    Variables filled in Formula : (16 << 8) + 16
    Result = 4112.00
    -----------------------------------------
    Formula for Misfire Cyl. 5 Current
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Misfire Cyl. 6 History
    Original Formula : (N1 << 8) + N2
    Variables filled in Formula : (16 << 8) + 16
    Result = 4112.00
    -----------------------------------------
    Formula for Misfire Cyl. 6 Current
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Fuel Trim Cell
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for MAF Flow Rate
    Original Formula : ((N1 << 8) + N2) / 100
    Variables filled in Formula : ((16 << 8) + 16) / 100
    Result = 41.12
    -----------------------------------------
    Formula for AFR (Commanded)
    Original Formula : N0 / 10
    Variables filled in Formula : 16 / 10
    Result = 1.60
    -----------------------------------------
    Formula for Last Shift Time
    Original Formula : N0 / 40
    Variables filled in Formula : 16 / 40
    Result = 0.40
    -----------------------------------------
    Formula for 1-2 Shift Time
    Original Formula : N0 / 40
    Variables filled in Formula : 16 / 40
    Result = 0.40
    -----------------------------------------
    Formula for 2-3 Shift Time
    Original Formula : N0 / 40
    Variables filled in Formula : 16 / 40
    Result = 0.40
    -----------------------------------------
    Formula for 3-4 Shift Time
    Original Formula : N0 / 40
    Variables filled in Formula : 16 / 40
    Result = 0.40
    -----------------------------------------
    Formula for 1-2 Shift Error
    Original Formula : N0 / 40
    Variables filled in Formula : 16 / 40
    Result = 0.40
    -----------------------------------------
    Formula for 2-3 Shift Error
    Original Formula : N0 / 40
    Variables filled in Formula : 16 / 40
    Result = 0.40
    -----------------------------------------
    Formula for 3-4 Shift Error
    Original Formula : N0 / 40
    Variables filled in Formula : 16 / 40
    Result = 0.40
    -----------------------------------------
    Formula for Fuel Loop Status
    Original Formula : ((N0 >> 3) & 1)
    Variables filled in Formula : ((16 >> 3) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Perf Shift Mode
    Original Formula : ((N0 >> 2) & 1)
    Variables filled in Formula : ((16 >> 2) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Desired Idle Speed
    Original Formula : N0 * 12.5
    Variables filled in Formula : 16 * 12.5
    Result = 200.00
    -----------------------------------------
    Formula for IAC Position
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for Power Enrichment Mode
    Original Formula : ((N0 >> 2) & 1)
    Variables filled in Formula : ((16 >> 2) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Decel Fuel Mode
    Original Formula : ((N0 >> 3) & 1)
    Variables filled in Formula : ((16 >> 3) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Transmission ISS
    Original Formula : ((N1 << 8) + N2) * 0.125
    Variables filled in Formula : ((16 << 8) + 16) * 0.125
    Result = 514.00
    -----------------------------------------
    Formula for Transmission OSS
    Original Formula : ((N1 << 8) + N2) * 0.125
    Variables filled in Formula : ((16 << 8) + 16) * 0.125
    Result = 514.00
    -----------------------------------------
    Formula for TCC Slip Speed
    Original Formula : ((N1 << 8) + N2) * 0.125
    Variables filled in Formula : ((16 << 8) + 16) * 0.125
    Result = 514.00
    -----------------------------------------
    Formula for Transmission Fluid Temp
    Original Formula : N0 - 40
    Variables filled in Formula : 16 - 40
    Result = -24.00
    -----------------------------------------
    Formula for TCC Duty Cycle
    Original Formula : N0 / 2.55
    Variables filled in Formula : 16 / 2.55
    Result = 6.27
    -----------------------------------------
    Formula for PC Solenoid Duty Cycle
    Original Formula : N0 / 2.55
    Variables filled in Formula : 16 / 2.55
    Result = 6.27
    -----------------------------------------
    Formula for PC Solenoid Actual Current
    Original Formula : N0 * 0.0195
    Variables filled in Formula : 16 * 0.0195
    Result = 0.31
    -----------------------------------------
    Formula for PC Solenoid Reference Current
    Original Formula : N0 * 0.0195
    Variables filled in Formula : 16 * 0.0195
    Result = 0.31
    -----------------------------------------
    Formula for 18X Crank Sensor
    Original Formula : 1310720 / (((N1 << 8) + N2) * 6)
    Variables filled in Formula : 1310720 / (((16 << 8) + 16) * 6)
    Result = 53.13
    -----------------------------------------
    Formula for 3X Crank Sensor
    Original Formula : 1310720 / ((N1 << 8) + N2)
    Variables filled in Formula : 1310720 / ((16 << 8) + 16)
    Result = 318.75
    -----------------------------------------
    Formula for Fans Low Speed
    Original Formula : ((N0 >> 6) & 1)
    Variables filled in Formula : ((16 >> 6) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Fans High Speed
    Original Formula : ((N0 >> 7) & 1)
    Variables filled in Formula : ((16 >> 7) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Gear Ratio
    Original Formula : N0 * 0.01563
    Variables filled in Formula : 16 * 0.01563
    Result = 0.25
    -----------------------------------------
    Formula for SPARK ADVANCE (Alt)
    Original Formula : ((-1 * N0 / 2) + 64) * -1
    Variables filled in Formula : ((-1 * 16 / 2) + 64) * -1
    Result = -56.00
    -----------------------------------------
    Formula for HO2 Bank 1 Sensor 2
    Original Formula : 0.434 * N0 * 10
    Variables filled in Formula : 0.434 * 16 * 10
    Result = 69.44
    -----------------------------------------
    Formula for Traction Control Desired Torque
    Original Formula : ((N1 << 8) + N2) * 5 / 256
    Variables filled in Formula : ((16 << 8) + 16) * 5 / 256
    Result = 80.31
    -----------------------------------------
    Formula for Fuel Level
    Original Formula : ((N1 << 8) + N2) * 0.01953125
    Variables filled in Formula : ((16 << 8) + 16) * 0.01953125
    Result = 80.31
    -----------------------------------------
    Formula for Transmission Range Switch A
    Original Formula : N0 & 1
    Variables filled in Formula : 16 & 1
    Result = 0.00
    -----------------------------------------
    Formula for Transmission Range Switch B
    Original Formula : ((N0 >> 1) & 1)
    Variables filled in Formula : ((16 >> 1) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Transmission Range Switch C
    Original Formula : ((N0 >> 2) & 1)
    Variables filled in Formula : ((16 >> 2) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Transmission Range Switch P
    Original Formula : ((N0 >> 3) & 1)
    Variables filled in Formula : ((16 >> 3) & 1)
    Result = 0.00
    -----------------------------------------
    Formula for Transmission Range
    Original Formula : N0 & 15
    Variables filled in Formula : 16 & 15
    Result = 0.00
    -----------------------------------------
    Formula for Boost Solenoid PWM
    Original Formula : ((N1 > 4) & 2) + ((N2 > 5) & 3)
    Variables filled in Formula : ((16 > 4) & 2) + ((16 > 5) & 3)
    Result = 1.00
    -----------------------------------------
    Formula for Engine Torque
    Original Formula : ((N1 << 8) + N2) * 0.33895
    Variables filled in Formula : ((16 << 8) + 16) * 0.33895
    Result = 1393.76
    -----------------------------------------
    Formula for EGR %
    Original Formula : N0 / 2.55
    Variables filled in Formula : 16 / 2.55
    Result = 6.27
    -----------------------------------------
    Formula for EGR Duty Cycle
    Original Formula : N0 / 2.55
    Variables filled in Formula : 16 / 2.55
    Result = 6.27
    -----------------------------------------
    Formula for Desired EGR %
    Original Formula : N0 / 2.55
    Variables filled in Formula : 16 / 2.55
    Result = 6.27
    -----------------------------------------
    Formula for EGR Closed Valve Pintle Position
    Original Formula : N0 / 51
    Variables filled in Formula : 16 / 51
    Result = 0.31
    -----------------------------------------
    Formula for EGR Flow Test Count
    Original Formula : N0
    Variables filled in Formula : 16
    Result = 16.00
    -----------------------------------------
    Formula for EGR Position Error
    Original Formula : (N0 - 128) / 1.28
    Variables filled in Formula : (16 - 128) / 1.28
    Result = -87.50
    -----------------------------------------
    Formula for Commanded EGR
    Original Formula : N0 / 2.55
    Variables filled in Formula : 16 / 2.55
    Result = 6.27
    -----------------------------------------
    Formula for EGR Error %
    Original Formula : N0 * 0.78125 - 100
    Variables filled in Formula : 16 * 0.78125 - 100
    Result = -87.50
    -----------------------------------------
    Formula for TP Sensor Voltage
    Original Formula : N0 / 51
    Variables filled in Formula : 16 / 51
    Result = 0.31
    -----------------------------------------
    Formula for CMP Sensor Signal Present
    Original Formula : ((N0 >> 1) & 1)
    Variables filled in Formula : ((16 >> 1) & 1)
    Result = 0.00
    -----------------------------------------
    End
    elapsed : 1ms

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •