vulp  2.3.0
Spine.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2022 Stéphane Caron
3 
4 #pragma once
5 
6 #include <mpacklog/Logger.h>
7 
8 #include <algorithm>
9 #include <future>
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
23 #include "vulp/utils/realtime.h"
24 
25 namespace vulp::spine {
26 
27 constexpr size_t kMebibytes = 1 << 20;
28 
45 class Spine {
46  public:
48  struct Parameters {
50  int cpu = -1;
51 
53  unsigned frequency = 1000u;
54 
56  std::string log_path = "/dev/null";
57 
59  std::string shm_name = "/vulp";
60 
62  size_t shm_size = 1 * kMebibytes;
63  };
64 
72  Spine(const Parameters& params, actuation::Interface& interface,
74 
81  void reset(const palimpsest::Dictionary& config);
82 
92  void run();
93 
95  void cycle();
96 
114  void simulate(unsigned nb_substeps);
115 
116  private:
118  void begin_cycle();
119 
129  void cycle_actuation();
130 
132  void end_cycle();
133 
135  void log_working_dict();
136 
137  protected:
139  const unsigned frequency_;
140 
147 
150 
152  std::future<actuation::moteus::Output> actuation_output_;
153 
155  std::vector<actuation::moteus::ServoReply> latest_replies_;
156 
158  palimpsest::Dictionary working_dict_;
159 
162 
164  mpacklog::Logger logger_;
165 
167  std::vector<char> ipc_buffer_;
168 
170  const bool& caught_interrupt_;
171 
174 
177 
180 };
181 
182 } // namespace vulp::spine
Base class for actuation interfaces.
Definition: Interface.h:24
Memory map to shared memory.
Loop transmitting actions to the actuation and observations to the agent.
Definition: Spine.h:45
mpacklog::Logger logger_
Logger for the working_dict_ produced at each cycle.
Definition: Spine.h:164
AgentInterface agent_interface_
Shared memory mapping for inter-process communication.
Definition: Spine.h:149
std::future< actuation::moteus::Output > actuation_output_
Future used to wait for moteus replies.
Definition: Spine.h:152
const unsigned frequency_
Frequency of the spine loop in [Hz].
Definition: Spine.h:139
void run()
Run the spine loop until termination.
Definition: Spine.cpp:80
State state_cycle_beginning_
State after the last Event::kCycleBeginning.
Definition: Spine.h:176
State state_cycle_end_
State after the last Event::kCycleEnd.
Definition: Spine.h:179
void reset(const palimpsest::Dictionary &config)
Reset the spine with a new configuration.
Definition: Spine.cpp:58
StateMachine state_machine_
Internal state machine.
Definition: Spine.h:173
observation::ObserverPipeline observer_pipeline_
Pipeline of observers, executed in that order.
Definition: Spine.h:161
void simulate(unsigned nb_substeps)
Alternative to run where the actuation interface is cycled a fixed number of times,...
Definition: Spine.cpp:102
std::vector< char > ipc_buffer_
Buffer used to serialize/deserialize dictionaries in IPC.
Definition: Spine.h:167
std::vector< actuation::moteus::ServoReply > latest_replies_
Latest servo replies. They are copied and thread-safe.
Definition: Spine.h:155
void cycle()
Spin one cycle of the spine loop.
Definition: Spine.cpp:96
const bool & caught_interrupt_
Boolean flag that becomes true when an interruption is caught.
Definition: Spine.h:170
actuation::Interface & actuation_
Interface that communicates with actuators.
Definition: Spine.h:146
palimpsest::Dictionary working_dict_
All data from observation to action goes to this dictionary.
Definition: Spine.h:158
Spine(const Parameters &params, actuation::Interface &interface, observation::ObserverPipeline &observers)
Initialize spine.
Definition: Spine.cpp:24
Spine state machine.
Definition: StateMachine.h:88
Inter-process communication protocol with the spine.
Definition: __init__.py:1
constexpr size_t kMebibytes
Definition: Spine.h:27
State
States of the state machine.
Definition: StateMachine.h:14
Spine parameters.
Definition: Spine.h:48
size_t shm_size
Size of the shared memory object in bytes.
Definition: Spine.h:62
std::string shm_name
Name of the shared memory object for inter-process communication.
Definition: Spine.h:59
unsigned frequency
Frequency of the spine loop in [Hz].
Definition: Spine.h:53
int cpu
CPUID for the spine thread (-1 to disable realtime).
Definition: Spine.h:50
std::string log_path
Path to output log file.
Definition: Spine.h:56