1
1
#include " power_main.h"
2
2
3
+ #include < Servo.h>
4
+
3
5
#include < micro_ros_arduino.h>
4
6
5
7
#include < stdio.h>
8
10
#include < rclc/rclc.h>
9
11
#include < rclc/executor.h>
10
12
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
12
23
13
24
rcl_subscription_t subscriber;
14
- std_msgs__msg__Int32 msg;
25
+ std_msgs__msg__Int16MultiArray msg;
15
26
rclc_executor_t executor;
16
27
rclc_support_t support;
17
28
rcl_allocator_t allocator;
@@ -25,23 +36,63 @@ rcl_timer_t timer;
25
36
26
37
27
38
void error_loop (){
39
+ free (msg.data .data );
40
+
28
41
while (1 ){
29
42
digitalWrite (LED_PIN, !digitalRead (LED_PIN));
30
43
delay (100 );
31
44
}
32
45
}
33
46
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
+
34
54
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);
38
87
}
39
88
40
89
void power_setup () {
41
90
set_microros_transports ();
42
91
43
92
pinMode (LED_PIN, OUTPUT);
44
- digitalWrite (LED_PIN, HIGH);
93
+ digitalWrite (LED_PIN, HIGH);
94
+
95
+ initThrusters ();
45
96
46
97
delay (2000 );
47
98
@@ -51,21 +102,25 @@ void power_setup() {
51
102
RCCHECK (rclc_support_init (&support, 0 , NULL , &allocator));
52
103
53
104
// create node
54
- RCCHECK (rclc_node_init_default (&node, " micro_ros_arduino_node " , " " , &support));
105
+ RCCHECK (rclc_node_init_default (&node, " power_node " , " " , &support));
55
106
56
107
// create subscriber
57
108
RCCHECK (rclc_subscription_init_default (
58
109
&subscriber,
59
110
&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 ;
62
117
63
118
// create executor
64
119
RCCHECK (rclc_executor_init (&executor, &support.context , 1 , &allocator));
65
120
RCCHECK (rclc_executor_add_subscription (&executor, &subscriber, &msg, &subscription_callback, ON_NEW_DATA));
66
121
}
67
122
68
123
void power_loop () {
69
- delay ( 100 );
124
+ updateThrusters (microseconds );
70
125
RCCHECK (rclc_executor_spin_some (&executor, RCL_MS_TO_NS (100 )));
71
126
}
0 commit comments