|
1 | 1 | #include <rclc/rclc.h> |
2 | 2 | #include <std_msgs/msg/string.h> |
3 | | -#include <std_msgs/msg/float64.h> |
| 3 | +#include <std_msgs/msg/int32.h> |
| 4 | +#include <std_msgs/msg/u_int32.h> |
4 | 5 |
|
5 | 6 | #include <stdio.h> |
6 | 7 |
|
7 | | -static rclc_publisher_t* publisher; |
| 8 | +static rclc_publisher_t* engine_pub; |
| 9 | +static uint32_t engine_power = 100; |
8 | 10 |
|
9 | | - |
10 | | -void altitude_on_message(const void* msgin) |
| 11 | +void engine_on_message(const void* msgin) |
11 | 12 | { |
12 | | - const std_msgs__msg__Float64* msg = (const std_msgs__msg__Float64*)msgin; |
13 | | - |
14 | | - // Publish warning if value is less than 1000 |
15 | | - if (msg->data <= 500) |
| 13 | + |
| 14 | + const std_msgs__msg__Int32* msg = (const std_msgs__msg__Int32*)msgin; |
| 15 | + |
| 16 | + if (msg->data + engine_power > 0) |
16 | 17 | { |
17 | | - std_msgs__msg__String Warning; |
18 | | - Warning.data.data = "Failure!!"; |
19 | | - Warning.data.size = strlen(Warning.data.data); |
20 | | - Warning.data.capacity = strlen(Warning.data.data); |
21 | | - |
22 | | - rclc_publish(publisher, (const void*)&Warning); |
| 18 | + engine_power += msg->data; |
23 | 19 | } |
24 | | - else if (msg->data <= 1000) |
| 20 | + else |
25 | 21 | { |
26 | | - std_msgs__msg__String Warning; |
27 | | - Warning.data.data = "Warning!!"; |
28 | | - Warning.data.size = strlen(Warning.data.data); |
29 | | - Warning.data.capacity = strlen(Warning.data.data); |
30 | | - |
31 | | - rclc_publish(publisher, (const void*)&Warning); |
| 22 | + engine_power = 0; |
32 | 23 | } |
33 | | -} |
34 | | - |
35 | 24 |
|
36 | | -void engine_on_message(const void* msgin) |
37 | | -{ |
38 | | - const std_msgs__msg__Float64* msg = (const std_msgs__msg__Float64*)msgin; |
| 25 | + // Publish new altitude |
| 26 | + std_msgs__msg__UInt32 msg_out; |
| 27 | + msg_out.data = engine_power; |
| 28 | + rclc_publish(engine_pub, (const void*)&msg_out); |
39 | 29 | } |
40 | 30 |
|
41 | 31 | int main(int argc, char* argv[]) |
42 | 32 | { |
43 | 33 | (void)argc; |
44 | 34 | (void)argv; |
| 35 | + |
| 36 | + rclc_node_t* node = NULL; |
| 37 | + rclc_subscription_t* engine_sub = NULL; |
| 38 | + |
| 39 | + |
| 40 | + |
45 | 41 | rclc_init(0, NULL); |
46 | | - rclc_node_t* node = rclc_create_node("actuator", ""); |
47 | | - rclc_subscription_t* altitude_sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float64), "position_sensor", altitude_on_message, 1, false); |
48 | | - rclc_subscription_t* engine_sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float64), "engine_power", engine_on_message, 1, false); |
49 | | - publisher = rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String), "status_report", 1); |
| 42 | + node = rclc_create_node("actuator", ""); |
| 43 | + engine_sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "std_msgs_msg_Int32", engine_on_message, 1, false); |
| 44 | + engine_pub = rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, UInt32), "std_msgs_msg_UInt32", 1); |
| 45 | + |
| 46 | + // Publish new altitude |
| 47 | + std_msgs__msg__UInt32 msg_out; |
| 48 | + msg_out.data = engine_power; |
| 49 | + rclc_publish(engine_pub, (const void*)&msg_out); |
50 | 50 |
|
51 | 51 | rclc_spin_node(node); |
52 | 52 |
|
53 | | - if (altitude_sub) rclc_destroy_subscription(altitude_sub); |
| 53 | + //if (altitude_sub) rclc_destroy_subscription(altitude_sub); |
54 | 54 | if (engine_sub) rclc_destroy_subscription(engine_sub); |
55 | | - if (publisher) rclc_destroy_publisher(publisher); |
| 55 | + if (engine_pub) rclc_destroy_publisher(engine_pub); |
56 | 56 | if (node) rclc_destroy_node(node); |
57 | 57 |
|
| 58 | + printf("Actuator node closed.\n"); |
| 59 | + |
58 | 60 | return 0; |
59 | 61 | } |
0 commit comments