Skip to content

Commit bd9573c

Browse files
authored
Merge pull request #14 from FrankVanris2/velocity_commandFix
Updated Code 2.0
2 parents 71ae8dc + a9f4f7f commit bd9573c

File tree

6 files changed

+105
-18
lines changed

6 files changed

+105
-18
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"configurations": [
3+
{
4+
"browse": {
5+
"databaseFilename": "${default}",
6+
"limitSymbolsToIncludedHeaders": false
7+
},
8+
"includePath": [
9+
"/opt/ros/jazzy/include/**",
10+
"/home/ubuntu/myProjects/ROS2_FollowBot/FollowBotAROS2/src/arduino_serial/include/**",
11+
"/home/ubuntu/myProjects/ROS2_FollowBot/FollowBotAROS2/src/ldlidar_sl_ros2/include/**",
12+
"/home/ubuntu/myProjects/ROS2_FollowBot/FollowBotAROS2/src/mapping/include/**",
13+
"/usr/include/**"
14+
],
15+
"name": "ROS",
16+
"intelliSenseMode": "gcc-arm64",
17+
"compilerPath": "/usr/bin/gcc",
18+
"cStandard": "gnu11",
19+
"cppStandard": "c++14"
20+
}
21+
],
22+
"version": 4
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"python.autoComplete.extraPaths": [
3+
"/home/ubuntu/myProjects/ROS2_FollowBot/FollowBotAROS2/install/path_making/lib/python3.12/site-packages",
4+
"/opt/ros/jazzy/lib/python3.12/site-packages"
5+
],
6+
"python.analysis.extraPaths": [
7+
"/home/ubuntu/myProjects/ROS2_FollowBot/FollowBotAROS2/install/path_making/lib/python3.12/site-packages",
8+
"/opt/ros/jazzy/lib/python3.12/site-packages"
9+
]
10+
}

FollowBotAROS2/src/arduino_serial/src/cmd_vel_serial_node.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,33 @@ class cmd_velSerialNode : public rclcpp::Node {
2121
"/cmd_vel", 10,
2222
std::bind(&cmd_velSerialNode::cmd_velCallback, this, std::placeholders::_1)
2323
);
24-
24+
2525
RCLCPP_INFO(this->get_logger(), "Started cmd_velSerialNode");
2626
}
2727

2828
private:
2929
//TODO: send as JSON
3030
void cmd_velCallback(geometry_msgs::msg::Twist::SharedPtr msg) {
31-
nlohmann::json js;
31+
RCLCPP_INFO(this->get_logger(), "cmd_velCallback: linear.x = %f, angular.z = %f", msg->linear.x, msg->angular.z);
32+
nlohmann::json js;
33+
34+
js["sensor_type"] = "cmd_vel"; // Add sensor_type field
3235

33-
js["sensor_type"] = "cmd_vel";
36+
// Creating a data object
37+
nlohmann::json data_obj;
3438

35-
js["linear"]["x"] = msg->linear.x;
36-
js["linear"]["y"] = msg->linear.y;
37-
js["linear"]["z"] = msg->linear.z;
39+
data_obj["linear"]["x"] = msg->linear.x;
40+
data_obj["linear"]["y"] = msg->linear.y;
41+
data_obj["linear"]["z"] = msg->linear.z;
3842

3943
// Add angular velocity components
40-
js["angular"]["x"] = msg->angular.x;
41-
js["angular"]["y"] = msg->angular.y;
42-
js["angular"]["z"] = msg->angular.z;
44+
data_obj["angular"]["x"] = msg->angular.x;
45+
data_obj["angular"]["y"] = msg->angular.y;
46+
data_obj["angular"]["z"] = msg->angular.z;
4347

44-
std::string json_str = js.dump(4);
48+
// Assign the populated "data" object to the main JSON
49+
js["data"] = data_obj;
50+
std::string json_str = js.dump(-1); // Use 4 for pretty printing if desired, or -1 for compact
4551

4652
SerialManager::get().write_line(json_str.c_str());
4753
RCLCPP_INFO(this->get_logger(), "published json: \n\t%s", json_str.c_str());

FollowBotAROS2/src/arduino_serial/src/include/serial_manager.hpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#pragma once
22
#include <libserial/SerialPort.h>
3+
#include <libserial/SerialStream.h>
34
#include <nlohmann/json.hpp>
45
#include <rclcpp/rclcpp.hpp>
56

7+
#include <iostream>
68
#include <thread>
79
#include <atomic>
810
#include <queue>
911
#include <mutex>
1012
#include <optional>
1113
#include <string>
14+
#include <algorithm> // For std::remove_if, std::isspace
15+
#include <cctype> // For std::isspace
1216

1317
class SerialManager {
1418
public:
@@ -50,13 +54,26 @@ class SerialManager {
5054
/**
5155
* Sends a string through serial port
5256
*/
53-
void write_line(std::string out) {
57+
void write_line(const std::string& line_to_write) {
58+
RCLCPP_INFO(rclcpp::get_logger("serial_manager"),
59+
"Attempting to write to serial: %s", line_to_write.c_str());
5460
std::lock_guard<std::mutex> lock(port_mutex_);
55-
RCLCPP_INFO(rclcpp::get_logger("serial_manager"), "In WriteLine()");
56-
port_.Write(out);
61+
if (port_.IsOpen()) {
62+
try {
63+
port_.Write(line_to_write);
64+
port_.Write("\n"); // Ensure a newline is sent if arduino expects it for each JSON Message
65+
RCLCPP_INFO(rclcpp::get_logger("serial_manager"),
66+
"Serial write successful");
67+
} catch (const std::exception& e) {
68+
RCLCPP_ERROR(rclcpp::get_logger("serial_manager"),
69+
"Serial write error: %s", e.what());
70+
}
71+
} else {
72+
RCLCPP_WARN(rclcpp::get_logger("serial_manager"),
73+
"Serial port not open. Cannot write.");
74+
}
5775
}
5876

59-
6077
/**
6178
* pop one message for each message type
6279
* return std::nullopt if queue empty
@@ -90,16 +107,50 @@ class SerialManager {
90107
std::lock_guard<std::mutex> lock(port_mutex_);
91108
if (port_.IsDataAvailable()) {
92109
port_.ReadLine(out);
110+
//RCLCPP_INFO(rclcpp::get_logger("serial_manager"), "Received: %s", out.c_str()); // NEED TO REMOVE
93111
return true;
94112
}
95113
return false;
96114
}
97115

116+
/**
117+
* trim lines to remove leading/trailing whitespace
118+
* If there is whitespace, remove it
119+
* @param line the line to trim
120+
*/
121+
std::string trim(const std::string& line) {
122+
size_t first = line.find_first_not_of(" \t\n\r");
123+
if (std::string::npos == first) {
124+
return line; // No non-whitespace characters found.
125+
}
126+
size_t last = line.find_last_not_of(" \t\n\r");
127+
return line.substr(first, (last - first + 1));
128+
}
129+
98130
bool read_and_demux() {
99131
std::string line;
100132
if (!read_line(line))
101133
return false;
102134

135+
// --- THE CRITICAL STEP: TRIM THE LINE ---
136+
std::string trimmed_line = trim(line); // only needed for debugging purposes.
137+
138+
// FOR DEBUGGING PURPOSES
139+
// Check if error occurs on arduino side for Json Deserialization
140+
if (!trimmed_line.empty() && trimmed_line == "JSON_ERROR_ACK") {
141+
RCLCPP_INFO(rclcpp::get_logger("serial_manager"),
142+
"Arduino acknowledged JSON error");
143+
return true;
144+
}
145+
146+
// FOR DEBUGGING PURPOSES
147+
// Check for specific confirmation message first
148+
if (!trimmed_line.empty() && trimmed_line.find("ARDUINO_CMD_VEL_ACK") == 0) {
149+
RCLCPP_INFO(rclcpp::get_logger("serial_manager"),
150+
"Arduino acknowledged cmd_vel receipt ACKNOWLEDGED");
151+
return true; // Successfully handled this line
152+
}
153+
103154
// message validation
104155
if (line.empty() || line.front() != '{')
105156
return false;

FollowBotAROS2/src/arduino_serial/src/serial_manager_node.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class SerialManagerNode : public rclcpp::Node {
1616
RCLCPP_INFO(get_logger(),
1717
"SerialManager initialized on %s.", port_name.c_str());
1818
}
19+
1920
};
2021

2122
#include <rclcpp_components/register_node_macro.hpp>

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ sudo apt install libserial-dev
8484
```
8585
```bash
8686
sudo apt update
87-
sudo apt install libbluetooth-dev
88-
```
89-
```bash
90-
sudo apt update
9187
sudo apt install libgeographiclib-dev
9288
```
9389
```bash

0 commit comments

Comments
 (0)