-
Notifications
You must be signed in to change notification settings - Fork 15
/
NMEA2000-Frequenz.ino
220 lines (170 loc) · 8.92 KB
/
NMEA2000-Frequenz.ino
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// NMEA2000 Frequenz messen (Motordrehzahl) mit Interrupt
// Version 0.1, 08.01.2021, AK-Homberger
#define ESP32_CAN_TX_PIN GPIO_NUM_5 // Set CAN TX port to 5
#define ESP32_CAN_RX_PIN GPIO_NUM_4 // Set CAN RX port to 4
#include <Arduino.h>
#include <Preferences.h>
#include <NMEA2000_CAN.h> // This will automatically choose right CAN library and create suitable NMEA2000 object
#include <N2kMessages.h>
#define RPM_Calibration_Value 1.0 // Translates Generator RPM to Engine RPM
#define Eingine_RPM_Pin 27 // Engine RPM is measured as interrupt on pin 27
// Set time offsets
#define SlowDataUpdatePeriod 333 // Time between CAN Messages sent
#define RPM_SendOffset 0
// Interrupt data
volatile uint64_t StartValue = 0; // First interrupt value
volatile uint64_t PeriodCount = 0; // period in counts of 0.000001 of a second
volatile unsigned long Last_int_time = 0; // Stores last Interrupt time
hw_timer_t * timer = NULL; // pointer to a variable of type hw_timer_t
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; // To lock/unlock interrupt
int NodeAddress; // To store last Node Address
Preferences preferences; // Nonvolatile storage on ESP32 - To store LastDeviceAddress
// Set the information for other bus devices, which messages we support
const unsigned long TransmitMessages[] PROGMEM = {127488L, // Engine Parameters, Rapid update
0
};
// Forward declaration to avoid compilation error
void IRAM_ATTR handleInterrupt();
//*****************************************************************************
void setup() {
uint8_t chipid[6];
uint32_t id = 0;
int i = 0;
// Init USB serial port
Serial.begin(115200);
delay(10);
// Init RPM measure
pinMode(Eingine_RPM_Pin, INPUT_PULLUP); // sets pin high
attachInterrupt(digitalPinToInterrupt(Eingine_RPM_Pin), handleInterrupt, FALLING); // attaches pin to interrupt on Falling Edge
timer = timerBegin(0, 80, true); // this returns a pointer to the hw_timer_t global variable
// 0 = first timer
// 80 is prescaler so 80MHZ divided by 80 = 1MHZ signal ie 0.000001 of a second
// true - counts up
timerStart(timer);
// Reserve enough buffer for sending all messages.
NMEA2000.SetN2kCANMsgBufSize(8);
NMEA2000.SetN2kCANReceiveFrameBufSize(150);
NMEA2000.SetN2kCANSendFrameBufSize(150);
// Generate unique number from chip id
esp_efuse_mac_get_default(chipid);
for (i = 0; i < 6; i++) id += (chipid[i] << (7 * i));
// Set product information
NMEA2000.SetProductInformation("1", // Manufacturer's Model serial code
100, // Manufacturer's product code
"My Sensor Module", // Manufacturer's Model ID
"1.0.2.25 (2019-07-07)", // Manufacturer's Software version code
"1.0.2.0 (2019-07-07)" // Manufacturer's Model version
);
// Set device information
NMEA2000.SetDeviceInformation(id, // Unique number. Use e.g. Serial number.
132, // Device function=Analog to NMEA 2000 Gateway. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
25, // Device class=Inter/Intranetwork Device. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
2046 // Just choosen free from code list on http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf
);
preferences.begin("nvs", false); // Open nonvolatile storage (nvs)
NodeAddress = preferences.getInt("LastNodeAddress", 34); // Read stored last NodeAddress, default 34
preferences.end();
Serial.printf("NodeAddress=%d\n", NodeAddress);
// If you also want to see all traffic on the bus use N2km_ListenAndNode instead of N2km_NodeOnly below
NMEA2000.SetMode(tNMEA2000::N2km_NodeOnly, NodeAddress);
NMEA2000.ExtendTransmitMessages(TransmitMessages);
NMEA2000.Open();
delay(200);
//*****************************************************************************
// Only for frequency simulation in loop()
ledcAttachPin(26, 1); // sets GPIO26 as signal output (for simulation only)
//*****************************************************************************
}
//*****************************************************************************
// RPM Event Interrupt
// Enters on falling edge
void IRAM_ATTR handleInterrupt()
{
portENTER_CRITICAL_ISR(&mux);
uint64_t TempVal = timerRead(timer); // value of timer at interrupt
PeriodCount = TempVal - StartValue; // period count between falling edges in 0.000001 of a second
StartValue = TempVal; // puts latest reading as start for next calculation
Last_int_time = millis();
portEXIT_CRITICAL_ISR(&mux);
}
//*****************************************************************************
// Calculate engine RPM from number of interupts per time
double ReadRPM() {
double RPM = 0;
portENTER_CRITICAL(&mux);
if (PeriodCount != 0) RPM = 1000000.00 / PeriodCount; // PeriodCount in 0.000001 of a second
if (millis() > Last_int_time + 500) RPM = 0; // No signals RPM=0;
portEXIT_CRITICAL(&mux);
return (RPM);
}
// Functions to control send interval and time offets
//*****************************************************************************
bool IsTimeToUpdate(unsigned long NextUpdate) {
return (NextUpdate < millis());
}
//*****************************************************************************
unsigned long InitNextUpdate(unsigned long Period, unsigned long Offset = 0) {
return millis() + Period + Offset;
}
//*****************************************************************************
void SetNextUpdate(unsigned long & NextUpdate, unsigned long Period) {
while ( NextUpdate < millis() ) NextUpdate += Period;
}
//*****************************************************************************
void SendN2kEngineRPM(void) {
static unsigned long SlowDataUpdated = InitNextUpdate(SlowDataUpdatePeriod, RPM_SendOffset);
tN2kMsg N2kMsg;
static double EngineRPM = 0;
if ( IsTimeToUpdate(SlowDataUpdated) ) {
SetNextUpdate(SlowDataUpdated, SlowDataUpdatePeriod);
EngineRPM = ReadRPM() * RPM_Calibration_Value;
// EngineRPM = (EngineRPM + (ReadRPM() * RPM_Calibration_Value)) / 2.0; // Implements a low-pass filter
Serial.printf("Engine RPM :%4.0f EngineRPM \n", EngineRPM);
SetN2kEngineParamRapid(N2kMsg, 0, EngineRPM, N2kDoubleNA, N2kInt8NA);
NMEA2000.SendMsg(N2kMsg);
}
}
//*****************************************************************************
// Function to check if SourceAddress has changed (due to address conflict on bus)
void CheckSourceAddressChange() {
int SourceAddress = NMEA2000.GetN2kSource();
if (SourceAddress != NodeAddress) { // Save potentially changed Source Address to NVS memory
NodeAddress = SourceAddress; // Set new Node Address (to save only once)
preferences.begin("nvs", false);
preferences.putInt("LastNodeAddress", SourceAddress);
preferences.end();
Serial.printf("Address Change: New Address=%d\n", SourceAddress);
}
}
//*****************************************************************************
void loop() {
SendN2kEngineRPM();
NMEA2000.ParseMessages();
CheckSourceAddressChange();
// For frequency simulation only
//**********************************
static unsigned long timer = 0;
if (millis() > timer + 100) {
timer = millis();
ledcSetup(1, analogRead(34), 7);
ledcWrite(1, 64);
}
//***********************************
// Dummy to empty input buffer to avoid board to stuck with e.g. NMEA Reader
if ( Serial.available() ) {
Serial.read();
}
}