Skip to content

Commit e1529e4

Browse files
committed
Updated V2V with new Waypoint. Removed unused members
1 parent 49a0741 commit e1529e4

File tree

2 files changed

+13
-72
lines changed

2 files changed

+13
-72
lines changed

lib/PlatoonService/src/V2VClient.cpp

+10-66
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
* Includes
3434
*****************************************************************************/
3535
#include "V2VClient.h"
36-
#include <SettingsHandler.h>
3736
#include <Logging.h>
38-
#include <ArduinoJson.h>
3937

4038
/******************************************************************************
4139
* Compiler Switches
@@ -57,18 +55,6 @@
5755
* Local Variables
5856
*****************************************************************************/
5957

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-
7258
/******************************************************************************
7359
* Public Methods
7460
*****************************************************************************/
@@ -163,26 +149,16 @@ void V2VClient::process()
163149

164150
bool V2VClient::sendWaypoint(const Waypoint& waypoint)
165151
{
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();
178154

179-
if ((jsonBufferSize - 1U) != serializeJson(jsonPayload, jsonBuffer, jsonBufferSize))
155+
if (true == payload.isEmpty())
180156
{
181-
LOG_ERROR("JSON serialization failed.");
157+
LOG_DEBUG("Failed to serialize waypoint.");
182158
}
183-
else if (false == m_mqttClient.publish(m_outputTopic, false, String(jsonBuffer)))
159+
else if (false == m_mqttClient.publish(m_outputTopic, false, payload))
184160
{
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());
186162
}
187163
else
188164
{
@@ -229,47 +205,15 @@ size_t V2VClient::getWaypointQueueSize() const
229205

230206
void V2VClient::targetWaypointTopicCallback(const String& payload)
231207
{
232-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
233-
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
208+
Waypoint* waypoint = Waypoint::deserialize(payload);
234209

235-
if (error != DeserializationError::Ok)
210+
if (nullptr == waypoint)
236211
{
237-
LOG_ERROR("JSON Deserialization Error %d.", error);
212+
LOG_ERROR("Failed to deserialize received waypoint.");
238213
}
239214
else
240215
{
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);
273217
}
274218
}
275219

lib/PlatoonService/src/V2VClient.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
class V2VClient
5959
{
6060
public:
61+
/** Platoon leader vehicle ID. */
62+
static const uint8_t PLATOON_LEADER_ID = 0U;
63+
6164
/**
6265
* Constructs a V2V client.
6366
*
@@ -111,12 +114,6 @@ class V2VClient
111114
size_t getWaypointQueueSize() const;
112115

113116
private:
114-
/** MQTT topic name for birth messages. */
115-
static const char* TOPIC_NAME_BIRTH;
116-
117-
/** MQTT topic name for will messages. */
118-
static const char* TOPIC_NAME_WILL;
119-
120117
/** Max topic length */
121118
static const uint8_t MAX_TOPIC_LENGTH = 64U;
122119

0 commit comments

Comments
 (0)