iCub-main
Loading...
Searching...
No Matches
eomcUtils.h
Go to the documentation of this file.
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3
4/* Copyright (C) 2012 iCub Facility, Istituto Italiano di Tecnologia
5 * Author: Valentina Gaggero
6 * email: valentina.gaggero@iit.it
7 * Permission is granted to copy, distribute, and/or modify this program
8 * under the terms of the GNU General Public License, version 2 or any
9 * later version published by the Free Software Foundation.
10 *
11 * A copy of the license can be found at
12 * http://www.robotcub.org/icub/license/gpl.txt
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 * Public License for more details
18 */
19
20//here there is only a name space that contains all utils funtion of embObjMotionControl
21#include <string>
22
23namespace yarp {
24 namespace dev {
25 namespace eomc
26
27{
28 enum { MAX_SHORT = 32767, MIN_SHORT = -32768, MAX_INT = 0x7fffffff, MIN_INT = 0x80000000, MAX_U32 = 0xffffffff, MIN_U32 = 0x00, MAX_U16 = 0xffff, MIN_U16 = 0x0000};
29 enum { CAN_SKIP_ADDR = 0x80 };
30
31 // saturation check and rounding for 16 bit unsigned integer
32 int U_16(double x)
33 {
34 if (x <= double(MIN_U16) )
35 return MIN_U16;
36 else
37 if (x >= double(MAX_U16))
38 return MAX_U16;
39 else
40 return int(x + .5);
41 }
42
43 // saturation check and rounding for 16 bit signed integer
44 short S_16(double x)
45 {
46 if (x <= double(-(MAX_SHORT))-1)
47 return MIN_SHORT;
48 else
49 if (x >= double(MAX_SHORT))
50 return MAX_SHORT;
51 else
52 if (x>0)
53 return short(x + .5);
54 else
55 return short(x - .5);
56 }
57
58 // saturation check and rounding for 32 bit unsigned integer
59 int U_32(double x)
60 {
61 if (x <= double(MIN_U32) )
62 return MIN_U32;
63 else
64 if (x >= double(MAX_U32))
65 return MAX_U32;
66 else
67 return int(x + .5);
68 }
69
70 // saturation check and rounding for 32 bit signed integer
71 int S_32(double x)
72 {
73 if (x <= double(-(MAX_INT))-1.0)
74 return MIN_INT;
75 else
76 if (x >= double(MAX_INT))
77 return MAX_INT;
78 else
79 if (x>0)
80 return int(x + .5);
81 else
82 return int(x - .5);
83 }
84
85 // bool EncoderType_iCub2eo(const std::string* in, uint8_t *out)
86 // {
87 // if (*in == "NONE")
88 // {
89 // *out = 0;
90 // return true;
91 // }
92 // else if (*in == "AEA")
93 // {
94 // *out = 1;
95 // return true;
96 // }
97 // else if (*in == "ROIE")
98 // {
99 // *out = 2;
100 // return true;
101 // }
102 // else if (*in == "HALL_ADC")
103 // {
104 // *out = 3;
105 // return true;
106 // }
107 // else if (*in == "MAIS")
108 // {
109 // *out = 4;
110 // return true;
111 // }
112 // else if (*in == "OPTICAL_QUAD")
113 // {
114 // *out = 5;
115 // return true;
116 // }
117 // else if (*in == "HALL_MOTOR_SENS")
118 // {
119 // *out = 6;
120 // return true;
121 // }
122 // *out = 0;
123 // return false;
124 // }
125
126 bool EncoderType_eo2iCub(const uint8_t *in, std::string* out)
127 {
128 if (*in == 0)
129 {
130 *out = "NONE";
131 return true;
132 }
133 else if (*in == 1)
134 {
135 *out = "AEA";
136 return true;
137 }
138 else if (*in == 2)
139 {
140 *out = "ROIE";
141 return true;
142 }
143 else if (*in == 3)
144 {
145 *out = "HALL_ADC";
146 return true;
147 }
148 else if (*in == 4)
149 {
150 *out = "MAIS";
151 return true;
152 }
153 else if (*in == 5)
154 {
155 *out = "OPTICAL_QUAD";
156 return true;
157 }
158 else if (*in == 6)
159 {
160 *out = "HALL_MOTOR_SENS";
161 return true;
162 }
163 else if(*in == 9)
164 {
165 *out = "AMO";
166 return true;
167 }
168 *out = "ERROR";
169 return false;
170 }
171
172 void copyPid_iCub2eo(const Pid *in, eOmc_PID_t *out)
173 {
174 memset(out, 0, sizeof(eOmc_PID_t)); // marco.accame: it is good thing to clear the out struct before copying. this prevent future members of struct not yet managed to be dirty.
175 out->kp = (float) (in->kp);
176 out->ki = (float) (in->ki);
177 out->kd = (float) (in->kd);
178 out->limitonintegral = (float)(in->max_int);
179 out->limitonoutput = (float)(in->max_output);
180 out->offset = (float) (in->offset);
181 out->scale = (int8_t) (in->scale);
182 out->kff = (float) (in->kff);
183 }
184
185 void copyPid_eo2iCub(eOmc_PID_t *in, Pid *out)
186 {
187 // marco.accame: in here i dont clear the out class because there is not a clear() method
188 out->kp = (double) in->kp;
189 out->ki = (double) in->ki;
190 out->kd = (double) in->kd;
191 out->max_int = (double) in->limitonintegral;
192 out->max_output = (double) in->limitonoutput;
193 out->offset = (double) in->offset;
194 out->scale = (double) in->scale;
195 out->setStictionValues(0.0, 0.0);
196 out->setKff(in->kff);
197 }
198
199 bool controlModeCommandConvert_yarp2embObj(int vocabMode, eOenum08_t &embOut)
200 {
201 bool ret = true;
202
203 switch(vocabMode)
204 {
205 case VOCAB_CM_IDLE:
206 embOut = eomc_controlmode_cmd_idle;
207 break;
208
209 case VOCAB_CM_POSITION:
210 embOut = eomc_controlmode_cmd_position;
211 break;
212
213 case VOCAB_CM_POSITION_DIRECT:
214 embOut = eomc_controlmode_cmd_direct;
215 break;
216
217 case VOCAB_CM_VELOCITY:
218 embOut = eomc_controlmode_cmd_velocity;
219 break;
220
221 case VOCAB_CM_VELOCITY_DIRECT:
222 embOut = eomc_controlmode_cmd_vel_direct;
223 break;
224
225 case VOCAB_CM_MIXED:
226 embOut = eomc_controlmode_cmd_mixed;
227 break;
228
229 case VOCAB_CM_TORQUE:
230 embOut = eomc_controlmode_cmd_torque;
231 break;
232
233 case VOCAB_CM_IMPEDANCE_POS:
234 embOut = eomc_controlmode_cmd_impedance_pos;
235 break;
236
237 case VOCAB_CM_IMPEDANCE_VEL:
238 embOut = eomc_controlmode_cmd_impedance_vel;
239 break;
240
241 case VOCAB_CM_PWM:
242 embOut = eomc_controlmode_cmd_openloop;
243 break;
244
245 case VOCAB_CM_CURRENT:
246 embOut = eomc_controlmode_cmd_current;
247 break;
248
249 case VOCAB_CM_FORCE_IDLE:
250 embOut = eomc_controlmode_cmd_force_idle;
251 break;
252
253 default:
254 ret = false;
255 break;
256 }
257 return ret;
258 }
259
260 int controlModeStatusConvert_embObj2yarp(eOenum08_t embObjMode)
261 {
262 int vocabOut;
263 switch(embObjMode)
264 {
265 case eomc_controlmode_idle:
266 vocabOut = VOCAB_CM_IDLE;
267 break;
268
269 case eomc_controlmode_position:
270 vocabOut = VOCAB_CM_POSITION;
271 break;
272
273 case eomc_controlmode_velocity:
274 vocabOut = VOCAB_CM_VELOCITY;
275 break;
276
277 case eomc_controlmode_cmd_vel_direct:
278 vocabOut = VOCAB_CM_VELOCITY_DIRECT;
279 break;
280
281 case eomc_controlmode_direct:
282 vocabOut = VOCAB_CM_POSITION_DIRECT;
283 break;
284
285 case eomc_controlmode_mixed:
286 case eomc_controlmode_velocity_pos: // they are the same, this will probably removed in the future
287 vocabOut = VOCAB_CM_MIXED;
288 break;
289
290 case eomc_controlmode_torque:
291 vocabOut = VOCAB_CM_TORQUE;
292 break;
293
294 case eomc_controlmode_calib:
295 vocabOut = VOCAB_CM_CALIBRATING;
296 break;
297
298 case eomc_controlmode_impedance_pos:
299 vocabOut = VOCAB_CM_IMPEDANCE_POS;
300 break;
301
302 case eomc_controlmode_impedance_vel:
303 vocabOut = VOCAB_CM_IMPEDANCE_VEL;
304 break;
305
306 case eomc_controlmode_openloop:
307 vocabOut = VOCAB_CM_PWM;
308 break;
309
310 case eomc_controlmode_current:
311 vocabOut = VOCAB_CM_CURRENT;
312 break;
313
314 case eomc_controlmode_hwFault:
315 vocabOut = VOCAB_CM_HW_FAULT;
316 break;
317
318 case eomc_controlmode_notConfigured:
319 vocabOut = VOCAB_CM_NOT_CONFIGURED;
320 break;
321
322 case eomc_controlmode_configured:
323 vocabOut = VOCAB_CM_CONFIGURED;
324 break;
325
326 default:
327 vocabOut = VOCAB_CM_UNKNOWN;
328 yError() << "Received an unknown controlMode from the EMS boards with value " << (int)embObjMode;
329 break;
330 }
331 return vocabOut;
332 }
333
334 bool controlModeStatusConvert_yarp2embObj(int vocabMode, eOmc_controlmode_t &embOut)
335 {
336 yError() << "controlModeStatusConvert_yarp2embObj" << " is not yet implemented for embObjMotionControl";
337 return false;
338 }
339
340 int controlModeCommandConvert_embObj2yarp(eOmc_controlmode_command_t embObjMode)
341 {
342 yError() << "embObjMotionControl::controlModeCommandConvert_embObj2yarp" << " is not yet implemented for embObjMotionControl";
343 return 0;
344
345 }
346
347 bool interactionModeCommandConvert_yarp2embObj(int vocabMode, eOenum08_t &embOut)
348 {
349 bool ret = true;
350
351 switch(vocabMode)
352 {
353 case VOCAB_IM_STIFF:
354 embOut = eOmc_interactionmode_stiff;
355 break;
356
357 case VOCAB_IM_COMPLIANT:
358 embOut = eOmc_interactionmode_compliant;
359 break;
360
361 default:
362 ret = false;
363 break;
364 }
365 return ret;
366 }
367
368
369 bool interactionModeStatusConvert_embObj2yarp(eOenum08_t embObjMode, int &vocabOut)
370 {
371 bool ret = true;
372 switch(embObjMode)
373 {
374 case eOmc_interactionmode_stiff:
375 vocabOut = VOCAB_IM_STIFF;
376 break;
377
378 case eOmc_interactionmode_compliant:
379 vocabOut = VOCAB_IM_COMPLIANT;
380 break;
381
382 default:
383 vocabOut = 666; //VOCAB_CM_UNKNOWN;
384 yError() << "Received an unknown interactionMode from the EMS boards with value " << embObjMode;
385 // ret = false;
386 break;
387 }
388 return ret;
389 }
390
391
392
393 }
394 }}
int S_32(double x)
Definition eomcUtils.h:71
int controlModeStatusConvert_embObj2yarp(eOenum08_t embObjMode)
Definition eomcUtils.h:260
int controlModeCommandConvert_embObj2yarp(eOmc_controlmode_command_t embObjMode)
Definition eomcUtils.h:340
bool interactionModeStatusConvert_embObj2yarp(eOenum08_t embObjMode, int &vocabOut)
Definition eomcUtils.h:369
bool controlModeCommandConvert_yarp2embObj(int vocabMode, eOenum08_t &embOut)
Definition eomcUtils.h:199
void copyPid_eo2iCub(eOmc_PID_t *in, Pid *out)
Definition eomcUtils.h:185
bool EncoderType_eo2iCub(const uint8_t *in, std::string *out)
Definition eomcUtils.h:126
short S_16(double x)
Definition eomcUtils.h:44
bool controlModeStatusConvert_yarp2embObj(int vocabMode, eOmc_controlmode_t &embOut)
Definition eomcUtils.h:334
void copyPid_iCub2eo(const Pid *in, eOmc_PID_t *out)
Definition eomcUtils.h:172
bool interactionModeCommandConvert_yarp2embObj(int vocabMode, eOenum08_t &embOut)
Definition eomcUtils.h:347
int U_16(double x)
Definition eomcUtils.h:32
int U_32(double x)
Definition eomcUtils.h:59
Copyright (C) 2008 RobotCub Consortium.
out
Definition sine.m:8