Skip to content

Commit

Permalink
Refs #21186: Register instance on construction and move shape locatio…
Browse files Browse the repository at this point in the history
…n to publisher

Signed-off-by: JesusPoderoso <[email protected]>
  • Loading branch information
JesusPoderoso committed Jul 15, 2024
1 parent 58784ab commit f41ccdd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 70 deletions.
6 changes: 0 additions & 6 deletions examples/cpp/topic_instances/CLIParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ class CLIParser
int width = 230;
int height = 265;
std::vector<std::string> colors;
// Non configurable variables
int x = 0;
int y = 0;
CLIParser::ShapeDirection direction = CLIParser::ShapeDirection::DOWN;
// Dependant variables
int lower_th = 0; // size - 5
int horizontal_th = 0; // width - lower_th;
Expand Down Expand Up @@ -656,8 +652,6 @@ class CLIParser
if (config.entity == CLIParser::EntityKind::PUBLISHER)
{
// Calculate shape bounds if applies
config.pub_config.shape_config.x = static_cast<int>(config.pub_config.shape_config.width / 2);
config.pub_config.shape_config.y = static_cast<int>(config.pub_config.shape_config.height / 2);
config.pub_config.shape_config.lower_th = config.pub_config.shape_config.size - 5;
config.pub_config.shape_config.horizontal_th =
config.pub_config.shape_config.width - config.pub_config.shape_config.lower_th;
Expand Down
106 changes: 45 additions & 61 deletions examples/cpp/topic_instances/PublisherApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ PublisherApp::PublisherApp(
{
throw std::runtime_error("DataWriter initialization failed");
}

// Register the instances
int n_dirs = 5; // There are 5 directions: LEFT, RIGHT, UP, DOWN, DIAGONAL
for (int i = 0; i < instances_; i++)
{
InstanceHandle_t instance;
ShapeType shape_;
shape_location shape_loc;
shape_loc.direction = CLIParser::ShapeDirection(i % n_dirs); // different directions per instance
shape_loc.x = static_cast<int>(shape_config_.width / 2);
shape_loc.y = static_cast<int>(shape_config_.height / 2);

// Create ShapeType and configuration associated to the instance
shape_.shapesize(shape_config_.size);
shape_.color(shape_config_.colors[i]);
shape_.x(shape_loc.x);
shape_.y(shape_loc.y);

// Register instance
std::cout << "Registering instance for " << shape_.color() << " shape" << std::endl;
instance = writer_->register_instance(&shape_);
instance_handles_.push_back(instance);

// Store shape and configuration
std::tuple<ShapeType, shape_location, uint32_t> shape_data(shape_, shape_loc, 0);
shapes_[instance] = shape_data;
}
}

PublisherApp::~PublisherApp()
Expand Down Expand Up @@ -185,8 +212,10 @@ void PublisherApp::run()
{
for (int i = 0; i < instances_; i++)
{
writer_->dispose(&shapes_[instance_handles_[i]].first, instance_handles_[i]);
std::cout << shapes_[instance_handles_[i]].first.color() << " shape instance disposed" << std::endl;
InstanceHandle_t instance = instance_handles_[i];
ShapeType shape_ = std::get<0>(shapes_[instance]);
writer_->dispose(&shape_, instance);
std::cout << shape_.color() << " shape instance disposed" << std::endl;
}
}
}
Expand All @@ -203,80 +232,35 @@ void PublisherApp::publish()

if (!is_stopped())
{
// Register instance check
bool instances_already_registered = (instance_handles_.size() > 0);

// Iterate per instance
for (int i = 0; i < instances_; i++)
{
InstanceHandle_t instance;
ShapeType shape_;
CLIParser::shape_configuration shape_configuration;
bool ret = false;

// Register instance if not already registered
if (!instances_already_registered)
{
int n_dirs = 5; // There are 5 directions: LEFT, RIGHT, UP, DOWN, DIAGONAL
CLIParser::ShapeDirection shape_dir = CLIParser::ShapeDirection(i % n_dirs); // different directions

// Create ShapeType and configuration associated to the instance
shape_configuration = CLIParser::shape_configuration(shape_config_);
shape_configuration.direction = shape_dir;
shape_.color(shape_configuration.colors[i]);

// Register instance
std::cout << "Registering instance for " << shape_.color() << " shape" << std::endl;
instance = writer_->register_instance(&shape_);
instance_handles_.push_back(instance);

// Store shape and configuration
std::pair<ShapeType, CLIParser::shape_configuration> shape_data(shape_, shape_configuration);
shapes_[instance] = shape_data;
}
else
{
instance = instance_handles_[i];
shape_ = shapes_[instance].first;
shape_configuration = shapes_[instance].second;
}
InstanceHandle_t instance = instance_handles_[i];
ShapeType shape_ = std::get<0>(shapes_[instance]);
shape_location shape_loc = std::get<1>(shapes_[instance]);
uint32_t samples_sent = std::get<2>(shapes_[instance]);

// Move the shape
move(shape_configuration.x, shape_configuration.y, shape_configuration.direction);
move(shape_loc.x, shape_loc.y, shape_loc.direction);

// Set shape values
shape_.shapesize(shape_configuration.size);
shape_.x(shape_configuration.x);
shape_.y(shape_configuration.y);

// Send shape
ret = writer_->write(&shape_, instance);
shape_.x(shape_loc.x);
shape_.y(shape_loc.y);

// Successfully performed write call
if (RETCODE_OK == ret)
if (RETCODE_OK == writer_->write(&shape_, instance))
{
samples_sent++;
// Print sent shape data
std::cout << shape_.color() << " Shape with size " << shape_.shapesize()
<< " at X:" << shape_.x() << ", Y:" << shape_.y() << " SENT" << std::endl;

// Add the sent sample to the corresponding instance counter
if (samples_per_instance_.count(instance) > 0)
{
// Instance exists in the map, increase the counter
samples_per_instance_[instance]++;
}
else
{
// Instance does not exist in the map, set it with value 1
samples_per_instance_[instance] = 1;
}

// Update sample data
std::pair<ShapeType, CLIParser::shape_configuration> shape_data(shape_, shape_configuration);
std::tuple<ShapeType, shape_location, uint32_t> shape_data(shape_, shape_loc, samples_sent);
shapes_[instance] = shape_data;

// Check if last sample has been sent
if (samples_ > 0 && samples_per_instance_[instance] == samples_)
if (samples_ > 0 && samples_sent == samples_)
{
std::cout << shape_.color() << " shape instance disposed" << std::endl;
writer_->dispose(&shape_, instance);
Expand Down Expand Up @@ -371,11 +355,11 @@ bool PublisherApp::instances_sent_all_samples()
{
bool ret = true;

if (samples_per_instance_.size() > 0)
if (shapes_.size() > 0)
{
for (const auto& instance : samples_per_instance_)
for (const auto& instance : shapes_)
{
if (instance.second < samples_)
if (std::get<2>(instance.second) < samples_)
{
ret = false;
break;
Expand Down
13 changes: 10 additions & 3 deletions examples/cpp/topic_instances/PublisherApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,22 @@ class PublisherApp : public Application, public DataWriterListener

uint16_t instances_;

//! Shape location for movement
struct shape_location
{
int x = 0;
int y = 0;
CLIParser::ShapeDirection direction = CLIParser::ShapeDirection::DOWN;
};

CLIParser::shape_configuration shape_config_;

std::vector<InstanceHandle_t> instance_handles_;

std::map<InstanceHandle_t, std::pair<ShapeType, CLIParser::shape_configuration>> shapes_;

std::map<InstanceHandle_t, uint32_t> samples_per_instance_;
std::map<InstanceHandle_t, std::tuple<ShapeType, shape_location, uint32_t>> shapes_;

std::atomic<bool> stop_;

};

} // namespace topic_instances
Expand Down

0 comments on commit f41ccdd

Please sign in to comment.