MotionLib  1.0.0
SamBuCa motion library
AsyncAxisController.hpp
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-27T09:08:31
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #ifndef ASYNCAXISCONTROLLER_HPP__
25 #define ASYNCAXISCONTROLLER_HPP__
26 
27 #include <algorithm>
28 #include <cmath>
29 #include <memory>
30 
31 #include <ccut/async.hpp>
32 #include <ccut/utils.hpp>
33 #include <logger/Logger.hpp>
34 #include <oatpp/core/macro/component.hpp>
35 
36 #include "Exception.hpp"
37 #include "FutureCoroutine.hpp"
38 #include "OatExt.hpp"
39 #include "SmcController.hpp"
40 #include "device/Axis.hpp"
41 #include "device/DeviceBase.hpp"
42 #include "device/DeviceStore.hpp"
43 #include "platform/PlatformFactory.hpp"
44 #include "util/serialize.hpp"
45 
46 #include OATPP_CODEGEN_BEGIN(ApiController)
47 
48 // clang-format off
50 {
51 public:
52  explicit AsyncAxisController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>,
53  objectMapper));
54  void addParams(const std::shared_ptr<oatpp::web::server::api::Endpoint::Info>& info);
55 
56  ENDPOINT_INFO(getPosition)
57  {
58  info->summary = "get current axis position";
59  info->addResponse<Float64>(Status::CODE_200, "application/json")
60  .addExample("example", Float64(1234));
61  addParams(info);
62  }
63  ENDPOINT_ASYNC("GET", "/axis/{id}/position", getPosition)
64  {
65  public:
66  ENDPOINT_ASYNC_INIT(getPosition)
67  Action act() override;
68  Action onResult(const smc::units::value_t &value);
69  };
70 
71  ENDPOINT_INFO(putPosition)
72  {
73  info->summary = "Absolute axis movement";
74  info->addResponse(Status::CODE_200, "application/json");
75  info->addConsumes<Float64>("application/json")
76  .addExample("absolute position", Float64(123));
77  addParams(info);
78  }
79  ENDPOINT_ASYNC("PUT", "/axis/{id}/position", putPosition)
80  {
81  public:
82  ENDPOINT_ASYNC_INIT(putPosition)
83  Action act() override;
84  Action onMoveTo(const Float64 &position);
85  };
86 
87 
88  ENDPOINT_INFO(postPosition)
89  {
90  info->summary = "Relative axis movement";
91  info->addResponse(Status::CODE_200, "application/json");
92  info->addConsumes<Float64>("application/json")
93  .addExample("relative position", Float64(-10));
94  addParams(info);
95  }
96  ENDPOINT_ASYNC("POST", "/axis/{id}/position", postPosition)
97  {
98  public:
99  ENDPOINT_ASYNC_INIT(postPosition)
100  Action act() override;
101  Action onMoveBy(const Float64 &position);
102  };
103 
104  ENDPOINT_INFO(putCurrentPosition)
105  {
106  info->summary = "Set axis current position";
107  info->addResponse(Status::CODE_200, "application/json");
108  info->addConsumes<Float64>("application/json")
109  .addExample("absolute position", Float64(123));
110  addParams(info);
111  }
112  ENDPOINT_ASYNC("PUT", "/axis/{id}/settings/position", putCurrentPosition)
113  {
114  public:
115  ENDPOINT_ASYNC_INIT(putCurrentPosition)
116  Action act() override;
117  Action onSetCurrentPosition(const Float64 &position);
118  };
119 
120  ENDPOINT_INFO(putStop)
121  {
122  info->summary = "Stop motion";
123  info->addResponse(Status::CODE_200, "application/json");
124  addParams(info);
125  }
126  ENDPOINT_ASYNC("PUT", "/axis/{id}/stop", putStop)
127  {
128  public:
129  ENDPOINT_ASYNC_INIT(putStop)
130  Action act() override;
131  };
132 
133  ENDPOINT_INFO(putPause)
134  {
135  info->summary = "Pause ongoing motion";
136  info->addResponse(Status::CODE_200, "application/json");
137  addParams(info);
138  }
139  ENDPOINT_ASYNC("PUT", "/axis/{id}/pause", putPause)
140  {
141  public:
142  ENDPOINT_ASYNC_INIT(putPause)
143  Action act() override;
144  };
145 
146  ENDPOINT_INFO(putResume)
147  {
148  info->summary = "Resume paused motion";
149  info->addResponse(Status::CODE_200, "application/json");
150  addParams(info);
151  }
152  ENDPOINT_ASYNC("PUT", "/axis/{id}/resume", putResume)
153  {
154  public:
155  ENDPOINT_ASYNC_INIT(putResume)
156  Action act() override;
157  };
158 
159  ENDPOINT_INFO(postTakeReference)
160  {
161  info->summary = "Begin the homing sequence";
162  info->description = "Timeout is in milliseconds";
163  info->addResponse(Status::CODE_200, "application/json");
164  info->addConsumes<Float64>("application/json")
165  .addExample("homing timeout (ms)", Float64(120*1000));
166  info->pathParams.add<String>("id").addExample("Grbl axis X",
167  String("grbl://1/axis/X"));
168  }
169  ENDPOINT_ASYNC("POST", "/axis/{id}/homing", postTakeReference)
170  {
171  public:
172  ENDPOINT_ASYNC_INIT(postTakeReference)
173  Action act() override;
174  Action onTakeReference(const Float64 &timeout);
175  };
176 
177  ENDPOINT_INFO(getState)
178  {
179  info->summary = "Get axis state";
180  info->addResponse<String>(Status::CODE_200, "application/json")
181  .addExample("State", String("idle"));
182  addParams(info);
183  }
184  ENDPOINT_ASYNC("GET", "/axis/{id}/state", getState)
185  {
186  public:
187  ENDPOINT_ASYNC_INIT(getState)
188  Action act() override;
189  Action onResult(const smc::Axis::State &state);
190  };
191 };
192 
193 //clang-format on
194 
195 #include OATPP_CODEGEN_END(ApiController)
196 
197 #endif
State
Axis State.
Definition: Axis.hpp:54