Skip to content

Commit

Permalink
Fix the production of indefinite charging tasks (#100)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai>
  • Loading branch information
mxgrey committed Dec 15, 2023
1 parent 5009f92 commit c57cbd1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions rmf_task/include/rmf_task/requests/ChargeBattery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class ChargeBattery
/// Generate the description for this request
static Task::ConstDescriptionPtr make();

/// Make a charging task that will last indefinitely.
static std::shared_ptr<Description> make_indefinite();

// Documentation inherited
Task::ConstModelPtr make_model(
rmf_traffic::Time earliest_start_time,
Expand Down
9 changes: 9 additions & 0 deletions rmf_task/src/rmf_task/requests/ChargeBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ Task::ConstDescriptionPtr ChargeBattery::Description::make()
return description;
}

//==============================================================================
auto ChargeBattery::Description::make_indefinite()
-> std::shared_ptr<Description>
{
std::shared_ptr<Description> description(new Description);
description->set_indefinite(true);
return description;
}

//==============================================================================
ChargeBattery::Description::Description()
: _pimpl(rmf_utils::make_impl<Implementation>(Implementation()))
Expand Down
47 changes: 41 additions & 6 deletions rmf_task/src/rmf_task/requests/ChargeBatteryFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,32 @@

#include <rmf_task/requests/ChargeBatteryFactory.hpp>
#include <rmf_task/requests/ChargeBattery.hpp>
#include <random>

namespace rmf_task {
namespace requests {

//==============================================================================
namespace {
std::string generate_uuid(const std::size_t length = 3)
{
std::stringstream ss;
for (std::size_t i = 0; i < length; ++i)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 255);
const auto random_char = dis(gen);
std::stringstream hexstream;
hexstream << std::hex << random_char;
auto hex = hexstream.str();
ss << (hex.length() < 2 ? '0' + hex : hex);
}
return ss.str();
}

} // anonymous namespace

//==============================================================================
class ChargeBatteryFactory::Implementation
{
Expand Down Expand Up @@ -63,20 +85,33 @@ bool ChargeBatteryFactory::indefinite() const
//==============================================================================
ConstRequestPtr ChargeBatteryFactory::make_request(const State& state) const
{

const std::string id = "Charge" + generate_uuid();
Task::ConstBookingPtr booking;
if (_pimpl->requester.has_value() && _pimpl->time_now_cb)
{
return ChargeBattery::make(
booking =
std::make_shared<const rmf_task::Task::Booking>(
std::move(id),
state.time().value(),
nullptr,
_pimpl->requester.value(),
_pimpl->time_now_cb(),
true);
}
else
{
booking =
std::make_shared<const rmf_task::Task::Booking>(
std::move(id),
state.time().value(),
nullptr,
true);
}
return ChargeBattery::make(
state.time().value(),
nullptr,
true);

const auto description = ChargeBattery::Description::make_indefinite();
description->set_indefinite(_pimpl->indefinite);

return std::make_shared<Request>(std::move(booking), std::move(description));
}

} // namespace requests
Expand Down

0 comments on commit c57cbd1

Please sign in to comment.