yocto_steppermotor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_steppermotor.cpp 29507 2017-12-28 14:14:56Z mvuilleu $
4  *
5  * Implements yFindStepperMotor(), the high-level API for StepperMotor 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
42 #include "yocto_steppermotor.h"
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__ "steppermotor"
50 
51 YStepperMotor::YStepperMotor(const string& func): YFunction(func)
52 //--- (YStepperMotor initialization)
53  ,_motorState(MOTORSTATE_INVALID)
54  ,_diags(DIAGS_INVALID)
55  ,_stepPos(STEPPOS_INVALID)
56  ,_speed(SPEED_INVALID)
57  ,_pullinSpeed(PULLINSPEED_INVALID)
58  ,_maxAccel(MAXACCEL_INVALID)
59  ,_maxSpeed(MAXSPEED_INVALID)
60  ,_stepping(STEPPING_INVALID)
61  ,_overcurrent(OVERCURRENT_INVALID)
62  ,_tCurrStop(TCURRSTOP_INVALID)
63  ,_tCurrRun(TCURRRUN_INVALID)
64  ,_alertMode(ALERTMODE_INVALID)
65  ,_auxMode(AUXMODE_INVALID)
66  ,_auxSignal(AUXSIGNAL_INVALID)
67  ,_command(COMMAND_INVALID)
68  ,_valueCallbackStepperMotor(NULL)
69 //--- (end of YStepperMotor initialization)
70 {
71  _className="StepperMotor";
72 }
73 
75 {
76 //--- (YStepperMotor cleanup)
77 //--- (end of YStepperMotor cleanup)
78 }
79 //--- (YStepperMotor implementation)
80 // static attributes
89 
91 {
92  if(json_val->has("motorState")) {
93  _motorState = (Y_MOTORSTATE_enum)json_val->getInt("motorState");
94  }
95  if(json_val->has("diags")) {
96  _diags = json_val->getInt("diags");
97  }
98  if(json_val->has("stepPos")) {
99  _stepPos = json_val->getDouble("stepPos") / 16.0;
100  }
101  if(json_val->has("speed")) {
102  _speed = floor(json_val->getDouble("speed") * 1000.0 / 65536.0 + 0.5) / 1000.0;
103  }
104  if(json_val->has("pullinSpeed")) {
105  _pullinSpeed = floor(json_val->getDouble("pullinSpeed") * 1000.0 / 65536.0 + 0.5) / 1000.0;
106  }
107  if(json_val->has("maxAccel")) {
108  _maxAccel = floor(json_val->getDouble("maxAccel") * 1000.0 / 65536.0 + 0.5) / 1000.0;
109  }
110  if(json_val->has("maxSpeed")) {
111  _maxSpeed = floor(json_val->getDouble("maxSpeed") * 1000.0 / 65536.0 + 0.5) / 1000.0;
112  }
113  if(json_val->has("stepping")) {
114  _stepping = (Y_STEPPING_enum)json_val->getInt("stepping");
115  }
116  if(json_val->has("overcurrent")) {
117  _overcurrent = json_val->getInt("overcurrent");
118  }
119  if(json_val->has("tCurrStop")) {
120  _tCurrStop = json_val->getInt("tCurrStop");
121  }
122  if(json_val->has("tCurrRun")) {
123  _tCurrRun = json_val->getInt("tCurrRun");
124  }
125  if(json_val->has("alertMode")) {
126  _alertMode = json_val->getString("alertMode");
127  }
128  if(json_val->has("auxMode")) {
129  _auxMode = json_val->getString("auxMode");
130  }
131  if(json_val->has("auxSignal")) {
132  _auxSignal = json_val->getInt("auxSignal");
133  }
134  if(json_val->has("command")) {
135  _command = json_val->getString("command");
136  }
137  return YFunction::_parseAttr(json_val);
138 }
139 
140 
150 {
151  Y_MOTORSTATE_enum res;
153  try {
156  {
159  }
160  }
161  }
162  res = _motorState;
163  } catch (std::exception) {
165  throw;
166  }
168  return res;
169 }
170 
179 {
180  int res = 0;
182  try {
185  {
188  }
189  }
190  }
191  res = _diags;
192  } catch (std::exception) {
194  throw;
195  }
197  return res;
198 }
199 
214 int YStepperMotor::set_stepPos(double newval)
215 {
216  string rest_val;
217  int res;
219  try {
220  char buf[32]; sprintf(buf,"%.2f", floor(newval * 100.0)/100.0); rest_val = string(buf);
221  res = _setAttr("stepPos", rest_val);
222  } catch (std::exception) {
224  throw;
225  }
227  return res;
228 }
229 
239 {
240  double res = 0.0;
242  try {
245  {
248  }
249  }
250  }
251  res = _stepPos;
252  } catch (std::exception) {
254  throw;
255  }
257  return res;
258 }
259 
269 {
270  double res = 0.0;
272  try {
275  {
278  }
279  }
280  }
281  res = _speed;
282  } catch (std::exception) {
284  throw;
285  }
287  return res;
288 }
289 
301 {
302  string rest_val;
303  int res;
305  try {
306  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
307  res = _setAttr("pullinSpeed", rest_val);
308  } catch (std::exception) {
310  throw;
311  }
313  return res;
314 }
315 
325 {
326  double res = 0.0;
328  try {
331  {
334  }
335  }
336  }
337  res = _pullinSpeed;
338  } catch (std::exception) {
340  throw;
341  }
343  return res;
344 }
345 
356 int YStepperMotor::set_maxAccel(double newval)
357 {
358  string rest_val;
359  int res;
361  try {
362  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
363  res = _setAttr("maxAccel", rest_val);
364  } catch (std::exception) {
366  throw;
367  }
369  return res;
370 }
371 
380 {
381  double res = 0.0;
383  try {
386  {
389  }
390  }
391  }
392  res = _maxAccel;
393  } catch (std::exception) {
395  throw;
396  }
398  return res;
399 }
400 
410 int YStepperMotor::set_maxSpeed(double newval)
411 {
412  string rest_val;
413  int res;
415  try {
416  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
417  res = _setAttr("maxSpeed", rest_val);
418  } catch (std::exception) {
420  throw;
421  }
423  return res;
424 }
425 
434 {
435  double res = 0.0;
437  try {
440  {
443  }
444  }
445  }
446  res = _maxSpeed;
447  } catch (std::exception) {
449  throw;
450  }
452  return res;
453 }
454 
464 {
465  Y_STEPPING_enum res;
467  try {
470  {
473  }
474  }
475  }
476  res = _stepping;
477  } catch (std::exception) {
479  throw;
480  }
482  return res;
483 }
484 
496 {
497  string rest_val;
498  int res;
500  try {
501  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
502  res = _setAttr("stepping", rest_val);
503  } catch (std::exception) {
505  throw;
506  }
508  return res;
509 }
510 
519 {
520  int res = 0;
522  try {
525  {
528  }
529  }
530  }
531  res = _overcurrent;
532  } catch (std::exception) {
534  throw;
535  }
537  return res;
538 }
539 
550 {
551  string rest_val;
552  int res;
554  try {
555  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
556  res = _setAttr("overcurrent", rest_val);
557  } catch (std::exception) {
559  throw;
560  }
562  return res;
563 }
564 
573 {
574  int res = 0;
576  try {
579  {
582  }
583  }
584  }
585  res = _tCurrStop;
586  } catch (std::exception) {
588  throw;
589  }
591  return res;
592 }
593 
605 {
606  string rest_val;
607  int res;
609  try {
610  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
611  res = _setAttr("tCurrStop", rest_val);
612  } catch (std::exception) {
614  throw;
615  }
617  return res;
618 }
619 
628 {
629  int res = 0;
631  try {
634  {
637  }
638  }
639  }
640  res = _tCurrRun;
641  } catch (std::exception) {
643  throw;
644  }
646  return res;
647 }
648 
660 {
661  string rest_val;
662  int res;
664  try {
665  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
666  res = _setAttr("tCurrRun", rest_val);
667  } catch (std::exception) {
669  throw;
670  }
672  return res;
673 }
674 
676 {
677  string res;
679  try {
682  {
685  }
686  }
687  }
688  res = _alertMode;
689  } catch (std::exception) {
691  throw;
692  }
694  return res;
695 }
696 
697 int YStepperMotor::set_alertMode(const string& newval)
698 {
699  string rest_val;
700  int res;
702  try {
703  rest_val = newval;
704  res = _setAttr("alertMode", rest_val);
705  } catch (std::exception) {
707  throw;
708  }
710  return res;
711 }
712 
714 {
715  string res;
717  try {
720  {
723  }
724  }
725  }
726  res = _auxMode;
727  } catch (std::exception) {
729  throw;
730  }
732  return res;
733 }
734 
735 int YStepperMotor::set_auxMode(const string& newval)
736 {
737  string rest_val;
738  int res;
740  try {
741  rest_val = newval;
742  res = _setAttr("auxMode", rest_val);
743  } catch (std::exception) {
745  throw;
746  }
748  return res;
749 }
750 
759 {
760  int res = 0;
762  try {
765  {
768  }
769  }
770  }
771  res = _auxSignal;
772  } catch (std::exception) {
774  throw;
775  }
777  return res;
778 }
779 
791 {
792  string rest_val;
793  int res;
795  try {
796  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
797  res = _setAttr("auxSignal", rest_val);
798  } catch (std::exception) {
800  throw;
801  }
803  return res;
804 }
805 
807 {
808  string res;
810  try {
813  {
816  }
817  }
818  }
819  res = _command;
820  } catch (std::exception) {
822  throw;
823  }
825  return res;
826 }
827 
828 int YStepperMotor::set_command(const string& newval)
829 {
830  string rest_val;
831  int res;
833  try {
834  rest_val = newval;
835  res = _setAttr("command", rest_val);
836  } catch (std::exception) {
838  throw;
839  }
841  return res;
842 }
843 
872 {
873  YStepperMotor* obj = NULL;
874  int taken = 0;
875  if (YAPI::_apiInitialized) {
877  taken = 1;
878  }try {
879  obj = (YStepperMotor*) YFunction::_FindFromCache("StepperMotor", func);
880  if (obj == NULL) {
881  obj = new YStepperMotor(func);
882  YFunction::_AddToCache("StepperMotor", func, obj);
883  }
884  } catch (std::exception) {
886  throw;
887  }
889  return obj;
890 }
891 
904 {
905  string val;
906  if (callback != NULL) {
908  } else {
910  }
911  _valueCallbackStepperMotor = callback;
912  // Immediately invoke value callback with current value
913  if (callback != NULL && this->isOnline()) {
914  val = _advertisedValue;
915  if (!(val == "")) {
916  this->_invokeValueCallback(val);
917  }
918  }
919  return 0;
920 }
921 
923 {
924  if (_valueCallbackStepperMotor != NULL) {
925  _valueCallbackStepperMotor(this, value);
926  } else {
928  }
929  return 0;
930 }
931 
933 {
934  string id;
935  string url;
936  string retBin;
937  int res = 0;
938  id = this->get_functionId();
939  id = (id).substr( 12, 1);
940  url = YapiWrapper::ysprintf("cmd.txt?%s=%s", id.c_str(),command.c_str());
941  //may throw an exception
942  retBin = this->_download(url);
943  res = ((u8)retBin[0]);
944  if (res == 49) {
945  if (!(res == 48)) {
946  _throw(YAPI_DEVICE_BUSY,"Motor command pipeline is full, try again later");
947  return YAPI_DEVICE_BUSY;
948  }
949  } else {
950  if (!(res == 48)) {
951  _throw(YAPI_IO_ERROR,"Motor command failed permanently");
952  return YAPI_IO_ERROR;
953  }
954  }
955  return YAPI_SUCCESS;
956 }
957 
965 {
966  return this->sendCommand("Z");
967 }
968 
978 {
979  return this->sendCommand(YapiWrapper::ysprintf("H%d",(int) floor(1000*speed+0.5)));
980 }
981 
993 {
994  return this->sendCommand(YapiWrapper::ysprintf("R%d",(int) floor(1000*speed+0.5)));
995 }
996 
1007 int YStepperMotor::moveTo(double absPos)
1008 {
1009  return this->sendCommand(YapiWrapper::ysprintf("M%d",(int) floor(16*absPos+0.5)));
1010 }
1011 
1022 int YStepperMotor::moveRel(double relPos)
1023 {
1024  return this->sendCommand(YapiWrapper::ysprintf("m%d",(int) floor(16*relPos+0.5)));
1025 }
1026 
1038 int YStepperMotor::moveRelSlow(double relPos,double maxSpeed)
1039 {
1040  return this->sendCommand(YapiWrapper::ysprintf("m%d@%d",(int) floor(16*relPos+0.5),(int) floor(1000*maxSpeed+0.5)));
1041 }
1042 
1051 int YStepperMotor::pause(int waitMs)
1052 {
1053  return this->sendCommand(YapiWrapper::ysprintf("_%d",waitMs));
1054 }
1055 
1063 {
1064  return this->sendCommand("!");
1065 }
1066 
1076 {
1077  return this->sendCommand(".");
1078 }
1079 
1091 {
1092  if (!(dir != 0)) {
1093  _throw(YAPI_INVALID_ARGUMENT,"direction must be +1 or -1");
1094  return YAPI_INVALID_ARGUMENT;
1095  }
1096  if (dir > 0) {
1097  return this->sendCommand(".+");
1098  }
1099  return this->sendCommand(".-");
1100 }
1101 
1109 {
1110  return this->sendCommand("B");
1111 }
1112 
1120 {
1121  return this->sendCommand("z");
1122 }
1123 
1125 {
1126  string hwid;
1127 
1128  if(YISERR(_nextFunction(hwid)) || hwid=="") {
1129  return NULL;
1130  }
1131  return YStepperMotor::FindStepperMotor(hwid);
1132 }
1133 
1135 {
1136  vector<YFUN_DESCR> v_fundescr;
1137  YDEV_DESCR ydevice;
1138  string serial, funcId, funcName, funcVal, errmsg;
1139 
1140  if(YISERR(YapiWrapper::getFunctionsByClass("StepperMotor", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
1141  v_fundescr.size() == 0 ||
1142  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
1143  return NULL;
1144  }
1145  return YStepperMotor::FindStepperMotor(serial+"."+funcId);
1146 }
1147 
1148 //--- (end of YStepperMotor implementation)
1149 
1150 //--- (YStepperMotor functions)
1151 //--- (end of YStepperMotor functions)
YStepperMotorValueCallback _valueCallbackStepperMotor
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static const double MAXACCEL_INVALID
Y_MOTORSTATE_enum get_motorState(void)
double get_pullinSpeed(void)
Y_MOTORSTATE_enum
virtual int emergencyStop(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
int set_stepping(Y_STEPPING_enum newval)
static const int AUXSIGNAL_INVALID
double get_speed(void)
static const string AUXMODE_INVALID
virtual int abortAndHiZ(void)
static const double MAXSPEED_INVALID
virtual int moveRel(double relPos)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
void(* YStepperMotorValueCallback)(YStepperMotor *func, const string &functionValue)
double get_maxAccel(void)
YStepperMotor * nextStepperMotor(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void _throw(YRETCODE errType, string errMsg)
Definition: yocto_api.cpp:2656
double speed(void)
int set_alertMode(const string &newval)
int set_pullinSpeed(double newval)
static const int TCURRRUN_INVALID
string command(void)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static YStepperMotor * FindStepperMotor(string func)
int set_auxMode(const string &newval)
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
double get_maxSpeed(void)
virtual int _parseAttr(YJSONObject *json_val)
virtual int sendCommand(string command)
virtual int changeSpeed(double speed)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
string get_functionId(void)
Definition: yocto_api.cpp:3229
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
Y_STEPPING_enum
static const int OVERCURRENT_INVALID
virtual int pause(int waitMs)
int set_stepPos(double newval)
virtual int abortAndBrake(void)
YStepperMotor(const string &func)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
static const int DIAGS_INVALID
int getInt(const string &key)
Definition: yocto_api.cpp:779
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
string get_command(void)
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
int set_maxAccel(double newval)
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
virtual int findHomePosition(double speed)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static const int TCURRSTOP_INVALID
static const double STEPPOS_INVALID
virtual int alertStepDir(int dir)
virtual int alertStepOut(void)
virtual int moveTo(double absPos)
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
Y_STEPPING_enum _stepping
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
Y_MOTORSTATE_enum _motorState
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
int set_maxSpeed(double newval)
Y_STEPPING_enum get_stepping(void)
int set_tCurrRun(int newval)
static const double PULLINSPEED_INVALID
string _className
Definition: yocto_api.h:1558
int set_overcurrent(int newval)
string get_auxMode(void)
static const string ALERTMODE_INVALID
int set_tCurrStop(int newval)
int set_auxSignal(int newval)
static const string COMMAND_INVALID
static const double SPEED_INVALID
virtual int reset(void)
virtual int _invokeValueCallback(string value)
double get_stepPos(void)
virtual int moveRelSlow(double relPos, double maxSpeed)
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
double maxSpeed(void)
virtual int registerValueCallback(YStepperMotorValueCallback callback)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
int set_command(const string &newval)
static const Y_MOTORSTATE_enum MOTORSTATE_INVALID
static YStepperMotor * FirstStepperMotor(void)
static const Y_STEPPING_enum STEPPING_INVALID
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
string get_alertMode(void)


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