Skip to content

Commit cfd63c8

Browse files
committed
wrote microros code for thurster subscriber - to test
1 parent ee5c674 commit cfd63c8

File tree

3 files changed

+101
-66
lines changed

3 files changed

+101
-66
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "power_micro_ros.h"
2+
3+
void micro_ros_init() {
4+
set_microros_transports();
5+
6+
allocator = rcl_get_default_allocator();
7+
8+
// Create init_options
9+
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
10+
11+
// Create node
12+
RCCHECK(rclc_node_init_default(&node, "power_node", "", &support));
13+
14+
// Create subscriber
15+
RCCHECK(rclc_subscription_init_default(
16+
&propulsion_microseconds_subscriber,
17+
&node,
18+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray),
19+
"/propulsion/microseconds"));
20+
21+
propulsion_microseconds_msg.data.data = (int16_t *)malloc(8 * sizeof(int16_t));
22+
propulsion_microseconds_msg.data.size = 8;
23+
propulsion_microseconds_msg.data.capacity = 8;
24+
25+
// Create executor
26+
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
27+
RCCHECK(rclc_executor_add_subscription(&executor, &propulsion_microseconds_subscriber, &propulsion_microseconds_msg, &propulsion_microseconds_subscription_callback, ON_NEW_DATA));
28+
}
29+
30+
void spin_micro_ros() {
31+
RCCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
32+
}
33+
34+
void error_loop() {
35+
// Ensure msg.data.data is allocated before freeing
36+
if (propulsion_microseconds_msg.data.data != NULL) {
37+
free(propulsion_microseconds_msg.data.data);
38+
}
39+
40+
int error = 0;
41+
42+
while(error <= 20) {
43+
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
44+
delay(100);
45+
46+
error += 1;
47+
}
48+
49+
digitalWrite(LED_PIN, HIGH);
50+
}
51+
52+
void propulsion_microseconds_subscription_callback(const void * msgin) {
53+
const std_msgs__msg__Int16MultiArray * propulsion_microseconds_msg = (const std_msgs__msg__Int16MultiArray *)msgin;
54+
55+
// Ensure we don't exceed the size of the `microseconds` array
56+
for (size_t i = 0; i < 8 && i < propulsion_microseconds_msg->data.size; i++) {
57+
microseconds[i] = propulsion_microseconds_msg->data.data[i]; // Access the data correctly
58+
}
59+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <micro_ros_arduino.h>
2+
3+
#include <Arduino.h>
4+
5+
#include <rcl/rcl.h>
6+
#include <rcl/error_handling.h>
7+
#include <rclc/rclc.h>
8+
#include <rclc/executor.h>
9+
10+
#include <std_msgs/msg/int16_multi_array.h>
11+
12+
#define LED_PIN 13
13+
14+
rcl_subscription_t propulsion_microseconds_subscriber;
15+
std_msgs__msg__Int16MultiArray propulsion_microseconds_msg;
16+
rclc_executor_t executor;
17+
rclc_support_t support;
18+
rcl_allocator_t allocator;
19+
rcl_node_t node;
20+
rcl_timer_t timer;
21+
22+
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
23+
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
24+
25+
extern int16_t microseconds[8];
26+
27+
void micro_ros_init();
28+
void spin_micro_ros();
29+
void error_loop();
30+
31+
void propulsion_microseconds_subscription_callback(const void * msgin);

pio_workspace/src/power_main.cpp

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,25 @@
33
#include "TMP36.h"
44
#include "adc_sensors.h"
55
#include "ThrusterControl.h"
6+
#include "power_micro_ros.h"
67

7-
#define LED 13
8-
#define TEMP_SENSE 23
9-
#define THRUSTER_DELAY 1000 // Delay time in milliseconds
10-
11-
ADCSensors ADCs;
12-
TMP36 TEMPSENSOR(TEMP_SENSE, 3.3);
13-
14-
// A variable to track the current thruster being updated
15-
int currentThruster = 0;
8+
#define LED_PIN 13
169

1710
void power_setup() {
18-
// an interrupt would generally be setup for water detection
11+
pinMode(LED_PIN, OUTPUT);
12+
digitalWrite(LED_PIN, HIGH);
1913

20-
pinMode(LED, OUTPUT);
21-
digitalWrite(LED, HIGH);
2214
initThrusters();
2315

24-
Serial.begin(115200);
25-
Serial.println("Initializing ADCs...");
26-
if (!ADCs.begin(true, true, &Wire1)) {
27-
Serial.println("Failed to init ADCs");
28-
}
29-
30-
Serial.println("Initializing Temperature Sensor");
31-
TEMPSENSOR.begin();
16+
micro_ros_init();
17+
18+
delay(2000);
3219
}
3320

3421
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-
}
39-
40-
// Update the current thruster
41-
microseconds[currentThruster] = 1560;
4222
updateThrusters(microseconds);
4323

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-
}
57-
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-
}
70-
71-
// Print the temperature
72-
float temperature = TEMPSENSOR.readTemperature();
73-
Serial.print("Temperature: ");
74-
Serial.println(temperature);
75-
76-
// Delay after updating one thruster
77-
delay(THRUSTER_DELAY);
78-
79-
// Reset the current thruster to the off state (1500)
80-
microseconds[currentThruster] = 1500;
81-
updateThrusters(microseconds);
82-
}
24+
spin_micro_ros();
25+
26+
delay(10);
27+
}

0 commit comments

Comments
 (0)