Elma
An event loop manager for embedded systems
Public Member Functions | List of all members
elma::Channel Class Reference

A channel for sending double values to and from Process objects. More...

#include <channel.h>

Public Member Functions

 Channel (string name)
 
 Channel (string name, int capacity)
 
Channelsend (json)
 
Channelflush ()
 
json latest ()
 
json earliest ()
 
int size ()
 
bool empty ()
 
bool nonempty ()
 
string name ()
 
int capacity ()
 

Detailed Description

A channel for sending double values to and from Process objects.

Here is an example that uses channels that enables a model of a car and a simple cruise controller to communicate.

#include <iostream>
#include <chrono>
#include "elma.h"
using namespace std::chrono;
using std::vector;
using namespace elma;
namespace feedback_example {
class Car : public Process {
public:
Car(std::string name) : Process(name) {}
void init() {}
void start() {
velocity = 0;
}
void update() {
if ( channel("Throttle").nonempty() ) {
force = channel("Throttle").latest();
}
velocity += ( delta() / 1000 ) * ( - k * velocity + force ) / m;
channel("Velocity").send(velocity);
std::cout << "t: " << milli_time() << " ms\t"
<< " u: " << force << " N\t"
<< " v: " << velocity << " m/s\n";
}
void stop() {}
private:
double velocity;
double force;
const double k = 0.02;
const double m = 1000;
};
class CruiseControl : public Process {
public:
CruiseControl(std::string name) : Process(name) {}
void init() {}
void start() {}
void update() {
if ( channel("Velocity").nonempty() ) {
speed = channel("Velocity").latest();
}
channel("Throttle").send(-KP*(speed - desired_speed));
}
void stop() {}
private:
double speed = 0;
const double desired_speed = 50.0,
KP = 314.15;
vector<double> _v;
};
}
int main() {
Channel throttle("Throttle");
Channel velocity("Velocity");
m.schedule(car, 10_ms)
.schedule(cc, 10_ms)
.add_channel(throttle)
.add_channel(velocity)
.init()
.run(1000_ms);
}

Definition at line 21 of file channel.h.

Constructor & Destructor Documentation

◆ Channel() [1/2]

elma::Channel::Channel ( string  name)
inline

Constructor

Parameters
nameThe name of the channel

Definition at line 27 of file channel.h.

◆ Channel() [2/2]

elma::Channel::Channel ( string  name,
int  capacity 
)
inline

Constructor

Parameters
nameThe name of the channel
capacityThe maximum number of values to store in the channel

Definition at line 32 of file channel.h.

Member Function Documentation

◆ capacity()

int elma::Channel::capacity ( )
inline

Getter

Returns
The capacity of the channel

Definition at line 57 of file channel.h.

◆ earliest()

json elma::Channel::earliest ( )

Get the oldest value. Throws an error if the channel is empty.

Returns
A reference to the channel, for chaining

Definition at line 38 of file channel.cc.

◆ empty()

bool elma::Channel::empty ( )
inline

Getter

Returns
Whether the channel is empty

Definition at line 45 of file channel.h.

◆ flush()

Channel & elma::Channel::flush ( )

Clear the channel, deleting all values in it

Returns
A reference to the channel, for chaining

Definition at line 20 of file channel.cc.

◆ latest()

json elma::Channel::latest ( )

Get the newest value. Throws an error if the channel is empty.

Returns
A reference to the channel, for chaining

Definition at line 28 of file channel.cc.

◆ name()

string elma::Channel::name ( )
inline

Getter

Returns
The name of the channel

Definition at line 53 of file channel.h.

◆ nonempty()

bool elma::Channel::nonempty ( )
inline

Getter

Returns
Whether the channel is not empty

Definition at line 49 of file channel.h.

◆ send()

Channel & elma::Channel::send ( json  value)

Send a value

Parameters
valueThe value to send into the channel
Returns
A reference to the channel, for chaining

Definition at line 10 of file channel.cc.

◆ size()

int elma::Channel::size ( )
inline

Getter

Returns
The number of values in the channel

Definition at line 41 of file channel.h.


The documentation for this class was generated from the following files: