1+ #include <rclc/rclc.h>
2+ #include <std_msgs/msg/string.h>
3+ #include <std_msgs/msg/float64.h>
4+ #include <std_msgs/msg/int32.h>
5+ #include <std_msgs/msg/u_int32.h>
6+
7+ #include <stdio.h>
8+
9+
10+ static float altitude ;
11+ static uint32_t engine_power ;
12+ static unsigned it ;
13+ static char Alert [200 ];
14+
15+ /**
16+ * @brief Update screen
17+ *
18+ */
19+ void UpdateDisplay ()
20+ {
21+ printf ("\r[received messages: %7u] [Altitude: %8.2f] [Engine power: %8u] [Status: %10s]" , it ++ , altitude , engine_power , Alert );
22+ }
23+
24+
25+ /**
26+ * @brief new alert callback
27+ *
28+ * @param msgin
29+ */
30+ void on_alert_message (const void * msgin )
31+ {
32+ const std_msgs__msg__String * msg = (const std_msgs__msg__String * )msgin ;
33+ strcpy (Alert , msg -> data .data );
34+
35+ UpdateDisplay ();
36+ }
37+
38+
39+ /**
40+ * @brief new altitude calback
41+ *
42+ * @param msgin
43+ */
44+ void on_altitude_message (const void * msgin )
45+ {
46+ std_msgs__msg__Float64 * msg = (std_msgs__msg__Float64 * )msgin ;
47+
48+ altitude = msg -> data ;
49+ UpdateDisplay ();
50+ }
51+
52+ /**
53+ * @brief
54+ *
55+ * @param msgin
56+ */
57+ void on_power_message (const void * msgin )
58+ {
59+ std_msgs__msg__UInt32 * msg = (std_msgs__msg__UInt32 * )msgin ;
60+
61+ engine_power = msg -> data ;
62+ UpdateDisplay ();
63+ }
64+
65+
66+ /**
67+ * @brief Main
68+ *
69+ * @param argc
70+ * @param argv
71+ * @return int
72+ */
73+ int main (int argc , char * argv [])
74+ {
75+ (void )argc ;
76+ (void )argv ;
77+
78+ rclc_node_t * node = NULL ;
79+ rclc_subscription_t * alert_subscription = NULL ;
80+ rclc_subscription_t * altitude_subscription = NULL ;
81+ rclc_subscription_t * power_subscription = NULL ;
82+
83+
84+ rclc_init (0 , NULL );
85+
86+ node = rclc_create_node ("display" , "" );
87+
88+ alert_subscription = rclc_create_subscription (node , RCLC_GET_MSG_TYPE_SUPPORT (std_msgs , msg , String ), "std_msgs_msg_String" , on_alert_message , 1 , false);
89+ altitude_subscription = rclc_create_subscription (node , RCLC_GET_MSG_TYPE_SUPPORT (std_msgs , msg , Float64 ), "std_msgs_msg_Float64" , on_altitude_message , 1 , false);
90+ power_subscription = rclc_create_subscription (node , RCLC_GET_MSG_TYPE_SUPPORT (std_msgs , msg , UInt32 ), "std_msgs_msg_UInt32" , on_power_message , 1 , false);
91+
92+ rclc_spin_node (node );
93+
94+ if (alert_subscription ) rclc_destroy_subscription (alert_subscription );
95+ if (altitude_subscription ) rclc_destroy_subscription (altitude_subscription );
96+ if (node ) rclc_destroy_node (node );
97+
98+ printf ("Display node closed." );
99+
100+ }
0 commit comments