element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Ben Heck Featured Content
  • Challenges & Projects
  • element14 presents
  • element14's The Ben Heck Show
  • Ben Heck Featured Content
  • More
  • Cancel
Ben Heck Featured Content
Forum Engine Management
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Ben Heck Featured Content to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 63 replies
  • Subscribers 46 subscribers
  • Views 4146 views
  • Users 0 members are here
Related

Engine Management

jack.chaney56
jack.chaney56 over 9 years ago

Hi Ben,

I am a programmer with a very small amount of skill with circuits, and am looking to create a platform for an engine management system, using an Arduino Mega 2560. I had done a bit of the coding, when I ran into some timing issues with the built in Arduino manager, so I switched over and started using AVR Studio and a programmer to go directly to the chip itself.  The code looks like it should work ok, but now I need some additional circuits to handle the energy levels of coils and injectors (Something like IGBTs). Sensors are being run through simple dividers (no protection yet), and cam and crank inputs are through a simple comparitor

 

Let me know what you think,

Jack

  • Sign in to reply
  • Cancel

Top Replies

  • jack.chaney56
    jack.chaney56 over 7 years ago +2
    Back again... After a bit of time away seeking enlightenment (and a steady paycheck), I am ready to get back to work on my project. I have continued to play around with the code and a number of components…
  • jack.chaney56
    jack.chaney56 over 7 years ago +2
    I want to start this thing right, so the shopping list for people that want to play along at home: Raspberry Pi - version is not significant if you don't mind a slow response when using Eclipse, but 3B…
  • jack.chaney56
    jack.chaney56 over 7 years ago +2
    Start off with two things. First, I forgot (neglected) to provide instruction on how to get the compiled code onto the Nano. Fault of familiarity; having done the process so many times, I had shifted to…
Parents
  • jack.chaney56
    jack.chaney56 over 7 years ago

    Conclude the discussion about angles.

    For operation of an engine, the measurements are all done as angles. Angle of advancement, angle of dwell, and top dead center reference. Switching over a little to engine operation theory... an engine running as a four cycle motor, each cylinder has four points of reference, two are at the upper limit of the stroke, and two at the lower limit of the stroke. The four cycles are the transitions between each of the four states, 1 compression/ignition, 2 power, 3 exhaust, and 4 fueling. The position when the piston is at the upper limit on the compression cycle is called its top dead center. For reference in an engine system, cylinder 1 is the reference, and its top dead center (TDC) is considered zero degrees. Because each cylinder has four cycles, return to TDC occurs every 720 degrees. Also, because each cylinder has four cycles, and operations are tied  to which cycle is in operation, the two rotations is referred to as a cam cycle, because the cam shaft is the device that controls the opening and closing of the valves at the proper time (in mechanical systems).

     

    Each of the pistons is spaced out to provide an ignition at equally spaced locations along the 720 degree cam rotation. This means each cylinder will have a referencing TDC angle. The angle is dependent on the number of cylinders. For a 4 cylinder motor, the TDCs will be at 0 degrees, 180 degrees, 360 degrees, and 540 degrees. Likewise, for a 6 cylinder, the TDCs will be at 0 degrees, 120 degrees, 240 degrees, 360 degrees, 480 degrees, and 600 degrees.  This will be important in just a little bit.

     

    Remembering from before, the angular measurement is 32768 degrees per rotation. so the conversion is angle * 32768 / 360. Many of the angles have direct conversions with no remainder portion (truncated values), but some do. It is important to have update values for the crank teeth. For some tooth configurations, like the common 4x the tooth angle is easily divisible to a whole number, however many advance wheels like 24x or 60-2 do not divide into convenient whole numbers. a quick mechanism for providing angular location. The method used is a carry over from the same one used to draw straight lines on pixel based displays. The method uses an error and correction operation to provide a running update of the angle to maintain a "close as possible" angle. The process works like this...

     

    A value of degrees per tooth is calculated as degrees per rev divided by teeth per crank, which provides a truncated integer division. The error correction value is the remainder after division or mod. The equations then are:

     

    toothAngle = DEG_PER_REV / teeth;

    toothError = DEG_PER_REV % teeth;

     

    The values are based on the number of teeth and are constant during runtime. This means, they are calculated in the engine off time, and don't impact the operation timing of engine running. The procedure works in a rather simple way. At each tooth interrupt, a value of cam angle is incremented by the value toothAngle, and at the same time, a cam error is incremented by toothError. When the value of cam error is greater than or equal to the value of teeth, the threshold is reached, and the cam angle is incremented by 1 and the cam error is corrected by subtracting the value of teeth. This provides a running value of the angle of the engine at the most recent tooth interrupt.

     

    The result is having the timer tic value of the most recent tooth interrupt (crkTime), the angle in the cam rotation of the most recent tooth interrupt (camAngle), and the predicted time until the next tooth interrupt event (crkDiff). With this information, it is possible to perform conversions between time and angle, resulting in creating predicted event times for setting timer interrupts. Because the conversions require math (multiplies and divides) also for providing values of proper precision, are performed using 32bit numbers, it is necessary to limit the amount of repetition and unnecessary conversion.

     

    The solution presented here is to use a window of operation. The window will exist based on the most current values of crkTime, crkDiff, and camAngle. Because an operation is needed, the window should be forward of the current position (>camAngle). The system here uses a value of 1/2 the tooth angle. This is the value that is used for "min", or the front edge of the window. The value of "max" will be one tooth angle after "min" with the idea being, from mid tooth to mid tooth. The basis of the operation is to perform an angle based comparison of the event angle to the min and max value. If the event angle falls between the min and max value, then the angle is converted to a time and the timers are updated to generate an event.

     

    In this installment, I will just provide snippets of code to add as correction to existing source. In the next session, I will provide the full file sources.

     

    /* in rtUpdateIgn() in the engine off section */
    ...
    toothAngle = DEG_PER_REV / teeth;
    toothError = DEG_PER_REV % teeth;

    /* in ISR(INT0_vect) after the if (isInt0Low... */
      camAngle += toothAngle;
      camError += toothError; if (toothError >= teeth) { camAngle++; camError -= teeth; }
    ...
    /* this doesn't provide correction for partial error correction */
      min = camAngle + (toothAngle / 2);
      max = min + toothAngle;
    ...
    /* this is actually a good place to invoke the scheduler, as long as it isn't */
    /* called to do too much in its operation. */

     

    The basis of the scheduler does a simple operation based on min and max

    /* because min and max are unsigned values making provision for wrap around is done */
    /* as duplication of similar operation (just easier than making things complex) */
    if (min < max)
         if ((min < eventAngle) && (eventAngle <= max)) { /* convert and update single event timers */ }
    } else {
         if ((min < eventAngle) || (eventAngle <= max)) { /* convert and update single event timers */ }
    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • jack.chaney56
    jack.chaney56 over 7 years ago

    Conclude the discussion about angles.

    For operation of an engine, the measurements are all done as angles. Angle of advancement, angle of dwell, and top dead center reference. Switching over a little to engine operation theory... an engine running as a four cycle motor, each cylinder has four points of reference, two are at the upper limit of the stroke, and two at the lower limit of the stroke. The four cycles are the transitions between each of the four states, 1 compression/ignition, 2 power, 3 exhaust, and 4 fueling. The position when the piston is at the upper limit on the compression cycle is called its top dead center. For reference in an engine system, cylinder 1 is the reference, and its top dead center (TDC) is considered zero degrees. Because each cylinder has four cycles, return to TDC occurs every 720 degrees. Also, because each cylinder has four cycles, and operations are tied  to which cycle is in operation, the two rotations is referred to as a cam cycle, because the cam shaft is the device that controls the opening and closing of the valves at the proper time (in mechanical systems).

     

    Each of the pistons is spaced out to provide an ignition at equally spaced locations along the 720 degree cam rotation. This means each cylinder will have a referencing TDC angle. The angle is dependent on the number of cylinders. For a 4 cylinder motor, the TDCs will be at 0 degrees, 180 degrees, 360 degrees, and 540 degrees. Likewise, for a 6 cylinder, the TDCs will be at 0 degrees, 120 degrees, 240 degrees, 360 degrees, 480 degrees, and 600 degrees.  This will be important in just a little bit.

     

    Remembering from before, the angular measurement is 32768 degrees per rotation. so the conversion is angle * 32768 / 360. Many of the angles have direct conversions with no remainder portion (truncated values), but some do. It is important to have update values for the crank teeth. For some tooth configurations, like the common 4x the tooth angle is easily divisible to a whole number, however many advance wheels like 24x or 60-2 do not divide into convenient whole numbers. a quick mechanism for providing angular location. The method used is a carry over from the same one used to draw straight lines on pixel based displays. The method uses an error and correction operation to provide a running update of the angle to maintain a "close as possible" angle. The process works like this...

     

    A value of degrees per tooth is calculated as degrees per rev divided by teeth per crank, which provides a truncated integer division. The error correction value is the remainder after division or mod. The equations then are:

     

    toothAngle = DEG_PER_REV / teeth;

    toothError = DEG_PER_REV % teeth;

     

    The values are based on the number of teeth and are constant during runtime. This means, they are calculated in the engine off time, and don't impact the operation timing of engine running. The procedure works in a rather simple way. At each tooth interrupt, a value of cam angle is incremented by the value toothAngle, and at the same time, a cam error is incremented by toothError. When the value of cam error is greater than or equal to the value of teeth, the threshold is reached, and the cam angle is incremented by 1 and the cam error is corrected by subtracting the value of teeth. This provides a running value of the angle of the engine at the most recent tooth interrupt.

     

    The result is having the timer tic value of the most recent tooth interrupt (crkTime), the angle in the cam rotation of the most recent tooth interrupt (camAngle), and the predicted time until the next tooth interrupt event (crkDiff). With this information, it is possible to perform conversions between time and angle, resulting in creating predicted event times for setting timer interrupts. Because the conversions require math (multiplies and divides) also for providing values of proper precision, are performed using 32bit numbers, it is necessary to limit the amount of repetition and unnecessary conversion.

     

    The solution presented here is to use a window of operation. The window will exist based on the most current values of crkTime, crkDiff, and camAngle. Because an operation is needed, the window should be forward of the current position (>camAngle). The system here uses a value of 1/2 the tooth angle. This is the value that is used for "min", or the front edge of the window. The value of "max" will be one tooth angle after "min" with the idea being, from mid tooth to mid tooth. The basis of the operation is to perform an angle based comparison of the event angle to the min and max value. If the event angle falls between the min and max value, then the angle is converted to a time and the timers are updated to generate an event.

     

    In this installment, I will just provide snippets of code to add as correction to existing source. In the next session, I will provide the full file sources.

     

    /* in rtUpdateIgn() in the engine off section */
    ...
    toothAngle = DEG_PER_REV / teeth;
    toothError = DEG_PER_REV % teeth;

    /* in ISR(INT0_vect) after the if (isInt0Low... */
      camAngle += toothAngle;
      camError += toothError; if (toothError >= teeth) { camAngle++; camError -= teeth; }
    ...
    /* this doesn't provide correction for partial error correction */
      min = camAngle + (toothAngle / 2);
      max = min + toothAngle;
    ...
    /* this is actually a good place to invoke the scheduler, as long as it isn't */
    /* called to do too much in its operation. */

     

    The basis of the scheduler does a simple operation based on min and max

    /* because min and max are unsigned values making provision for wrap around is done */
    /* as duplication of similar operation (just easier than making things complex) */
    if (min < max)
         if ((min < eventAngle) && (eventAngle <= max)) { /* convert and update single event timers */ }
    } else {
         if ((min < eventAngle) || (eventAngle <= max)) { /* convert and update single event timers */ }
    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
  • jack.chaney56
    jack.chaney56 over 7 years ago in reply to jack.chaney56

    I did want to add one last item here. In the min / max operation (the scheduler), there is a reference to the "convert and update single event timers", but I did not provide the operation. This is the primary reason for splitting the total degrees of the circle into two component parts (PART1 and PART2). The reason is it is possible to use a calculated value to eliminate much of the operation.  The way it works is to use a base value, where:

     

    base = crkDiff * teeth / PART1;

     

    Then if you are converting from time to angle the operation is:

     

    angle = time * PART2 / base;

     

    ...and converting from angle to time is:

     

    time = angle * base / PART2;

     

    The time at the scheduler will be the number of tics from the last tooth. So to set the event timer compare value will be set to (crkTime + time), where time is the converted value.

     

    Jack

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube