|
1 | 1 | #include "power_main.h"
|
2 | 2 |
|
3 |
| -#include <Servo.h> |
4 |
| - |
5 |
| -#include <micro_ros_arduino.h> |
6 |
| - |
7 |
| -#include <stdio.h> |
8 |
| -#include <rcl/rcl.h> |
9 |
| -#include <rcl/error_handling.h> |
10 |
| -#include <rclc/rclc.h> |
11 |
| -#include <rclc/executor.h> |
12 |
| - |
13 |
| -#include <std_msgs/msg/int16_multi_array.h> |
14 |
| - |
15 |
| -#define THRUSTER_1 2 |
16 |
| -#define THRUSTER_2 3 |
17 |
| -#define THRUSTER_3 4 |
18 |
| -#define THRUSTER_4 5 |
19 |
| -#define THRUSTER_5 6 |
20 |
| -#define THRUSTER_6 7 |
21 |
| -#define THRUSTER_7 8 |
22 |
| -#define THRUSTER_8 9 |
23 |
| - |
24 |
| -rcl_subscription_t subscriber; |
25 |
| -std_msgs__msg__Int16MultiArray msg; |
26 |
| -rclc_executor_t executor; |
27 |
| -rclc_support_t support; |
28 |
| -rcl_allocator_t allocator; |
29 |
| -rcl_node_t node; |
30 |
| -rcl_timer_t timer; |
31 |
| - |
32 |
| -#define LED_PIN 13 |
33 |
| - |
34 |
| -#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}} |
35 |
| -#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}} |
36 |
| - |
37 |
| - |
38 |
| -void error_loop() { |
39 |
| - // Ensure msg.data.data is allocated before freeing |
40 |
| - if (msg.data.data != NULL) { |
41 |
| - free(msg.data.data); |
42 |
| - } |
43 |
| - |
44 |
| - int error = 0; |
45 |
| - |
46 |
| - while(error <= 20) { |
47 |
| - digitalWrite(LED_PIN, !digitalRead(LED_PIN)); |
48 |
| - delay(100); |
49 |
| - |
50 |
| - error += 1; |
51 |
| - } |
52 |
| -} |
| 3 | +#include "TMP36.h" |
| 4 | +#include "adc_sensors.h" |
| 5 | +#include "ThrusterControl.h" |
53 | 6 |
|
54 |
| -// Creates array of 8 thrusters |
55 |
| -Servo thrusters[8]; |
| 7 | +#define LED 13 |
| 8 | +#define TEMP_SENSE 23 |
| 9 | +#define THRUSTER_DELAY 1000 // Delay time in milliseconds |
56 | 10 |
|
57 |
| -// Signals to push to thrusters |
58 |
| -int16_t microseconds[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500}; |
59 |
| -const int16_t offCommand[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500}; |
| 11 | +ADCSensors ADCs; |
| 12 | +TMP36 TEMPSENSOR(TEMP_SENSE, 3.3); |
60 | 13 |
|
61 |
| -void subscription_callback(const void * msgin) { |
62 |
| - const std_msgs__msg__Int16MultiArray * msg = (const std_msgs__msg__Int16MultiArray *)msgin; |
| 14 | +// A variable to track the current thruster being updated |
| 15 | +int currentThruster = 0; |
63 | 16 |
|
64 |
| - // Ensure we don't exceed the size of the `microseconds` array |
65 |
| - for (size_t i = 0; i < 8 && i < msg->data.size; i++) { |
66 |
| - microseconds[i] = msg->data.data[i]; // Access the data correctly |
67 |
| - } |
68 |
| -} |
| 17 | +void power_setup() { |
| 18 | + // an interrupt would generally be setup for water detection |
69 | 19 |
|
70 |
| -// Updates thrusters' PWM signals from array |
71 |
| -void updateThrusters(const int16_t microseconds[8]) { |
72 |
| - for (int i = 0; i < 8; i++) { |
73 |
| - thrusters[i].writeMicroseconds(microseconds[i]); |
| 20 | + pinMode(LED, OUTPUT); |
| 21 | + digitalWrite(LED, HIGH); |
| 22 | + initThrusters(); |
| 23 | + |
| 24 | + Serial.begin(115200); |
| 25 | + Serial.println("Initializing ADCs..."); |
| 26 | + if (!ADCs.begin(true, true, &Wire1)) { |
| 27 | + Serial.println("Failed to init ADCs"); |
74 | 28 | }
|
75 |
| -} |
76 | 29 |
|
77 |
| -void initThrusters() { |
78 |
| - thrusters[0].attach(THRUSTER_1); |
79 |
| - thrusters[1].attach(THRUSTER_2); |
80 |
| - thrusters[2].attach(THRUSTER_3); |
81 |
| - thrusters[3].attach(THRUSTER_4); |
82 |
| - thrusters[4].attach(THRUSTER_5); |
83 |
| - thrusters[5].attach(THRUSTER_6); |
84 |
| - thrusters[6].attach(THRUSTER_7); |
85 |
| - thrusters[7].attach(THRUSTER_8); |
86 |
| - |
87 |
| - updateThrusters(offCommand); |
| 30 | + Serial.println("Initializing Temperature Sensor"); |
| 31 | + TEMPSENSOR.begin(); |
88 | 32 | }
|
89 | 33 |
|
90 |
| -void power_setup() { |
91 |
| - set_microros_transports(); |
92 |
| - |
93 |
| - pinMode(LED_PIN, OUTPUT); |
94 |
| - digitalWrite(LED_PIN, HIGH); |
95 |
| - |
96 |
| - initThrusters(); |
97 |
| - |
98 |
| - delay(2000); |
99 |
| - |
100 |
| - allocator = rcl_get_default_allocator(); |
| 34 | +void power_loop() { |
| 35 | + // Reset the microseconds array to default value (1500) for all thrusters |
| 36 | + for (int i = 0; i < 8; i++) { |
| 37 | + microseconds[i] = 1500; |
| 38 | + } |
101 | 39 |
|
102 |
| - // Create init_options |
103 |
| - RCCHECK(rclc_support_init(&support, 0, NULL, &allocator)); |
| 40 | + // Update the current thruster |
| 41 | + microseconds[currentThruster] = 1560; |
| 42 | + updateThrusters(microseconds); |
104 | 43 |
|
105 |
| - // Create node |
106 |
| - RCCHECK(rclc_node_init_default(&node, "power_node", "", &support)); |
| 44 | + // Increment to the next thruster for the next loop |
| 45 | + currentThruster = (currentThruster + 1) % 8; |
| 46 | + |
| 47 | + // Print the voltages, currents, and temperature |
| 48 | + float* voltages = ADCs.senseVoltage(); |
| 49 | + if (voltages) { |
| 50 | + Serial.print("Voltage 1: "); |
| 51 | + Serial.println(voltages[0]); |
| 52 | + Serial.print("Voltage 2: "); |
| 53 | + Serial.println(voltages[1]); |
| 54 | + } else { |
| 55 | + Serial.println("Failed to read voltages"); |
| 56 | + } |
107 | 57 |
|
108 |
| - // Create subscriber |
109 |
| - RCCHECK(rclc_subscription_init_default( |
110 |
| - &subscriber, |
111 |
| - &node, |
112 |
| - ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray), |
113 |
| - "/propulsion/microseconds")); |
| 58 | + // Print the currents |
| 59 | + float* currents = ADCs.senseCurrent(); |
| 60 | + if (currents) { |
| 61 | + for (int i = 0; i < 8; i++) { |
| 62 | + Serial.print("Current "); |
| 63 | + Serial.print(i + 1); |
| 64 | + Serial.print(": "); |
| 65 | + Serial.println(currents[i]); |
| 66 | + } |
| 67 | + } else { |
| 68 | + Serial.println("Failed to read currents"); |
| 69 | + } |
114 | 70 |
|
115 |
| - msg.data.data = (int16_t *)malloc(8 * sizeof(int16_t)); |
116 |
| - msg.data.size = 8; |
117 |
| - msg.data.capacity = 8; |
| 71 | + // Print the temperature |
| 72 | + float temperature = TEMPSENSOR.readTemperature(); |
| 73 | + Serial.print("Temperature: "); |
| 74 | + Serial.println(temperature); |
118 | 75 |
|
119 |
| - // Create executor |
120 |
| - RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator)); |
121 |
| - RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &msg, &subscription_callback, ON_NEW_DATA)); |
122 |
| -} |
| 76 | + // Delay after updating one thruster |
| 77 | + delay(THRUSTER_DELAY); |
123 | 78 |
|
124 |
| -void power_loop() { |
| 79 | + // Reset the current thruster to the off state (1500) |
| 80 | + microseconds[currentThruster] = 1500; |
125 | 81 | updateThrusters(microseconds);
|
126 |
| - RCCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100))); |
127 |
| - |
128 |
| - // Optional: Add a small delay to prevent high-frequency looping |
129 |
| - delay(10); |
130 | 82 | }
|
0 commit comments