MotionLib  1.0.0
SamBuCa motion library
MGrblPlatform_Base.cpp
1 /*
2  * Copyright (C) 2022 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2022-07-13
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #include <memory>
26 #include <mutex>
27 #include <vector>
28 
29 #include <ccut/async.hpp>
30 
31 #include "MGrblAxis.hpp"
32 #include "MGrblGpio.hpp"
33 #include "MGrblMainTrigger.hpp"
34 #include "MGrblPlatform.hpp"
35 #include "MGrblPlatformDevice.hpp"
36 #include "device/DeviceBase.hpp"
37 
38 namespace smc {
39 namespace internal {
40 
41 PlatformBase::DeviceTypeList MGrblPlatform::getSupportedDevices() const
42 {
43  return DeviceTypeList{DeviceType::AXIS, DeviceType::TRIGGER,
45 }
46 
47 PlatformBase::DeviceList MGrblPlatform::generateDevices(
48  const DeviceTypeList &deviceType)
49 {
50  DeviceList ret;
51  std::vector<GrblPlatform::Shared> platforms{getPlatforms()};
52 
53  // cppcheck-suppress [knownConditionTrueFalse]
54  if (platforms.empty())
55  return ret;
56 
57  if (deviceType.count(DeviceType::AXIS))
58  {
59  DeviceList axes;
60  for (const std::shared_ptr<GrblPlatform> &p : platforms)
61  {
62  /* override keep only last */
63  axes = p->generateDevices(DeviceTypeList{DeviceType::AXIS});
64  }
65 
66  for (auto &axis : axes)
67  {
68  ret.emplace_back(createAxis(axis->uid(), platforms.back()));
69  }
70  }
71 
72  if (deviceType.count(DeviceType::TRIGGER))
73  {
74  bool hasTriggers = false;
75  for (size_t i = 0; i < platforms.size(); i++)
76  {
77  DeviceList triggers = platforms[i]->generateDevices(
78  DeviceTypeList{DeviceType::TRIGGER});
79  if (triggers.size() == 1)
80  {
81  const std::string platformUrl = triggers.front()->uid() + "/" +
82  std::to_string(i);
83  ret.emplace_back(std::make_shared<MGrblTrigger>(
84  platformUrl, shared_from_this(), platforms[i]));
85  {
86  std::lock_guard<std::mutex> guard{m_lock};
87  m_triggers[platformUrl] = i;
88  }
89  hasTriggers = true;
90  }
91  }
92 
93  if (hasTriggers)
94  {
95  ret.emplace_back(std::make_shared<MGrblMainTrigger>(
96  m_mainTriggerUrl, shared_from_this()));
97  }
98  }
99  if (deviceType.count(DeviceType::GPIO))
100  {
101  for (size_t i = 0; i < platforms.size(); i++)
102  {
103  DeviceList gpios = platforms[i]->generateDevices(
104  DeviceTypeList{DeviceType::GPIO});
105 
106  // Check for Pause/Resume
107  decltype(gpios)::const_iterator it = std::find_if(
108  gpios.begin(), gpios.end(), [](DeviceBase::Shared d) {
109  return (d->uid().find("pauseResume") != std::string::npos);
110  });
111  if (it != gpios.end())
112  {
113  const std::string platformUrl = (*it)->uid() + "/" +
114  std::to_string(i);
115  ret.emplace_back(std::make_shared<MGrblGpio>(
116  platformUrl, shared_from_this(), platforms[i]));
117  {
118  std::lock_guard<std::mutex> guard{m_lock};
119  m_gpios[platformUrl] = i;
120  }
121  }
122  }
123  }
124  if (deviceType.count(DeviceType::PLATFORM))
125  {
126  ret.emplace_back(std::make_shared<MGrblPlatformDevice>(
127  m_ctx->prefix + m_ctx->id + "/platform", shared_from_this(),
128  platforms.front()));
129  }
130  return ret;
131 }
132 
133 void MGrblPlatform::setNotificationWorker(
134  const NotificationWorker::Shared &worker)
135 {
136  PlatformBase::setNotificationWorker(worker);
137  for (const std::shared_ptr<GrblPlatform> &p : getPlatforms())
138  p->setNotificationWorker(worker);
139 }
140 
141 } // namespace internal
142 } // namespace smc
const std::string & to_string(Axis::State state)
convert State to string
Definition: Axis.cpp:78
@ PLATFORM
special device representing the platform itself
@ AXIS
actuator device
@ GPIO
gpio like device
@ TRIGGER
hardware/software trigger device
main motion-lib namespace
Definition: Client.cpp:30