Skip to content

Commit eb1c1d6

Browse files
committed
Added code for pwm and pub/sub
1 parent 232d527 commit eb1c1d6

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
},
66
"git.detectSubmodules": false,
77
"cmake.sourceDirectory": "${workspaceFolder}/src/seahawk",
8+
"files.associations": {
9+
"stdio.h": "c",
10+
"rmw_microros.h": "c",
11+
"int32.h": "c"
12+
},
813
}

src/seahawk/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ add_executable(seahawk
1717
target_link_libraries(seahawk
1818
pico_stdlib
1919
microros
20+
hardware_pwm
21+
hardware_pio
2022
)
2123

2224
target_include_directories(seahawk PUBLIC

src/seahawk/seahawk.c

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,50 @@
99

1010
#include "pico/stdlib.h"
1111
#include "pico_uart_transports.h"
12+
#include "hardware/pio.h"
13+
#include "hardware/pwm.h"
14+
1215

1316
const uint LED_PIN = 25;
1417

1518
rcl_publisher_t publisher;
19+
rcl_subscription_t subscriber;
1620
std_msgs__msg__Int32 msg;
1721

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)
1946
{
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);
2249
}
2350

2451
int main()
2552
{
53+
const rosidl_message_type_support_t * type_support =
54+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32);
55+
2656
rmw_uros_set_custom_transport(
2757
true,
2858
NULL,
@@ -58,20 +88,26 @@ int main()
5888
rclc_support_init(&support, 0, NULL, &allocator);
5989

6090
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+
6198
rclc_publisher_init_default(
6299
&publisher,
63100
&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");
72103

73104
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);
75111

76112
gpio_put(LED_PIN, 1);
77113

0 commit comments

Comments
 (0)