forked from micro-ROS/micro_ros_azure_rtos_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
157 lines (127 loc) · 3.72 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include "board_init.h"
#include "stm32l475e_iot01.h"
#include "cmsis_utils.h"
#include "stm_networking.h"
#include "secrets.h"
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <std_msgs/msg/int32.h>
#include <rmw_microros/rmw_microros.h>
#include <microros_transports.h>
#define AZURE_THREAD_STACK_SIZE 4096*2
#define AZURE_THREAD_PRIORITY 4
TX_THREAD azure_thread;
ULONG azure_thread_stack[AZURE_THREAD_STACK_SIZE / sizeof(ULONG)];
rcl_publisher_t publisher;
void subscription_callback(const void * msgin)
{
const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin;
if (msg->data == 0)
{
BSP_LED_Off(LED2);
} else {
BSP_LED_On(LED2);
}
}
void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
{
(void) last_call_time;
(void) timer;
static std_msgs__msg__Int32 msg = {0};
if(RMW_RET_OK == rcl_publish(&publisher, &msg, NULL)){
printf("Sent: %ld\n", msg.data);
msg.data++;
} else {
printf("Failed to send\n");
}
}
void microros_thread(ULONG parameter)
{
// Initialize the network
if (stm32_network_init(WIFI_SSID, WIFI_PSK, WPA2_PSK_AES) != NX_SUCCESS)
{
printf("Failed to initialize the network\r\n");
return;
}
rcl_allocator_t allocator = rcl_get_default_allocator();
//create init_options
rclc_support_t support;
rclc_support_init(&support, 0, NULL, &allocator);
// create nodes
rcl_node_t node;
rclc_node_init_default(&node, "threadx_node", "", &support);
// create publisher
rclc_publisher_init_default(
&publisher,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
"threadx_publisher");
// create subscriber
rcl_subscription_t subscriber;
rclc_subscription_init_default(
&subscriber,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
"threadx_subscriber");
// create timer,
rcl_timer_t timer;
rclc_timer_init_default(
&timer,
&support,
RCL_MS_TO_NS(1000),
timer_callback);
// create executor
rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
rclc_executor_init(&executor, &support.context, 2, &allocator);
std_msgs__msg__Int32 msg = {0};
rclc_executor_add_subscription(&executor, &subscriber, &msg, subscription_callback, ON_NEW_DATA);
rclc_executor_add_timer(&executor, &timer);
while(1){
rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
tx_thread_sleep((ULONG) 0.1*TX_TIMER_TICKS_PER_SECOND);
}
(void)! rcl_publisher_fini(&publisher, &node);
(void)! rcl_node_fini(&node);
}
void tx_application_define(void* first_unused_memory)
{
systick_interval_set(TX_TIMER_TICKS_PER_SECOND);
// Configure micro-ROS Agent IP and port
static custom_transport_args args = {
.agent_ip_address = IP_ADDRESS(192,168,1,57),
.agent_port = 8888
};
printf("\r\nStarting micro-ROS thread\r\n\r\n");
rmw_uros_set_custom_transport(
false,
(void *) &args,
azure_transport_open,
azure_transport_close,
azure_transport_write,
azure_transport_read);
// Create Azure thread
UINT status = tx_thread_create(&azure_thread,
"micro-ROS thread",
microros_thread,
0,
azure_thread_stack,
AZURE_THREAD_STACK_SIZE,
AZURE_THREAD_PRIORITY,
AZURE_THREAD_PRIORITY,
TX_NO_TIME_SLICE,
TX_AUTO_START);
if (status != TX_SUCCESS)
{
printf("Thread creation failed\r\n");
tx_thread_sleep(5);
}
}
int main(void)
{
// Initialize the board
board_init();
// Enter the ThreadX kernel
tx_kernel_enter();
return 0;
}