yocto_motor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_motor.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindMotor(), the high-level API for Motor 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_motor.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__ "motor"
50 
51 YMotor::YMotor(const string& func): YFunction(func)
52 //--- (YMotor initialization)
53  ,_motorStatus(MOTORSTATUS_INVALID)
54  ,_drivingForce(DRIVINGFORCE_INVALID)
55  ,_brakingForce(BRAKINGFORCE_INVALID)
56  ,_cutOffVoltage(CUTOFFVOLTAGE_INVALID)
57  ,_overCurrentLimit(OVERCURRENTLIMIT_INVALID)
58  ,_frequency(FREQUENCY_INVALID)
59  ,_starterTime(STARTERTIME_INVALID)
60  ,_failSafeTimeout(FAILSAFETIMEOUT_INVALID)
61  ,_command(COMMAND_INVALID)
62  ,_valueCallbackMotor(NULL)
63 //--- (end of YMotor initialization)
64 {
65  _className="Motor";
66 }
67 
69 {
70 //--- (YMotor cleanup)
71 //--- (end of YMotor cleanup)
72 }
73 //--- (YMotor implementation)
74 // static attributes
80 
82 {
83  if(json_val->has("motorStatus")) {
84  _motorStatus = (Y_MOTORSTATUS_enum)json_val->getInt("motorStatus");
85  }
86  if(json_val->has("drivingForce")) {
87  _drivingForce = floor(json_val->getDouble("drivingForce") * 1000.0 / 65536.0 + 0.5) / 1000.0;
88  }
89  if(json_val->has("brakingForce")) {
90  _brakingForce = floor(json_val->getDouble("brakingForce") * 1000.0 / 65536.0 + 0.5) / 1000.0;
91  }
92  if(json_val->has("cutOffVoltage")) {
93  _cutOffVoltage = floor(json_val->getDouble("cutOffVoltage") * 1000.0 / 65536.0 + 0.5) / 1000.0;
94  }
95  if(json_val->has("overCurrentLimit")) {
96  _overCurrentLimit = json_val->getInt("overCurrentLimit");
97  }
98  if(json_val->has("frequency")) {
99  _frequency = floor(json_val->getDouble("frequency") * 1000.0 / 65536.0 + 0.5) / 1000.0;
100  }
101  if(json_val->has("starterTime")) {
102  _starterTime = json_val->getInt("starterTime");
103  }
104  if(json_val->has("failSafeTimeout")) {
105  _failSafeTimeout = json_val->getInt("failSafeTimeout");
106  }
107  if(json_val->has("command")) {
108  _command = json_val->getString("command");
109  }
110  return YFunction::_parseAttr(json_val);
111 }
112 
113 
134 {
135  Y_MOTORSTATUS_enum res;
137  try {
140  {
143  }
144  }
145  }
146  res = _motorStatus;
147  } catch (std::exception) {
149  throw;
150  }
152  return res;
153 }
154 
156 {
157  string rest_val;
158  int res;
160  try {
161  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
162  res = _setAttr("motorStatus", rest_val);
163  } catch (std::exception) {
165  throw;
166  }
168  return res;
169 }
170 
184 int YMotor::set_drivingForce(double newval)
185 {
186  string rest_val;
187  int res;
189  try {
190  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
191  res = _setAttr("drivingForce", rest_val);
192  } catch (std::exception) {
194  throw;
195  }
197  return res;
198 }
199 
209 {
210  double res = 0.0;
212  try {
215  {
218  }
219  }
220  }
221  res = _drivingForce;
222  } catch (std::exception) {
224  throw;
225  }
227  return res;
228 }
229 
242 int YMotor::set_brakingForce(double newval)
243 {
244  string rest_val;
245  int res;
247  try {
248  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
249  res = _setAttr("brakingForce", rest_val);
250  } catch (std::exception) {
252  throw;
253  }
255  return res;
256 }
257 
267 {
268  double res = 0.0;
270  try {
273  {
276  }
277  }
278  }
279  res = _brakingForce;
280  } catch (std::exception) {
282  throw;
283  }
285  return res;
286 }
287 
303 int YMotor::set_cutOffVoltage(double newval)
304 {
305  string rest_val;
306  int res;
308  try {
309  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
310  res = _setAttr("cutOffVoltage", rest_val);
311  } catch (std::exception) {
313  throw;
314  }
316  return res;
317 }
318 
331 {
332  double res = 0.0;
334  try {
337  {
340  }
341  }
342  }
343  res = _cutOffVoltage;
344  } catch (std::exception) {
346  throw;
347  }
349  return res;
350 }
351 
362 {
363  int res = 0;
365  try {
368  {
371  }
372  }
373  }
374  res = _overCurrentLimit;
375  } catch (std::exception) {
377  throw;
378  }
380  return res;
381 }
382 
398 {
399  string rest_val;
400  int res;
402  try {
403  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
404  res = _setAttr("overCurrentLimit", rest_val);
405  } catch (std::exception) {
407  throw;
408  }
410  return res;
411 }
412 
425 int YMotor::set_frequency(double newval)
426 {
427  string rest_val;
428  int res;
430  try {
431  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
432  res = _setAttr("frequency", rest_val);
433  } catch (std::exception) {
435  throw;
436  }
438  return res;
439 }
440 
449 {
450  double res = 0.0;
452  try {
455  {
458  }
459  }
460  }
461  res = _frequency;
462  } catch (std::exception) {
464  throw;
465  }
467  return res;
468 }
469 
481 {
482  int res = 0;
484  try {
487  {
490  }
491  }
492  }
493  res = _starterTime;
494  } catch (std::exception) {
496  throw;
497  }
499  return res;
500 }
501 
514 int YMotor::set_starterTime(int newval)
515 {
516  string rest_val;
517  int res;
519  try {
520  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
521  res = _setAttr("starterTime", rest_val);
522  } catch (std::exception) {
524  throw;
525  }
527  return res;
528 }
529 
543 {
544  int res = 0;
546  try {
549  {
552  }
553  }
554  }
555  res = _failSafeTimeout;
556  } catch (std::exception) {
558  throw;
559  }
561  return res;
562 }
563 
579 {
580  string rest_val;
581  int res;
583  try {
584  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
585  res = _setAttr("failSafeTimeout", rest_val);
586  } catch (std::exception) {
588  throw;
589  }
591  return res;
592 }
593 
595 {
596  string res;
598  try {
601  {
604  }
605  }
606  }
607  res = _command;
608  } catch (std::exception) {
610  throw;
611  }
613  return res;
614 }
615 
616 int YMotor::set_command(const string& newval)
617 {
618  string rest_val;
619  int res;
621  try {
622  rest_val = newval;
623  res = _setAttr("command", rest_val);
624  } catch (std::exception) {
626  throw;
627  }
629  return res;
630 }
631 
660 {
661  YMotor* obj = NULL;
662  int taken = 0;
663  if (YAPI::_apiInitialized) {
665  taken = 1;
666  }try {
667  obj = (YMotor*) YFunction::_FindFromCache("Motor", func);
668  if (obj == NULL) {
669  obj = new YMotor(func);
670  YFunction::_AddToCache("Motor", func, obj);
671  }
672  } catch (std::exception) {
674  throw;
675  }
677  return obj;
678 }
679 
692 {
693  string val;
694  if (callback != NULL) {
696  } else {
698  }
699  _valueCallbackMotor = callback;
700  // Immediately invoke value callback with current value
701  if (callback != NULL && this->isOnline()) {
702  val = _advertisedValue;
703  if (!(val == "")) {
704  this->_invokeValueCallback(val);
705  }
706  }
707  return 0;
708 }
709 
711 {
712  if (_valueCallbackMotor != NULL) {
713  _valueCallbackMotor(this, value);
714  } else {
716  }
717  return 0;
718 }
719 
727 {
728  return this->set_command("K");
729 }
730 
736 {
737  return this->set_motorStatus(Y_MOTORSTATUS_IDLE);
738 }
739 
750 int YMotor::drivingForceMove(double targetPower,int delay)
751 {
752  return this->set_command(YapiWrapper::ysprintf("P%d,%d",(int) floor(targetPower*10+0.5),delay));
753 }
754 
765 int YMotor::brakingForceMove(double targetPower,int delay)
766 {
767  return this->set_command(YapiWrapper::ysprintf("B%d,%d",(int) floor(targetPower*10+0.5),delay));
768 }
769 
771 {
772  string hwid;
773 
774  if(YISERR(_nextFunction(hwid)) || hwid=="") {
775  return NULL;
776  }
777  return YMotor::FindMotor(hwid);
778 }
779 
781 {
782  vector<YFUN_DESCR> v_fundescr;
783  YDEV_DESCR ydevice;
784  string serial, funcId, funcName, funcVal, errmsg;
785 
786  if(YISERR(YapiWrapper::getFunctionsByClass("Motor", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
787  v_fundescr.size() == 0 ||
788  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
789  return NULL;
790  }
791  return YMotor::FindMotor(serial+"."+funcId);
792 }
793 
794 //--- (end of YMotor implementation)
795 
796 //--- (YMotor functions)
797 //--- (end of YMotor functions)
static const int FAILSAFETIMEOUT_INVALID
Definition: yocto_motor.h:137
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
int get_starterTime(void)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
int set_motorStatus(Y_MOTORSTATUS_enum newval)
int set_cutOffVoltage(double newval)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
Y_MOTORSTATUS_enum _motorStatus
Definition: yocto_motor.h:97
YMotor * nextMotor(void)
int set_command(const string &newval)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
static const string COMMAND_INVALID
Definition: yocto_motor.h:138
string get_command(void)
static const double CUTOFFVOLTAGE_INVALID
Definition: yocto_motor.h:133
u64 _cacheExpiration
Definition: yocto_api.h:1571
int _starterTime
Definition: yocto_motor.h:103
static const double FREQUENCY_INVALID
Definition: yocto_motor.h:135
static const int STARTERTIME_INVALID
Definition: yocto_motor.h:136
int set_brakingForce(double newval)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
static YMotor * FindMotor(string func)
double _brakingForce
Definition: yocto_motor.h:99
int get_failSafeTimeout(void)
static const Y_MOTORSTATUS_enum MOTORSTATUS_INVALID
Definition: yocto_motor.h:130
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
double get_brakingForce(void)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
double get_frequency(void)
static const int OVERCURRENTLIMIT_INVALID
Definition: yocto_motor.h:134
int set_frequency(double newval)
string _command
Definition: yocto_motor.h:105
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
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
int set_starterTime(int newval)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_motor.cpp:81
double _drivingForce
Definition: yocto_motor.h:98
YMotorValueCallback _valueCallbackMotor
Definition: yocto_motor.h:106
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
virtual int keepALive(void)
int _overCurrentLimit
Definition: yocto_motor.h:101
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
void(* YMotorValueCallback)(YMotor *func, const string &functionValue)
Definition: yocto_motor.h:54
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
Y_MOTORSTATUS_enum get_motorStatus(void)
virtual int registerValueCallback(YMotorValueCallback callback)
bool isOnline(void)
Definition: yocto_api.cpp:3291
int set_drivingForce(double newval)
double _frequency
Definition: yocto_motor.h:102
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_overCurrentLimit(int newval)
static const double BRAKINGFORCE_INVALID
Definition: yocto_motor.h:132
YMotor(const string &func)
Definition: yocto_motor.cpp:51
int get_overCurrentLimit(void)
string _className
Definition: yocto_api.h:1558
double _cutOffVoltage
Definition: yocto_motor.h:100
int set_failSafeTimeout(int newval)
Y_MOTORSTATUS_enum
Definition: yocto_motor.h:57
virtual int _invokeValueCallback(string value)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static YMotor * FirstMotor(void)
virtual int resetStatus(void)
static const double DRIVINGFORCE_INVALID
Definition: yocto_motor.h:131
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
int _failSafeTimeout
Definition: yocto_motor.h:104
#define YISERR(retcode)
Definition: ydef.h:394
double get_cutOffVoltage(void)
double get_drivingForce(void)
virtual int drivingForceMove(double targetPower, int delay)
bool has(const string &key)
Definition: yocto_api.cpp:735
virtual int brakingForceMove(double targetPower, int delay)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
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