MotionLib  1.0.0
SamBuCa motion library
MockPlatform.hpp
1 /*
2  * Copyright (C) 2021 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: 2021-10-29
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #ifndef MOCK_PLATFORM_HPP__
26 #define MOCK_PLATFORM_HPP__
27 
28 #include <cstdint>
29 #include <map>
30 #include <mutex>
31 
32 #include <ccut/yml.hpp>
33 
34 #include "Exception.hpp"
35 #include "Units.hpp"
36 #include "device/DeviceBase.hpp"
37 #include "platform/PlatformBase.hpp"
38 
39 #define MOCK_PLATFORM_STEPS_PER_MM 250
40 #define MOCK_PLATFORM_DELAY_MS 20
41 #define MOCK_PLATFORM_COMMAD_TIMEOUT_MS 40
42 
43 namespace smc {
44 namespace internal {
50 class MockPlatform : public PlatformBase
51 {
52 public:
57  static inline PlatformBase::Shared create(const ccut::yml::NodeRef &config)
58  {
59  return PlatformBase::Shared(new MockPlatform(config));
60  }
61 
62  virtual ~MockPlatform();
63 
64  inline std::shared_ptr<MockPlatform> shared_from_this()
65  {
66  return std::static_pointer_cast<MockPlatform>(
67  PlatformBase::shared_from_this());
68  }
69 
70  inline std::shared_ptr<const MockPlatform> shared_from_this() const
71  {
72  return std::static_pointer_cast<const MockPlatform>(
73  PlatformBase::shared_from_this());
74  }
75 
81  enum class MotorLocation
82  {
83  X,
84  Y,
85  Z,
86  A,
87  B,
88  C
89  };
90  friend std::ostream &operator<<(std::ostream &os, MotorLocation l)
91  {
92  switch (l)
93  {
94  case MotorLocation::X: os << "X"; return os;
95  case MotorLocation::Y: os << "Y"; return os;
96  case MotorLocation::Z: os << "Z"; return os;
97  case MotorLocation::A: os << "A"; return os;
98  case MotorLocation::B: os << "B"; return os;
99  case MotorLocation::C: os << "C"; return os;
100  default: throw Exception(ErrorCode::InvalidArguments);
101  }
102  }
103  static MotorLocation motorFromStr(const std::string &str)
104  {
105  if (str == "X")
106  {
107  return MotorLocation::X;
108  }
109  if (str == "Y")
110  {
111  return MotorLocation::Y;
112  }
113  if (str == "Z")
114  {
115  return MotorLocation::Z;
116  }
117  if (str == "A")
118  {
119  return MotorLocation::A;
120  }
121  if (str == "B")
122  {
123  return MotorLocation::B;
124  }
125  if (str == "C")
126  {
127  return MotorLocation::C;
128  }
129  throw Exception(ErrorCode::InvalidArguments);
130  }
131 
132  using PlatformBase::stop;
133 
134  // PlatformBase overrides
135  DeviceTypeList getSupportedDevices() const override;
136  DeviceList generateDevices(const DeviceTypeList &deviceType) override;
137 
138  size_t getPlatformId() const;
139 
140  void setMotorPosition(const std::string &motor,
141  units::Value::value_type destPosition);
142  units::Value::value_type getMotorPosition(const std::string &motor);
143 
144  units::Value::value_type getLvdtAngle(const std::string &lvdt);
145  void setLvdtAngle(const std::string &lvdt, units::Value::value_type value);
146 
147  void setGpioState(const std::string &gpio, const units::io_port_t &state);
148  units::io_port_t getGpioState(const std::string &gpio);
149 
150  static const std::string s_loggerCat;
151 
152 protected:
153  explicit MockPlatform(const ccut::yml::NodeRef &node);
154 
155  size_t m_platformId;
156 
157  std::mutex m_lock;
158  std::map<std::string, MotorLocation> m_motors;
159  std::map<MotorLocation, units::Value::value_type> m_motorPositions;
160 
161  std::map<std::string, uint8_t> m_lvdts;
162  std::map<uint8_t, units::Value::value_type> m_lvdtPositions;
163 
164  std::map<std::string, uint8_t> m_gpios;
165  std::map<uint8_t, units::io_port_t> m_gpioStates;
166 };
167 
170 } /* namespace internal */
171 } /* namespace smc */
172 
173 #endif /* MOCK_PLATFORM_HPP__ */
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
static PlatformBase::Shared create(const ccut::yml::NodeRef &config)
Create new instance of MockPlatform and return shared_ptr owning it.
MotorLocation
Location of motor within given platform instance (e.g.
main motion-lib namespace
Definition: Client.cpp:30