Skip to content

Commit

Permalink
Updated calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcourt authored and tomcourt committed Jul 13, 2017
1 parent 9dd5c32 commit 81efe23
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 158 deletions.
41 changes: 21 additions & 20 deletions README.md

Large diffs are not rendered by default.

49 changes: 26 additions & 23 deletions enguino/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ const char *green = "green";
const char *yellow = "yellow";
const char *red = "red";

#define divisor 13 // 1<<13 = 8192, this allows factors between -2.0 to 1.999
// for the 0-1023 ADC values, multiply range by 8 for full scale
#define divisor 13 // 1<<13 = 8192

#define MX(x) (int)((x)*(1<<divisor) + 0.5) // expressed as floating point converted to integer m
#define GMX(range) (int)(1000.0 / (range) * (1<<divisor) + 0.5)
#define GB(low) (int)(-((low) + 0.5))
#define ADCtoDivV (5.0/1023.0) // multiply this by adc input to get DC volts
#define ADCtoV10 (40 * ADCtoDivV) // 1:4 voltage divider, results in tenths of a volt
#define V10toADC (1/(40 * ADCtoDivV)) // 1:4 voltage divide
#define SCALE(factor) (int)((factor)*(1<<divisor) + 0.5) // Converts a floating point factor to integer
#define GRNG(range) SCALE(1000.0/(range)) // Used for gfactor, given range returns gfactor that will result in 0-1000
#define GMIN(low) (int)(-((low) + 0.5))
#define ADCtoV (5.0/1024.0) // multiply this by adc input to get DC volts
#define toV (40 * ADCtoV) // 1:4 voltage divider, V/10 = adc*toV
#define fromV (1/toV) // 1:4 voltage divide, (V/10) * fromV = adc
// For Van's gauges or other 240-33 ohm gauges that have a linear voltage response instead of linear resistance use st_volts with the following:
#define RVoff -495 // 495 is when ADC when gauge reads 0
#define RVscale (1000.0/(124+RVoff)) // 124 is when ADC when gauge reads max
#define RVSCALE(factor) SCALE((factor)*RVscale)
#define RVRNG(range) SCALE(RVscale*(range)/1000.0)

// Sensor defintions and scaling
// -----------------------------
// The sensors numeric values, y in the following forumala, are scaled as follow: y = m * x + b.
// Sensors gauge position are scaled a bit differerently with this formuala: y = (m + b) * x
// The sensors are scaled using the following formula, are scaled as follow: y = factor * (adc + offset).
// All of these numbers must be integers. Factors are represented as a ratio with an integer numerator and a denominator of 8192.
// Some numberic values are multipled by 10 and then have a decimal point added when displayed.
// The MX macro converts a floating point factor for m into an integer factor. The gauge pointers vary from 0 to 1000.
// Use GB() and GMX to set the graphs b and m values based on lowest input and input range (hi-low) respectively.
// Some numeric values are multipled by 10 and then have a decimal point added when displayed.
// SCALE(1.) returns the raw values (ADC, RPM's). SCALE(range/full_scale) returns a value from 0 to range.
// The gauge pointers vary from 0 to 1000. Use GMIN() and GRNG to set the graphs b and m values based on lowest input and input range (hi-low) respectively.

// sensor-type range
// -------------- --------
Expand All @@ -39,16 +42,16 @@ const char *red = "red";
// st_tachometer
// st_fuel_flow

// sensor-type, pin, decimal, voffset, vfactor, moffset, mfactor, lowAlarm, lowAlert, highAlert, highAlarm
const Sensor vtS = { st_volts, 0, 1, 0, MX(ADCtoV10), GB(100*V10toADC), GMX(60*V10toADC), 110, 130, 9999, 160 };
const Sensor opS = { st_r240to33, 1, 0, 0, MX(.1), 0, MX(1.), 25, 55, 9999, 95 };
const Sensor otS = { st_thermistorF, 2, 0, 0, MX(.1), GB(50*10), GMX(200*10), -1, 140, 9999, 250 };
const Sensor fpS = { st_r240to33, 3, 1, 0, MX(.15), 0, MX(1.), 5, 20, 60, 80 };
const Sensor flS = { st_r240to33, 4, 1, 0, MX(.16), 0, MX(1.), 25, 50, 9999, 999 };
const Sensor taS = { st_tachometer, 15, 0, 0, MX(1.), 0, GMX(3000), -1, 500, 9999, 2700 };
const Sensor maS = { st_volts, -1, 1, 100, 2000, 0, MX(1.), -1, -1, 9999, 9999 };
const Sensor chS = { st_k_type_tcF, 16, 0, 0, MX(.25), GB(100*4), GMX(400*4), -1, 150, 400, 500 };
const Sensor egS = { st_k_type_tcF, 20, 0, 0, MX(.25), GB(1000*4), GMX(600*4), -1, -1, 9999, 9999 };
// sensor-type, pin, decimal, voffset, vfactor, goffset, gfactor, lowAlarm, lowAlert, highAlert, highAlarm
const Sensor vtS = { st_volts, 0, 1, 0, SCALE(200/1024.0), GMIN(100*fromV), GRNG(60*fromV), 110, 130, 9999, 160 };
const Sensor opS = { st_volts, 1, 0, RVoff, RVRNG(100), RVoff, RVSCALE(1.), 25, 55, 9999, 95 };
const Sensor otS = { st_thermistorF, 2, 0, 0, SCALE(.1), GMIN(50*10), GRNG(200*10), -1, 140, 9999, 250 };
const Sensor fpS = { st_volts, 3, 1, RVoff, RVRNG(150), RVoff, RVSCALE(1.5), 5, 20, 60, 80 };
const Sensor flS = { st_volts, 4, 1, RVoff, RVRNG(160), RVoff, RVSCALE(1.), 25, 50, 9999, 999 };
const Sensor taS = { st_tachometer, 15, 0, 0, SCALE(1.), 0, GRNG(3000), -1, 500, 9999, 2700 };
const Sensor maS = { st_volts, -1, 1, 100, 2000, 0, SCALE(1.), -1, -1, 9999, 9999 };
const Sensor chS = { st_k_type_tcF, 16, 0, 0, SCALE(.25), GMIN(100*4), GRNG(400*4), -1, 150, 400, 500 };
const Sensor egS = { st_k_type_tcF, 20, 0, 0, SCALE(.25), GMIN(1000*4), GRNG(600*4), -1, -1, 9999, 9999 };

// Labels
// ------
Expand Down
4 changes: 2 additions & 2 deletions enguino/egTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ typedef struct {
byte decimal; // add decimal point 'decimal' positons from the right (0 is integer)
int voffset; // used to display reading, int_reading has 'decimal' point shifted right
int vfactor; // int_reading = multiply * sensor >> divisor + offset
int moffset; // used to calculate marker position
int mfactor; // 0-4000 vertical gauge, 0-8000 horizontal gauge, 0-2400 round gauge
int goffset; // used to calculate gauge marker position
int gfactor; // 0-4000 vertical gauge, 0-8000 horizontal gauge, 0-2400 round gauge
int lowAlarm;
int lowAlert;
int highAlert;
Expand Down
141 changes: 32 additions & 109 deletions enguino/enguino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ IPAddress ip(192, 168, 0, 111);
// A made up MAC address. Only real critera is the first bytes 2 lsb must be 1 (for local) and 0 (for unicast).
byte mac[] = { 0xDE, 0x15, 0x24, 0x33, 0x42, 0x51 };

int readSensor(const Sensor *s, byte n = 0);

// #define RANDOM_SENSORS 1

// Initialize the Ethernet server library
Expand All @@ -30,7 +28,7 @@ EthernetClient client;

bool leanMode;
int peakEGT[4];
byte hobbsCount = 90;
byte hobbsCount = 3600/40; // update hobbs 40 times an hour
bool engineRunning;

volatile unsigned long lastTachTime;
Expand All @@ -43,6 +41,8 @@ bool didKeyDown;
bool didChangeDim;
byte auxScreen = 2;

#include "sensors.h"

// printLED functions for the auxiliary display
#include "printLED.h"

Expand All @@ -62,89 +62,6 @@ byte auxScreen = 2;



InterpolateTable thermistor = {
64, 32,
(byte []) {
3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5,
5, 5, 5, 6, 6, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4
},
(int []) {
1538, 1480, 1428, 1382, 1341, 1303, 1269, 1208,
1154, 1107, 1064, 1026, 990, 957, 897, 844,
796, 752, 711, 635, 564, 431, 363, 291,
252, 210, 164, 112, 83, 50, 13, -30
},
};

InterpolateTable r240to33 = {
48, 28,
(byte []){
5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
},
(int []){
1105, 1064, 1019, 972, 921, 894, 866,
837, 806, 775, 742, 707, 671, 634,
595, 553, 510, 465, 417, 367, 314,
258, 199, 137, 70, 0, -75, -155
},
};

int readSensor(const Sensor *s, byte n = 0) {
int p = s->pin + n;

#ifdef RANDOM_SENSORS
if (p < 16)
return rand() & 0x3ff;
if (p < 20)
return rand() & 0x7ff; // CHT
if (p < 24)
return rand() & 0x7ff + 4000; // EGT
return FAULT;

#else
int v;
if (p < 0)
return FAULT;

int t = s->type;
int toF = 0;
if (p == 15) {
// RPM's are occasionaly screwed up because of IRQ latency.
// Throw out highest and lowest and average the middle 2
noInterrupts();
int r[4];
memcpy(r, rpm, sizeof(rpm));
interrupts();
sort(r, N(r));
v = (r[1]+r[2])>>1;
}
else if (p < 16) {
v = analogRead(p);
if (t == st_r240to33)
v = interpolate(&r240to33, v);
else if (t == st_thermistorC || t == st_thermistorF) {
v = interpolate(&thermistor, v);
if (t == st_thermistorF)
toF = 32 * 10;
}
}
else if (p < 24) {
noInterrupts();
v = tcTemp[p-16];
interrupts();
if (t == st_j_type_tcC || t == st_j_type_tcF)
v = multiplyAndScale(v - tcTemp[8], 25599, 15) + tcTemp[8];
if (t == st_k_type_tcF || t == st_j_type_tcF)
toF = 32 * 4;
}
if (toF && v != FAULT)
v = (v*9)/5 + toF;
return v;
#endif
}


void serveUpWebPage(char url, char var, word num) {
switch(url) {
case '?': // lean/cancel/ button pressed or return from setup
Expand Down Expand Up @@ -259,7 +176,7 @@ blink:


void setup() {
// Serial.begin(9600);
Serial.begin(9600);
// while (!Serial)
// ; // wait for serial port to connect. Stops here until Serial Monitor is started. Good for debugging setup

Expand All @@ -285,7 +202,6 @@ void setup() {


void loop() {
// listen for incoming clients
if (switchPress) {
if (!didKeyDown) {
auxScreen += 2;
Expand All @@ -308,6 +224,7 @@ void loop() {
didKeyDown = true;
}

// listen for incoming clients
client = server.available();
if (client) {
// an http request ends with a blank line
Expand Down Expand Up @@ -369,29 +286,35 @@ void loop() {
client.stop();
}

if (eighthSecondCount == 4)
goto halfSecond;
if (eighthSecondCount >= 8) {
if (tachDidPulse)
tachDidPulse = false;
else
memset(rpm, 0, sizeof(rpm));
engineRunning = scaleValue(&vtS, readSensor(&vtS)) > 130; // greater than 13.0 volts means engine is running

if (engineRunning) {
if (--hobbsCount == 0) {
if (++(ee_status.hobbs) > 39999) {
ee_status.hobbs = 0;
ee_status.hobbs1k++;
if (eighthSecondTick) {
eighthSecondTick = false;



if (eighthSecondCount == 4)
goto halfSecond;
if (eighthSecondCount >= 8) {
if (tachDidPulse)
tachDidPulse = false;
else
memset(rpm, 0, sizeof(rpm));
engineRunning = scaleValue(&vtS, readSensor(&vtS)) > 130; // greater than 13.0 volts means engine is running

if (engineRunning) {
if (--hobbsCount == 0) {
if (++(ee_status.hobbs) > 39999) {
ee_status.hobbs = 0;
ee_status.hobbs1k++;
}
eeUpdateStatus();
hobbsCount = 90;
}
eeUpdateStatus();
hobbsCount = 90;
}
}

eighthSecondCount -= 8;


eighthSecondCount -= 8;

halfSecond:
auxDisplay(auxScreen);
auxDisplay(auxScreen);
}
}
}
6 changes: 3 additions & 3 deletions enguino/printGauges.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Implementation for printPrefix and pringGauge

int scaleMark(const Sensor *s, int val) {
int mark = multiplyAndScale(s->mfactor, val+s->moffset, divisor);
int mark = multiplyAndScale(s->gfactor, val+s->goffset, divisor);
if (mark < 0)
mark = 0;
if (mark > 1000)
Expand All @@ -12,7 +12,7 @@ int scaleMark(const Sensor *s, int val) {
int scaleValue(const Sensor *s, int val) {
if (val == FAULT)
return val;
return multiplyAndScale(s->vfactor,val, divisor) + s->voffset;
return multiplyAndScale(s->vfactor,val+s->voffset, divisor);
}

// vertical gauge is
Expand All @@ -27,7 +27,7 @@ void printVertical(const Gauge *g, bool showLabels=true, byte pinOffset=0) {

int val = readSensor(g->sensor, pinOffset);
int mark = scaleMark(g->sensor, val) << 2;

const char *color = 0;

// fill in the color regions of the gauage
Expand Down
2 changes: 1 addition & 1 deletion enguino/printLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ void printLED(byte line, byte *txt) {
// print the fuel gauge (e.g. 2.5:17) (left tank : right tank)
void printLEDFuel(int left, int right) {
memset(ledBuffer, 0, sizeof(ledBuffer));
printLEDRawHalfDigits(2, left);
printLEDRawHalfDigits(4, right);
printLEDRawHalfDigits(2, right);
ledBuffer[5] = LED_COLON;
writeLED(1);
}
Expand Down
Loading

0 comments on commit 81efe23

Please sign in to comment.