25 #include "MockPlatform.hpp"
31 #include <ccut/yml.hpp>
32 #include <logger/Logger.hpp>
34 #include "Exception.hpp"
35 #include "MockAxis.hpp"
36 #include "MockGpio.hpp"
37 #include "MockPositionSensor.hpp"
40 using namespace logger;
41 namespace yml = ccut::yml;
43 static const std::string s_platformPrefix =
"mock://";
44 static const uint32_t s_motorStepsPerMM = 250;
45 static const float s_lvdtStepsPerDegree = 4;
46 static const int32_t s_lvdtConstDegrees = 90;
50 using namespace units;
54 const std::string MockPlatform::s_loggerCat{
"smc:mock"};
56 MockPlatform::~MockPlatform() {}
58 MockPlatform::MockPlatform(
const yml::NodeRef &node)
60 yml::get(node,
"id") >> yml::default_to(m_platformId, 0);
62 yml::NodeRef elts = yml::get(node,
"axis");
64 info(s_loggerCat) <<
"no axis configured";
65 else if (!elts.is_seq())
66 throw Exception(ErrorCode::InvalidArguments,
"invalid axis type");
69 for (yml::NodeRef elt : elts)
73 MotorLocation motor = motorFromStr(token);
75 std::stringstream uri;
76 uri << s_platformPrefix << m_platformId <<
"/axis/" << motor;
77 debug(s_loggerCat) <<
"motor created: " << uri.str();
78 m_motors[uri.str()] = motor;
82 elts = yml::get(node,
"positions");
84 info(s_loggerCat) <<
"no positions configured";
85 else if (!elts.is_seq())
86 throw Exception(ErrorCode::InvalidArguments,
"invalid positions type");
89 for (yml::NodeRef elt : elts)
94 std::stringstream uri;
95 uri << s_platformPrefix << m_platformId <<
"/position/"
97 debug(s_loggerCat) <<
"position created: " << uri.str();
98 m_lvdts[uri.str()] = token;
102 elts = yml::get(node,
"gpios");
104 info(s_loggerCat) <<
"no gpios configured";
105 else if (!elts.is_seq())
106 throw Exception(ErrorCode::InvalidArguments,
"invalid gpios type");
109 for (yml::NodeRef elt : elts)
114 std::stringstream uri;
115 uri << s_platformPrefix << m_platformId <<
"/gpio/" << int(token);
116 debug(s_loggerCat) <<
"gpio created: " << uri.str();
117 m_gpios[uri.str()] = token;
122 for (
const auto &motor : m_motors)
125 m_motorPositions.insert(
126 std::pair<MotorLocation, units::Value::value_type>(motor.second,
131 for (
const auto &lvdt : m_lvdts)
133 m_lvdtPositions.insert(std::pair<uint8_t, units::Value::value_type>(
134 lvdt.second, s_lvdtConstDegrees));
138 for (
const auto &gpio : m_gpios)
141 std::pair<uint8_t, io_port_t>(gpio.second, IOPort{false}));
145 size_t MockPlatform::getPlatformId()
const
147 return (m_platformId);
150 void MockPlatform::setMotorPosition(
const std::string &motor,
151 units::Value::value_type destPosition)
153 std::lock_guard<std::mutex> lock(m_lock);
154 auto it = m_motors.find(motor);
156 if (it == m_motors.end())
159 <<
"Bad motor address (" << motor
160 <<
") referenced on MockPlatform id: " << m_platformId;
161 throw Exception(ErrorCode::InvalidArguments);
164 m_motorPositions.find(it->second)->second = destPosition;
167 units::Value::value_type MockPlatform::getMotorPosition(
const std::string &motor)
169 std::lock_guard<std::mutex> lock(m_lock);
170 auto it = m_motors.find(motor);
172 if (it == m_motors.end())
175 <<
"Bad motor address (" << motor
176 <<
") referenced on MockPlatform id: " << m_platformId;
177 throw Exception(ErrorCode::InvalidArguments);
180 return m_motorPositions.find(it->second)->second;
183 units::Value::value_type MockPlatform::getLvdtAngle(
const std::string &lvdt)
185 std::lock_guard<std::mutex> lock(m_lock);
186 auto it = m_lvdts.find(lvdt);
188 if (it == m_lvdts.end())
191 <<
"Bad lvdt address (" << lvdt
192 <<
") referenced on MockPlatform id: " << m_platformId;
193 throw Exception(ErrorCode::InvalidArguments);
196 return m_lvdtPositions.find(it->second)->second;
199 void MockPlatform::setLvdtAngle(
const std::string &lvdt,
200 units::Value::value_type value)
202 std::lock_guard<std::mutex> lock(m_lock);
203 auto it = m_lvdts.find(lvdt);
205 if (it == m_lvdts.end())
208 <<
"Bad lvdt address (" << lvdt
209 <<
") referenced on MockPlatform id: " << m_platformId;
210 throw Exception(ErrorCode::InvalidArguments);
213 m_lvdtPositions.find(it->second)->second = value;
216 void MockPlatform::setGpioState(
const std::string &gpio,
217 const units::io_port_t &state)
219 std::lock_guard<std::mutex> lock(m_lock);
220 auto it = m_gpios.find(gpio);
222 if (it == m_gpios.end())
225 <<
"Bad gpio address (" << gpio
226 <<
") referenced on MockPlatform id: " << m_platformId;
227 throw Exception(ErrorCode::InvalidArguments);
230 m_gpioStates.find(it->second)->second = state;
233 io_port_t MockPlatform::getGpioState(
const std::string &gpio)
235 std::lock_guard<std::mutex> lock(m_lock);
236 auto it = m_gpios.find(gpio);
238 if (it == m_gpios.end())
241 <<
"Bad gpio address (" << gpio
242 <<
") referenced on MockPlatform id: " << m_platformId;
243 throw Exception(ErrorCode::InvalidArguments);
246 return m_gpioStates.find(it->second)->second;
249 std::set<DeviceBase::DeviceType> MockPlatform::getSupportedDevices()
const
251 std::set<DeviceBase::DeviceType> supported = {
252 DeviceBase::DeviceType::AXIS, DeviceBase::DeviceType::POSITION_SENSOR,
253 DeviceBase::DeviceType::GPIO};
257 PlatformBase::DeviceList MockPlatform::generateDevices(
258 const DeviceTypeList &deviceType)
262 if (deviceType.count(DeviceBase::DeviceType::AXIS))
264 for (std::pair<
const std::string,
268 DeviceBase::Shared motor = std::make_shared<MockAxis>(
269 motorEntry.first, shared_from_this());
270 devices.push_back(motor);
273 if (deviceType.count(DeviceBase::DeviceType::POSITION_SENSOR))
275 for (std::pair<const std::string, uint8_t> lvdtEntry : m_lvdts)
277 DeviceBase::Shared lvdt = std::make_shared<MockPositionSensor>(
278 lvdtEntry.first, shared_from_this());
279 devices.push_back(lvdt);
282 if (deviceType.count(DeviceBase::DeviceType::GPIO))
284 for (std::pair<const std::string, uint8_t> gpioEntry : m_gpios)
286 DeviceBase::Shared gpio = std::make_shared<MockGpio>(
287 gpioEntry.first, shared_from_this());
288 devices.push_back(gpio);
main motion-lib namespace