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

EVALG-77 follow up: Change design to use Last-Value Cache and Read #703

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
9 changes: 2 additions & 7 deletions tutorials/application_design/c++11/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

using CoordSequence = rti::core::bounded_sequence<Coord, 100>;

namespace { // Coord namespace
// Coord namespace

std::string to_string(const Coord &coord)
{
Expand All @@ -29,7 +29,7 @@ std::string to_string(const Coord &coord)
return ss.str();
}

} // namespace
// namespace

namespace rti::core { // bounded_sequence namespace

Expand Down Expand Up @@ -60,11 +60,6 @@ static std::mt19937 gen { rd() };

}; // namespace details

void set_random_seed(unsigned seed)
{
details::gen.seed(seed);
}

double random_range(double min, double max)
{
return std::uniform_real_distribution<>(min, max)(details::gen);
Expand Down
69 changes: 37 additions & 32 deletions tutorials/application_design/c++11/publisher.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class PublisherSimulation {
explicit PublisherSimulation(
dds::pub::DataWriter<VehicleMetrics> metrics_writer,
dds::pub::DataWriter<VehicleTransit> transit_writer)
: metrics_writer_(metrics_writer),
transit_writer_(transit_writer),
vehicle_vin_(utils::new_vin()),
vehicle_fuel_(100.0),
vehicle_route_(utils::new_route()),
vehicle_position_(vehicle_route_[0])
: metrics_writer_(metrics_writer), transit_writer_(transit_writer)
{
auto new_vin = utils::new_vin();
auto new_route = utils::new_route();

metrics_.vehicle_vin(new_vin);
metrics_.fuel_level(100.0);

transit_.vehicle_vin(new_vin);
transit_.current_route(new_route);
transit_.current_position(new_route.front());
}

bool has_ended() const
Expand All @@ -39,74 +43,75 @@ class PublisherSimulation {

void run();

friend std::string to_string(const PublisherSimulation &sim);
std::string to_string() const;

private:
dds::pub::DataWriter<VehicleMetrics> metrics_writer_;
dds::pub::DataWriter<VehicleTransit> transit_writer_;

std::string vehicle_vin_;
double vehicle_fuel_;
CoordSequence vehicle_route_;
Coord vehicle_position_;
VehicleMetrics metrics_;
VehicleTransit transit_;

bool is_out_of_fuel() const
{
return vehicle_fuel_ <= 0.0;
return metrics_.fuel_level() <= 0.0;
}

bool is_on_standby() const
{
return vehicle_route_.empty();
return !transit_.current_route().has_value()
|| transit_.current_route().value().empty();
}
};

void PublisherSimulation::run()
{
while (!has_ended()) {
metrics_writer_.write(VehicleMetrics { vehicle_vin_, vehicle_fuel_ });
metrics_writer_.write(metrics_);

transit_writer_.write(VehicleTransit { vehicle_vin_,
vehicle_position_,
vehicle_route_ });
transit_writer_.write(transit_);

std::this_thread::sleep_for(std::chrono::seconds(1));

if (is_on_standby()) {
std::cout << "Vehicle '" << vehicle_vin_
std::cout << "Vehicle '" << metrics_.vehicle_vin()
<< "' has reached its destination, now moving to a "
"new location..."
<< std::endl;
vehicle_route_ = utils::new_route();
vehicle_route_[0] = vehicle_position_;
auto new_route = utils::new_route();
new_route.front() = transit_.current_position();

transit_.current_route(new_route);
}

vehicle_fuel_ -= 10 * utils::random_stduniform();
vehicle_position_ = vehicle_route_.front();
vehicle_route_.erase(vehicle_route_.begin());
metrics_.fuel_level() -= 10 * utils::random_stduniform();
transit_.current_position(transit_.current_route().value().front());
transit_.current_route().value().erase(
transit_.current_route().value().begin());

if (is_out_of_fuel()) {
vehicle_fuel_ = 0.0;
std::cout << "Vehicle '" << vehicle_vin_ << "' ran out of fuel!"
<< std::endl;
metrics_.fuel_level(0.0);
std::cout << "Vehicle '" << metrics_.vehicle_vin()
<< "' ran out of fuel!" << std::endl;
}
}
}

std::string to_string(const PublisherSimulation &sim)
{
return sim.to_string();
}

std::string PublisherSimulation::to_string() const
{
std::ostringstream ss;
ss << "PublisherSimulation(vehicle_vin: " << sim.vehicle_vin_;
ss << ", vehicle_fuel: " << sim.vehicle_fuel_;
ss << ", vehicle_route: " << to_string(sim.vehicle_route_);
ss << ", vehicle_position: " << to_string(sim.vehicle_position_) << ")";
ss << "PublisherSimulation(metrics: " << metrics_;
ss << ", transit: " << transit_ << ")";
return ss.str();
}

int main(int argc, char **argv)
{
utils::set_random_seed(std::time(nullptr));

rti::domain::register_type<VehicleMetrics>();
rti::domain::register_type<VehicleTransit>();

Expand Down
Loading
Loading