1+ /*FreeRtos includes*/
2+ #include "FreeRTOS.h"
3+ #include "task.h"
4+
5+ #include <rcl/rcl.h>
6+ #include <geometry_msgs/msg/point32.h>
7+ #include "example_interfaces/srv/add_two_ints.h"
8+ #include <geometry_msgs/msg/twist.h>
9+
10+ #include <rcutils/allocator.h>
11+
12+ #include "config.h"
13+ #include "log.h"
14+ #include "crc.h"
15+ #include "worker.h"
16+ #include "num.h"
17+ #include "debug.h"
18+ #include "radiolink.h"
19+ #include <time.h>
20+
21+ #include "microrosapp.h"
22+
23+ #define RCCHECK (msg ) if((rc != RCL_RET_OK)){DEBUG_PRINT("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)rc); goto clean;}
24+ #define RCSOFTCHECK (msg ) if((rc != RCL_RET_OK)){DEBUG_PRINT("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)rc);}
25+
26+ static int pitchid , rollid , yawid ;
27+ static int Xid , Yid , Zid ;
28+
29+ float sign (float x ){
30+ return (x >= 0 ) ? 1.0 : -1.0 ;
31+ }
32+
33+ void appMain (){
34+ while (1 ){
35+ absoluteUsedMemory = 0 ;
36+ usedMemory = 0 ;
37+
38+ // ####################### RADIO INIT #######################
39+
40+ int radio_connected = logGetVarId ("radio" , "isConnected" );
41+
42+ while (!logGetUint (radio_connected )) vTaskDelay (100 );
43+ DEBUG_PRINT ("Radio connected\n" );
44+
45+ // ####################### MICROROS INIT #######################
46+
47+ DEBUG_PRINT ("Free heap pre uROS: %d bytes\n" , xPortGetFreeHeapSize ());
48+ vTaskDelay (50 );
49+
50+ rcl_context_t context ;
51+ rcl_init_options_t init_options ;
52+ rcl_ret_t rc ;
53+ rcl_ret_t rc_aux __attribute__((unused ));
54+
55+ init_options = rcl_get_zero_initialized_init_options ();
56+ rcl_allocator_t freeRTOS_allocator = rcutils_get_zero_initialized_allocator ();
57+ freeRTOS_allocator .allocate = __crazyflie_allocate ;
58+ freeRTOS_allocator .deallocate = __crazyflie_deallocate ;
59+ freeRTOS_allocator .reallocate = __crazyflie_reallocate ;
60+ freeRTOS_allocator .zero_allocate = __crazyflie_zero_allocate ;
61+
62+ if (!rcutils_set_default_allocator (& freeRTOS_allocator )) {
63+ DEBUG_PRINT ("Error on default allocators (line %d)\n" ,__LINE__ );
64+ vTaskSuspend ( NULL );
65+ }
66+
67+ rc = rcl_init_options_init (& init_options , rcutils_get_default_allocator ());
68+ RCCHECK ()
69+
70+ context = rcl_get_zero_initialized_context ();
71+
72+ rc = rcl_init (0 , NULL , & init_options , & context );
73+ RCCHECK ()
74+
75+ rc = rcl_init_options_fini (& init_options );
76+
77+ rcl_node_t node = rcl_get_zero_initialized_node ();
78+ rcl_node_options_t node_ops = rcl_node_get_default_options ();
79+
80+ rc = rcl_node_init (& node , "crazyflie_node" , "" , & context , & node_ops );
81+ RCCHECK ()
82+
83+ // Create publisher 1
84+ const char * drone_odom = "/drone/odometry" ;
85+
86+ rcl_publisher_t pub_odom = rcl_get_zero_initialized_publisher ();
87+ const rosidl_message_type_support_t * pub_type_support_odom = ROSIDL_GET_MSG_TYPE_SUPPORT (geometry_msgs , msg , Point32 );
88+ rcl_publisher_options_t pub_opt_odom = rcl_publisher_get_default_options ();
89+ pub_opt_odom .qos .reliability = RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT ;
90+
91+
92+ rc = rcl_publisher_init (
93+ & pub_odom ,
94+ & node ,
95+ pub_type_support_odom ,
96+ drone_odom ,
97+ & pub_opt_odom );
98+
99+ if (rc != RCL_RET_OK ){
100+ rc_aux = rcl_node_fini (& node );
101+ }
102+ RCCHECK ()
103+
104+ // Create publisher 2
105+ const char * drone_attitude = "/drone/attitude" ;
106+
107+ rcl_publisher_t pub_attitude = rcl_get_zero_initialized_publisher ();
108+ const rosidl_message_type_support_t * pub_type_support_att = ROSIDL_GET_MSG_TYPE_SUPPORT (geometry_msgs , msg , Point32 );
109+ rcl_publisher_options_t pub_opt_att = rcl_publisher_get_default_options ();
110+ pub_opt_att .qos .reliability = RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT ;
111+
112+ rc = rcl_publisher_init (
113+ & pub_attitude ,
114+ & node ,
115+ pub_type_support_att ,
116+ drone_attitude ,
117+ & pub_opt_att );
118+
119+ if (rc != RCL_RET_OK ){
120+ rc_aux = rcl_publisher_fini (& pub_odom , & node );
121+ rc_aux = rcl_node_fini (& node );
122+ }
123+ RCCHECK ()
124+
125+ // Init messages
126+ geometry_msgs__msg__Point32 pose ;
127+ geometry_msgs__msg__Point32__init (& pose );
128+ geometry_msgs__msg__Point32 odom ;
129+ geometry_msgs__msg__Point32__init (& odom );
130+
131+ //Get pitch, roll and yaw value
132+ pitchid = logGetVarId ("stateEstimate" , "pitch" );
133+ rollid = logGetVarId ("stateEstimate" , "roll" );
134+ yawid = logGetVarId ("stateEstimate" , "yaw" );
135+
136+ //Get X,Y and Z value
137+ Xid = logGetVarId ("stateEstimate" , "x" );
138+ Yid = logGetVarId ("stateEstimate" , "y" );
139+ Zid = logGetVarId ("stateEstimate" , "z" );
140+
141+ DEBUG_PRINT ("Free heap post uROS configuration: %d bytes\n" , xPortGetFreeHeapSize ());
142+ DEBUG_PRINT ("uROS Used Memory %d bytes\n" , usedMemory );
143+ DEBUG_PRINT ("uROS Absolute Used Memory %d bytes\n" , absoluteUsedMemory );
144+
145+ // ####################### MAIN LOOP #######################
146+
147+ // static P2PPacket pk;
148+ // pk.port = 0;
149+ // pk.size = 11;
150+ // memcpy(pk.data, "Hello World", 11);
151+ // radiolinkSendP2PPacketBroadcast(&pk);
152+
153+ radiolinkSetPowerDbm (-50 );
154+
155+ while (logGetUint (radio_connected )){
156+
157+ pose .x = logGetFloat (pitchid );
158+ pose .y = logGetFloat (rollid );
159+ pose .z = logGetFloat (yawid );
160+ odom .x = logGetFloat (Xid );
161+ odom .y = logGetFloat (Yid );
162+ odom .z = logGetFloat (Zid );
163+
164+ // Debug
165+ // int radio_rssi = logGetVarId("radio", "rssi");
166+ // odom.x = logGetFloat(radio_rssi);
167+ // odom.y = xPortGetFreeHeapSize();
168+
169+ rc = rcl_publish ( & pub_attitude , (const void * ) & pose , NULL );
170+ RCSOFTCHECK ()
171+
172+ rc = rcl_publish ( & pub_odom , (const void * ) & odom , NULL );
173+ RCSOFTCHECK ()
174+
175+ vTaskDelay (10 /portTICK_RATE_MS );
176+ }
177+
178+ rc = rcl_publisher_fini (& pub_odom , & node );
179+ rc = rcl_publisher_fini (& pub_attitude , & node );
180+ rc = rcl_node_fini (& node );
181+ clean :
182+ rc = rcl_shutdown (& context );
183+ DEBUG_PRINT ("Connection lost, retriying\n" );
184+ }
185+
186+ vTaskSuspend ( NULL );
187+ }
0 commit comments