1#ifndef FASTCAT_MANAGER_H_
2#define FASTCAT_MANAGER_H_
9#include <condition_variable>
15#include <unordered_map>
19#include <yaml-cpp/yaml.h>
21#include "fastcat/thread_safe_queue.h"
22#include "fastcat/device_base.h"
23#include "fastcat/jsd/actuator.h"
24#include "fastcat/jsd/jsd_device_base.h"
29typedef std::pair<std::string, std::shared_ptr<DeviceBase>>
DevicePair;
30typedef std::pair<std::string, jsd_t*>
JSDPair;
73 bool ConfigFromYaml(
const YAML::Node& node,
double external_time = -1);
110 bool Process(
double external_time = -1);
218 fastcat::DeviceStateType);
252 bool ConfigJSDBusFromYaml(
const YAML::Node& node,
double external_time);
253 bool ConfigFastcatBusFromYaml(
const YAML::Node& node,
double external_time);
254 bool ConfigOfflineBusFromYaml(
const YAML::Node& node,
double external_time);
255 bool WriteCommands();
256 bool ConfigSignals();
257 bool SortFastcatDevice(
258 std::shared_ptr<DeviceBase> device,
259 std::vector<std::shared_ptr<DeviceBase>>& sorted_devices,
260 std::vector<std::string> parents);
262 bool LoadActuatorPosFile();
263 bool ValidateActuatorPosFile();
266 std::string PosFilePath()
const;
267 bool SetActuatorPositions();
268 void GetActuatorPositions();
271 std::string BuildActuatorPosYaml();
275 void WritePosFileToDisk(
const std::string& contents);
276 void InvalidateActuatorPosFile();
279 void SyncPosFileDirectory();
282 void StartPosWriter();
283 void StopPosWriter();
284 void PosWriterLoop();
291 uint64_t PostPosWriteRequest(std::string contents,
bool wait);
292 uint64_t PostPosInvalidateRequest(
bool wait);
295 void WaitForPosWriter(uint64_t seq);
300 bool AllBrakesEngaged();
305 void UpdatePositionFileOnBrakeState(
double monotonic_time);
306 bool CheckDeviceNameIsUnique(std::string name);
307 struct JsdBusInitParams {
310 bool enable_autorecovery;
313 double target_loop_rate_hz_ = 0.0;
314 bool zero_latency_required_ =
true;
315 bool faulted_ =
true;
316 bool actuator_fault_on_missing_pos_file_ =
true;
317 bool online_devices_exist_ =
false;
318 std::string actuator_position_directory_;
319 std::map<std::string, jsd_t*> jsd_map_;
321 std::map<std::string, std::shared_ptr<DeviceBase>> device_map_;
322 std::vector<std::shared_ptr<DeviceBase>> fastcat_device_list_;
323 std::vector<std::shared_ptr<JsdDeviceBase>> jsd_device_list_;
324 std::shared_ptr<ThreadSafeQueue<DeviceCmd>> cmd_queue_;
325 std::vector<DeviceState> states_;
326 std::map<std::string, ActuatorPosData> actuator_pos_map_;
327 std::unordered_map<std::string, bool> unique_device_map_;
328 std::shared_ptr<std::queue<SdoResponse>> sdo_response_queue_;
330 std::vector<JsdBusInitParams> pending_jsd_inits_;
332 std::mutex parameter_mutex_;
336 bool prev_all_brakes_engaged_ =
true;
342 bool saw_motion_since_last_save_ =
false;
346 double brakes_engaged_since_ = -1.0;
352 double pos_save_settle_sec_ = 0.5;
361 bool pos_file_enabled_ =
false;
368 std::thread pos_writer_thread_;
369 std::mutex pos_writer_mutex_;
370 std::condition_variable pos_writer_cv_;
371 std::condition_variable pos_writer_done_cv_;
372 std::string pos_pending_contents_;
373 bool pos_pending_write_ =
false;
374 bool pos_pending_invalidate_ =
false;
375 bool pos_writer_stop_ =
false;
377 uint64_t pos_request_seq_ = 0;
378 uint64_t pos_processed_seq_ = 0;
383 std::atomic<bool> pos_writer_running_{
false};
Fastcat::Manager is the main application interface to manage all fastcat devices.
Definition manager.h:36
bool RecoverBus(std::string ifname)
Attempts to recover a faulty JSD bus by name.
Definition manager.cc:505
std::vector< std::shared_ptr< const DeviceState > > GetDeviceStatePointers()
Returns list of device state pointers.
Definition manager.cc:461
double GetTargetLoopRate()
Public getter to the YAML target_loop_rate_hz parameter.
Definition manager.cc:475
bool ExecuteDeviceFault(std::string device_name)
Triggers a single device to fault.
Definition manager.cc:1036
bool CreateConfigFromYaml(const YAML::Node &node, double external_time=-1)
Parses YAML configuration and creates device objects (no hardware init)
Definition manager.cc:164
void SetExplicitInterpolationAlgorithmLinear()
Set interpolation algorithm to use 1st order linear interpolation between knot points for both positi...
Definition manager.cc:1802
void SaveActuatorPositions()
Capture current actuator positions and write them to file.
Definition manager.cc:114
bool IsSdoResponseQueueEmpty()
checks if the SdoResponse Queue is empty
Definition manager.cc:1107
void ExecuteAllDeviceFaults()
Triggers all devices to Fault.
Definition manager.cc:1076
bool ConfigFromYaml(const YAML::Node &node, double external_time=-1)
Method that accepts a fastcat topology yaml and intializes bus.
Definition manager.cc:291
void ExecuteAllDeviceResets()
Triggers all devices to Reset.
Definition manager.cc:1094
void QueueCommand(DeviceCmd &cmd)
Interface to command devices on the bus.
Definition manager.cc:445
void SetExplicitInterpolationAlgorithmCubic()
Set interpolation algorithm to use 3rd order cubic interpolation between knot points for all actuator...
Definition manager.cc:1789
bool InitHardware()
Initializes EtherCAT hardware (executes deferred jsd_init calls)
Definition manager.cc:300
void SetExplicitInterpolationTimestampSourceClock()
Set interpolation algorithm to use the timestamp when the CSP message was received according to fastc...
Definition manager.cc:1828
bool GetActuatorParams(const std::string &name, fastcat::Actuator::ActuatorParams ¶m)
get actuator parameters
Definition manager.cc:490
void Shutdown()
Shutdown the bus and joins all threads.
Definition manager.cc:109
bool IsFaulted()
Public getter retrieve fault status.
Definition manager.cc:477
void SetExplicitInterpolationTimestampSourceCspMessage()
Set interpolation algorithm to use the timestamp in the CSP message generated by the calling module f...
Definition manager.cc:1815
std::vector< DeviceState > GetDeviceStates()
Returns list of device states.
Definition manager.cc:447
bool ExecuteDeviceReset(std::string device_name)
Triggers a single device to reset.
Definition manager.cc:1056
bool Process(double external_time=-1)
Updates synchronous PDO and background async SDO requests.
Definition manager.cc:336
void GetDeviceNamesByType(std::vector< std::string > &, fastcat::DeviceStateType)
names of actuator devices
Definition manager.cc:479
bool SetInterpolationCyclesStale(size_t cycles)
CSP interpolation will transition to a holding state if it has not received a CSP message within the ...
Definition manager.cc:1858
bool SetExplicitInterpolationCyclesDelay(size_t delay)
Set number of cycles of the calling module to delay the onset of explicit interpolation,...
Definition manager.cc:1841
Manager()
Definition manager.cc:89
~Manager()
Definition manager.cc:95
bool PopSdoResponseQueue(SdoResponse &res)
get the result of a background SDO operation
Definition manager.cc:1112
Definition device_base.h:18
std::pair< std::string, std::shared_ptr< DeviceBase > > DevicePair
Definition manager.h:29
std::pair< std::string, jsd_t * > JSDPair
Definition manager.h:30
Definition actuator.h:110