Take the following code and save it in a filed called generatecltfactor.vbs:
Code: Select all
' Custom Coolant Factor Include File Generator
' Copyright (C) 2008 - Mike Soultanian - msoultan@csulb.edu
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
'***************************************************************
'This file was created with the algorithm used in Megatune 2.25
'***************************************************************
'CHANGELOG
'v1.0 - Release
'v1.1 - Code cleanup and documentation added
'USAGE
'To use this script, you will need to edit the script and change a few values
'in the section below between the BEGIN/END EDITABLE VALUES makers
'
'First thing to do is select whether or not you are using Celsious. This is
'pretty straightforward
'
'The next thing to do is select your failsafe value. This is the value that is
'used by the MegaSquirt when the sensor fails. Pick a value that will keep
'the car running so you can "limp" home. 20F, for example, would not be a
'good choice because the MegaSquirt would still think it's in warmup mode.
'
'The next two variables select your temperature range. The Megasquirt
'defaults to -40F/300F for most engines and -40F/600F for engines using
'the expanded temperature range. You will want to select values inside
'that range, but a little bit outside of the range of temperatures that you
'think your sensor will encounter. This will allow for the most resolution
'while also giving you a little headroom should your engine get really hot
'or cold. It's better to see to what temperature extremes your engine
'reaches instead of railing at a particular value.
'
'To build your table, you need to have three pairs of measured
'temperature/resistance data. You can get this data by measuring your
'sensor at room temperature, in warm water, and in boiling water. Try and
'get three values that are as spread apart as possible as that will yield the
'most accurate table.
'
'The last setting is the bias resistor value. You will want to pick a value
'for which a known resistor value exists. Standard values can be found
'here:
' http://www.elexp.com/t_eia.htm
'or just do a Google search for "standard resistor values". When I did this,
'I looked at what resistors were available in my assortment pack that I
'purchased from Radio Shack. Chances are if you're doing this mod, this
'should be pretty straightforward.
'
'Continuing on, pick a resistor value and then run the generator to see what
'the table looks like. If everything is scrunched up at the top, lower the
'value until you start to see an even number of failsafe values at the top
'and bottom of your table. Once you find a bias value that works and
'represents a real resistor value, you can generate your final table and
'you're done editing this file.
'
'Now you will want to take the cltfactor.inc file that was generated by this
'script and overwrite the default GM lookup table that is included with the
'MegaSquirt source code. Recompile the source code and the new coolant
'lookup table will be included in the S19 package. Upload that file to your
'MegaSquirt and then install a resistor matching the value you selected for
'the bias resistor on your MegaSquirt circuit board and enjoy your new
'setup!
'
'Should you run into any problems with this script, please feel free to email
'me.
'***************************************************************
'**********BEGIN EDITABLE VALUES*****************************
'***************************************************************
'change the following values as you see fit
blnDegressInCelsius = FALSE ' change this to TRUE if you want to use Celsius values
const conFailsafeValue = 350 'failsafe temperature that should be used when the sensor is faulty
const conMinTemp = -40 'minimum temperature
const conMaxTemp = 400 'maximum temperature
'here is where you specify your three measured temp/resistance pairs
'pair 1
const conTemp1 = 7 'Temp 1
const conRes1 = 11600 'Resistance 1
'pair 2
const conTemp2 = 129 'Temp 2
const conRes2 = 703 'Resistance 2
'pair 3
const conTemp3 = 204 'Temp 3
const conRes3 = 207 'Resistance 3
'The value below is what you will be using as your bias resistor, R7. Larger values
'shifts everything up, smaller value shifts everything down. Try and get your table
' to be evenly centered in between the failsafe sensor values that you selected above
const conBiasResistorValue = 1000 'R7 coolant bias resistor value
'don't change anything else below here
'***************************************************************
'**********END EDITABLE VALUES*******************************
'***************************************************************
const intADCResolution = 1023 'ADC resolution - 1 - DON'T TOUCH - this should be 1023 for the MSII
'let's convert the temperatures to degrees kelvin
dim t(3)
t(0) = conTemp1
t(1) = conTemp2
t(2) = conTemp3
for i = 0 to 2
if NOT blnDegressInCelsius then
t(i) = (t(i)-32.0)*5.0/9.0
end if
t(i) = t(i) + 273.15
next
'let's figure out our coefficients
c11 = log(conRes1)
c12 = c11^3
c13 = 1.0 / t(0)
c21 = log(conRes2)
c22 = c21^3
c23 = 1.0 / t(1)
c31 = log(conRes3)
c32 = c31^3.0
c33 = 1.0 / t(2)
C = ((c23-c13) - (c33-c13)*(c21-c11)/(c31-c11)) / ((c22-c12) - (c32-c12)*(c21-c11)/(c31-c11))
B = (c33-c13 - C*(c32-c12)) / (c31-c11)
A = c13 - B*c11 - C*c12
Const ForReading = 1, ForWriting = 2, ForAppending = 8
dim objFS, objFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile("cltfactor.inc", ForWriting, True, 0)
'let's output the file header
objFile.WriteLine "//------------------------------------------------------------------------------"
objFile.WriteLine "//-- Custom coolant factor include file generated by"
objFile.WriteLine "//-- Mike Soultanian's Custom CLT VBScript - " & Now()
objFile.WriteLine "//-- This file was created with the algorithm used in Megatune 2.25"
objFile.WriteLine "//--"
objFile.WriteLine "//-- Bias = " & Round(conBiasResistorValue, 1)
objFile.WriteLine "//-- ADC Resolution = " & intADCResolution + 1
objFile.WriteLine "//-- Temperature range: " & conMinTemp & "F to " & conMaxTemp & "F"
objFile.WriteLine "//--"
objFile.WriteLine "//-- Resistance" & vbTab & vbTab & "tInput"
objFile.WriteLine "//-- ------------" & vbTab & "-----------------"
objFile.WriteLine "//-- " & conRes1 & " ohm" & vbTab & vbTab & Round(t(0),1) & "K" & vbTab & "(" & conTemp1 & IIF(blnDegressInCelsius, "C", "F") & ") " & vbTab & Round(Tu(Tf(conRes1), blnDegressInCelsius), 1)
objFile.WriteLine "//-- " & conRes2 & " ohm" & vbTab & vbTab & Round(t(1),1) & "K" & vbTab & "(" & conTemp2 & IIF(blnDegressInCelsius, "C", "F") & ") " & vbTab & Round(Tu(Tf(conRes2), blnDegressInCelsius), 1)
objFile.WriteLine "//-- " & conRes3 & " ohm" & vbTab & vbTab & Round(t(2),1) & "K" & vbTab & "(" & conTemp3 & IIF(blnDegressInCelsius, "C", "F") & ") " & vbTab & Round(Tu(Tf(conRes3), blnDegressInCelsius), 1)
objFile.WriteLine "//------------------------------------------------------------------------------"
objFile.WriteLine
objFile.WriteLine "#ifndef GCC_BUILD"
objFile.WriteLine "#pragma ROM_VAR CLT_ROM"
objFile.WriteLine "#endif"
objFile.WriteLine "const int cltfactor_table[1024] LOOKUP_ATTR = {"
objFile.WriteLine vbTab & vbTab & "//" & vbTab & vbTab & "ADC" & vbTab & vbTab & "Volts" & vbTab & vbTab & "Temp" & vbTab & vbTab & "Ohms"
'this is where the table code starts
for adcCount = 0 to intADCResolution
res = conBiasResistorValue / ((intADCResolution / IIf(adcCount = 0 OR adcCount = intADCResolution, 0.01, adcCount)) - 1.0)
temp = Tf(res)
if temp <= conMinTemp then
temp = conFailsafeValue
elseif temp >= conMaxTemp then
temp = conFailsafeValue
end if
tt = temp*10
objFile.WriteLine Round(tt) & IIf(adcCount <> intADCResolution, ",", "};") & vbTab & vbTab & "//" & vbTab & vbTab & adcCount & vbTab & vbTab & Round(5.0*adcCount/intADCResolution,2) & vbTab & vbTab & Round(Tu(tt/10.0, blnDegressInCelsius),1) & vbTab & vbTab & Round(res,1)
next
'output the table footer and we're done!
objFile.WriteLine "#ifndef GCC_BUILD"
objFile.WriteLine "#pragma ROM_VAR DEFAULT"
objFile.WriteLine "#endif"
objFile.Close
'function declarations
function iif(psdStr, trueStr, falseStr)
if psdStr then
iif = trueStr
else
iif = falseStr
end if
end function
function k2f(t)
k2f = (t * 9.0/5.0) - 459.67
end function
function Tk (R)
Tk = 1.0 / (A + B*log(R) + C*(log(R))^3)
end function
function Tf (R)
Tf = k2f(Tk(R))
end function
function Tu (f, blnDegressInCelsius)
if blnDegressInCelsius then
Tu = ((f-32.0)*5.0/9.0)
else
Tu = f
end if
end function
Create a batch filed called generatecltfactor.bat with the following contents and save it in the same folder as the vbs file:
Code: Select all
cscript generatecltfactor.vbsLet me know if you have any questions.
enjoy!
Mike