-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_example.cpp
More file actions
191 lines (156 loc) · 6.58 KB
/
command_example.cpp
File metadata and controls
191 lines (156 loc) · 6.58 KB
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
/**
* @file command_example.cpp
* @brief Demonstrates variadic command handling in modules
*/
#include "messages/messages.hpp"
#include <iostream>
#include <thread>
#include <cmath>
using namespace user_app;
// ============================================================================
// Define Command Payloads
// ============================================================================
struct ResetCmd {
bool hard_reset{false};
};
struct CalibrateCmd {
float offset{0.0f};
};
struct SetModeCmd {
uint32_t mode{0};
};
// Add commands to registry (in real code, this goes in user_messages.hpp)
namespace user_app {
// Extended CommRaT application with commands
using ExtendedApp = commrat::CommRaT<
commrat::Message::Data<TemperatureData>,
commrat::Message::Command<ResetCmd>,
commrat::Message::Command<CalibrateCmd>,
commrat::Message::Command<SetModeCmd>
>;
}
// ============================================================================
// Module with Multiple Command Handlers
// ============================================================================
/**
* @brief Sensor module that handles multiple command types
*
* Notice the variadic CommandTypes at the end:
* Module<Output<TempData>, PeriodicInput, ResetCmd, CalibrateCmd, SetModeCmd>
*
* The module automatically dispatches commands to the correct on_command() handler.
*/
class CommandableSensor : public ExtendedApp::Module<Output<TemperatureData>, PeriodicInput,
ResetCmd, CalibrateCmd, SetModeCmd> {
public:
explicit CommandableSensor(const ModuleConfig& config)
: ExtendedApp::Module<Output<TemperatureData>, PeriodicInput, ResetCmd, CalibrateCmd, SetModeCmd>(config) {}
protected:
void process(TemperatureData& output) override {
float raw_temp = 20.0f + std::sin(counter_++ * 0.1f) * 5.0f;
float calibrated_temp = raw_temp + calibration_offset_;
std::cout << "[Sensor] Mode=" << mode_
<< " Temp=" << calibrated_temp << "°C"
<< " (offset=" << calibration_offset_ << ")\n";
output = {
.temperature_celsius = calibrated_temp
};
}
// Command handlers - framework calls the right one automatically!
void on_command(const ResetCmd& cmd) {
std::cout << "[Sensor] Reset command received (hard="
<< cmd.hard_reset << ")\n";
if (cmd.hard_reset) {
calibration_offset_ = 0.0f;
mode_ = 0;
counter_ = 0;
}
}
void on_command(const CalibrateCmd& cmd) {
std::cout << "[Sensor] Calibrate command received (offset="
<< cmd.offset << ")\n";
calibration_offset_ = cmd.offset;
}
void on_command(const SetModeCmd& cmd) {
std::cout << "[Sensor] SetMode command received (mode="
<< cmd.mode << ")\n";
mode_ = cmd.mode;
}
private:
float calibration_offset_ = 0.0f;
uint32_t mode_ = 0;
int counter_ = 0;
};
// ============================================================================
// Main - Send Commands to Module
// ============================================================================
int main() {
std::cout << "=== Variadic Command Handling Example ===\n\n";
// Create sensor module with command handling
ModuleConfig sensor_config{
.name = "CommandableSensor",
.outputs = commrat::SimpleOutputConfig{.system_id = 0, .instance_id = 0},
.inputs = commrat::NoInputConfig{},
.period = std::chrono::milliseconds(200),
.message_slots = 10,
.max_subscribers = 8,
.priority = 10,
.realtime = false
};
CommandableSensor sensor(sensor_config);
sensor.start();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// Create control mailbox to send commands
commrat::MailboxConfig control_config{
.mailbox_id = 200,
.message_slots = 10,
.max_message_size = 4096,
.send_priority = 10,
.realtime = false,
.mailbox_name = "ControlMailbox"
};
// Use RegistryMailbox to send any message type
using AppRegistry = commrat::MessageRegistry<
commrat::MessageDefinition<TemperatureData, commrat::MessagePrefix::UserDefined, commrat::UserSubPrefix::Data, 65535>,
commrat::MessageDefinition<ResetCmd, commrat::MessagePrefix::UserDefined, commrat::UserSubPrefix::Commands, 65535>,
commrat::MessageDefinition<CalibrateCmd, commrat::MessagePrefix::UserDefined, commrat::UserSubPrefix::Commands, 65535>,
commrat::MessageDefinition<SetModeCmd, commrat::MessagePrefix::UserDefined, commrat::UserSubPrefix::Commands, 65535>
>;
commrat::RegistryMailbox<AppRegistry> control(control_config);
control.start();
std::cout << "\n=== Sending Commands ===\n\n";
// Send SetMode command
// Send SetMode command (to CMD mailbox = base + 0)
uint32_t sensor_cmd_mailbox = 131072; // system_id=0, instance_id=0, CMD=0
std::cout << ">>> Sending SetMode(mode=1)\n";
SetModeCmd set_mode{.mode = 1};
control.send(set_mode, sensor_cmd_mailbox);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Send Calibrate command
std::cout << "\n>>> Sending Calibrate(offset=2.5)\n";
CalibrateCmd calibrate{.offset = 2.5f};
control.send(calibrate, sensor_cmd_mailbox);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Send SetMode again
std::cout << "\n>>> Sending SetMode(mode=2)\n";
set_mode.mode = 2;
control.send(set_mode, sensor_cmd_mailbox);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Send Reset command
std::cout << "\n>>> Sending Reset(hard=true)\n";
ResetCmd reset{.hard_reset = true};
control.send(reset, sensor_cmd_mailbox);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Cleanup
std::cout << "\n=== Stopping ===\n";
control.stop();
sensor.stop();
std::cout << "\n=== Summary ===\n";
std::cout << "✓ Module declared with variadic CommandTypes:\n";
std::cout << " Module<TempData, PeriodicInput, ResetCmd, CalibrateCmd, SetModeCmd>\n\n";
std::cout << "✓ Framework automatically dispatches to correct on_command() handler\n";
std::cout << "✓ Type-safe command handling at compile-time\n";
std::cout << "✓ Commands sent as payload types (ResetCmd, CalibrateCmd, etc.)\n";
std::cout << "✓ No manual command ID checking or casting!\n";
return 0;
}