|
9 | 9 |
|
10 | 10 | #include "pico/stdlib.h" |
11 | 11 | #include "pico_uart_transports.h" |
| 12 | +#include "hardware/pio.h" |
| 13 | +#include "hardware/pwm.h" |
| 14 | + |
12 | 15 |
|
13 | 16 | const uint LED_PIN = 25; |
14 | 17 |
|
15 | 18 | rcl_publisher_t publisher; |
| 19 | +rcl_subscription_t subscriber; |
16 | 20 | std_msgs__msg__Int32 msg; |
17 | 21 |
|
18 | | -void timer_callback(rcl_timer_t *timer, int64_t last_call_time) |
| 22 | +// configures the pwm generators |
| 23 | +void config_pwm(uint gpio[], int num_motors, uint16_t wrap) { |
| 24 | + for (int i = 0; i < num_motors; i++) { |
| 25 | + // Sets all GPIO pins to use PWM |
| 26 | + gpio_set_function(gpio[i], GPIO_FUNC_PWM); |
| 27 | + // Sets the wrap for each slice |
| 28 | + pwm_set_wrap(pwm_gpio_to_slice_num(gpio[i]), wrap); |
| 29 | + /* |
| 30 | + * If phase correct is set to false the counter |
| 31 | + * will reset to 0 after reaching the level, |
| 32 | + * otherwise it will decrease down to 0 |
| 33 | + */ |
| 34 | + pwm_set_phase_correct(pwm_gpio_to_slice_num(gpio[i]), false); |
| 35 | + } |
| 36 | +} |
| 37 | +// Sets all of the motors to the given levels |
| 38 | +void sub_callback(uint gpio[], int num_motors, uint16_t levels[]) { |
| 39 | + for (int i = 0; i < num_motors; i++) { |
| 40 | + pwm_set_gpio_level(gpio[i], levels[i]); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +void subscription_callback(const void * msgin) |
19 | 46 | { |
20 | | - rcl_ret_t ret = rcl_publish(&publisher, &msg, NULL); |
21 | | - msg.data++; |
| 47 | + // Process message |
| 48 | + rcl_ret_t ret = rcl_publish(&publisher, (const std_msgs__msg__Int32 *)msgin, NULL); |
22 | 49 | } |
23 | 50 |
|
24 | 51 | int main() |
25 | 52 | { |
| 53 | + const rosidl_message_type_support_t * type_support = |
| 54 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32); |
| 55 | + |
26 | 56 | rmw_uros_set_custom_transport( |
27 | 57 | true, |
28 | 58 | NULL, |
@@ -58,20 +88,26 @@ int main() |
58 | 88 | rclc_support_init(&support, 0, NULL, &allocator); |
59 | 89 |
|
60 | 90 | rclc_node_init_default(&node, "pico_node", "", &support); |
| 91 | + |
| 92 | + rclc_subscription_init_default( |
| 93 | + &subscriber, |
| 94 | + &node, |
| 95 | + type_support, |
| 96 | + "pico_in"); |
| 97 | + |
61 | 98 | rclc_publisher_init_default( |
62 | 99 | &publisher, |
63 | 100 | &node, |
64 | | - ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), |
65 | | - "pico_publisher"); |
66 | | - |
67 | | - rclc_timer_init_default( |
68 | | - &timer, |
69 | | - &support, |
70 | | - RCL_MS_TO_NS(1000), |
71 | | - timer_callback); |
| 101 | + type_support, |
| 102 | + "pico_out"); |
72 | 103 |
|
73 | 104 | rclc_executor_init(&executor, &support.context, 1, &allocator); |
74 | | - rclc_executor_add_timer(&executor, &timer); |
| 105 | + rclc_executor_add_subscription( |
| 106 | + &executor, |
| 107 | + &subscriber, |
| 108 | + &msg, |
| 109 | + &subscription_callback, |
| 110 | + ON_NEW_DATA); |
75 | 111 |
|
76 | 112 | gpio_put(LED_PIN, 1); |
77 | 113 |
|
|
0 commit comments