Skip to content

Commit

Permalink
Merge branch 'add_unicast_support' of github.com:itsarune/Software in…
Browse files Browse the repository at this point in the history
…to add_unicast_support
  • Loading branch information
itsarune committed Jan 19, 2025
2 parents 448125f + 28be87f commit 2e06a07
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/software/embedded/services/network/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include "proto/ip_notification.pb.h"
#include "proto/primitive.pb.h"
#include "proto/robot_status_msg.pb.h"
#include "proto/robot_log_msg.pb.h"
#include "proto/robot_status_msg.pb.h"
#include "shared/constants.h"
#include "shared/robot_constants.h"
#include "software/embedded/services/network/proto_tracker.h"
Expand Down Expand Up @@ -40,8 +40,7 @@ class NetworkService
unsigned short robot_status_sender_port,
unsigned short full_system_to_robot_ip_notification_port,
unsigned short robot_to_full_system_ip_notification_port,
unsigned short robot_logs_port,
const std::string& interface);
unsigned short robot_logs_port, const std::string& interface);

/**
* When the network service is polled, it sends the robot_status and returns
Expand Down
46 changes: 40 additions & 6 deletions src/software/logger/network_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,50 @@ class NetworkLoggerSingleton
static void initializeLogger(int robot_id, bool enable_log_merging);

/**
* Updates the underlying UDP sender associated with this network sink. Useful when a new FullSystem is connected.
*
* @param new_sender the new UDP sender to use
* Updates the underlying UDP sender associated with this network sink. Useful when a
* new FullSystem is connected.
*/
static void replaceUdpSender(std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>> new_sender);
static void replaceUdpSender(
std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>> new_sender)
{
std::shared_ptr<NetworkLoggerSingleton> logger = NetworkLogger();
if (!logger)
{
return;
}
logger->network_sink_handle->call(&NetworkSink::replaceUdpSender, new_sender);
}

private:
NetworkLoggerSingleton(int robot_id, bool enable_log_merging);
static std::shared_ptr<NetworkLoggerSingleton> NetworkLogger()
{
static std::shared_ptr<NetworkLoggerSingleton> s(
new NetworkLoggerSingleton(robot_id, enable_log_merging));
return s;
}

static std::shared_ptr<NetworkLoggerSingleton> instance;
NetworkLoggerSingleton(int robot_id, bool enable_log_merging)
{
logWorker = g3::LogWorker::createLogWorker();

network_sink_handle = logWorker->addSink(
std::make_unique<NetworkSink>(robot_id, enable_log_merging),
&NetworkSink::sendToNetwork);

// Sink for outputting logs to the terminal
auto colour_cout_sink_handle =
logWorker->addSink(std::make_unique<ColouredCoutSink>(true),
&ColouredCoutSink::displayColouredLog);

auto csv_sink_handle = logWorker->addSink(std::make_unique<CSVSink>(CSV_PATH),
&CSVSink::appendToFile);

// Sink for PlotJuggler plotting
auto plotjuggler_handle = logWorker->addSink(std::make_unique<PlotJugglerSink>(),
&PlotJugglerSink::sendToPlotJuggler);

g3::initializeLogging(logWorker.get());
}

std::unique_ptr<g3::LogWorker> logWorker;
std::unique_ptr<g3::SinkHandle<NetworkSink>> network_sink_handle;
Expand Down
3 changes: 2 additions & 1 deletion src/software/logger/network_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void NetworkSink::sendToNetwork(g3::LogMessageMover log_entry)

void NetworkSink::sendOneLogToNetwork(const g3::LogMessage& log)
{
std::optional<std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>>> log_sender;
std::optional<std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>>>
log_sender;
{
std::unique_lock<std::mutex> lock(robot_log_sender_mutex);
log_sender = robot_log_sender;
Expand Down
9 changes: 6 additions & 3 deletions src/software/logger/network_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class NetworkSink
NetworkSink(int robot_id, bool enable_log_merging);

/**
* Replaces the underlying UDP sender with a new one. Intended to be used when a new Full-System node is connected.
* Replaces the underlying UDP sender with a new one. Intended to be used when a new
* Full-System node is connected.
*
* @param new_sender the new UDP sender to use
*/
void replaceUdpSender(std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>> new_sender);
void replaceUdpSender(
std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>> new_sender);

/**
* This function is called on every call to LOG(). It sends a RobotLog proto on the
Expand All @@ -51,5 +53,6 @@ class NetworkSink
LogMerger log_merger;

std::mutex robot_log_sender_mutex;
std::optional<std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>>> robot_log_sender;
std::optional<std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>>>
robot_log_sender;
};
10 changes: 6 additions & 4 deletions src/software/thunderscope/wifi_communication_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def __connect_to_robot(self, robot_id: int) -> None:
except tbots_cpp.TbotsNetworkException:
logger.error(f"Error connecting to robot {robot_id}: {error}")


def __forward_to_proto_unix_io(self, type: Type[Message], data: Message) -> None:
"""Forwards to proto unix IO iff running is true
Expand Down Expand Up @@ -206,7 +205,9 @@ def __receive_robot_status(self, robot_status: Message) -> None:
)
self.__forward_to_proto_unix_io(RobotStatus, robot_status)

def __setup_full_system(self, referee_interface: str, vision_interface: str) -> None:
def __setup_full_system(
self, referee_interface: str, vision_interface: str
) -> None:
"""Connect to the SSL Referee and SSL Vision interfaces.
:param referee_interface: the interface to listen for SSL Referee data
Expand Down Expand Up @@ -240,15 +241,16 @@ def __setup_full_system(self, referee_interface: str, vision_interface: str) ->
SSL_VISION_ADDRESS,
SSL_VISION_PORT,
vision_interface,
lambda data: self.__forward_to_proto_unix_io(SSL_WrapperPacket, data),
lambda data: self.__forward_to_proto_unix_io(
SSL_WrapperPacket, data
),
True,
)
self.current_network_config.vision_interface = vision_interface
except tbots_cpp.TbotsNetworkException as e:
logger.error(f"Error setting up vision interface:\n{e}")
self.current_network_config.vision_interface = DISCONNECTED


def __setup_robot_communication(self, robot_communication_interface: str) -> None:
"""Set up senders and listeners for communicating with the robots
Expand Down

0 comments on commit 2e06a07

Please sign in to comment.