MotionLib  1.0.0
SamBuCa motion library
MockPositionSensor.cpp
1 /*
2 ** Copyright (C) 2025 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: 2025-04-16T13:38:08
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "MockPositionSensor.hpp"
25 
26 #include <ccut/async.hpp>
27 
28 #include "MockPlatform.hpp"
29 #include "device/PositionSensor.hpp"
30 
31 using namespace logger;
32 using namespace smc;
33 using namespace smc::internal;
34 
35 MockPositionSensor::MockPositionSensor(
36  const std::string &uid,
37  const std::shared_ptr<MockPlatform> &mock) :
38  PositionSensor{uid},
39  m_mock(mock)
40 {}
41 
42 std::future<units::value_t> MockPositionSensor::getPosition(
43 
44  units::unit_t unit) const
45 {
46  std::shared_ptr<MockPlatform> mock(m_mock.lock());
47  if (!mock)
48  return ccut::make_future_error<units::value_t>(
49  Exception(ErrorCode::Canceled, "platform stopped (deleted)"));
50 
51  info(MockPlatform::s_loggerCat)
52  << "Querying POSITION " << uid()
53  << " on MockPlatform id: " << mock->getPlatformId() << " for position";
54  std::promise<units::value_t> positionPromise;
55  auto positionFuture = positionPromise.get_future();
56  try
57  {
58  units::Value lvdtPosition{units::unit_t::MILLIMETERS,
59  mock->getLvdtAngle(uid())};
60  if (!convert(lvdtPosition, unit))
61  throw Exception(ErrorCode::IncompatibleUnits);
62 
63  positionPromise.set_value(lvdtPosition);
64  }
65  catch (...)
66  {
67  positionPromise.set_exception(std::current_exception());
68  }
69  return positionFuture;
70 }
71 
73  const units::value_t &value) const
74 {
75  std::shared_ptr<MockPlatform> mock(m_mock.lock());
76  if (!mock)
77  return ccut::make_future_error(
78  Exception(ErrorCode::Canceled, "platform stopped (deleted)"));
79 
80  info(MockPlatform::s_loggerCat)
81  << "Set POSITION " << uid()
82  << " on MockPlatform id: " << mock->getPlatformId();
83  std::promise<void> prom;
84  try
85  {
86  units::value_t lvdtPosition{value};
87  if (!convert(lvdtPosition, units::MILLIMETERS))
88  throw Exception(ErrorCode::IncompatibleUnits);
89 
90  mock->setLvdtAngle(uid(), lvdtPosition.value);
91  prom.set_value();
92  }
93  catch (...)
94  {
95  prom.set_exception(std::current_exception());
96  }
97  return prom.get_future();
98 }
99 
101 {
102  std::shared_ptr<MockPlatform> mock(m_mock.lock());
103  if (!mock)
104  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
105 
106  info(MockPlatform::s_loggerCat)
107  << "Get last POSITION " << uid()
108  << " on MockPlatform id: " << mock->getPlatformId();
109 
110  units::value_t ret{units::unit_t::MILLIMETERS, mock->getLvdtAngle(uid())};
111 
112  if (!convert(ret, unit))
113  throw Exception(ErrorCode::IncompatibleUnits);
114 
115  return ret;
116 }
virtual bool convert(units::Value &value, units::unit_t unit) const
Convert value in a manner specific to this device.
Definition: DeviceBase.cpp:86
DeviceId uid() const
Get the address of device.
Definition: DeviceBase.cpp:76
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
units::value_t lastPosition(units::unit_t unit) const override
Get last known position.
std::future< void > setActualPosition(const units::value_t &value) const override
Set the current position value.
main motion-lib namespace
Definition: Client.cpp:30