55#include <rclc/rclc.h>
66#include <rclc/executor.h>
77#include <std_msgs/msg/int32.h>
8+ #include <std_msgs/msg/int16_multi_array.h>
89#include <rmw_microros/rmw_microros.h>
910
1011#include "pico/stdlib.h"
@@ -17,35 +18,47 @@ const uint LED_PIN = 25;
1718
1819rcl_publisher_t publisher ;
1920rcl_subscription_t subscriber ;
20- std_msgs__msg__Int32 msg ;
21+ std_msgs__msg__Int16MultiArray msg ;
22+ const uint GPIO [8 ] = {10 , 11 , 12 , 13 , 18 , 19 , 20 , 21 };
23+ const int NUM_MOTORS = 8 ;
24+ const uint8_t CLOCK_DIV = 125 ;
2125
2226// 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 ++ ) {
27+ void config_pwm (uint16_t wrap ) {
28+ for (int i = 0 ; i < NUM_MOTORS ; i ++ ) {
2529 // Sets all GPIO pins to use PWM
26- gpio_set_function (gpio [i ], GPIO_FUNC_PWM );
30+ gpio_set_function (GPIO [i ], GPIO_FUNC_PWM );
2731 // Sets the wrap for each slice
28- pwm_set_wrap (pwm_gpio_to_slice_num (gpio [i ]), wrap );
32+ pwm_set_wrap (pwm_gpio_to_slice_num (GPIO [i ]), wrap );
2933 /*
3034 * If phase correct is set to false the counter
3135 * will reset to 0 after reaching the level,
3236 * otherwise it will decrease down to 0
3337 */
34- pwm_set_phase_correct (pwm_gpio_to_slice_num (gpio [i ]), false);
38+ pwm_set_phase_correct (pwm_gpio_to_slice_num (GPIO [i ]), false);
39+ // Set clock division
40+ pwm_set_clkdiv_int_frac (pwm_gpio_to_slice_num (GPIO [i ]), CLOCK_DIV , 0 );
41+ // Set pins to 1500microseconds for neutral
42+ pwm_set_gpio_level (GPIO [i ], 1500 );
43+ // Enable PWM
44+ pwm_set_enabled (pwm_gpio_to_slice_num (GPIO [i ]), true);
3545 }
3646}
3747// 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 ]);
48+ void set_duty_cycle ( uint16_t levels []) {
49+ for (int i = 0 ; i < NUM_MOTORS ; i ++ ) {
50+ pwm_set_gpio_level (GPIO [i ], levels [i ]);
4151 }
4252}
4353
4454
4555void subscription_callback (const void * msgin )
4656{
4757 // Process message
48- rcl_ret_t ret = rcl_publish (& publisher , (const std_msgs__msg__Int32 * )msgin , NULL );
58+ // rcl_ret_t ret = rcl_publish(&publisher, (const std_msgs__msg__Int32 *)msgin, NULL);
59+ // Set the msgin to a Int16MultiArray
60+ msg = * ((std_msgs__msg__Int16MultiArray * ) msgin );
61+ set_duty_cycle ((uint16_t * ) msg .data .data );
4962}
5063
5164int main ()
@@ -84,14 +97,8 @@ int main()
8497 // Unreachable agent, exiting program.
8598 return ret ;
8699 }
87- uint gpio [8 ] = {10 , 11 , 12 , 13 , 18 , 19 , 20 , 21 };
88- uint16_t levels [8 ] = {1500 , 2500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 };
89- config_pwm (gpio , 8 , 3000 );
90- for (int i = 0 ; i < 8 ; i ++ ) {
91- pwm_set_gpio_level (gpio [i ], levels [i ]);
92- pwm_set_clkdiv_int_frac (pwm_gpio_to_slice_num (gpio [i ]), 125 , 0 );
93- pwm_set_enabled (pwm_gpio_to_slice_num (gpio [i ]), true);
94- }
100+ config_pwm (3000 );
101+
95102
96103 rclc_support_init (& support , 0 , NULL , & allocator );
97104
@@ -103,11 +110,11 @@ int main()
103110 type_support ,
104111 "pico_in" );
105112
106- rclc_publisher_init_default (
107- & publisher ,
108- & node ,
109- type_support ,
110- "pico_out" );
113+ // rclc_publisher_init_default(
114+ // &publisher,
115+ // &node,
116+ // type_support,
117+ // "pico_out");
111118
112119 rclc_executor_init (& executor , & support .context , 1 , & allocator );
113120 rclc_executor_add_subscription (
@@ -119,7 +126,6 @@ int main()
119126
120127 gpio_put (LED_PIN , 1 );
121128
122- msg .data = 0 ;
123129 while (true)
124130 {
125131 rclc_executor_spin_some (& executor , RCL_MS_TO_NS (100 ));
0 commit comments