Skip to content

Commit f8c476e

Browse files
committed
implemented power board logic, to be tested with auv
1 parent d186ce8 commit f8c476e

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

pio_workspace/src/power_main.cpp

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include "power_main.h"
44

55
#include <Arduino.h>
6+
7+
#include "ThrusterControl.h"
8+
#include "adc_sensors.h"
9+
#include "TMP36.h"
10+
611
#include <micro_ros_arduino.h>
712

813
#include <stdio.h>
@@ -12,13 +17,19 @@
1217
#include <rclc/executor.h>
1318
#include <rmw_microros/rmw_microros.h>
1419

15-
#include <std_msgs/msg/int32.h>
1620
#include <std_msgs/msg/int16_multi_array.h>
1721
#include <std_msgs/msg/float32.h>
1822
#include <std_msgs/msg/float32_multi_array.h>
1923

2024
#define LED_PIN 13
2125

26+
27+
#define ENABLE_VOLTAGE_SENSE false
28+
#define ENABLE_CURRENT_SENSE false
29+
30+
ADCSensors adcSensors;
31+
TMP36 temperatureSensor(23, 3.3);
32+
2233
bool micro_ros_init_successful;
2334

2435
rcl_subscription_t propulsion_microseconds_subscriber;
@@ -72,16 +83,9 @@ void propulsion_microseconds_callback(const void * msgin)
7283
{
7384
const std_msgs__msg__Int16MultiArray * msg = (const std_msgs__msg__Int16MultiArray *)msgin;
7485

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+
}
8589
}
8690

8791
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) {
9195
RCSOFTCHECK(rcl_publish(&power_thrusters_current_publisher, &power_thrusters_current_msg, NULL));
9296
RCSOFTCHECK(rcl_publish(&power_board_temperature_publisher, &power_board_temperature_msg, NULL));
9397
RCSOFTCHECK(rcl_publish(&power_teensy_temperature_publisher, &power_teensy_temperature_msg, NULL));
94-
95-
// can add logic to messages here
9698
}
9799
}
98100

99-
bool create_entities()
100-
{
101+
bool create_entities() {
101102
allocator = rcl_get_default_allocator();
102103

103104
// create init_options
@@ -162,10 +163,10 @@ void connectUSB() {
162163
USB1_USBCMD = 1;
163164
}
164165

165-
void destroy_entities()
166-
{
166+
void destroy_entities() {
167167
disconnectUSB();
168168
delay(25);
169+
updateThrusters(offCommand);
169170

170171
rmw_context_t * rmw_context = rcl_context_get_rmw_context(&support.context);
171172
(void) rmw_uros_set_context_entity_destroy_session_timeout(rmw_context, 0);
@@ -184,18 +185,30 @@ void destroy_entities()
184185
connectUSB();
185186
}
186187

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+
187204
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();
199212

200213
// Configure serial transport
201214
Serial.begin(115200);
@@ -248,6 +261,9 @@ void power_setup() {
248261
}
249262

250263
void power_loop() {
264+
senseData();
265+
updateThrusters(microseconds);
266+
251267
switch (state) {
252268
case WAITING_AGENT:
253269
EXECUTE_EVERY_N_MS(500, state = (RMW_RET_OK == rmw_uros_ping_agent(100, 1)) ? AGENT_AVAILABLE : WAITING_AGENT;);

0 commit comments

Comments
 (0)