Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2V Communication Manager #92

Merged
merged 13 commits into from
Feb 6, 2024
50 changes: 44 additions & 6 deletions lib/ConvoyFollower/src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ void App::setup()
{
LOG_FATAL("Failed to setup MQTT client.");
}
else if (V2VClient::PLATOON_LEADER_ID == settings.getPlatoonVehicleId())
else if (V2VCommManager::PLATOON_LEADER_ID == settings.getPlatoonVehicleId())
{
/* Correct config.json file loaded? */
LOG_FATAL("Platoon Vehicle ID must not be 0 for a follower.");
}
else if (false == m_v2vClient.init(settings.getPlatoonPlatoonId(), settings.getPlatoonVehicleId()))
else if (false == m_v2vCommManager.init(settings.getPlatoonPlatoonId(), settings.getPlatoonVehicleId()))
{
LOG_FATAL("Failed to initialize V2V client.");
}
Expand Down Expand Up @@ -216,7 +216,7 @@ void App::loop()
m_mqttClient.process();

/* Process V2V Communication */
m_v2vClient.process();
processV2VCommunication();

/* Process System State Machine */
m_systemStateMachine.process();
Expand All @@ -225,12 +225,12 @@ void App::loop()
processPeriodicTasks();

/* Process V2V event queue. */
while (0U < m_v2vClient.getWaypointQueueSize())
while (0U < m_v2vCommManager.getWaypointQueueSize())
{
/* Waypoints are pending. */
Waypoint waypoint;

if (false == m_v2vClient.getNextWaypoint(waypoint))
if (false == m_v2vCommManager.getNextWaypoint(waypoint))
{
LOG_WARNING("Failed to get next waypoint from V2V client.");
}
Expand Down Expand Up @@ -412,7 +412,7 @@ void App::processPeriodicTasks()
{
LOG_WARNING("Failed to get waypoint from driving state.");
}
else if (false == m_v2vClient.sendWaypoint(payload))
else if (false == m_v2vCommManager.sendWaypoint(payload))
{
LOG_WARNING("Waypoint could not be sent.");
}
Expand Down Expand Up @@ -478,6 +478,44 @@ void App::processPeriodicTasks()
}
}

void App::processV2VCommunication()
{
V2VCommManager::V2VStatus v2vStatus = V2VCommManager::V2V_STATUS_OK;
V2VCommManager::VehicleStatus vehicleStatus = V2VCommManager::VEHICLE_STATUS_OK;

if (true == ErrorState::getInstance().isActive())
{
vehicleStatus = V2VCommManager::VEHICLE_STATUS_ERROR;
}

v2vStatus = m_v2vCommManager.process(vehicleStatus);

switch (v2vStatus)
{
case V2VCommManager::V2V_STATUS_OK:
/* All good. Nothing to do. */
break;

case V2VCommManager::V2V_STATUS_NOT_INIT:
LOG_WARNING("V2V not initialized.");
break;

case V2VCommManager::V2V_STATUS_LOST_FOLLOWER:
case V2VCommManager::V2V_STATUS_FOLLOWER_ERROR:
LOG_ERROR("Follower Error");
setErrorState();
break;

case V2VCommManager::V2V_STATUS_GENERAL_ERROR:
LOG_ERROR("V2V Communication error.");
setErrorState();
break;

default:
break;
}
}

/******************************************************************************
* External Functions
*****************************************************************************/
Expand Down
13 changes: 9 additions & 4 deletions lib/ConvoyFollower/src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include <MqttClient.h>
#include <SimpleTimer.hpp>
#include <StateMachine.h>
#include <V2VClient.h>
#include <V2VCommManager.h>
#include "SerialMuxChannels.h"

/******************************************************************************
Expand All @@ -72,7 +72,7 @@ class App
m_serialMuxProtChannelIdStatus(0U),
m_smpServer(Board::getInstance().getDevice().getStream(), this),
m_mqttClient(),
m_v2vClient(m_mqttClient),
m_v2vCommManager(m_mqttClient),
m_systemStateMachine(),
m_sendWaypointTimer(),
m_commandTimer(),
Expand Down Expand Up @@ -166,9 +166,9 @@ class App
MqttClient m_mqttClient;

/**
* V2V client instance.
* V2V communication manager instance.
*/
V2VClient m_v2vClient;
V2VCommManager m_v2vCommManager;

/**
* The system state machine.
Expand Down Expand Up @@ -200,6 +200,11 @@ class App
*/
SimpleTimer m_statusTimeoutTimer;

/**
* Process V2V communication.
*/
void processV2VCommunication();

private:
/**
* Handler of fatal errors in the Application.
Expand Down
46 changes: 42 additions & 4 deletions lib/ConvoyLeader/src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ void App::setup()
{
LOG_FATAL("Failed to setup MQTT client.");
}
else if (V2VClient::PLATOON_LEADER_ID != settings.getPlatoonVehicleId())
else if (V2VCommManager::PLATOON_LEADER_ID != settings.getPlatoonVehicleId())
{
/* Correct config.json file loaded? */
LOG_FATAL("Platoon Vehicle ID must be 0 for the leader.");
}
else if (false == m_v2vClient.init(settings.getPlatoonPlatoonId(), settings.getPlatoonVehicleId()))
else if (false == m_v2vCommManager.init(settings.getPlatoonPlatoonId(), settings.getPlatoonVehicleId()))
{
LOG_FATAL("Failed to initialize V2V client.");
}
Expand Down Expand Up @@ -220,7 +220,7 @@ void App::loop()
m_mqttClient.process();

/* Process V2V Communication */
m_v2vClient.process();
processV2VCommunication();

/* Process System State Machine */
m_systemStateMachine.process();
Expand Down Expand Up @@ -390,7 +390,7 @@ void App::processPeriodicTasks()

if ((true == m_sendWaypointTimer.isTimeout()) && (true == m_mqttClient.isConnected()))
{
if (false == m_v2vClient.sendWaypoint(m_latestVehicleData))
if (false == m_v2vCommManager.sendWaypoint(m_latestVehicleData))
{
LOG_WARNING("Waypoint could not be sent.");
}
Expand Down Expand Up @@ -449,6 +449,44 @@ void App::processPeriodicTasks()
}
}

void App::processV2VCommunication()
{
V2VCommManager::V2VStatus v2vStatus = V2VCommManager::V2V_STATUS_OK;
V2VCommManager::VehicleStatus vehicleStatus = V2VCommManager::VEHICLE_STATUS_OK;

if (true == ErrorState::getInstance().isActive())
{
vehicleStatus = V2VCommManager::VEHICLE_STATUS_ERROR;
}

v2vStatus = m_v2vCommManager.process(vehicleStatus);

switch (v2vStatus)
{
case V2VCommManager::V2V_STATUS_OK:
/* All good. Nothing to do. */
break;

case V2VCommManager::V2V_STATUS_NOT_INIT:
LOG_WARNING("V2V not initialized.");
break;

case V2VCommManager::V2V_STATUS_LOST_FOLLOWER:
case V2VCommManager::V2V_STATUS_FOLLOWER_ERROR:
LOG_ERROR("Follower Error");
setErrorState();
break;

case V2VCommManager::V2V_STATUS_GENERAL_ERROR:
LOG_ERROR("V2V Communication error.");
setErrorState();
break;

default:
break;
}
}

/******************************************************************************
* External Functions
*****************************************************************************/
Expand Down
13 changes: 9 additions & 4 deletions lib/ConvoyLeader/src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include <MqttClient.h>
#include <SimpleTimer.hpp>
#include <StateMachine.h>
#include <V2VClient.h>
#include <V2VCommManager.h>
#include "SerialMuxChannels.h"

/******************************************************************************
Expand All @@ -72,7 +72,7 @@ class App
m_serialMuxProtChannelIdStatus(0U),
m_smpServer(Board::getInstance().getDevice().getStream(), this),
m_mqttClient(),
m_v2vClient(m_mqttClient),
m_v2vCommManager(m_mqttClient),
m_systemStateMachine(),
m_latestVehicleData(),
m_sendWaypointTimer(),
Expand Down Expand Up @@ -167,9 +167,9 @@ class App
MqttClient m_mqttClient;

/**
* V2V client instance.
* V2V communication manager instance.
*/
V2VClient m_v2vClient;
V2VCommManager m_v2vCommManager;

/**
* The system state machine.
Expand Down Expand Up @@ -231,6 +231,11 @@ class App
*/
void processPeriodicTasks();

/**
* Process V2V communication.
*/
void processV2VCommunication();

private:
/* Not allowed. */
App(const App& app); /**< Copy construction of an instance. */
Expand Down
127 changes: 127 additions & 0 deletions lib/PlatoonService/src/Follower.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* MIT License
*
* Copyright (c) 2023 - 2024 Andreas Merkle <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*******************************************************************************
DESCRIPTION
*******************************************************************************/
/**
* @brief Follower class for V2V communication.
* @author Gabryel Reyes <[email protected]>
*
* @addtogroup PlatoonService
*
* @{
*/
#ifndef FOLLOWER_H
#define FOLLOWER_H

/******************************************************************************
* Compile Switches
*****************************************************************************/

/******************************************************************************
* Includes
*****************************************************************************/

#include <stdint.h>

/******************************************************************************
* Macros
*****************************************************************************/

/******************************************************************************
* Types and Classes
*****************************************************************************/

/** Follower class. */
class Follower
{
public:
/**
* Follower Constructor.
*/
Follower() : m_lastHeartbeatTimestamp(0U), m_status(0U)
{
}

/**
* Follower Destructor.
*/
~Follower()
{
}

/**
* Get the last heartbeat timestamp.
*
* @return Last heartbeat timestamp.
*/
uint32_t getLastHeartbeatTimestamp() const
{
return m_lastHeartbeatTimestamp;
}

/**
* Set the last heartbeat timestamp.
*
* @param[in] timestamp Last heartbeat timestamp.
*/
void setLastHeartbeatTimestamp(uint32_t timestamp)
{
m_lastHeartbeatTimestamp = timestamp;
}

/**
* Get the status.
*
* @return Status.
*/
uint8_t getStatus() const
{
return m_status;
}

/**
* Set the status.
*
* @param[in] status Status.
*/
void setStatus(uint8_t status)
{
m_status = status;
}

private:
/** Last heartbeat timestamp. */
uint32_t m_lastHeartbeatTimestamp;

/** Status. */
uint8_t m_status;
};

/******************************************************************************
* Functions
*****************************************************************************/

#endif /* FOLLOWER_H */
/** @} */
Loading
Loading