Main Menu

Fixed pulsewidth PWM

Started by Rx7man, April 16, 2015, 03:44:21 PM

Rx7man

OK, I'm looking some of this up on arduino.cc, and haven't quite found a solution..

In an engine controller, you need to have a fixed pulsewidth for your injectors (recalculated frequently), but it has to fire on a certain signal, often from the ignition pickup.

So if you desire 20ms of pulse for each rotation, I'm going to say you have to use an interrupt on the ignition input pin that starts the injection, then set a timer to handle raise another interrupt that shuts it off... this works fine until you need to do this with a couple different outputs (lets say different cylinders) and you would quickly use up the interrupts, and the interrupt priorities could become an issue as well...

I think I'm kinda diving into this with both feet considering I haven't written a single line of Arduino code yet!, just trying to visualize the problem and solution first... The Holley Commander 950 was able to do it with 2 injector outputs and an ignition output, and that's only a 4mhz processor (Atmel mc68hc11f1cfn4)
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Usually the input is counting crankshaft angle, via cam input or toothed wheel.

Then you do reverse math to get the injection event at whatever btdc you want
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

OK, that's true.. and that works for defining when the start of the injection event happens, what about for the length?  you would still need a timer raising an interrupt right?
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

You can use mili's and or the basic milis timer, i have in the library post.

Arduino only has 1 interrupt pin for incoming, so kinda don't want to waste it :)
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

Ah, OK.. but it does have more interrupts that are timer based right? You would use the pinned interrupt to start the whole sequence, and use timer based ones for the rest of it
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Right, you could setup multiple timer libraries. It becomes a serious problem with mixing certain libraries because of overlapping pin usage but...

You would use a pin interrupt to fire a main method, that lets you know precisely when x happens, and calculate around it. It really depends on what you want to do and how to do it.

You may need to use a mega if you need more awesomeness, pin 8 on the Uno is what I use to count shaft speed with freqcounter library. Works great so far, been well past 7,500,000hz / 60 = 125k rpm
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

Well, I do have a Mega 2360, a Leonardo, and a Sparkfun Redboard all coming in the mail, as well as a couple prototyping boards, breadboards, etc so I can set things up... Perhaps V1 of this project won't be doing everything I'm dreaming of.. Perhaps I'll just start with some digital outputs at 30hz or something to control some solenoid valves that are timing-independent.

I guess the VGT turbo has an integral shaft speed sensor?  That's pretty impressive to count that well :)
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

#7
When I did ladder logic timers for packaging equipment, we did timing like this

If Timer1 and not Timer 2, then output 1
If timer 3 and not timer 4, then output 2, etc

If you recalculated your on/off times on every revolution

you could create an array of times for each event, and if you fired an interrupt every millisecond (should be enough resolution for most applications) you could get the times for each event.. Biggest problem would be when things get recaculated while things are happening...

So it would be something like.. no, this is NOT working code, it's brainstorming..



int InjectorPin
int InjectorStart = 10 // How much time after the crank signal you want to fire
int InjectorDuration = 20;
int InjectorStartedTime;

loop{
Get Inputs  //not gettting into this!
Calculate pulsewidths //or this
}

CrankSensor_ISR{
Calculate start of injection cycles
}



Millisecond_Event_ISR(){
int time = millis();                                     //yeah, millis is unsigned long..
bool InjectorActive = digitalread(InjectorPin) //Check to see if the injector is already firing

if(time >= InjectorStart &! InjectorActive{ //If it should be firing and isn't already
digitalwrite(Injector1Pin, HIGH) // start injection
InjectorStartedTime = time //Set the injection start time
}
elseif (time>= InjectorStart+InjectorDuration & InjectorActive)  //Duration has expired and we're still injecting
{
digitalwrite(Injector1pin, LOW) // stop injecting
}
Repeat above for each injection cycle on each injector
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

I'm working up a sketch for mapping TPSvsRPM to desired boost.. just to get into this style of programming.. the "Map" command is going to be really handy and well used I think.. going to make a 6x12 map, 6 rows for TPS setting, 12 columns for RPM, and you'll be able to set where the RPM pointers end up so you can have higher resolution at low RPMs
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Quote from: Rx7man on April 17, 2015, 11:03:41 PM
I'm working up a sketch for mapping TPSvsRPM to desired boost.. just to get into this style of programming.. the "Map" command is going to be really handy and well used I think.. going to make a 6x12 map, 6 rows for TPS setting, 12 columns for RPM, and you'll be able to set where the RPM pointers end up so you can have higher resolution at low RPMs

I haven't programmed tables at all in arduino yet, interested to see what you come up with.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

I think I got it... the code to interpolate between the points isn't the prettiest looking stuff out there, and I can't test it yet (I'll probably do it in VB as well to run some simulations)... I'll post the code once I do that.

the Arduino IDE is driving me nuts because Shift+backspace deletes to the left of the cursor, and most of the time my typos are when I'm trying to go for the braces and parentheses, which means normally I just leave my shift key pressed.

I'm also having a hard time remembering the ; at the end of a line, and have to get used to commenting with // rather than the vb '...  The order of the arguments to a For loop is also counterintuitive for me.

Oh, one more thing I find ridiculous.. Declaring arrays... you declare it with the NUMBER OF ELEMENTS you want, which is 1 based, but for everything else you have to do 0 based... and on a language that doesn't check for bounds, it's a recipe for a screwup somewhere along the lines!
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Array declaration seems the same with everything I've used, except I get lazy if I don't have to declare size I don't.

CPP

int foo [5];
or
int foo [5] = { 16, 2, 77, 40, 12071 };


Java

int foo = new int[5];
or
int foo = new int[] { 16, 2, 77, 40, 12071};


You can comment with // per line, or /*, then finish with */

// commented
uncommented int;
/* Text
but still commented
until it is finished
*/
uncommented int2;
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

Oh, I know HOW to do it, it's just not natural for me coming from VB.. .in VB nearly everything is done with ( and ), no square brackets, and curly brackets only for array initialization.

I think I'm going to make it so I can do all my programming in VC, then copy and paste into Arduino IDE.. it'll at least give me an interface I'm used to, with autocomplete and function parameter suggestions
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

OK, I've gotten the basics working, a 8x14 map for TPS vs RPM, it's expandable to anything you want, and you can set the increments however you like.... so for RPM values you can have 250 RPM increments where you need the resolution, and then 1000 RPM increments for the top end... I rewrote it in VB so I can do some testing on it (some of it is a little complex, and easy to get stuff backward) while keeping it in Arduino compatible formats.. I won't be able to copy it back, but it'll be close at least.

hakcenter.. can you read VB code?
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

As long as it is in english, haha
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.