Elma
An event loop manager for embedded systems
feedback.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <chrono>
3 #include "elma.h"
4 
6 
7 using namespace std::chrono;
8 using std::vector;
9 using namespace elma;
10 
11 namespace feedback_example {
12 
14 
16  class Car : public Process {
17 
18  public:
21  Car(std::string name) : Process(name) {}
22 
24  void init() {}
25 
28  void start() {
29  velocity = 0;
30  }
31 
36  void update() {
37  if ( channel("Throttle").nonempty() ) {
38  force = channel("Throttle").latest();
39  }
40  velocity += ( delta() / 1000 ) * ( - k * velocity + force ) / m;
41  channel("Velocity").send(velocity);
42  std::cout << "t: " << milli_time() << " ms\t"
43  << " u: " << force << " N\t"
44  << " v: " << velocity << " m/s\n";
45  }
46 
48  void stop() {}
49 
50  private:
51  double velocity;
52  double force;
53  const double k = 0.02;
54  const double m = 1000;
55  };
56 
58 
60  class CruiseControl : public Process {
61 
62  public:
63 
66  CruiseControl(std::string name) : Process(name) {}
67 
69  void init() {}
70 
72  void start() {}
73 
77  void update() {
78  if ( channel("Velocity").nonempty() ) {
79  speed = channel("Velocity").latest();
80  }
81  channel("Throttle").send(-KP*(speed - desired_speed));
82  }
83 
85  void stop() {}
86 
87  private:
88  double speed = 0;
89  const double desired_speed = 50.0,
90  KP = 314.15;
91  vector<double> _v;
92 
93  };
94 
95 }
96 
97 int main() {
98 
99  Manager m;
100 
101  feedback_example::Car car("Car");
102  feedback_example::CruiseControl cc("Control");
103  Channel throttle("Throttle");
104  Channel velocity("Velocity");
105 
106  m.schedule(car, 10_ms)
107  .schedule(cc, 10_ms)
108  .add_channel(throttle)
109  .add_channel(velocity)
110  .init()
111  .run(1000_ms);
112 
113 }
Car(std::string name)
Definition: feedback.cc:21
The Process Manager class.
Definition: manager.h:27
void stop()
Nothing to do to stop.
Definition: feedback.cc:48
Manager & schedule(Process &process, high_resolution_clock::duration period)
Definition: manager.cc:13
void stop()
Nothing to do to stop.
Definition: feedback.cc:85
void init()
Nothing to do to initialize.
Definition: feedback.cc:24
CruiseControl(std::string name)
Definition: feedback.cc:66
Example: A cruise controller for a Car process. See examples/feedback.cc.
Definition: feedback.cc:60
Manager & run(high_resolution_clock::duration runtime)
Definition: manager.cc:190
Manager & add_channel(Channel &)
Definition: manager.cc:43
void start()
Nothing to do to start.
Definition: feedback.cc:72
Manager & init()
Definition: manager.cc:109
void init()
Nothing to do to initialize.
Definition: feedback.cc:69
Example: A car simulation process. See examples/feedback.cc.
Definition: feedback.cc:16
A channel for sending double values to and from Process objects.
Definition: channel.h:21
Definition: channel.cc:5
An abstract base class for processes.
Definition: process.h:24