bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
A robot and its trailer, co-simulated in two processes

A robot tows a trailer on a stiff tow bar. The robot, with its speed controller, is one subsystem; the trailer and the bar are another. Each runs in a participant of its own, a thread or a process on any machine that reaches an MQTT broker, and every macro step they exchange the motion of the hitch and the force of the bar, repeating the step until the two agree. The result is that of the same co-simulation in one process, to the last bit.

The speeds, and the force of the bar on the robot

What you need

Nothing beyond bitsim for the threads of one process. For two processes, bitsim built with MQTT and a broker such as mosquitto; see Installing bitsim.

The subsystems

Subsystem State Inputs Outputs
robot, 20 kg position, speed, and the integral of its speed error the force of the bar the hitch: position and speed
trailer, 50 kg, and the bar position and speed the hitch: position and speed the force of the bar on the robot

The robot's PI controller pushes with at most 150 N to follow a speed that rises to 1 m/s, holds, and falls back to 0. The bar is 0.5 m long, with a stiffness of 20 kN/m: it barely stretches, and that is what makes the coupling hard.

Every subsystem a participant

CoSimParticipant runs the strong coupling of Two masses on a stiff spring, co-simulated with one subsystem per participant:

CouplingLayout layout; // the same on every participant
layout.add("robot", 1, 2);
layout.add("trailer", 2, 1);
layout.connect("robot", 0, "trailer", 0);
layout.connect("robot", 1, "trailer", 1);
layout.connect("trailer", 0, "robot", 0);
MQTT_Transport transport(broker, "distributed_towing");
CoSimParticipant me(layout, "robot", *robot, transport, coupling);
me.initialise();
for (...)
me.step(H);
me.leave();

Every try of a macro step, a participant steps its own subsystem, sends its outputs, and waits for those of the other, with bitsim's Synchroniser in lockstep. Then each participant has the outputs of both. From the layout, which both share, each works out the inputs of both subsystems, how far they moved, and whether to try again, so the two decide alike without another message. Both run the same jacobi_step() as CoSimulation does in one process, with the same numbers, so they get its result to the last bit.

Running the example

./distributed_towing # in one process, then two threads on an in-process bus
./distributed_towing --transport mqtt --participant robot &
./distributed_towing --transport mqtt --participant trailer; wait

--macro-step sets the macro step (0.01 s), --broker the broker (tcp://localhost:1883), and --loose couples the two processes loosely.

Output

A robot tows a trailer on a stiff bar for 8 s, macro step 0.010 s
1. In one process: the force of the bar [N]
largest force largest error tries per step
whole system, one solver 57.0
loose coupling 134.7 130.4 1.0
strong coupling 56.9 0.1903 12.1
strong steps that did not converge: 0
2. The robot and the trailer in participants of their own, threads on a bus
9663 tries in 0.596 s: 62 microseconds a try for the exchange and the step
the largest bar force -56.860307151 N
both saw the bar force of the one process at every step, to the last bit: true

As two processes over mosquitto, on this laptop:

robot: 800 macro steps, 9663 tries, in 1.27 s, 131 microseconds a try; the largest bar force -56.860307151 N
trailer: 800 macro steps, 9663 tries, in 1.26 s, 130 microseconds a try; the largest bar force -56.860307151 N

The processes took the same 9663 tries as the threads, and saw the same forces.

Loosely coupled, the bar force is wrong by up to 130 N, more than twice the largest true force, and while the robot cruises it is wrong all the time. That is not noise: with its inputs held, the trailer sees the hitch where it was at the start of the step, 1 cm behind where it is at 1 m/s and 0.01 s, and the bar turns 1 cm into 200 N. The stiffer the coupling and the faster the robot, the larger the error. The speeds look almost right, since the error is a force that the robot's controller mostly cancels, which is why such errors go unnoticed. Strongly coupled, the force is within 0.2 N of the whole system everywhere, for 12 tries per step.

Every try costs a round trip: 62 microseconds between threads, and 131 through mosquitto with set_tcp_nodelay true. Without it mosquitto delays small messages by about 40 ms, and a strong step would take half a second. Strong coupling across processes pays off for subsystems whose steps cost more than a round trip: a physics engine, a detailed model of a motor, or a tool of its own.

Things to try

  1. Run the two processes with --loose. How long do they take, and what is the largest force?
  2. Halve the macro step. How many tries does a strong step take, and how large is the loose error?
  3. Make the bar 10 times stiffer. Up to which macro step does loose coupling stay stable?

The code

// A robot tows a trailer on a stiff tow bar. The robot and its speed controller are one
// subsystem, the trailer and the bar another, each a participant of its own: a thread, or a
// process that meets the other on an MQTT broker. Every macro step they exchange the motion of
// the hitch and the force of the bar, and repeat the step until the two agree.
//
// Usage: distributed_towing [--macro-step H] [--csv towing.csv]
// distributed_towing --transport mqtt --participant robot|trailer [--broker URL]
// [--macro-step H] [--loose]
//
// Without --transport, the example co-simulates in one process, loosely and tightly, against the
// whole system with one solver, and then with the two participants as threads on an in-process
// bus. With --transport mqtt it is one participant; start both:
// ./distributed_towing --transport mqtt --participant robot &
// ./distributed_towing --transport mqtt --participant trailer; wait
#include "bitsim/bitsim_config.h"
#ifdef BITSIM_MQTT
#endif
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
namespace
{
using namespace bitsim::cosim;
using vec = std::vector<real_t>;
const real_t M_ROBOT = 20.0; // [kg]
const real_t M_TRAILER = 50.0; // [kg]
const real_t ROLLING = 5.0; // the rolling resistance of the trailer [N s/m]
const real_t K_BAR = 20000.0; // the stiffness of the tow bar [N/m]
const real_t D_BAR = 100.0; // its damping [N s/m]
const real_t L_BAR = 0.5; // its length [m]
const real_t KP = 400.0, KI = 200.0, F_MAX = 150.0; // the speed controller of the robot
const real_t DURATION = 8.0; // [s]
const uint_t SUBSTEPS = 10;
// the speed the robot is told to drive at: up to 1 m/s, along, and to a stop
real_t target_speed(real_t t)
{
if (t < 1.0)
return 0.0;
if (t < 2.0)
return t - 1.0;
if (t < 5.0)
return 1.0;
if (t < 6.0)
return 6.0 - t;
return 0.0;
}
real_t motor_force(real_t t, real_t v, real_t integral)
{
const real_t e = target_speed(t) - v;
return std::clamp(KP * e + KI * integral, -F_MAX, F_MAX);
}
// the force of the bar on the robot, from the robot's hitch (x, v) and the trailer's
real_t bar_force_on_robot(const vec &robot, const vec &trailer)
{
const real_t stretch = robot[0] - trailer[0] - L_BAR;
return -K_BAR * stretch - D_BAR * (robot[1] - trailer[1]);
}
// the robot: state (x, v, integral of the speed error); input the force of the bar; outputs the
// hitch (x, v)
std::unique_ptr<OdeSubsystem> robot()
{
return std::make_unique<OdeSubsystem>(
vec{0.0, 0.0, 0.0}, 1, 2,
[](real_t t, const vec &x, const vec &u)
{
const real_t f = motor_force(t, x[1], x[2]);
return vec{x[1], (f + u[0]) / M_ROBOT, target_speed(t) - x[1]};
},
[](const vec &x, const vec &) {
return vec{x[0], x[1]};
},
SUBSTEPS);
}
// the trailer and the bar: state (x, v) of the trailer; inputs the robot's hitch (x, v); output
// the force of the bar on the robot
std::unique_ptr<OdeSubsystem> trailer()
{
return std::make_unique<OdeSubsystem>(
vec{-L_BAR, 0.0}, 2, 1,
[](real_t, const vec &x, const vec &u) {
return vec{x[1], (-bar_force_on_robot(u, x) - ROLLING * x[1]) / M_TRAILER};
},
[](const vec &x, const vec &u) { return vec{bar_force_on_robot(u, x)}; }, SUBSTEPS);
}
{
l.add("robot", 1, 2);
l.add("trailer", 2, 1);
l.connect("robot", 0, "trailer", 0);
l.connect("robot", 1, "trailer", 1);
l.connect("trailer", 0, "robot", 0);
return l;
}
CouplingConfig loose() { return CouplingConfig(); }
{
c.tolerance = 1e-9;
c.extrapolate = true; // a better first guess, fewer tries
return c;
}
// the bar force at every macro step
struct Run
{
vec force;
vec robot_v;
vec trailer_v;
uint_t tries{0};
uint_t not_converged{0};
double seconds{0.0};
};
Run in_one_process(const CouplingConfig &config, real_t H)
{
auto r = robot();
auto t = trailer();
CoSimulation cs(config);
cs.add("robot", *r);
cs.add("trailer", *t);
const auto wiring = layout();
for (const auto &c : wiring.connections())
{
cs.connect(c.from, c.output, c.to, c.input);
}
cs.initialise();
Run run;
const auto n = static_cast<uint_t>(std::llround(DURATION / H));
for (uint_t k = 0; k < n; ++k)
{
cs.step(H);
run.force.push_back(cs.inputs("robot")[0]);
run.robot_v.push_back(r->state()[1]);
run.trailer_v.push_back(t->state()[1]);
}
run.tries = cs.total_iterations();
run.not_converged = cs.n_not_converged();
return run;
}
// the whole system with one solver: (x_r, v_r, integral, x_t, v_t)
vec reference_force(real_t H)
{
const auto substeps = static_cast<uint_t>(std::llround(H / 1e-5));
OdeSubsystem whole(
{0.0, 0.0, 0.0, -L_BAR, 0.0}, 0, 1,
[](real_t t, const vec &x, const vec &)
{
const real_t bar = bar_force_on_robot({x[0], x[1]}, {x[3], x[4]});
const real_t f = motor_force(t, x[1], x[2]);
return vec{x[1], (f + bar) / M_ROBOT, target_speed(t) - x[1], x[4],
(-bar - ROLLING * x[4]) / M_TRAILER};
},
[](const vec &x, const vec &) {
return vec{bar_force_on_robot({x[0], x[1]}, {x[3], x[4]})};
},
substeps);
vec force;
const auto n = static_cast<uint_t>(std::llround(DURATION / H));
for (uint_t k = 0; k < n; ++k)
{
whole.do_step(k * H, H, {}, {});
force.push_back(whole.outputs({})[0]);
}
return force;
}
// the value of v furthest from 0, with its sign
real_t largest(const vec &v)
{
return *std::max_element(v.begin(), v.end(),
[](real_t a, real_t b) { return std::abs(a) < std::abs(b); });
}
real_t max_error(const vec &a, const vec &b)
{
real_t e = 0.0;
for (std::size_t k = 0; k < a.size(); ++k)
{
e = std::max(e, std::abs(a[k] - b[k]));
}
return e;
}
// one participant of a distributed run; returns the bar force at every step as it saw it
Run participate(const std::string &name, bitsim::distributed::Transport &transport,
const CouplingConfig &config, real_t H, std::unique_ptr<OdeSubsystem> &mine)
{
mine = name == "robot" ? robot() : trailer();
CoSimParticipant p(layout(), name, *mine, transport, config);
Run run;
const auto start = std::chrono::steady_clock::now();
p.initialise();
const auto n = static_cast<uint_t>(std::llround(DURATION / H));
for (uint_t k = 0; k < n; ++k)
{
p.step(H);
// every participant knows the inputs of all: the force of the bar on the robot
run.force.push_back(p.inputs("robot")[0]);
}
run.seconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
run.tries = p.total_iterations();
run.not_converged = p.n_not_converged();
p.leave();
return run;
}
} // namespace
int main(int argc, char **argv)
{
real_t H = 0.01;
std::string csv_path = "towing.csv", transport = "inprocess", participant,
broker = "tcp://localhost:1883";
bool use_loose = false;
for (int i = 1; i < argc; ++i)
{
const std::string a = argv[i];
auto next = [&]() -> std::string
{
if (i + 1 >= argc)
{
std::cerr << a << " needs a value\n";
std::exit(1);
}
return argv[++i];
};
if (a == "--macro-step")
H = std::stod(next());
else if (a == "--csv")
csv_path = next();
else if (a == "--transport")
transport = next();
else if (a == "--participant")
participant = next();
else if (a == "--broker")
broker = next();
else if (a == "--loose")
use_loose = true;
else
{
std::cerr << "unknown option " << a << "\n";
return 1;
}
}
if (transport == "mqtt")
{
#ifdef BITSIM_MQTT
if (participant != "robot" && participant != "trailer")
{
std::cerr << "--transport mqtt needs --participant robot or trailer\n";
return 1;
}
bitsim::distributed::MQTT_Transport mqtt(broker, "distributed_towing");
std::unique_ptr<OdeSubsystem> mine;
const auto run = participate(participant, mqtt, use_loose ? loose() : strong(), H, mine);
const real_t peak = largest(run.force);
std::printf("%s: %zu macro steps, %u tries, in %.2f s, %.0f microseconds a try; the "
"largest bar force %.9f N\n",
participant.c_str(), run.force.size(), static_cast<unsigned>(run.tries),
run.seconds, 1e6 * run.seconds / run.tries, peak);
return 0;
#else
std::cerr << "this bitsim was built without MQTT\n";
return 1;
#endif
}
if (transport != "inprocess")
{
std::cerr << "--transport is inprocess or mqtt\n";
return 1;
}
std::printf("A robot tows a trailer on a stiff bar for %.0f s, macro step %.3f s\n\n", DURATION,
H);
const auto ref = reference_force(H);
const auto l = in_one_process(loose(), H);
const auto s = in_one_process(strong(), H);
const auto n = ref.size();
auto peak = [](const vec &v)
{
real_t p = 0.0;
for (const real_t f : v)
p = std::max(p, std::abs(f));
return p;
};
std::printf("1. In one process: the force of the bar [N]\n");
std::printf(" largest force largest error tries per step\n");
std::printf(" whole system, one solver %12.1f\n", peak(ref));
std::printf(" loose coupling %12.4g %12.4g %14.1f\n", peak(l.force),
max_error(l.force, ref), static_cast<real_t>(l.tries) / n);
std::printf(" strong coupling %12.1f %12.4g %14.1f\n", peak(s.force),
max_error(s.force, ref), static_cast<real_t>(s.tries) / n);
std::printf(" strong steps that did not converge: %u\n\n",
static_cast<unsigned>(s.not_converged));
std::printf("2. The robot and the trailer in participants of their own, threads on a bus\n");
auto tr = bus.connect();
auto tt = bus.connect();
Run on_robot, on_trailer;
std::unique_ptr<OdeSubsystem> robot_sub, trailer_sub;
std::thread a([&] { on_robot = participate("robot", *tr, strong(), H, robot_sub); });
std::thread b([&] { on_trailer = participate("trailer", *tt, strong(), H, trailer_sub); });
a.join();
b.join();
std::printf(" %u tries in %.3f s: %.0f microseconds a try for the exchange and the step\n",
static_cast<unsigned>(on_robot.tries), on_robot.seconds,
1e6 * on_robot.seconds / on_robot.tries);
std::printf(" the largest bar force %.9f N\n", largest(on_robot.force));
std::printf(" both saw the bar force of the one process at every step, to the last bit: "
"%s\n",
on_robot.force == s.force && on_trailer.force == s.force ? "true" : "false");
std::ofstream csv(csv_path);
csv << "t,target,robot,trailer,force whole system,force strong,force loose\n";
for (std::size_t k = 0; k < n; ++k)
{
const real_t t = (k + 1) * H;
csv << t << "," << target_speed(t) << "," << s.robot_v[k] << "," << s.trailer_v[k] << ","
<< ref[k] << "," << s.force[k] << "," << l.force[k] << "\n";
}
std::printf("\nthe speeds and the bar force are in %s\n", csv_path.c_str());
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
One subsystem of a co-simulation, in a process or on a machine of its own.
Definition cosim_participant.h:44
A co-simulation in one process: the master algorithm that steps coupled subsystems,...
Definition co_simulation.h:37
The subsystems of a co-simulation, their sizes, and which output feeds which input.
Definition coupling.h:35
uint_t add(const std::string &name, uint_t n_inputs, uint_t n_outputs)
Add a subsystem; its index is the number of subsystems before it.
Definition coupling.cpp:12
void connect(const std::string &from, uint_t output, const std::string &to, uint_t input)
Definition coupling.cpp:36
A subsystem given by its equations: the state x moves with dx/dt = f(t, x, u), and the outputs are y ...
Definition cosim_subsystem.h:70
Participants that are threads of one process, joined by queues.
Definition transport.h:57
std::unique_ptr< Transport > connect()
A transport for a new participant.
Definition transport.cpp:37
Participants in several processes, on one machine or several, that meet on an MQTT broker.
Definition mqtt_transport.h:29
How the participants of a distributed simulation reach each other: everything one publishes,...
Definition transport.h:34
int main()
Definition discounted_returns.cpp:19
Definition co_simulation.cpp:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
CartPoleState start()
Definition cart_pole_lqr_mpc.cpp:73
Outcome participant(uint_t agent, Transport &transport, uint_t n_episodes)
Definition distributed_independent_learners.cpp:85
H
Definition extended_kalman_filter.py:42
json run(const Config &c, real_t learning_rate, const fs::path &dir, bitrl::network::TensorboardServer *tensorboard)
Definition log_experiment.cpp:106
real_t largest(const std::vector< real_t > &v)
Definition point_to_point.cpp:46
How a co-simulation couples its subsystems.
Definition coupling.h:102
uint_t max_iterations
JACOBI: the most tries of a macro step; 1 couples loosely.
Definition coupling.h:104
real_t tolerance
JACOBI: done when no input moves by more than this.
Definition coupling.h:105
bool extrapolate
Definition coupling.h:111