Skip to content

Commit 8415bd6

Browse files
committed
microros power board code compiles
1 parent 9a9ffc6 commit 8415bd6

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

pio_workspace/src/power_main.cpp

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "power_main.h"
22

3+
#include <Servo.h>
4+
35
#include <micro_ros_arduino.h>
46

57
#include <stdio.h>
@@ -8,10 +10,19 @@
810
#include <rclc/rclc.h>
911
#include <rclc/executor.h>
1012

11-
#include <std_msgs/msg/int32.h>
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
1223

1324
rcl_subscription_t subscriber;
14-
std_msgs__msg__Int32 msg;
25+
std_msgs__msg__Int16MultiArray msg;
1526
rclc_executor_t executor;
1627
rclc_support_t support;
1728
rcl_allocator_t allocator;
@@ -25,23 +36,63 @@ rcl_timer_t timer;
2536

2637

2738
void error_loop(){
39+
free(msg.data.data);
40+
2841
while(1){
2942
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
3043
delay(100);
3144
}
3245
}
3346

47+
// creates array of 8 thrusters
48+
Servo thrusters[8];
49+
50+
// signals to push to thrusters
51+
int16_t microseconds[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
52+
const int16_t offCommand[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
53+
3454
void subscription_callback(const void * msgin)
35-
{
36-
const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin;
37-
digitalWrite(LED_PIN, (msg->data == 0) ? LOW : HIGH);
55+
{
56+
const std_msgs__msg__Int16MultiArray * msg = (const std_msgs__msg__Int16MultiArray *)msgin;
57+
58+
// Ensure we don't exceed the size of the `microseconds` array
59+
for (size_t i = 0; i < 8 && i < msg->data.size; i++) {
60+
microseconds[i] = msg->data.data[i]; // Access the data correctly
61+
}
62+
}
63+
64+
// updates thrusters' pwm signals from array
65+
void updateThrusters(const int16_t microseconds[8]) {
66+
thrusters[0].writeMicroseconds(microseconds[0]);
67+
thrusters[1].writeMicroseconds(microseconds[1]);
68+
thrusters[2].writeMicroseconds(microseconds[2]);
69+
thrusters[3].writeMicroseconds(microseconds[3]);
70+
thrusters[4].writeMicroseconds(microseconds[4]);
71+
thrusters[5].writeMicroseconds(microseconds[5]);
72+
thrusters[6].writeMicroseconds(microseconds[6]);
73+
thrusters[7].writeMicroseconds(microseconds[7]);
74+
}
75+
76+
void initThrusters() {
77+
thrusters[0].attach(THRUSTER_1);
78+
thrusters[1].attach(THRUSTER_2);
79+
thrusters[2].attach(THRUSTER_3);
80+
thrusters[3].attach(THRUSTER_4);
81+
thrusters[4].attach(THRUSTER_5);
82+
thrusters[5].attach(THRUSTER_6);
83+
thrusters[6].attach(THRUSTER_7);
84+
thrusters[7].attach(THRUSTER_8);
85+
86+
updateThrusters(offCommand);
3887
}
3988

4089
void power_setup() {
4190
set_microros_transports();
4291

4392
pinMode(LED_PIN, OUTPUT);
44-
digitalWrite(LED_PIN, HIGH);
93+
digitalWrite(LED_PIN, HIGH);
94+
95+
initThrusters();
4596

4697
delay(2000);
4798

@@ -51,21 +102,25 @@ void power_setup() {
51102
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
52103

53104
// create node
54-
RCCHECK(rclc_node_init_default(&node, "micro_ros_arduino_node", "", &support));
105+
RCCHECK(rclc_node_init_default(&node, "power_node", "", &support));
55106

56107
// create subscriber
57108
RCCHECK(rclc_subscription_init_default(
58109
&subscriber,
59110
&node,
60-
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
61-
"micro_ros_arduino_subscriber"));
111+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray),
112+
"/propulsion/microseconds"));
113+
114+
msg.data.data = (int16_t *)malloc(8 * sizeof(int16_t));
115+
msg.data.size = 8;
116+
msg.data.capacity = 8;
62117

63118
// create executor
64119
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
65120
RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &msg, &subscription_callback, ON_NEW_DATA));
66121
}
67122

68123
void power_loop() {
69-
delay(100);
124+
updateThrusters(microseconds);
70125
RCCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
71126
}

0 commit comments

Comments
 (0)