yocto_multiaxiscontroller.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_multiaxiscontroller.cpp 29507 2017-12-28 14:14:56Z mvuilleu $
4  *
5  * Implements yFindMultiAxisController(), the high-level API for MultiAxisController functions
6  *
7  * - - - - - - - - - License information: - - - - - - - - -
8  *
9  * Copyright (C) 2011 and beyond by Yoctopuce Sarl, Switzerland.
10  *
11  * Yoctopuce Sarl (hereafter Licensor) grants to you a perpetual
12  * non-exclusive license to use, modify, copy and integrate this
13  * file into your software for the sole purpose of interfacing
14  * with Yoctopuce products.
15  *
16  * You may reproduce and distribute copies of this file in
17  * source or object form, as long as the sole purpose of this
18  * code is to interface with Yoctopuce products. You must retain
19  * this notice in the distributed source file.
20  *
21  * You should refer to Yoctopuce General Terms and Conditions
22  * for additional information regarding your rights and
23  * obligations.
24  *
25  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 'AS IS' WITHOUT
26  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
27  * WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS
28  * FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO
29  * EVENT SHALL LICENSOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
30  * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
31  * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR
32  * SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
33  * LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR
34  * CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON THE
35  * BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF
36  * WARRANTY, OR OTHERWISE.
37  *
38  *********************************************************************/
39 
40 
41 #define _CRT_SECURE_NO_DEPRECATE //do not use windows secure crt
43 #include "yapi/yjson.h"
44 #include "yapi/yapi.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <math.h>
48 #include <stdlib.h>
49 #define __FILE_ID__ "multiaxiscontroller"
50 
52 //--- (YMultiAxisController initialization)
53  ,_nAxis(NAXIS_INVALID)
54  ,_globalState(GLOBALSTATE_INVALID)
55  ,_command(COMMAND_INVALID)
56  ,_valueCallbackMultiAxisController(NULL)
57 //--- (end of YMultiAxisController initialization)
58 {
59  _className="MultiAxisController";
60 }
61 
63 {
64 //--- (YMultiAxisController cleanup)
65 //--- (end of YMultiAxisController cleanup)
66 }
67 //--- (YMultiAxisController implementation)
68 // static attributes
70 
72 {
73  if(json_val->has("nAxis")) {
74  _nAxis = json_val->getInt("nAxis");
75  }
76  if(json_val->has("globalState")) {
77  _globalState = (Y_GLOBALSTATE_enum)json_val->getInt("globalState");
78  }
79  if(json_val->has("command")) {
80  _command = json_val->getString("command");
81  }
82  return YFunction::_parseAttr(json_val);
83 }
84 
85 
94 {
95  int res = 0;
97  try {
100  {
103  }
104  }
105  }
106  res = _nAxis;
107  } catch (std::exception) {
109  throw;
110  }
112  return res;
113 }
114 
125 {
126  string rest_val;
127  int res;
129  try {
130  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
131  res = _setAttr("nAxis", rest_val);
132  } catch (std::exception) {
134  throw;
135  }
137  return res;
138 }
139 
150 {
151  Y_GLOBALSTATE_enum res;
153  try {
156  {
159  }
160  }
161  }
162  res = _globalState;
163  } catch (std::exception) {
165  throw;
166  }
168  return res;
169 }
170 
172 {
173  string res;
175  try {
178  {
181  }
182  }
183  }
184  res = _command;
185  } catch (std::exception) {
187  throw;
188  }
190  return res;
191 }
192 
193 int YMultiAxisController::set_command(const string& newval)
194 {
195  string rest_val;
196  int res;
198  try {
199  rest_val = newval;
200  res = _setAttr("command", rest_val);
201  } catch (std::exception) {
203  throw;
204  }
206  return res;
207 }
208 
237 {
238  YMultiAxisController* obj = NULL;
239  int taken = 0;
240  if (YAPI::_apiInitialized) {
242  taken = 1;
243  }try {
244  obj = (YMultiAxisController*) YFunction::_FindFromCache("MultiAxisController", func);
245  if (obj == NULL) {
246  obj = new YMultiAxisController(func);
247  YFunction::_AddToCache("MultiAxisController", func, obj);
248  }
249  } catch (std::exception) {
251  throw;
252  }
254  return obj;
255 }
256 
269 {
270  string val;
271  if (callback != NULL) {
273  } else {
275  }
277  // Immediately invoke value callback with current value
278  if (callback != NULL && this->isOnline()) {
279  val = _advertisedValue;
280  if (!(val == "")) {
281  this->_invokeValueCallback(val);
282  }
283  }
284  return 0;
285 }
286 
288 {
289  if (_valueCallbackMultiAxisController != NULL) {
291  } else {
293  }
294  return 0;
295 }
296 
298 {
299  string url;
300  string retBin;
301  int res = 0;
302  url = YapiWrapper::ysprintf("cmd.txt?X=%s",command.c_str());
303  //may throw an exception
304  retBin = this->_download(url);
305  res = ((u8)retBin[0]);
306  if (res == 49) {
307  if (!(res == 48)) {
308  _throw(YAPI_DEVICE_BUSY,"Motor command pipeline is full, try again later");
309  return YAPI_DEVICE_BUSY;
310  }
311  } else {
312  if (!(res == 48)) {
313  _throw(YAPI_IO_ERROR,"Motor command failed permanently");
314  return YAPI_IO_ERROR;
315  }
316  }
317  return YAPI_SUCCESS;
318 }
319 
327 {
328  return this->sendCommand("Z");
329 }
330 
339 int YMultiAxisController::findHomePosition(vector<double> speed)
340 {
341  string cmd;
342  int i = 0;
343  int ndim = 0;
344  ndim = (int)speed.size();
345  cmd = YapiWrapper::ysprintf("H%d",(int) floor(1000*speed[0]+0.5));
346  i = 1;
347  while (i < ndim) {
348  cmd = YapiWrapper::ysprintf("%s,%d", cmd.c_str(),(int) floor(1000*speed[i]+0.5));
349  i = i + 1;
350  }
351  return this->sendCommand(cmd);
352 }
353 
365 int YMultiAxisController::moveTo(vector<double> absPos)
366 {
367  string cmd;
368  int i = 0;
369  int ndim = 0;
370  ndim = (int)absPos.size();
371  cmd = YapiWrapper::ysprintf("M%d",(int) floor(16*absPos[0]+0.5));
372  i = 1;
373  while (i < ndim) {
374  cmd = YapiWrapper::ysprintf("%s,%d", cmd.c_str(),(int) floor(16*absPos[i]+0.5));
375  i = i + 1;
376  }
377  return this->sendCommand(cmd);
378 }
379 
391 int YMultiAxisController::moveRel(vector<double> relPos)
392 {
393  string cmd;
394  int i = 0;
395  int ndim = 0;
396  ndim = (int)relPos.size();
397  cmd = YapiWrapper::ysprintf("m%d",(int) floor(16*relPos[0]+0.5));
398  i = 1;
399  while (i < ndim) {
400  cmd = YapiWrapper::ysprintf("%s,%d", cmd.c_str(),(int) floor(16*relPos[i]+0.5));
401  i = i + 1;
402  }
403  return this->sendCommand(cmd);
404 }
405 
415 {
416  return this->sendCommand(YapiWrapper::ysprintf("_%d",waitMs));
417 }
418 
426 {
427  return this->sendCommand("!");
428 }
429 
437 {
438  return this->sendCommand("B");
439 }
440 
448 {
449  return this->sendCommand("z");
450 }
451 
453 {
454  string hwid;
455 
456  if(YISERR(_nextFunction(hwid)) || hwid=="") {
457  return NULL;
458  }
460 }
461 
463 {
464  vector<YFUN_DESCR> v_fundescr;
465  YDEV_DESCR ydevice;
466  string serial, funcId, funcName, funcVal, errmsg;
467 
468  if(YISERR(YapiWrapper::getFunctionsByClass("MultiAxisController", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
469  v_fundescr.size() == 0 ||
470  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
471  return NULL;
472  }
473  return YMultiAxisController::FindMultiAxisController(serial+"."+funcId);
474 }
475 
476 //--- (end of YMultiAxisController implementation)
477 
478 //--- (YMultiAxisController functions)
479 //--- (end of YMultiAxisController functions)
static YMultiAxisController * FirstMultiAxisController(void)
YMultiAxisControllerValueCallback _valueCallbackMultiAxisController
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
virtual int moveRel(vector< double > relPos)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int pause(int waitMs)
virtual int _invokeValueCallback(string value)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
static const string COMMAND_INVALID
static const Y_GLOBALSTATE_enum GLOBALSTATE_INVALID
u64 _cacheExpiration
Definition: yocto_api.h:1571
void _throw(YRETCODE errType, string errMsg)
Definition: yocto_api.cpp:2656
Y_GLOBALSTATE_enum _globalState
virtual int _parseAttr(YJSONObject *json_val)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
string _download(const string &url)
Definition: yocto_api.cpp:3053
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
Y_GLOBALSTATE_enum get_globalState(void)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
virtual int moveTo(vector< double > absPos)
static YMultiAxisController * FindMultiAxisController(string func)
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
bool isOnline(void)
Definition: yocto_api.cpp:3291
virtual int sendCommand(string command)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
virtual int registerValueCallback(YMultiAxisControllerValueCallback callback)
YMultiAxisController * nextMultiAxisController(void)
string _className
Definition: yocto_api.h:1558
YMultiAxisController(const string &func)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
void(* YMultiAxisControllerValueCallback)(YMultiAxisController *func, const string &functionValue)
bool has(const string &key)
Definition: yocto_api.cpp:735
int set_command(const string &newval)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int findHomePosition(vector< double > speed)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563


yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:11