-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotarytesterbeta
99 lines (86 loc) · 4.66 KB
/
rotarytesterbeta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*TR-01 Rotary Compression Tester
Originally distributed by John Doss 2017, modified for output in bar and
normalized values for 250rpm by Miro Bogner 2018
This code assumes you're using a 0-200 psi - 0.5-4.5 vdc pressure transducer
connected to the AI0 pin. You can use sensors with other pressure or voltage
ranges but you'll need to modify the code.
*/
#define SENSOR 0 // Analog input pin that sensor is connected too
const float dead_space = 1.18; // correction factor to match values to the Mazda OEM Tester
const float bar_factor = 1.6833E-1; // factor to calculate bar (x10) from analog sensor values
const float a0 = 8.6; // polynom Fit a0 to norm values to 250rpm
const float a1 = -4.8E-02; // polynom Fit a1 to norm values to 250rpm
const float a2 = 5.4E-05; // polynom Fit a2 to norm values to 250rpm
const int no_measurement_series = 5; // We record 5 sets of values as we consider the first and last two unreliable
const int reliable_measurement = no_measurement_series / 2;
const int no_chambers = 3; // The rotary engine has three chambers
const int max_threshold = 15; // Threshold to look for max and min values, cannot be zero due to noise
const int min_threshold = 15;
const int measurements = 10; // Number of measurements to find the sensors baseline
float baseline; // variable to store the sensors baseline before compression is tested
int max_buffer[no_measurement_series][no_chambers]; // Buffer to store pressure values
unsigned long rpm[no_measurement_series]; // Buffer to store rpm values
int measurement[measurements]; // Buffer to store baseline measurements
char buf1[5], buf2[75]; // Definde char buffers for temp and output
void setup() {
Serial.begin(57600); // serial speed
int sum = 0; // Function to find the sensors baseline
for (int i = 0; i < measurements; ++i) {
measurement[i] = (analogRead(SENSOR));
sum = sum + measurement[i];
delay(50);
}
baseline = (float) sum / (float) measurements;
Serial.println(" TR-01 Open-Source");
Serial.println(" Rotary Engine Compression Tester");
Serial.println("Firmware v1.2 Tester Version 2/2020");
Serial.println("");
}
int find_max() { // Function to look for pressure peaks
int current_max = 0;
int sensor_measurement = analogRead(SENSOR);
while ((current_max - sensor_measurement) < max_threshold) {
if (sensor_measurement > current_max)
current_max = sensor_measurement;
sensor_measurement = (analogRead(SENSOR));
}
int current_min = sensor_measurement;
while ((sensor_measurement - current_min) < min_threshold) {
sensor_measurement = (analogRead(SENSOR));
if (sensor_measurement < current_min)
current_min = sensor_measurement;
}
return current_max;
}
void loop() {
for (int i = 0; i < no_measurement_series; ++i) {
unsigned long start_time = millis(); // record cycle begining time for RPM calculation
for (int chamber = 0; chamber < no_chambers; ++chamber) {
max_buffer[i][chamber] = find_max();
}
unsigned long end_time = millis(); // record cycle end time for RPM calculation
rpm[i] = round(180000.f / (end_time - start_time));
}
float pressure_chamber[no_chambers];
float correction = a0 + a1 * rpm[reliable_measurement] + a2 * sq(rpm[reliable_measurement]);
for (int chamber = 0; chamber < no_chambers; ++chamber) {
pressure_chamber[chamber] = (float) round((max_buffer[reliable_measurement][chamber] - baseline) * bar_factor * dead_space) / 10.f;
}
strcpy(buf2, "BAR: ");
for (int chamber = 0; chamber < no_chambers; ++chamber) {
dtostrf(pressure_chamber[chamber], 3, 1, buf1);
strcat(buf2, buf1);
strcat(buf2, " ");
}
strcat(buf2, "RPM: ");
itoa(rpm[reliable_measurement], buf1, 10);
strcat(buf2, buf1);
strcat(buf2, " BAR: ");
for (int chamber = 0; chamber < no_chambers; ++chamber) {
dtostrf(pressure_chamber[chamber] + correction, 3, 1, buf1);
strcat(buf2, buf1);
strcat(buf2, " ");
}
strcat(buf2, "@250 RPM");
Serial.println(buf2);
}