-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_data.cpp
63 lines (50 loc) · 2.28 KB
/
send_data.cpp
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
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include <chrono>
#include <thread>
int main() {
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
// Data comes in in the form of integers with a max value of ~10000.
// This data is then processed to be in a realistic range (for example, speed=10000 is mapped to speed=120km/h)
// This program assumes that this mapping has already been performed.
// Only necessary for random generation purposes
srand(1);
// random generation limits
int HI = 10;
int LO = -10;
int speedHI = 120;
int speedLO = -100;
int rpmHI = 2000;
int brake_pressureHI = 2000;
double speed = 120, rpm = 2000, steering_pos = 0, throttle_pos = 15, brake_pressure = 800;
// Main loop
while(true) {
// Generate random data
speed = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(speedHI-LO)));
rpm = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(rpmHI-LO)));
steering_pos = (-100) + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(100-(-100))));
throttle_pos = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
brake_pressure = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(brake_pressureHI-LO)));
// Reset speed to positive if a negative value is generated
if (speed < 0) {
speed = 5;
}
// Compose string of values to insert into database
std::string dataline = "(NOW(), " + std::to_string(speed) + ", "
+ std::to_string(rpm) + ", " + std::to_string(steering_pos) + ", "
+ std::to_string(throttle_pos) + ", " + std::to_string(brake_pressure) + ");";
// Compose SQL query
std::string command = "docker exec timescaledb "
"psql --command=\"INSERT INTO vehicle_data VALUES " + dataline + "\" postgresql://datawriter:password@localhost:5432/postgres";
// Execute SQL query
system(command.c_str());
// Wait
sleep_for(milliseconds(500));
std::cout << "Data added: " << dataline << std::endl;
}
}