Skip to content

Commit

Permalink
Manually merged changes from old branch (micro-ROS#56)
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Blight <[email protected]>

Co-authored-by: Andy Blight <[email protected]>
  • Loading branch information
AndyBlightLeeds and andyblight authored Apr 7, 2021
1 parent d843dbb commit 22209a4
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/int32_sub_pub/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
sdkconfig
sdkconfig.old
7 changes: 7 additions & 0 deletions examples/int32_sub_pub/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)

set (EXTRA_COMPONENT_DIRS "./../../.")

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(int32_pub_sub)

3 changes: 3 additions & 0 deletions examples/int32_sub_pub/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
idf_component_register(SRCS main.c
INCLUDE_DIRS ""
)
15 changes: 15 additions & 0 deletions examples/int32_sub_pub/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
menu "micro-ROS example-app settings"

config MICRO_ROS_APP_STACK
int "Stack the micro-ROS app (Bytes)"
default 16000
help
Stack size in Bytes of the micro-ROS app

config MICRO_ROS_APP_TASK_PRIO
int "Priority of the micro-ROS app"
default 5
help
Priority of micro-ros task higher value means higher priority

endmenu
5 changes: 5 additions & 0 deletions examples/int32_sub_pub/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

124 changes: 124 additions & 0 deletions examples/int32_sub_pub/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_system.h"

#include <uros_network_interfaces.h>
#include <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <std_msgs/msg/int32.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <rmw_uros/options.h>
#include "uxr/client/config.h"

#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc);vTaskDelete(NULL);}}
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}}

rcl_publisher_t publisher;
rcl_subscription_t subscriber;
std_msgs__msg__Int32 send_msg;
std_msgs__msg__Int32 recv_msg;

void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
{
(void) last_call_time;
if (timer != NULL) {
RCSOFTCHECK(rcl_publish(&publisher, &send_msg, NULL));
printf("Sent: %d\n", send_msg.data);
send_msg.data++;
}
}

void subscription_callback(const void * msgin)
{
const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin;
printf("Received: %d\n", msg->data);
}

void micro_ros_task(void * arg)
{
rcl_allocator_t allocator = rcl_get_default_allocator();
rclc_support_t support;

// Create init_options.
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
RCCHECK(rcl_init_options_init(&init_options, allocator));
rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&init_options);
// Use static agent IP and port.
RCCHECK(rmw_uros_options_set_udp_address(CONFIG_MICRO_ROS_AGENT_IP, CONFIG_MICRO_ROS_AGENT_PORT, rmw_options));

// Setup support structure.
RCCHECK(rclc_support_init_with_options(&support, 0, NULL, &init_options, &allocator));

// Create node.
rcl_node_t node = rcl_get_zero_initialized_node();
RCCHECK(rclc_node_init_default(&node, "int32_publisher_subscriber_rclc", "", &support));

// Create publisher.
RCCHECK(rclc_publisher_init_default(
&publisher,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
"int32_publisher"));

// Create subscriber.
RCCHECK(rclc_subscription_init_default(
&subscriber,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
"int32_subscriber"));

// Create timer.
rcl_timer_t timer = rcl_get_zero_initialized_timer();
const unsigned int timer_timeout = 1000;
RCCHECK(rclc_timer_init_default(
&timer,
&support,
RCL_MS_TO_NS(timer_timeout),
timer_callback));

// Create executor.
rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
RCCHECK(rclc_executor_init(&executor, &support.context, 2, &allocator));
unsigned int rcl_wait_timeout = 1000; // in ms
RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));

// Add timer and subscriber to executor.
RCCHECK(rclc_executor_add_timer(&executor, &timer));
RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &recv_msg, &subscription_callback, ON_NEW_DATA));

// Spin forever.
send_msg.data = 0;
while(1){
rclc_executor_spin_some(&executor, 100);
usleep(100000);
}

// Free resources.
RCCHECK(rcl_subscription_fini(&subscriber, &node));
RCCHECK(rcl_publisher_fini(&publisher, &node));
RCCHECK(rcl_node_fini(&node));

vTaskDelete(NULL);
}

void app_main(void)
{
#ifdef UCLIENT_PROFILE_UDP
// Start the networking if required
ESP_ERROR_CHECK(uros_network_interface_initialize());
#endif // UCLIENT_PROFILE_UDP

//pin micro-ros task in APP_CPU to make PRO_CPU to deal with wifi:
xTaskCreate(micro_ros_task,
"uros_task",
CONFIG_MICRO_ROS_APP_STACK,
NULL,
CONFIG_MICRO_ROS_APP_TASK_PRIO,
NULL);
}
3 changes: 3 additions & 0 deletions examples/int32_sub_pub/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_ESP_MAIN_TASK_STACK_SIZE=3000
CONFIG_ESP_TASK_WDT=n

0 comments on commit 22209a4

Please sign in to comment.