|
13 | 13 |
|
14 | 14 | #define LED_PIN 13
|
15 | 15 |
|
16 |
| -MicroROSWrapper micro_ros("power_node"); |
| 16 | +#define ENABLE_VOLTAGE_SENSE false |
| 17 | +#define ENABLE_CURRENT_SENSE false |
| 18 | + |
| 19 | +MicroROSWrapper micro_ros; |
| 20 | +ADCSensors adcSensors; |
| 21 | +TMP36 temperatureSensor(23, 3.3); |
17 | 22 |
|
18 | 23 | // Define the subscribers and publishers as rcl_publisher_t and rcl_subscription_t
|
19 | 24 | rcl_subscription_t* propulsion_microseconds_subscriber;
|
20 | 25 | rcl_publisher_t* power_thrusters_current_publisher;
|
21 | 26 | rcl_publisher_t* power_batteries_voltage_publisher;
|
22 |
| -rcl_publisher_t* power_board_temprature_publisher; |
23 |
| -rcl_publisher_t* power_teensy_temprature_publisher; |
| 27 | +rcl_publisher_t* power_board_temperature_publisher; |
| 28 | +rcl_publisher_t* power_teensy_temperature_publisher; |
24 | 29 |
|
25 | 30 | std_msgs__msg__Int16MultiArray propulsion_microseconds_msg;
|
26 | 31 | std_msgs__msg__Float32MultiArray power_thrusters_current_msg;
|
27 | 32 | std_msgs__msg__Float32MultiArray power_batteries_voltage_msg;
|
28 | 33 | std_msgs__msg__Float32 power_board_temperature_msg;
|
29 | 34 | std_msgs__msg__Float32 power_teensy_temperature_msg;
|
30 | 35 |
|
31 |
| -TMP36 temperatureSensor(23, 3.3); |
32 |
| -ADCSensors adcSensors; |
33 |
| - |
34 | 36 | 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 |
| - } |
| 37 | + const std_msgs__msg__Int16MultiArray *propulsion_microseconds_msg = (const std_msgs__msg__Int16MultiArray *)msgin; |
| 38 | + for (size_t i = 0; i < 8; i++) { |
| 39 | + microseconds[i] = propulsion_microseconds_msg->data.data[i]; // Access the data correctly |
| 40 | + } |
40 | 41 | }
|
41 | 42 |
|
42 |
| -void power_setup() { |
43 |
| - pinMode(LED_PIN, OUTPUT); |
44 |
| - digitalWrite(LED_PIN, HIGH); |
45 |
| - |
46 |
| - initThrusters(); |
47 |
| - |
48 |
| - // init all messages |
49 |
| - power_board_temperature_msg.data = 0.0; |
50 |
| - power_teensy_temperature_msg.data = 0.0; |
51 |
| - power_thrusters_current_msg.data.data = (float *)malloc(8 * sizeof(float)); |
52 |
| - power_batteries_voltage_msg.data.data = (float *)malloc(2 * sizeof(float)); |
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; |
57 |
| - propulsion_microseconds_msg.data.size = 8; |
58 |
| - |
59 |
| - temperatureSensor.begin(); |
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 |
| - ); |
| 43 | +bool create_entities() { |
| 44 | + micro_ros.init("power_node"); |
| 45 | + |
| 46 | + // Create subscribers and publishers using the MicroROSWrapper class |
| 47 | + propulsion_microseconds_subscriber = micro_ros.createSubscriber( |
| 48 | + "/propulsion/microseconds", |
| 49 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray), |
| 50 | + propulsion_microseconds_callback |
| 51 | + ); |
| 52 | + |
| 53 | + power_thrusters_current_publisher = micro_ros.createPublisher( |
| 54 | + "/power/thrusters/current", |
| 55 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32MultiArray) |
| 56 | + ); |
| 57 | + |
| 58 | + power_batteries_voltage_publisher = micro_ros.createPublisher( |
| 59 | + "/power/batteries/voltage", |
| 60 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32MultiArray) |
| 61 | + ); |
| 62 | + |
| 63 | + power_board_temperature_publisher = micro_ros.createPublisher( |
| 64 | + "/power/board/temperature", |
| 65 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32) |
| 66 | + ); |
| 67 | + |
| 68 | + power_teensy_temperature_publisher = micro_ros.createPublisher( |
| 69 | + "/power/teensy/temperature", |
| 70 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32) |
| 71 | + ); |
| 72 | + return true; |
88 | 73 | }
|
89 | 74 |
|
90 |
| -void power_loop() { |
91 |
| - power_board_temperature_msg.data = temperatureSensor.readTemperature(); |
92 |
| - micro_ros.publishData(&power_board_temperature_msg, *power_board_temprature_publisher); // Dereference pointer |
| 75 | +void publishAndSubscribe() { |
| 76 | + power_board_temperature_msg.data = temperatureSensor.readTemperature(); |
| 77 | + micro_ros.publishData(&power_board_temperature_msg, *power_board_temperature_publisher); // Dereference pointer |
93 | 78 |
|
94 |
| - power_teensy_temperature_msg.data = tempmonGetTemp(); |
95 |
| - micro_ros.publishData(&power_teensy_temperature_msg, *power_teensy_temprature_publisher); // Dereference pointer |
| 79 | + power_teensy_temperature_msg.data = tempmonGetTemp(); |
| 80 | + micro_ros.publishData(&power_teensy_temperature_msg, *power_teensy_temperature_publisher); // Dereference pointer |
96 | 81 |
|
97 |
| - float* current_data = adcSensors.senseCurrent(); |
98 |
| - for (size_t i = 0; i < 8; i++) { |
99 |
| - power_thrusters_current_msg.data.data[i] = current_data[i]; |
100 |
| - } |
101 |
| - micro_ros.publishData(&power_thrusters_current_msg, *power_thrusters_current_publisher); // Dereference pointer |
| 82 | + float* current_data = adcSensors.senseCurrent(); |
| 83 | + for (size_t i = 0; i < 8; i++) { |
| 84 | + power_thrusters_current_msg.data.data[i] = current_data[i]; |
| 85 | + } |
| 86 | + micro_ros.publishData(&power_thrusters_current_msg, *power_thrusters_current_publisher); // Dereference pointer |
102 | 87 |
|
103 |
| - float* voltage_data = adcSensors.senseVoltage(); |
104 |
| - for (size_t i = 0; i < 2; i++) { |
105 |
| - power_batteries_voltage_msg.data.data[i] = voltage_data[i]; |
106 |
| - } |
107 |
| - micro_ros.publishData(&power_batteries_voltage_msg, *power_batteries_voltage_publisher); // Dereference pointer |
| 88 | + float* voltage_data = adcSensors.senseVoltage(); |
| 89 | + for (size_t i = 0; i < 2; i++) { |
| 90 | + power_batteries_voltage_msg.data.data[i] = voltage_data[i]; |
| 91 | + } |
| 92 | + micro_ros.publishData(&power_batteries_voltage_msg, *power_batteries_voltage_publisher); // Dereference pointer |
| 93 | +} |
108 | 94 |
|
109 |
| - updateThrusters(microseconds); |
| 95 | +void power_setup() { |
| 96 | + pinMode(LED_PIN, OUTPUT); |
| 97 | + digitalWrite(LED_PIN, HIGH); |
| 98 | + |
| 99 | + initThrusters(); |
| 100 | + |
| 101 | + adcSensors.begin(ENABLE_VOLTAGE_SENSE, ENABLE_CURRENT_SENSE, &Wire1); |
| 102 | + temperatureSensor.begin(); |
| 103 | + |
| 104 | + // init all messages |
| 105 | + power_board_temperature_msg.data = 0.0; |
| 106 | + power_teensy_temperature_msg.data = 0.0; |
| 107 | + power_thrusters_current_msg.data.data = (float *)malloc(8 * sizeof(float)); |
| 108 | + power_batteries_voltage_msg.data.data = (float *)malloc(2 * sizeof(float)); |
| 109 | + propulsion_microseconds_msg.data.data = (int16_t *)malloc(8 * sizeof(int16_t)); |
| 110 | + |
| 111 | + power_thrusters_current_msg.data.size = 8; |
| 112 | + power_batteries_voltage_msg.data.size = 2; |
| 113 | + propulsion_microseconds_msg.data.size = 8; |
| 114 | + |
| 115 | + create_entities(); |
| 116 | +} |
110 | 117 |
|
| 118 | +void power_loop() { |
| 119 | + if (micro_ros.pingAgent()) { |
| 120 | + publishAndSubscribe(); |
111 | 121 | micro_ros.spin();
|
| 122 | + updateThrusters(microseconds); |
| 123 | + |
| 124 | + } else { |
| 125 | + updateThrusters(offCommand); |
| 126 | + micro_ros.destroyMicroROS(); |
| 127 | + |
| 128 | + while (micro_ros.pingAgent()) { |
| 129 | + |
| 130 | + } |
| 131 | + create_entities(); |
| 132 | + } |
| 133 | + |
| 134 | + delay(10); |
112 | 135 | }
|
0 commit comments