STAR
Cal STAR โ€” firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
send_all_sensors.cpp
Go to the documentation of this file.
1/**
2 * Test to send fake data for ALL sensor types (PT, TC, RTD, LC) to Elodin DB
3 * This validates that all VTables work correctly
4 */
5
6#include <array>
7#include <chrono>
8#include <cstdint>
9#include <iomanip>
10#include <iostream>
11#include <thread>
12#include <vector>
13
14// Our includes
15#include "../../utl/db.hpp"
16#include "../include/elodin/DatabaseConfig.hpp"
17#include "../include/elodin/ElodinClient.hpp"
19
20using namespace vtable;
21using namespace vtable::builder;
22
23int main(int argc, char* argv[]) {
24 if (argc < 2) {
25 std::cerr << "Usage: " << argv[0] << " <db_port> [messages_per_type]" << std::endl;
26 std::cerr << "Example: " << argv[0] << " 2240 10" << std::endl;
27 return 1;
28 }
29
30 uint16_t port = std::stoi(argv[1]);
31 int messages_per_type = (argc > 2) ? std::stoi(argv[2]) : 10;
32
33 std::cout << "\n๐Ÿงช Sending fake data for ALL sensor types to Elodin" << std::endl;
34 std::cout << "===================================================" << std::endl;
35 std::cout << "Database: 127.0.0.1:" << port << std::endl;
36 std::cout << "Messages per type: " << messages_per_type << std::endl;
37 std::cout << "Sensor types: PT, TC, RTD, LC" << std::endl;
38 std::cout << "===================================================\n" << std::endl;
39
40 try {
41 // Connect to Elodin DB
42 daq_comms::elodin::ElodinClient client;
43 if (!client.connect("127.0.0.1", port)) {
44 std::cerr << "โŒ Failed to connect to Elodin DB: " << client.last_error() << std::endl;
45 return 1;
46 }
47 std::cout << "โœ… Connected to Elodin DB" << std::endl;
48
49 // Register all VTables
50 if (!daq_comms::elodin::DatabaseConfig::register_tables(client)) {
51 std::cerr << "โŒ Failed to register tables" << std::endl;
52 return 1;
53 }
54 std::cout << "โœ… Registered all VTables" << std::endl;
55
56 // Wait for Elodin to process VTable registrations
57 std::cout << "โณ Waiting 5 seconds for Elodin to fully process VTable registrations..."
58 << std::endl;
59 std::this_thread::sleep_for(std::chrono::milliseconds(5000));
60
61 // Packet IDs
62 std::array<uint8_t, 2> pt_packet_id{0x20, 0x00};
63 std::array<uint8_t, 2> tc_packet_id{0x21, 0x00};
64 std::array<uint8_t, 2> rtd_packet_id{0x22, 0x00};
65 std::array<uint8_t, 2> lc_packet_id{0x23, 0x00};
66
67 auto start_time = std::chrono::steady_clock::now();
68
69 std::cout << "\n๐Ÿ“ค Starting REAL-TIME data transmission (interleaved)..." << std::endl;
70 std::cout << " Rate: ~10 Hz (all sensor types sent together every 100ms)" << std::endl;
71 std::cout << " Pattern: PT โ†’ TC โ†’ RTD โ†’ LC (repeated)" << std::endl;
72 std::cout << std::endl;
73
74 // Send interleaved messages - all sensor types together for real-time display
75 for (int i = 0; i < messages_per_type; ++i) {
76 auto now = std::chrono::steady_clock::now();
77 auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time);
78 uint64_t timestamp_ns = elapsed.count();
79 uint32_t sample_timestamp_ms = i * 100;
80
81 // Send PT message
82 {
84 msg.setField<0>(timestamp_ns);
85 msg.setField<1>(static_cast<uint8_t>(i % 3));
86 msg.setField<2>(std::array<uint8_t, 3>{0, 0, 0});
87 msg.setField<3>(1000 + i * 10);
88 msg.setField<4>(sample_timestamp_ms);
89 msg.setField<5>(0);
90
91 if (!client.publish(pt_packet_id, msg)) {
92 std::cerr << "โŒ Failed to publish PT message #" << (i + 1) << std::endl;
93 }
94 }
95
96 // Send TC message (same timestamp for real-time sync)
97 {
99 msg.setField<0>(timestamp_ns);
100 msg.setField<1>(static_cast<uint8_t>(i % 2));
101 msg.setField<2>(std::array<uint8_t, 3>{0, 0, 0});
102 msg.setField<3>(2000 + i * 20);
103 msg.setField<4>(sample_timestamp_ms);
104 msg.setField<5>(0);
105
106 if (!client.publish(tc_packet_id, msg)) {
107 std::cerr << "โŒ Failed to publish TC message #" << (i + 1) << std::endl;
108 }
109 }
110
111 // Send RTD message (same timestamp for real-time sync)
112 {
114 msg.setField<0>(timestamp_ns);
115 msg.setField<1>(static_cast<uint8_t>(i % 2));
116 msg.setField<2>(std::array<uint8_t, 3>{0, 0, 0});
117 msg.setField<3>(3000 + i * 30);
118 msg.setField<4>(sample_timestamp_ms);
119 msg.setField<5>(0);
120
121 if (!client.publish(rtd_packet_id, msg)) {
122 std::cerr << "โŒ Failed to publish RTD message #" << (i + 1) << std::endl;
123 }
124 }
125
126 // Send LC message (same timestamp for real-time sync)
127 {
129 msg.setField<0>(timestamp_ns);
130 msg.setField<1>(static_cast<uint8_t>(0));
131 msg.setField<2>(std::array<uint8_t, 3>{0, 0, 0});
132 msg.setField<3>(4000 + i * 40);
133 msg.setField<4>(sample_timestamp_ms);
134 msg.setField<5>(0);
135
136 if (!client.publish(lc_packet_id, msg)) {
137 std::cerr << "โŒ Failed to publish LC message #" << (i + 1) << std::endl;
138 }
139 }
140
141 // Progress update
142 if (i < 5 || i % 10 == 0) {
143 std::cout << "[send_all_sensors] โœ… Sent batch #" << (i + 1)
144 << " (PT+TC+RTD+LC, timestamp=" << timestamp_ns << "ns)" << std::endl;
145 }
146
147 // Wait 100ms before next batch (10 Hz rate)
148 std::this_thread::sleep_for(std::chrono::milliseconds(100));
149 }
150
151 std::cout << "\n โœ… Sent " << messages_per_type
152 << " batches (4 messages each = " << (messages_per_type * 4) << " total messages)"
153 << std::endl;
154
155 client.flush_buffer();
156
157 auto end_time = std::chrono::steady_clock::now();
158 auto duration =
159 std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
160
161 std::cout << "\n===================================" << std::endl;
162 std::cout << "โœ… Transmission Complete!" << std::endl;
163 std::cout << " Total messages sent: " << (messages_per_type * 4) << std::endl;
164 std::cout << " Duration: " << duration.count() << " ms" << std::endl;
165 std::cout << "===================================" << std::endl;
166 std::cout << "\n๐Ÿ“Š View data in Elodin editor:" << std::endl;
167 std::cout << " elodin editor <db_path>" << std::endl;
168 std::cout << std::endl;
169
170 client.disconnect();
171
172 } catch (const std::exception& e) {
173 std::cerr << "โŒ Error: " << e.what() << std::endl;
174 return 1;
175 }
176
177 return 0;
178}
static const emmc_port_t port
Definition emmc_stm32.c:210
CommsMessage< uint64_t, uint8_t, std::array< uint8_t, 3 >, uint32_t, uint32_t, uint8_t > RawTCMessage
Raw Thermocouple sample message for Elodin CRITICAL: Includes padding bytes to ensure uint32_t fields...
CommsMessage< uint64_t, uint8_t, std::array< uint8_t, 3 >, uint32_t, uint32_t, uint8_t > RawRTDMessage
Raw RTD sample message for Elodin CRITICAL: Includes padding bytes to ensure uint32_t fields are 4-by...
CommsMessage< uint64_t, uint8_t, std::array< uint8_t, 3 >, uint32_t, uint32_t, uint8_t > RawLCMessage
Raw Load Cell sample message for Elodin CRITICAL: Includes padding bytes to ensure uint32_t fields ar...
CommsMessage< uint64_t, uint8_t, std::array< uint8_t, 3 >, uint32_t, uint32_t, uint8_t > RawPTMessage
Raw Pressure Transducer sample message for Elodin.
static bool elapsed(uint32_t now, uint32_t then, uint32_t interval)
Definition self_test.c:10
void setField(ValueType &&value)
int main()