|
33 | 33 | * Includes
|
34 | 34 | *****************************************************************************/
|
35 | 35 | #include "V2VClient.h"
|
36 |
| -#include <SettingsHandler.h> |
37 | 36 | #include <Logging.h>
|
38 |
| -#include <ArduinoJson.h> |
39 | 37 |
|
40 | 38 | /******************************************************************************
|
41 | 39 | * Compiler Switches
|
|
57 | 55 | * Local Variables
|
58 | 56 | *****************************************************************************/
|
59 | 57 |
|
60 |
| -/* MQTT topic name for birth messages. */ |
61 |
| -const char* V2VClient::TOPIC_NAME_BIRTH = "birth"; |
62 |
| - |
63 |
| -/* MQTT topic name for will messages. */ |
64 |
| -const char* V2VClient::TOPIC_NAME_WILL = "will"; |
65 |
| - |
66 |
| -/** Default size of the JSON Document for parsing. */ |
67 |
| -static const uint32_t JSON_DOC_DEFAULT_SIZE = 1024U; |
68 |
| - |
69 |
| -/** Platoon leader vehicle ID. */ |
70 |
| -static const uint8_t PLATOON_LEADER_ID = 0U; |
71 |
| - |
72 | 58 | /******************************************************************************
|
73 | 59 | * Public Methods
|
74 | 60 | *****************************************************************************/
|
@@ -163,26 +149,16 @@ void V2VClient::process()
|
163 | 149 |
|
164 | 150 | bool V2VClient::sendWaypoint(const Waypoint& waypoint)
|
165 | 151 | {
|
166 |
| - bool isSuccessful = false; |
167 |
| - StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload; |
168 |
| - |
169 |
| - jsonPayload["X"] = waypoint.xPos; /**< X position [mm]. */ |
170 |
| - jsonPayload["Y"] = waypoint.yPos; /**< Y position [mm]. */ |
171 |
| - jsonPayload["Orientation"] = waypoint.orientation; /**< Orientation [mrad]. */ |
172 |
| - jsonPayload["Left"] = waypoint.left; /**< Left motor speed [steps/s]. */ |
173 |
| - jsonPayload["Right"] = waypoint.right; /**< Right motor speed [steps/s]. */ |
174 |
| - jsonPayload["Center"] = waypoint.center; /**< Center speed [steps/s]. */ |
175 |
| - |
176 |
| - size_t jsonBufferSize = measureJson(jsonPayload) + 1U; |
177 |
| - char jsonBuffer[jsonBufferSize]; |
| 152 | + bool isSuccessful = false; |
| 153 | + String payload = waypoint.serialize(); |
178 | 154 |
|
179 |
| - if ((jsonBufferSize - 1U) != serializeJson(jsonPayload, jsonBuffer, jsonBufferSize)) |
| 155 | + if (true == payload.isEmpty()) |
180 | 156 | {
|
181 |
| - LOG_ERROR("JSON serialization failed."); |
| 157 | + LOG_DEBUG("Failed to serialize waypoint."); |
182 | 158 | }
|
183 |
| - else if (false == m_mqttClient.publish(m_outputTopic, false, String(jsonBuffer))) |
| 159 | + else if (false == m_mqttClient.publish(m_outputTopic, false, payload)) |
184 | 160 | {
|
185 |
| - LOG_ERROR("Failed to publish MQTT message to %s.", m_outputTopic); |
| 161 | + LOG_ERROR("Failed to publish MQTT message to %s.", m_outputTopic.c_str()); |
186 | 162 | }
|
187 | 163 | else
|
188 | 164 | {
|
@@ -229,47 +205,15 @@ size_t V2VClient::getWaypointQueueSize() const
|
229 | 205 |
|
230 | 206 | void V2VClient::targetWaypointTopicCallback(const String& payload)
|
231 | 207 | {
|
232 |
| - StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload; |
233 |
| - DeserializationError error = deserializeJson(jsonPayload, payload.c_str()); |
| 208 | + Waypoint* waypoint = Waypoint::deserialize(payload); |
234 | 209 |
|
235 |
| - if (error != DeserializationError::Ok) |
| 210 | + if (nullptr == waypoint) |
236 | 211 | {
|
237 |
| - LOG_ERROR("JSON Deserialization Error %d.", error); |
| 212 | + LOG_ERROR("Failed to deserialize received waypoint."); |
238 | 213 | }
|
239 | 214 | else
|
240 | 215 | {
|
241 |
| - JsonVariant jsonXPos = jsonPayload["X"]; /**< X position [mm]. */ |
242 |
| - JsonVariant jsonYPos = jsonPayload["Y"]; /**< Y position [mm]. */ |
243 |
| - JsonVariant jsonOrientation = jsonPayload["Orientation"]; /**< Orientation [mrad]. */ |
244 |
| - JsonVariant jsonLeft = jsonPayload["Left"]; /**< Left motor speed [steps/s]. */ |
245 |
| - JsonVariant jsonRight = jsonPayload["Right"]; /**< Right motor speed [steps/s]. */ |
246 |
| - JsonVariant jsonCenter = jsonPayload["Center"]; /**< Center speed [steps/s]. */ |
247 |
| - |
248 |
| - if ((false == jsonXPos.isNull()) && (false == jsonYPos.isNull()) && (false == jsonOrientation.isNull()) && |
249 |
| - (false == jsonLeft.isNull()) && (false == jsonRight.isNull()) && (false == jsonCenter.isNull())) |
250 |
| - { |
251 |
| - Waypoint* waypoint = new (std::nothrow) Waypoint(); |
252 |
| - |
253 |
| - if (nullptr != waypoint) |
254 |
| - { |
255 |
| - waypoint->xPos = jsonXPos.as<int32_t>(); |
256 |
| - waypoint->yPos = jsonYPos.as<int32_t>(); |
257 |
| - waypoint->orientation = jsonOrientation.as<int32_t>(); |
258 |
| - waypoint->left = jsonLeft.as<int16_t>(); |
259 |
| - waypoint->right = jsonRight.as<int16_t>(); |
260 |
| - waypoint->center = jsonCenter.as<int16_t>(); |
261 |
| - |
262 |
| - m_waypointQueue.push(waypoint); |
263 |
| - } |
264 |
| - else |
265 |
| - { |
266 |
| - LOG_ERROR("Failed to allocate memory for received waypoint."); |
267 |
| - } |
268 |
| - } |
269 |
| - else |
270 |
| - { |
271 |
| - LOG_WARNING("Received invalid waypoint."); |
272 |
| - } |
| 216 | + m_waypointQueue.push(waypoint); |
273 | 217 | }
|
274 | 218 | }
|
275 | 219 |
|
|
0 commit comments