Skip to content

Commit 171471d

Browse files
committed
code works with jazzy
1 parent 6f57db9 commit 171471d

File tree

2 files changed

+56
-48
lines changed

2 files changed

+56
-48
lines changed

pio_workspace/src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ void setup() {
1212
#ifdef ACTUATOR_H
1313
actuator_setup();
1414
#elif DISPLAY_H
15-
display_setup
15+
display_setup();
16+
#elif POWER_H
17+
power_setup();
18+
#endif
19+
}
1620

1721
void loop() {
1822
#ifdef ACTUATOR_H

pio_workspace/src/power_main.cpp

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,30 @@ rcl_timer_t timer;
3535
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
3636

3737

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+
}
4043

41-
while(1){
44+
int error = 0;
45+
46+
while(error <= 20) {
4247
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
4348
delay(100);
49+
50+
error += 1;
4451
}
4552
}
4653

47-
// creates array of 8 thrusters
54+
// Creates array of 8 thrusters
4855
Servo thrusters[8];
4956

50-
// signals to push to thrusters
57+
// Signals to push to thrusters
5158
int16_t microseconds[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
5259
const int16_t offCommand[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
5360

54-
void subscription_callback(const void * msgin)
55-
{
61+
void subscription_callback(const void * msgin) {
5662
const std_msgs__msg__Int16MultiArray * msg = (const std_msgs__msg__Int16MultiArray *)msgin;
5763

5864
// Ensure we don't exceed the size of the `microseconds` array
@@ -61,66 +67,64 @@ void subscription_callback(const void * msgin)
6167
}
6268
}
6369

64-
// updates thrusters' pwm signals from array
70+
// Updates thrusters' PWM signals from array
6571
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+
}
7475
}
7576

7677
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);
8788
}
8889

8990
void power_setup() {
90-
set_microros_transports();
91+
set_microros_transports();
9192

92-
pinMode(LED_PIN, OUTPUT);
93-
digitalWrite(LED_PIN, HIGH);
93+
pinMode(LED_PIN, OUTPUT);
94+
digitalWrite(LED_PIN, HIGH);
9495

95-
initThrusters();
96+
initThrusters();
9697

97-
delay(2000);
98+
delay(2000);
9899

99-
allocator = rcl_get_default_allocator();
100+
allocator = rcl_get_default_allocator();
100101

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));
103104

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));
106107

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"));
113114

114115
msg.data.data = (int16_t *)malloc(8 * sizeof(int16_t));
115116
msg.data.size = 8;
116117
msg.data.capacity = 8;
117118

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));
121122
}
122123

123124
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);
126130
}

0 commit comments

Comments
 (0)