3
3
#include " power_main.h"
4
4
5
5
#include < Arduino.h>
6
+
7
+ #include " ThrusterControl.h"
8
+ #include " adc_sensors.h"
9
+ #include " TMP36.h"
10
+
6
11
#include < micro_ros_arduino.h>
7
12
8
13
#include < stdio.h>
12
17
#include < rclc/executor.h>
13
18
#include < rmw_microros/rmw_microros.h>
14
19
15
- #include < std_msgs/msg/int32.h>
16
20
#include < std_msgs/msg/int16_multi_array.h>
17
21
#include < std_msgs/msg/float32.h>
18
22
#include < std_msgs/msg/float32_multi_array.h>
19
23
20
24
#define LED_PIN 13
21
25
26
+
27
+ #define ENABLE_VOLTAGE_SENSE false
28
+ #define ENABLE_CURRENT_SENSE false
29
+
30
+ ADCSensors adcSensors;
31
+ TMP36 temperatureSensor (23 , 3.3 );
32
+
22
33
bool micro_ros_init_successful;
23
34
24
35
rcl_subscription_t propulsion_microseconds_subscriber;
@@ -72,16 +83,9 @@ void propulsion_microseconds_callback(const void * msgin)
72
83
{
73
84
const std_msgs__msg__Int16MultiArray * msg = (const std_msgs__msg__Int16MultiArray *)msgin;
74
85
75
- digitalWrite (2 , msg->data .data [0 ] == 0 ? LOW : HIGH);
76
- digitalWrite (3 , msg->data .data [1 ] == 0 ? LOW : HIGH);
77
- digitalWrite (4 , msg->data .data [2 ] == 0 ? LOW : HIGH);
78
- digitalWrite (5 , msg->data .data [3 ] == 0 ? LOW : HIGH);
79
- digitalWrite (6 , msg->data .data [4 ] == 0 ? LOW : HIGH);
80
- digitalWrite (7 , msg->data .data [5 ] == 0 ? LOW : HIGH);
81
- digitalWrite (8 , msg->data .data [6 ] == 0 ? LOW : HIGH);
82
- digitalWrite (9 , msg->data .data [7 ] == 0 ? LOW : HIGH);
83
-
84
- // logic can go here
86
+ for (int i = 0 ; i < 8 ; i++) {
87
+ microseconds[i] = msg->data .data [i];
88
+ }
85
89
}
86
90
87
91
void timer_callback (rcl_timer_t * timer, int64_t last_call_time) {
@@ -91,13 +95,10 @@ void timer_callback(rcl_timer_t * timer, int64_t last_call_time) {
91
95
RCSOFTCHECK (rcl_publish (&power_thrusters_current_publisher, &power_thrusters_current_msg, NULL ));
92
96
RCSOFTCHECK (rcl_publish (&power_board_temperature_publisher, &power_board_temperature_msg, NULL ));
93
97
RCSOFTCHECK (rcl_publish (&power_teensy_temperature_publisher, &power_teensy_temperature_msg, NULL ));
94
-
95
- // can add logic to messages here
96
98
}
97
99
}
98
100
99
- bool create_entities ()
100
- {
101
+ bool create_entities () {
101
102
allocator = rcl_get_default_allocator ();
102
103
103
104
// create init_options
@@ -162,10 +163,10 @@ void connectUSB() {
162
163
USB1_USBCMD = 1 ;
163
164
}
164
165
165
- void destroy_entities ()
166
- {
166
+ void destroy_entities () {
167
167
disconnectUSB ();
168
168
delay (25 );
169
+ updateThrusters (offCommand);
169
170
170
171
rmw_context_t * rmw_context = rcl_context_get_rmw_context (&support.context );
171
172
(void ) rmw_uros_set_context_entity_destroy_session_timeout (rmw_context, 0 );
@@ -184,18 +185,30 @@ void destroy_entities()
184
185
connectUSB ();
185
186
}
186
187
188
+ void senseData () {
189
+ power_board_temperature_msg.data = temperatureSensor.readTemperature ();
190
+
191
+ power_teensy_temperature_msg.data = tempmonGetTemp ();
192
+
193
+ float * current_data = adcSensors.senseCurrent ();
194
+ for (size_t i = 0 ; i < 8 ; i++) {
195
+ power_thrusters_current_msg.data .data [i] = current_data[i];
196
+ }
197
+
198
+ float * voltage_data = adcSensors.senseVoltage ();
199
+ for (size_t i = 0 ; i < 2 ; i++) {
200
+ power_batteries_voltage_msg.data .data [i] = voltage_data[i];
201
+ }
202
+ }
203
+
187
204
void power_setup () {
188
- pinMode (13 , OUTPUT);
189
- digitalWrite (13 , HIGH);
190
-
191
- pinMode (2 , OUTPUT);
192
- pinMode (3 , OUTPUT);
193
- pinMode (4 , OUTPUT);
194
- pinMode (5 , OUTPUT);
195
- pinMode (6 , OUTPUT);
196
- pinMode (7 , OUTPUT);
197
- pinMode (8 , OUTPUT);
198
- pinMode (9 , OUTPUT);
205
+ pinMode (LED_PIN, OUTPUT);
206
+ digitalWrite (LED_PIN, HIGH);
207
+
208
+ initThrusters ();
209
+
210
+ adcSensors.begin (ENABLE_VOLTAGE_SENSE, ENABLE_CURRENT_SENSE, &Wire1);
211
+ temperatureSensor.begin ();
199
212
200
213
// Configure serial transport
201
214
Serial.begin (115200 );
@@ -248,6 +261,9 @@ void power_setup() {
248
261
}
249
262
250
263
void power_loop () {
264
+ senseData ();
265
+ updateThrusters (microseconds);
266
+
251
267
switch (state) {
252
268
case WAITING_AGENT:
253
269
EXECUTE_EVERY_N_MS (500 , state = (RMW_RET_OK == rmw_uros_ping_agent (100 , 1 )) ? AGENT_AVAILABLE : WAITING_AGENT;);
0 commit comments