3#include <micro_ros_arduino.h>
4#include <nav_msgs/msg/odometry.h>
6#include <rclc/executor.h>
8#include <std_msgs/msg/int32.h>
9#include <geometry_msgs/msg/point32.h>
10#include <geometry_msgs/msg/twist.h>
12#include "TinyRobotics/communication/MessageHandler.h"
13#include "TinyRobotics/maps/IMap.h"
14#include "TinyRobotics/utils/AllocatorPSRAM.h"
15#include "TinyRobotics/utils/LoggerClass.h"
17namespace tinyrobotics {
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
54 MicroROS(
const char* node_name =
"tinyrobotics_node") {
55 ros_node_name = node_name;
58 void setCallback(
void (*cb)(
const void* msgin)) { cmd_callback = cb; }
73 if (cmd_callback ==
nullptr) {
74 TRLogger.error(
"MicroROSS: Callback function not set!");
78 if (!is_tranport_defined) set_microros_transports();
80 allocator = rcl_get_default_allocator();
81 rclc_support_init(&support, 0, NULL, &allocator);
83 rclc_node_init_default(&node, ros_node_name,
"", &support);
86 rclc_subscription_init_default(
87 &sub_cmd, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(geometry_msgs, msg, Twist),
91 rclc_publisher_init_default(
92 &publisher, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(nav_msgs, msg, Odometry),
96 rclc_executor_init(&executor, &support.context, 1, &allocator);
97 rclc_executor_add_subscription(&executor, &sub_cmd, &cmd_msg, cmd_callback,
105 auto rc = rcl_publish(&publisher, &msg, NULL);
106 return rc == RCL_RET_OK;
111 nav_msgs__msg__Odometry msg;
113 memset(&msg, 0,
sizeof(msg));
116 msg.pose.pose.position.x = state.getPosition().x;
117 msg.pose.pose.position.y = state.getPosition().y;
118 msg.pose.pose.position.z = state.getPosition().z;
121 float cy = cosf(state.getOrientation()
.yaw * 0.5f);
122 float sy = sinf(state.getOrientation()
.yaw * 0.5f);
123 float cp = cosf(state.getOrientation()
.pitch * 0.5f);
124 float sp = sinf(state.getOrientation()
.pitch * 0.5f);
125 float cr = cosf(state.getOrientation()
.roll * 0.5f);
126 float sr = sinf(state.getOrientation()
.roll * 0.5f);
127 msg.pose.pose.orientation.w = cr * cp * cy + sr * sp * sy;
128 msg.pose.pose.orientation.x = sr * cp * cy - cr * sp * sy;
129 msg.pose.pose.orientation.y = cr * sp * cy + sr * cp * sy;
130 msg.pose.pose.orientation.z = cr * cp * sy - sr * sp * cy;
133 msg.twist.twist.linear.x = state.getSpeed().x;
134 msg.twist.twist.linear.y = state.getSpeed().y;
135 msg.twist.twist.linear.z = state.getSpeed().z;
138 msg.twist.twist.angular.x = state.getAngularVelocity().x;
139 msg.twist.twist.angular.y = state.getAngularVelocity().y;
140 msg.twist.twist.angular.z = state.getAngularVelocity().z;
146 return sendOdometry(msg);
152 position, orientation, Speed3D(0, 0, 0, SpeedUnit::MPS),
153 AngularVelocity3D(0, 0, 0, AngularVelocityUnit::RadPerSec));
158 bool sendPoint(
const geometry_msgs__msg__Point32& msg) {
159 auto rc = rcl_publish(&publisher, &msg, NULL);
160 return rc == RCL_RET_OK;
163 bool sendCoordinate(
const Coordinate<DistanceM>& coordinate) {
164 geometry_msgs__msg__Point32 msg;
165 msg.x = coordinate.x;
166 msg.y = coordinate.y;
167 msg.z = coordinate.z;
168 return sendPoint(msg);
172 template <
typename T>
175 coordinate.x = entry.getCoordinate().x;
176 coordinate.y = entry.getCoordinate().y;
179 return sendCoordinate(coordinate);
182 rclc_executor_t& getExecutor() {
return executor; }
185 const char* ros_node_name =
nullptr;
186 void (*cmd_callback)(
const void* msgin) =
nullptr;
187 bool is_tranport_defined =
false;
190 rclc_executor_t executor;
191 rclc_support_t support;
192 rcl_allocator_t allocator;
194 rcl_subscription_t sub_cmd;
195 rcl_publisher_t publisher;
196 geometry_msgs__msg__Twist cmd_msg;
204 sendOdometry(msg.value);
209 sendCoordinate(msg.value);
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Abstract interface for 2D grid maps and occupancy maps in TinyRobotics.
Definition: IMap.h:79
Interface for handling messages in the TinyRobotics framework.
Definition: MessageHandler.h:18
micro-ROS interface for vehicle control and odometry on Arduino/ESP32.
Definition: MicroROS.h:52
bool sendPoint(const geometry_msgs__msg__Point32 &msg)
Publish Point32 (occupancy grid map message not available in micro-ROS)
Definition: MicroROS.h:158
bool sendOdometry(const MotionState3D state)
Publish odometry message.
Definition: MicroROS.h:110
bool sendOdometry(Coordinate< DistanceM > position, Orientation3D orientation)
Publish odometry message.
Definition: MicroROS.h:150
bool sendOdometry(const nav_msgs__msg__Odometry &msg)
Publish odometry message.
Definition: MicroROS.h:104
bool sendMap(IMap< T > &entry)
Publish Point32 (occupancy grid map message not available in micro-ROS)
Definition: MicroROS.h:173
bool onMessage(const Message< float > &msg) override
Handle an incoming message (pure virtual).
Definition: MicroROS.h:198
Represents the full 3D motion state of a robot or vehicle.
Definition: MotionState3D.h:53
Simple 3D orientation class (yaw, pitch, roll in radians)
Definition: Orientation3D.h:13
float roll
Roll angle (radians)
Definition: Orientation3D.h:22
float pitch
Pitch angle (radians)
Definition: Orientation3D.h:21
float yaw
Yaw angle (radians)
Definition: Orientation3D.h:20
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72