-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (102 loc) · 3.69 KB
/
index.js
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
const { spawn } = require('child_process');
const kvsClient = spawn('/home/pi/rovy-cam/kvsWebrtcClientMasterGstSample', ['test-stream']);
const { mqttMotorChannel, mqttSensorTopic } = require("./constants");
const { mqttClient } = require('./iot');
const { motorSerialPort, motorPortWrite } = require('./motorSerial');
const { gps, gpsSerialParser } = require('./gpsSerial');
const { lteSerialPort, lteSerialParser, ltePortWrite } = require('./lteSerial');
const { bme280, forcedRead } = require('./bme280');
const { analogSensors, ADS1115 } = require('./analogSensors');
const { telemetry } = require('./data');
const motor = require("./motorControl");
// MOTOR SERIAL LISTENERS
motorSerialPort.on('open', async function() {
console.log('Motor Serial Port: Ready');
const procedures = motor.calculateValues('0,0');
if(procedures?.length) {
for (let step = 0; step < procedures.length; step++) {
await motorPortWrite(procedures[step]);
}
}
});
motorSerialPort.on('error', function(err) {
console.log('Error: ', err.message)
});
// MQTT LISTENERS
mqttClient.on("connect", () => {
console.log('connected to mqtt broker');
mqttClient.subscribe(mqttMotorChannel, (err) => {
if(!err) console.log(`subscribed to [${mqttMotorChannel}]`);
});
});
mqttClient.on("message", async (topic, message) => {
if(topic.includes('rovy/motor')) {
const procedures = motor.calculateValues(message.toString());
if(procedures?.length) {
for (let step = 0; step < procedures.length; step++) {
await motorPortWrite(procedures[step]);
}
}
}
});
// GPS SERIAL LISTENERS
gps.on('data', data => {
if(gps.state.lat && gps.state.lon) {
telemetry.updateGPS = {
lat: { value: gps.state.lat },
lon: { value: gps.state.lon},
speed: { value: Math.round(gps.state.speed)},
track: { value: Math.round(gps.state.track), unit: "°"},
alt: { value: Math.rount(gps.state.alt), unit: "m"},
}
}
})
gpsSerialParser.on('data', (data)=>{
if(data.includes('$GPVTG') || data.includes('$GPGGA') || data.includes('$GPHDT') || data.includes('$GPRMC')) gps.update(data);
});
// LTE SERIAL LISTENER
lteSerialPort.on('open', async()=>{
console.log('LTE Serial Port: Ready');
await ltePortWrite('AT+CSQ\r\n');
});
lteSerialParser.on('data', (data)=>{
data = data.toString();
if(data.includes('+CSQ:')) {
data = data.replace('+CSQ: ', '');
// MQTT SEND HERE
}
});
// BME280 LISTENER
bme280.open({forcedMode: true}).then(sensor => {
setInterval(_ => {
forcedRead(sensor).then((data)=>{
telemetry.updateTemperature = Math.round(data.temperature);
telemetry.updateHumidity = Math.round(data.humidity);
});
}, 15000);
}).catch(console.log);
// ANALOG SENSORS
analogSensors.openPromisified(1).then(async (bus) => {
const ads1115 = await ADS1115(bus);
const full = 26480;
setInterval(async () => {
let value1 = await ads1115.measure('0+GND');
let value2 = await ads1115.measure('2+GND');
const batteryVoltage = (5*(value2/full)) / 0.2
const irRange = 5*(value1/full);
telemetry.updateBattery = parseFloat(batteryVoltage.toFixed(2));
telemetry.updateRangeSensor = parseFloat(irRange.toFixed(4));
}, 1000);
});
// INTERVAL READER
setInterval(()=>{
console.log('telemetry', telemetry);
mqttClient.publish(mqttSensorTopic, JSON.stringify(telemetry));
}, 500)
// KVS OUT LISTENERS
kvsClient.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
kvsClient.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});