Skip to content

sim_control.hpp#

ROS/acres_sim/include/acres_sim/sim_control.hpp Generated

Client of the game's simulator-control channel (Documentation/Reference/sim-control.md, -SimControl=<port>) that offers it on ROS 2 at the root namespace: the simulation_interfaces services and action (/get_simulator_features, /get_simulation_state, /set_simulation_state, /step_simulation, /simulate_steps, /reset_simulation, /get_entities, /get_entity_state, /set_entity_state, /spawn_entity, /delete_entity, /get_spawnables, /get_named_poses), the ACRES services (/acres/set_conditions, /acres/set_vehicle_shift, /acres/record, /acres/farm_state, /acres/field_query) and the topic /sim/episode (transient local).

Fields map one to one onto the channel's JSON: a pose is {"position": [x, y, z], "orientation": [qx, qy, qz, qw]}, a twist {"linear": [..], "angular": [..]}, a Vector3 [x, y, z]; the channel's result codes pass through into simulation_interfaces/Result.

Lockstep: /step_simulation and /simulate_steps run in the vehicle bridges' command callback group, so the DBW and guidance commands published before a request are forwarded to the game first (rclcpp runs ready subscriptions before services and waitables, and the request handler also drains whatever the subscriptions still hold); then the step request goes out, and the answer waits for the game's reply and for every bridge's barriers (VehicleBridge::wait_barrier), so all the step's sensor data and reports are on ROS when it arrives.

pose_to_json#

{"position": [x, y, z], "orientation": [qx, qy, qz, qw]} of a pose.

Json pose_to_json(const geometry_msgs::msg::Pose& pose);

pose_from_json#

The pose of {"position": [..], "orientation": [..]} (missing parts: origin, identity).

geometry_msgs::msg::Pose pose_from_json(const Json& json);

twist_to_json#

{"linear": [..], "angular": [..]} of a twist.

Json twist_to_json(const geometry_msgs::msg::Twist& twist);

twist_from_json#

The twist of {"linear": [..], "angular": [..]}.

geometry_msgs::msg::Twist twist_from_json(const Json& json);

vector_to_json#

[x, y, z] of a vector.

Json vector_to_json(const geometry_msgs::msg::Vector3& v);

result_from_reply#

A simulation_interfaces/Result from a reply ("result", "error"), or RESULT_OPERATION_FAILED with error when there is no reply.

Argument Description
reply The channel's reply, or nothing.
error Why there is none.

Returns: The result.

simulation_interfaces::msg::Result result_from_reply(const std::optional<Json>& reply, const std::string& error);

episode_from_json#

The Episode message of an "episode" event (fields of acres_interfaces/Episode; header stamp from "time_s").

acres_interfaces::msg::Episode episode_from_json(const Json& event);

SimControl#

class SimControl

The control client and its ROS interfaces.

Name Type Unit Default Description
BridgeList using The vehicle bridges of the process, whose barriers a step waits for.
HelloCallback using Called on every connection with the game's hello reply (on the reader thread).

SimControl::SimControl#

Creates the services, the action and /sim/episode on node (absolute names).

Argument Description
node The process's main node.
host The game's host (loopback only).
port The -SimControl= port.
command_group The vehicle bridges' command callback group (step service and action).
bridges The vehicle bridges.
barrier_timeout_s How long a step waits for the barriers after the game's reply, s.
request_timeout_s Timeout of a request, s (a step adds 0.2 s per step).
SimControl(rclcpp::Node::SharedPtr node, const std::string& host, int port, rclcpp::CallbackGroup::SharedPtr command_group, BridgeList bridges, double barrier_timeout_s, double request_timeout_s);

SimControl::~SimControl#

Stops the client.

~SimControl();

SimControl::on_hello#

Sets the hello callback (before start()).

void on_hello(HelloCallback callback) ;

SimControl::start#

Connects (and reconnects forever) in the background.

void start();

SimControl::stop#

Disconnects, fails the pending requests and ends the running action.

void stop();

SimControl::request#

Sends one request and waits for its reply.

Argument Description
request The request object (op and fields; the id is added).
timeout_s Timeout, s.
error Why there is no reply.
on_progress Called with every "progress" event of this request (reader thread).

Returns: The reply, or nothing (not connected, closed, timed out).

std::optional<Json> request(Json request, double timeout_s, std::string* error, std::function<void(const Json&)> on_progress = ;);

SimControl::connected#

True while connected.

bool connected() const ;