Skip to content

Commit

Permalink
Daemon: Fix compilation and remove weak_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Mar 19, 2024
1 parent 52c7d2f commit 75c9796
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace behaviour_tree
this->car_system->getMessagingSystem()->getCommandSignal().connect(std::bind(&BehaviourTreeHandler::handleCommand, this, std::placeholders::_1, std::placeholders::_2));
}

void handleCommand(const std::string message, const rapidjson::Value &message_json)
void handleCommand(const std::string message, const rapidjson::Document& message_json)
{
if (message_json["command"].GetString() != "behaviour_tree")
{
Expand Down Expand Up @@ -80,9 +80,8 @@ namespace behaviour_tree
{
assert(this->behaviour_tree != nullptr);
assert(this->car_system != nullptr);

this->tick_count = 0;
std::shared_ptr<Context> context = std::make_shared<CarContext>(CarContext(this->behaviour_tree, this->car_system));
std::shared_ptr<Context> context = std::make_shared<CarContext>(this->behaviour_tree, this->car_system);
this->context = context;
}

Expand Down
24 changes: 11 additions & 13 deletions app/rpi/common/include/car/plugin/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ namespace car::plugin
public:
void initialize(std::shared_ptr<system::CarSystem> car_system)
{
for (std::weak_ptr<Plugin>& plugin : this->plugins)
for (std::shared_ptr<Plugin>& plugin : this->plugins)
{
plugin.lock()->initialize(car_system);
plugin->initialize(car_system);
}
}

void update()
{
for (std::weak_ptr<Plugin>& plugin : this->plugins)
spdlog::info("Updating plugins: {}", this->plugins.size());
for (std::shared_ptr<Plugin>& plugin : this->plugins)
{
plugin.lock()->update();
plugin->update();
}
}

void stop()
{
for (std::weak_ptr<Plugin>& plugin : this->plugins)
for (std::shared_ptr<Plugin>& plugin : this->plugins)
{
plugin.lock()->stop();
plugin->stop();
}
}

Expand All @@ -62,14 +63,11 @@ namespace car::plugin
std::string type_name = std::string(utils::TypeName<T>());
type_name = utils::getStringAfterLastColon(type_name);

for (std::weak_ptr<Plugin>& plugin : this->plugins)
for (std::shared_ptr<Plugin>& plugin : this->plugins)
{
if (auto locked_plugin = plugin.lock())
if (plugin->getName() == type_name)
{
if (locked_plugin->getName() == type_name)
{
return std::static_pointer_cast<T>(locked_plugin);
}
return plugin;
}
}

Expand All @@ -78,7 +76,7 @@ namespace car::plugin


private:
std::vector<std::weak_ptr<Plugin>> plugins;
std::vector<std::shared_ptr<Plugin>> plugins;
};
}

Expand Down
4 changes: 2 additions & 2 deletions app/rpi/common/include/car/system/messaging/MessagingSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace car::system::messaging
// Necessary for the reloading the configuration
void setConfiguration(std::shared_ptr<configuration::Configuration> configuration);

nod::signal<void(const std::string, const rapidjson::Value&)>& getCommandSignal() { return this->command_signal; }
nod::signal<void(const std::string, const rapidjson::Document&)>& getCommandSignal() { return this->command_signal; }
nod::signal<void(const std::string)>& getMessageSignal() { return this->message_signal; }
nod::signal<void(const std::string)>& getDisconnectSignal() { return this->on_disconnect_signal; }

Expand All @@ -51,7 +51,7 @@ namespace car::system::messaging
nod::signal<void(std::string)> on_disconnect_signal;

nod::signal<void(const std::string)> message_signal;
nod::signal<void(const std::string, const rapidjson::Value&)> command_signal;
nod::signal<void(const std::string, const rapidjson::Document&)> command_signal;

private:
tl::expected<std::string, std::string> getFirstMessage();
Expand Down
3 changes: 2 additions & 1 deletion app/rpi/common/src/car/system/CarSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ namespace car::system
{
if (this->messaging_system->isConnected())
{
this->messaging_system->sendMessage(this->lidar_device->getLidarMessage());
const std::string lidar_message = this->lidar_device->getLidarMessage();
this->messaging_system->sendMessage(lidar_message);
}
this->plugin_manager->update();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace car::system::messaging

if (!message_json.HasMember("type") || !message_json["type"].IsString())
{
spdlog::error("Type does not exist in json", message);
spdlog::error("Type does not exist in json: {}", message);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion app/rpi/daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ This will only work for Linux.
`xmake uninstall --admin rpi_daemon`

## Speedrun
`xmake && sudo systemctl stop rpi_daemon && xmake install --admin rpi_daemon && sudo systemctl daemon-reload && sudo systemctl start rpi_daemon`
`sudo systemctl stop rpi_daemon && xmake && xmake install --admin rpi_daemon && sudo systemctl daemon-reload && sudo systemctl start rpi_daemon`
48 changes: 24 additions & 24 deletions app/rpi/daemon/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class rpi_daemon : public daemon
{
if (reader.ParseError() < 0)
{
spdlog::critical("Could not load 'rpi_daemon.service'\n");
dlog::critical("Could not load 'rpi_daemon.service'\n");
return;
}

Expand All @@ -52,65 +52,65 @@ class rpi_daemon : public daemon
this->connection_interval = std::chrono::seconds(reader.GetUnsigned("RaspberryPi", "connection_interval", 1));
std::string car_name = reader.GetString("RaspberryPi", "car_name", "");

spdlog::info("Started daemon with host: " + host + " and car_name: " + car_name + "\n");
dlog::info("Started daemon with host: " + host + " and car_name: " + car_name + "\n");

std::shared_ptr<Configuration> configuration = std::make_shared<Configuration>(Configuration{host, car_name});
this->any_configuration_empty = host.empty() || car_name.empty();
if (this->any_configuration_empty)
{
spdlog::warn("A property in the configuration is empty, this daemon will not run with an empty property.");
dlog::warning("A property in the configuration is empty, this daemon will not run with an empty property.");
}

spdlog::info("Created the Configuration");
dlog::info("Created the Configuration");

spdlog::info("Starting to create the Sub Systems");
dlog::info("Starting to create the Sub Systems");

std::unique_ptr<LidarDevice> lidar_device = getLidarDevice();
spdlog::info("Created the LidarDevice");
dlog::info("Created the LidarDevice");

std::unique_ptr<MessagingSystem> messaging_system = std::make_unique<MessagingSystem>();
spdlog::info("Created the MessengingSystem");
dlog::info("Created the MessengingSystem");

std::unique_ptr<MovementSystem> movement_system = std::make_unique<MovementSystem>(std::make_unique<DummyMovementController>());
// std::unique_ptr<MovementSystem> movement_system = std::make_unique<MovementSystem>(std::make_unique<DeviceMovementController>());
spdlog::info("Created the MovementSystem");
dlog::info("Created the MovementSystem");

std::shared_ptr<BehaviourTreeHandler> behaviour_tree_handler = std::make_shared<BehaviourTreeHandler>(BehaviourTreeHandler());
spdlog::info("Created the BehaviourTreeHandler");
std::shared_ptr<BehaviourTreeHandler> behaviour_tree_handler = std::make_shared<BehaviourTreeHandler>();
dlog::info("Created the BehaviourTreeHandler");

std::unique_ptr<PluginManager> plugin_manager = std::make_unique<PluginManager>();
spdlog::info("Created the PluginManager");
dlog::info("Created the PluginManager");

plugin_manager->addPlugin(behaviour_tree_handler);
spdlog::info("Added the BehaviourTreeHandler to the PluginManager");
dlog::info("Added the BehaviourTreeHandler to the PluginManager");

spdlog::info("Creating the Car System");
dlog::info("Creating the Car System");
this->car_system = std::make_shared<CarSystem>(
configuration,
std::move(lidar_device),
std::move(messaging_system),
std::move(movement_system),
std::move(plugin_manager));

spdlog::info("Initializing the Car System");
dlog::info("Initializing the Car System");
this->car_system->initialize();
spdlog::info("Completed initializing the Car System");
dlog::info("Completed initializing the Car System");

spdlog::info("Starting up the Car System");
dlog::info("Starting up the Car System");
this->car_system->start();
spdlog::info("Completed starting the Car System");
dlog::info("Completed starting the Car System");

spdlog::info("Completed starting up Daemon");
dlog::info("Completed starting up Daemon");
}

inline void update()
void update()
{
if (this->any_configuration_empty)
{
return;
}

std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
const std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
const bool CAN_CONNECT = !this->car_system->getMessagingSystem()->isConnected() && now - this->last_connected >= this->connection_interval;
if (CAN_CONNECT)
{
Expand All @@ -119,7 +119,7 @@ class rpi_daemon : public daemon
this->car_system->update();
}

inline void connect(std::chrono::time_point<std::chrono::steady_clock> &now)
void connect(const std::chrono::time_point<std::chrono::steady_clock> &now)
{
if (!this->attempted_to_reconnect)
{
Expand Down Expand Up @@ -185,7 +185,7 @@ class rpi_daemon : public daemon
this->any_configuration_empty = host.empty() || car_name.empty();
if (this->any_configuration_empty)
{
spdlog::warn("A property in the configuration is empty, this daemon will not run with an empty property.");
dlog::warning("A property in the configuration is empty, this daemon will not run with an empty property.");
}

this->car_system->setConfiguration(std::move(configuration));
Expand Down Expand Up @@ -290,12 +290,12 @@ std::unique_ptr<LidarDevice> getLidarDevice()
#endif
if (maybe_scanner.has_value())
{
spdlog::info("Found and using Lidar Scanner\n");
dlog::info("Found and using Lidar Scanner\n");
return std::move(maybe_scanner.value());
}
else
{
spdlog::warn("Unable to connect to the Lidar Scanner, defaulting to LidarDummy\n");
dlog::warning("Unable to connect to the Lidar Scanner, defaulting to LidarDummy\n");
return std::make_unique<LidarDummy>();
}
}

0 comments on commit 75c9796

Please sign in to comment.