-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCANSender.ino
282 lines (257 loc) · 7.81 KB
/
CANSender.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Licensed under the Apache License, Version 2.0 license. See LICENSE file in the project root for full license information.
// Luigi Di Marcantonio AGROSBUS Project UNIVPM
//Main library________________________
#include <CAN.h>
//Sensors library_____________________
#include <DHT.h> // temperature and humidity sensor library
#include <DHT_U.h> // temperature and humidity sensor library
#include <HCSR04.h> //Ultrasonic distance sensor library
//_____________________________________
#define DHTPIN A2 //temperature and humidity sensor PINs
#define DHTTYPE DHT11 //temperature and humidity sensor type
#define NODE_ID 0x12
DHT_Unified dht(DHTPIN, DHTTYPE); //Type and PIN
UltraSonicDistanceSensor distanceSensor(A4, A3); //Ultrasonic distance sensor used PIN
void encodeUINT16(float value, int digits, unsigned short int * valueDST);
//union to manage data encoding
typedef union sensorS {
unsigned char buf[2];
unsigned short int value;
}sensorS;
sensorS Humidity;
sensorS Humidity_Ext;
sensorS Temperature;
sensorS Temperature_Ext;
sensorS Radar;
uint32_t delayMS;
//--SIMULATION TYPE--------------------------+
int simulation_type = 0; // |
// 0= continuous sending of packages |
// 1= send packets if sensor values change |
// 2= random selected delay and sensor |
//-------------------------------------------+
float temperature_value = 0;
float humidity_value = 0;
float radar_value = 0;
//function to encode data
void encodeUINT16(float value, int digits, unsigned short int * valueDST) {
* valueDST = (unsigned short int)(value * pow(10, digits));
return;
}
//setup function or arduino
void setup() {
Serial.begin(9600);
while (!Serial);
//Sensors Setup
dht.begin();
sensor_t sensor;
dht.temperature().getSensor( & sensor);
dht.humidity().getSensor( & sensor);
delayMS = sensor.min_delay / 1000;
//Simulation type--------------------
Serial.println("CAN Sender");
Serial.println("Selected simulation type: ");
Serial.print(simulation_type);
if (simulation_type == 0)
Serial.println(" = Continuous sending of packages.");
if (simulation_type == 1)
Serial.println(" = Send packets if sensor values change.");
if (simulation_type == 2)
Serial.println("= Radarandom selected delay and sensor.");
//----------------------------------------
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
//callback function to manage incoming CAN-bus frame
void receiverCallback(int packetSize) {
Serial.print("\n\n\n");
Serial.print("ID: ");
Serial.print(CAN.packetId());
Serial.print(" LENGHT: ");
Serial.print(packetSize);
Serial.print(" VALUES: ");
if(CAN.packetId() != NODE_ID) //messege not of interest
return;
//get raw bytes
for (int i = 0; i < packetSize; i++) {
int l = CAN.read();
Serial.print(l, DEC);
Serial.print(" | ");
if (l == 0)
digitalWrite(LED_BUILTIN, LOW);
else if (l == 1)
digitalWrite(LED_BUILTIN, HIGH);
}
}
//function to send frame on CAN-bus
void sender(int id, unsigned char * data, unsigned short int len) {
int i = 0;
CAN.beginPacket(id);
while (i < len) {
CAN.write(data[i]);
i++;
}
CAN.endPacket();
}
//function to send frame on CAN-bus with extended format
void extended_packet_sender(int id, unsigned char * data, unsigned short int len) {
int i = 0;
CAN.beginExtendedPacket(id);
while (i < len) {
CAN.write(data[i]);
i++;
}
CAN.endPacket();
}
//function to manage the temperature sensor
void temperature_sensor() {
sensors_event_t event;
dht.temperature().getEvent( & event);
Serial.print("Sending temperature data packet... ");
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
} else {
Serial.print(event.temperature); //TEST
encodeUINT16(event.temperature, 1, & Temperature.value);
Serial.println(" ValueDST: ");
Serial.print(Temperature.value);
sender(0x12, Temperature.buf, 2); //send packet with temperature data sensor
Serial.println(" temp fatto");
}
}
//function to manage the temperature sensor sending with extended format
void extended_temperature_sensor() {
sensors_event_t event;
dht.temperature().getEvent( & event);
Serial.print("Sending temperature data packet... ");
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
} else {
Serial.print(event.temperature); //TEST
encodeUINT16(event.temperature, 1, & Temperature_Ext.value);
Serial.println(" ValueDST extended packet temperature: ");
Serial.print(Temperature.value);
extended_packet_sender(0x12, Temperature.buf, 2); //send extended packet with temperature data sensor
Serial.println(" Temperature data sent");
}
}
//function to manage humidity sensor
void humidity_sensor() {
sensors_event_t event;
dht.humidity().getEvent( & event);
Serial.print("Sending humidity data packet ");
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
} else {
Serial.print(event.relative_humidity); //TEST
encodeUINT16(event.relative_humidity, 1, & Humidity.value);
Serial.println(" ValueDST Humidity: ");
Serial.print(Humidity.value);
sender(0x13, Humidity.buf, 2);
Serial.println(" Humidity data packet sent ");
}
}
//function to manage humidity sensor sending with extended format
void extended_humidity_sensor() {
sensors_event_t event;
dht.humidity().getEvent( & event);
Serial.print("Sending humidity data packet ");
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
} else {
Serial.print(event.relative_humidity); //TEST
encodeUINT16(event.relative_humidity, 1, & Humidity_Ext.value);
Serial.println(" ValueDST humidity: ");
Serial.print(Temperature.value);
extended_packet_sender(0x15, Humidity_Ext.buf, 2);
Serial.println(" Humidity data packet sent");
}
}
//function to manage distance sensor
void anti_collision_radar() {
double distance = distanceSensor.measureDistanceCm();
Serial.println(distance);
encodeUINT16(distance, 2, & Radar.value);
sender(0x14, Radar.buf, 2);
}
//function to send data only on changed values
void send_if_new_data_are_changed() {
sensors_event_t event;
dht.temperature().getEvent( & event);
dht.humidity().getEvent( & event);
if (temperature_value != event.temperature) {
temperature_sensor();
temperature_value = event.temperature;
}
if (humidity_value != event.relative_humidity) {
humidity_sensor();
humidity_value = event.relative_humidity;
}
if (radar_value != distanceSensor.measureDistanceCm()) {
anti_collision_radar();
radar_value = distanceSensor.measureDistanceCm();
}
}
//funtion to manage random scheduling for data sending
int random_() {
int v1 = rand() % 3;
int v2 = rand() % 6;
int delay_time[3] = {
1000,
500,
20
};
int delay_ = delay_time[v1];
switch (v2) {
case 1: {
extended_temperature_sensor();
delay(delay_);
}
case 2: {
humidity_sensor();
delay(delay_);
}
case 3: {
anti_collision_radar();
delay(delay_);
}
case 4: {
extended_humidity_sensor();
delay(delay_);
}
case 5: {
delay(delay_);
}
case 6: {
delay(delay_);
}
}
}
//loop Arduino function
void loop() {
if (simulation_type > 2) {
Serial.println("Selected simulation type is wrong...");
delay(10000);
}
if (simulation_type == 0) {
temperature_sensor();
delay(500);
humidity_sensor();
delay(500);
extended_humidity_sensor();
delay(500);
anti_collision_radar();
delay(500);
}
if (simulation_type == 1) {
send_if_new_data_are_changed();
}
if (simulation_type == 2)
random_();
receiverCallback(int packetSize);
Serial.println("All done!");
delay(1000);
}