15
15
16
16
MicroROSWrapper micro_ros (" power_node" );
17
17
18
- void propulsion_microseconds_callback (const void *msgin) {
19
- const std_msgs__msg__Int16MultiArray *propulsion_microseconds_msg = (const std_msgs__msg__Int16MultiArray *)msgin;
20
- // Ensure we don't exceed the size of the `microseconds` array
21
- for (size_t i = 0 ; i < 8 && i < propulsion_microseconds_msg->data .size ; i++) {
22
- microseconds[i] = propulsion_microseconds_msg->data .data [i]; // Access the data correctly
23
- }
24
- }
18
+ // Define the subscribers and publishers as rcl_publisher_t and rcl_subscription_t
19
+ rcl_subscription_t * propulsion_microseconds_subscriber;
20
+ rcl_publisher_t * power_thrusters_current_publisher;
21
+ rcl_publisher_t * power_batteries_voltage_publisher;
22
+ rcl_publisher_t * power_board_temprature_publisher;
23
+ rcl_publisher_t * power_teensy_temprature_publisher;
25
24
26
- Subscriber propulsion_microseconds_subscriber = micro_ros.createSubscriber(
27
- " /propulsion/microseconds" ,
28
- ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Int16MultiArray),
29
- propulsion_microseconds_callback);
30
-
31
25
std_msgs__msg__Int16MultiArray propulsion_microseconds_msg;
32
-
33
- Publisher power_thrusters_current_publisher = micro_ros.createPublisher(
34
- " /power/thrusters/current" ,
35
- ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32MultiArray));
36
-
37
26
std_msgs__msg__Float32MultiArray power_thrusters_current_msg;
38
-
39
- Publisher power_batteries_voltage_publisher = micro_ros.createPublisher(
40
- " /power/batteries/voltage" ,
41
- ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32MultiArray));
42
-
43
27
std_msgs__msg__Float32MultiArray power_batteries_voltage_msg;
44
-
45
- Publisher power_board_temprature_publisher = micro_ros.createPublisher(
46
- " /power/board/temperature" ,
47
- ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32));
48
-
49
28
std_msgs__msg__Float32 power_board_temperature_msg;
50
-
51
- Publisher power_teensy_temprature_publisher = micro_ros.createPublisher(
52
- " /power/teensy/temperature" ,
53
- ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32));
54
-
55
29
std_msgs__msg__Float32 power_teensy_temperature_msg;
56
30
57
31
TMP36 temperatureSensor (23 , 3.3 );
58
32
ADCSensors adcSensors;
59
33
34
+ void propulsion_microseconds_callback (const void *msgin) {
35
+ const std_msgs__msg__Int16MultiArray *propulsion_microseconds_msg = (const std_msgs__msg__Int16MultiArray *)msgin;
36
+ // Ensure we don't exceed the size of the `microseconds` array
37
+ for (size_t i = 0 ; i < 8 && i < propulsion_microseconds_msg->data .size ; i++) {
38
+ microseconds[i] = propulsion_microseconds_msg->data .data [i]; // Access the data correctly
39
+ }
40
+ }
41
+
60
42
void power_setup () {
61
43
pinMode (LED_PIN, OUTPUT);
62
44
digitalWrite (LED_PIN, HIGH);
@@ -65,43 +47,64 @@ void power_setup() {
65
47
66
48
// init all messages
67
49
power_board_temperature_msg.data = 0.0 ;
68
-
69
50
power_teensy_temperature_msg.data = 0.0 ;
70
-
71
51
power_thrusters_current_msg.data .data = (float *)malloc (8 * sizeof (float ));
72
- propulsion_microseconds_msg.data .size = 8 ;
73
- propulsion_microseconds_msg.data .capacity = 8 ;
74
-
75
52
power_batteries_voltage_msg.data .data = (float *)malloc (2 * sizeof (float ));
76
- power_batteries_voltage_msg.data .size = 2 ;
77
- power_batteries_voltage_msg.data .capacity = 2 ;
78
-
79
53
propulsion_microseconds_msg.data .data = (int16_t *)malloc (8 * sizeof (int16_t ));
54
+
55
+ power_thrusters_current_msg.data .size = 8 ;
56
+ power_batteries_voltage_msg.data .size = 2 ;
80
57
propulsion_microseconds_msg.data .size = 8 ;
81
- propulsion_microseconds_msg.data .capacity = 8 ;
82
58
83
59
temperatureSensor.begin ();
84
60
adcSensors.begin (true , true , &Wire1);
61
+
62
+ // Create subscribers and publishers using the MicroROSWrapper class
63
+ propulsion_microseconds_subscriber = micro_ros.createSubscriber (
64
+ " /propulsion/microseconds" ,
65
+ ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Int16MultiArray),
66
+ propulsion_microseconds_callback
67
+ );
68
+
69
+ power_thrusters_current_publisher = micro_ros.createPublisher (
70
+ " /power/thrusters/current" ,
71
+ ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32MultiArray)
72
+ );
73
+
74
+ power_batteries_voltage_publisher = micro_ros.createPublisher (
75
+ " /power/batteries/voltage" ,
76
+ ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32MultiArray)
77
+ );
78
+
79
+ power_board_temprature_publisher = micro_ros.createPublisher (
80
+ " /power/board/temperature" ,
81
+ ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32)
82
+ );
83
+
84
+ power_teensy_temprature_publisher = micro_ros.createPublisher (
85
+ " /power/teensy/temperature" ,
86
+ ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Float32)
87
+ );
85
88
}
86
89
87
90
void power_loop () {
88
91
power_board_temperature_msg.data = temperatureSensor.readTemperature ();
89
- power_board_temprature_publisher. publish (&power_board_temperature_msg);
92
+ micro_ros. publishData (&power_board_temperature_msg, *power_board_temprature_publisher); // Dereference pointer
90
93
91
94
power_teensy_temperature_msg.data = tempmonGetTemp ();
92
- power_teensy_temprature_publisher. publish (&power_teensy_temperature_msg);
95
+ micro_ros. publishData (&power_teensy_temperature_msg, *power_teensy_temprature_publisher); // Dereference pointer
93
96
94
97
float * current_data = adcSensors.senseCurrent ();
95
98
for (size_t i = 0 ; i < 8 ; i++) {
96
99
power_thrusters_current_msg.data .data [i] = current_data[i];
97
100
}
98
- power_thrusters_current_publisher. publish (&power_thrusters_current_msg);
101
+ micro_ros. publishData (&power_thrusters_current_msg, *power_thrusters_current_publisher); // Dereference pointer
99
102
100
103
float * voltage_data = adcSensors.senseVoltage ();
101
104
for (size_t i = 0 ; i < 2 ; i++) {
102
105
power_batteries_voltage_msg.data .data [i] = voltage_data[i];
103
106
}
104
- power_batteries_voltage_publisher. publish (&power_batteries_voltage_msg);
107
+ micro_ros. publishData (&power_batteries_voltage_msg, *power_batteries_voltage_publisher); // Dereference pointer
105
108
106
109
updateThrusters (microseconds);
107
110
0 commit comments