Elma
An event loop manager for embedded systems
Public Types | Public Member Functions | Protected Attributes | Friends | List of all members
elma::Process Class Referenceabstract

An abstract base class for processes. More...

#include <process.h>

+ Inheritance diagram for elma::Process:
+ Collaboration diagram for elma::Process:

Public Types

enum  status_type { UNINITIALIZED, STOPPED, RUNNING }
 

Public Member Functions

 Process (int n=0)
 Default constructor. Names process "no name".
 
 Process (std::string name, int n=0)
 Constructor that takes a name for the process. More...
 
virtual void init ()=0
 
virtual void start ()=0
 
virtual void update ()=0
 
virtual void stop ()=0
 
string name ()
 
void set_name (std::string str)
 
status_type status ()
 
high_resolution_clock::duration period ()
 
int num_updates ()
 
time_point< high_resolution_clock > start_time ()
 
high_resolution_clock::duration last_update ()
 
high_resolution_clock::duration previous_update ()
 
Channelchannel (string name)
 Access a channel with the given name. More...
 
double milli_time ()
 The time since the last update in millisconds, as a double. More...
 
double delta ()
 The most recent amount of time between updates. More...
 
void watch (string event_name, std::function< void(Event &)> handler)
 
void emit (const Event &event)
 
void http_get (std::string url, std::function< void(json &)> handler)
 
void halt ()
 
void set_manager (Manager *m_ptr)
 

Protected Attributes

Manager_manager_ptr
 

Friends

class Manager
 

Detailed Description

An abstract base class for processes.

Derived classes should imlement the init, start, update, and stop methods. For example, here is a process that prints out a message every time its update method is called.

#include <iostream>
#include <chrono>
#include "elma.h"
using namespace std::chrono;
using namespace elma;
namespace basic_example {
class BasicProcess : public Process {
public:
BasicProcess(std::string name) : Process(name) {}
void init() {}
void start() {}
void update() {
std::cout << name() << ": "
<< "ms\n";
}
void stop() {}
};
}
int main() {
Manager m; // Make a manager
basic_example:: BasicProcess p("A"), q("B"); // Make two processes
m.schedule(p, 1_ms) // Schedule the first to run every millisecond
.schedule(q, 5_ms) // Schedule the second to run every 5 milliseconds
.init() // Initialize the manager (which calls init for the processes)
.run(11_ms); // Run for 11 milliseconds
}

Definition at line 24 of file process.h.

Member Enumeration Documentation

◆ status_type

Status of the process, as managed by a Manager. Get the status using the status() getter.

Definition at line 32 of file process.h.

Constructor & Destructor Documentation

◆ Process()

elma::Process::Process ( std::string  name,
int  n = 0 
)
inline

Constructor that takes a name for the process.

Parameters
nameThe name of the process

Definition at line 41 of file process.h.

Member Function Documentation

◆ channel()

Channel & elma::Process::channel ( string  name)

Access a channel with the given name.

Parameters
nameThe name of the channel
Returns
A reference to the channel

Definition at line 11 of file process.cc.

◆ delta()

double elma::Process::delta ( )

The most recent amount of time between updates.

A common use of this method is in the update() method for Euler integration as in xnew = x + delta() * f(x)

Returns
The most recent delta, in milliseconds

Definition at line 54 of file process.cc.

◆ init()

virtual void elma::Process::init ( )
pure virtual

Initialization method. This method should be overridden by derived classes. It will usually be called once, after all processes and communication objects have been added to the manager, but before the Manager starts running.

Implemented in driving_example::Driver, feedback_example::CruiseControl, driving_example::CruiseControl, elma::StateMachine, basic_example::BasicProcess, driving_example::Car, feedback_example::Car, microwave_example::Power, and toggle_switch_example::Trigger.

◆ last_update()

high_resolution_clock::duration elma::Process::last_update ( )
inline

Getter

Returns
The duration of time between the start time and the most recent time the Manager called the update() method.

Definition at line 97 of file process.h.

◆ milli_time()

double elma::Process::milli_time ( )

The time since the last update in millisconds, as a double.

Returns
The time since the last update, in milliseconds

Definition at line 43 of file process.cc.

◆ name()

string elma::Process::name ( )
inline

Getter

Returns
The name of the process.

Definition at line 72 of file process.h.

◆ num_updates()

int elma::Process::num_updates ( )
inline

Getter

Returns
The number of updates since the process was last started by the Manager.

Definition at line 88 of file process.h.

◆ period()

high_resolution_clock::duration elma::Process::period ( )
inline

Getter

Returns
The period of the process.

Definition at line 84 of file process.h.

◆ previous_update()

high_resolution_clock::duration elma::Process::previous_update ( )
inline

Getter

Returns
The duration of time between the start time and the second most recent time the Manager called the update() method.

Definition at line 102 of file process.h.

◆ set_name()

void elma::Process::set_name ( std::string  str)
inline

Setter

Returns
Set the name of the process.

Definition at line 76 of file process.h.

◆ start()

virtual void elma::Process::start ( )
pure virtual

Start method. This method should be overridden by derived classes. It will be called just before the manager starts running. It may be called multiple times, if the manager is started and stopped.

Implemented in driving_example::Driver, driving_example::CruiseControl, feedback_example::CruiseControl, elma::StateMachine, microwave_example::Power, driving_example::Car, feedback_example::Car, basic_example::BasicProcess, and toggle_switch_example::Trigger.

◆ start_time()

time_point<high_resolution_clock> elma::Process::start_time ( )
inline

Getter

Returns
The time the process was last started by the Manager.

Definition at line 92 of file process.h.

◆ status()

status_type elma::Process::status ( )
inline

Getter

Returns
The status of the process

Definition at line 80 of file process.h.

◆ stop()

virtual void elma::Process::stop ( )
pure virtual

Stop method. This method should be overridden by derived classes. It will be called just after the manager stops running. It may be called multiple times, if the manager is started and stopped.

Implemented in driving_example::Driver, driving_example::CruiseControl, feedback_example::CruiseControl, elma::StateMachine, feedback_example::Car, driving_example::Car, microwave_example::Power, basic_example::BasicProcess, and toggle_switch_example::Trigger.

◆ update()

virtual void elma::Process::update ( )
pure virtual

Update method. This method should be overridden by derived classes. It will be called repeatedly by the manager at a frequency determined by the period used when the process is scheduled with the Manager (see Manager::schedule).

Implemented in driving_example::Driver, driving_example::CruiseControl, feedback_example::CruiseControl, elma::StateMachine, microwave_example::Power, driving_example::Car, feedback_example::Car, basic_example::BasicProcess, and toggle_switch_example::Trigger.


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