@@ -35,24 +35,30 @@ rcl_timer_t timer;
35
35
#define RCSOFTCHECK (fn ) { rcl_ret_t temp_rc = fn; if ((temp_rc != RCL_RET_OK)){}}
36
36
37
37
38
- void error_loop (){
39
- free (msg.data .data );
38
+ void error_loop () {
39
+ // Ensure msg.data.data is allocated before freeing
40
+ if (msg.data .data != NULL ) {
41
+ free (msg.data .data );
42
+ }
40
43
41
- while (1 ){
44
+ int error = 0 ;
45
+
46
+ while (error <= 20 ) {
42
47
digitalWrite (LED_PIN, !digitalRead (LED_PIN));
43
48
delay (100 );
49
+
50
+ error += 1 ;
44
51
}
45
52
}
46
53
47
- // creates array of 8 thrusters
54
+ // Creates array of 8 thrusters
48
55
Servo thrusters[8 ];
49
56
50
- // signals to push to thrusters
57
+ // Signals to push to thrusters
51
58
int16_t microseconds[] = {1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 };
52
59
const int16_t offCommand[] = {1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 };
53
60
54
- void subscription_callback (const void * msgin)
55
- {
61
+ void subscription_callback (const void * msgin) {
56
62
const std_msgs__msg__Int16MultiArray * msg = (const std_msgs__msg__Int16MultiArray *)msgin;
57
63
58
64
// Ensure we don't exceed the size of the `microseconds` array
@@ -61,66 +67,64 @@ void subscription_callback(const void * msgin)
61
67
}
62
68
}
63
69
64
- // updates thrusters' pwm signals from array
70
+ // Updates thrusters' PWM signals from array
65
71
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 ]);
72
+ for (int i = 0 ; i < 8 ; i++) {
73
+ thrusters[i].writeMicroseconds (microseconds[i]);
74
+ }
74
75
}
75
76
76
77
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);
78
+ thrusters[0 ].attach (THRUSTER_1);
79
+ thrusters[1 ].attach (THRUSTER_2);
80
+ thrusters[2 ].attach (THRUSTER_3);
81
+ thrusters[3 ].attach (THRUSTER_4);
82
+ thrusters[4 ].attach (THRUSTER_5);
83
+ thrusters[5 ].attach (THRUSTER_6);
84
+ thrusters[6 ].attach (THRUSTER_7);
85
+ thrusters[7 ].attach (THRUSTER_8);
86
+
87
+ updateThrusters (offCommand);
87
88
}
88
89
89
90
void power_setup () {
90
- set_microros_transports ();
91
+ set_microros_transports ();
91
92
92
- pinMode (LED_PIN, OUTPUT);
93
- digitalWrite (LED_PIN, HIGH);
93
+ pinMode (LED_PIN, OUTPUT);
94
+ digitalWrite (LED_PIN, HIGH);
94
95
95
- initThrusters ();
96
+ initThrusters ();
96
97
97
- delay (2000 );
98
+ delay (2000 );
98
99
99
- allocator = rcl_get_default_allocator ();
100
+ allocator = rcl_get_default_allocator ();
100
101
101
- // create init_options
102
- RCCHECK (rclc_support_init (&support, 0 , NULL , &allocator));
102
+ // Create init_options
103
+ RCCHECK (rclc_support_init (&support, 0 , NULL , &allocator));
103
104
104
- // create node
105
- RCCHECK (rclc_node_init_default (&node, " power_node" , " " , &support));
105
+ // Create node
106
+ RCCHECK (rclc_node_init_default (&node, " power_node" , " " , &support));
106
107
107
- // create subscriber
108
- RCCHECK (rclc_subscription_init_default (
109
- &subscriber,
110
- &node,
111
- ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Int16MultiArray),
112
- " /propulsion/microseconds" ));
108
+ // Create subscriber
109
+ RCCHECK (rclc_subscription_init_default (
110
+ &subscriber,
111
+ &node,
112
+ ROSIDL_GET_MSG_TYPE_SUPPORT (std_msgs, msg, Int16MultiArray),
113
+ " /propulsion/microseconds" ));
113
114
114
115
msg.data .data = (int16_t *)malloc (8 * sizeof (int16_t ));
115
116
msg.data .size = 8 ;
116
117
msg.data .capacity = 8 ;
117
118
118
- // create executor
119
- RCCHECK (rclc_executor_init (&executor, &support.context , 1 , &allocator));
120
- RCCHECK (rclc_executor_add_subscription (&executor, &subscriber, &msg, &subscription_callback, ON_NEW_DATA));
119
+ // Create executor
120
+ RCCHECK (rclc_executor_init (&executor, &support.context , 1 , &allocator));
121
+ RCCHECK (rclc_executor_add_subscription (&executor, &subscriber, &msg, &subscription_callback, ON_NEW_DATA));
121
122
}
122
123
123
124
void power_loop () {
124
- updateThrusters (microseconds);
125
- RCCHECK (rclc_executor_spin_some (&executor, RCL_MS_TO_NS (100 )));
125
+ updateThrusters (microseconds);
126
+ RCCHECK (rclc_executor_spin_some (&executor, RCL_MS_TO_NS (100 )));
127
+
128
+ // Optional: Add a small delay to prevent high-frequency looping
129
+ delay (10 );
126
130
}
0 commit comments