Skip to content

Commit 9bcfd36

Browse files
committed
Swapped subscriber to expect an Int16MultiArray, initialized msg so that it was fully allocated
1 parent 4adf8aa commit 9bcfd36

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/seahawk/seahawk.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const uint LED_PIN = 25;
1818

1919
rcl_publisher_t publisher;
2020
rcl_subscription_t subscriber;
21-
std_msgs__msg__Int16MultiArray msg;
2221
const uint GPIO[8] = {10, 11, 12, 13, 18, 19, 20, 21};
2322
const int NUM_MOTORS = 8;
2423
const uint8_t CLOCK_DIV = 125;
@@ -54,17 +53,15 @@ void set_duty_cycle(uint16_t levels[]) {
5453

5554
void subscription_callback(const void * msgin)
5655
{
57-
// Process message
58-
// rcl_ret_t ret = rcl_publish(&publisher, (const std_msgs__msg__Int32 *)msgin, NULL);
5956
// Set the msgin to a Int16MultiArray
60-
msg = *((std_msgs__msg__Int16MultiArray *) msgin);
61-
set_duty_cycle((uint16_t *) msg.data.data);
57+
std_msgs__msg__Int16MultiArray *msg = (std_msgs__msg__Int16MultiArray *) msgin;
58+
set_duty_cycle((uint16_t *) msg->data.data);
6259
}
6360

6461
int main()
6562
{
6663
const rosidl_message_type_support_t * type_support =
67-
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32);
64+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray);
6865

6966
rmw_uros_set_custom_transport(
7067
true,
@@ -97,25 +94,45 @@ int main()
9794
// Unreachable agent, exiting program.
9895
return ret;
9996
}
97+
10098
config_pwm(3000);
101-
10299

103100
rclc_support_init(&support, 0, NULL, &allocator);
104-
101+
105102
rclc_node_init_default(&node, "pico_node", "", &support);
103+
104+
// Define msg
105+
std_msgs__msg__Int16MultiArray msg;
106+
107+
// Define msg data Int16 Sequence
108+
rosidl_runtime_c__int16__Sequence msg_data;
109+
msg_data.size = 0;
110+
msg_data.capacity = 8;
111+
int16_t msg_data_data[8] = {0, 0, 0, 0, 0, 0, 0, 0};
112+
msg_data.data = msg_data_data;
113+
114+
msg.data = msg_data;
115+
116+
// Define msg layout MultiArray Layout
117+
118+
std_msgs__msg__MultiArrayLayout msg_layout;
119+
120+
std_msgs__msg__MultiArrayDimension__Sequence msg_layout_dim;
121+
msg_layout.dim = msg_layout_dim;
122+
msg_layout.data_offset = 0;
123+
124+
msg.layout = msg_layout;
106125

107-
rclc_subscription_init_default(
126+
127+
128+
129+
130+
ret = rclc_subscription_init_default(
108131
&subscriber,
109132
&node,
110133
type_support,
111134
"pico_in");
112135

113-
// rclc_publisher_init_default(
114-
// &publisher,
115-
// &node,
116-
// type_support,
117-
// "pico_out");
118-
119136
rclc_executor_init(&executor, &support.context, 1, &allocator);
120137
rclc_executor_add_subscription(
121138
&executor,
@@ -126,6 +143,7 @@ int main()
126143

127144
gpio_put(LED_PIN, 1);
128145

146+
129147
while (true)
130148
{
131149
rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));

0 commit comments

Comments
 (0)