Source

libkondo4 / ics.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
 * Kondo ICS 3.0 Library
 *
 * Copyright 2010 - Christopher Vo (cvo1@cs.gmu.edu)
 * George Mason University - Autonomous Robotics Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ics.h"

/*-----------------------------------------------------------------------------
 * Convenience macros
 */
#define ics_error(ki, err) { \
  snprintf(ki->error, 128, "ERROR: %s: %s\n", __func__, err); \
  return -1; }
#define ics_ftdi_error(ki) { \
  snprintf(ki->error, 128, "ERROR: %s: %s\n", __func__, \
    ftdi_get_error_string(&ki->ftdic)); \
  return -1; }

/*-----------------------------------------------------------------------------
 * Iniitialize the ICS interface
 * Here we mainly setup the FTDI USB-to-serial communication
 * 115200 baud, 8 bits, even parity, 1 stop bit.
 * Returns: 0 if successful, < 0 otherwise
 */
int ics_init(ICSData * r)
{
	assert(r);
	r->debug = 0;

	// init usb
	if (ftdi_init(&r->ftdic) < 0)
		ics_ftdi_error(r);

	// select first interface
	if (ftdi_set_interface(&r->ftdic, INTERFACE_C) < 0)
		ics_ftdi_error(r);

	// open usb device
	if (ftdi_usb_open(&r->ftdic, ICS_USB_VID, ICS_USB_PID) < 0)
		ics_ftdi_error(r);

	// set baud rate
	if (ftdi_set_baudrate(&r->ftdic, ICS_BAUD) < 0)
		ics_ftdi_error(r);

	// set line parameters (8E1)
	if (ftdi_set_line_property(&r->ftdic, BITS_8, STOP_BIT_1, EVEN) < 0)
		ics_ftdi_error(r);

	return 0;
}

/*-----------------------------------------------------------------------------
 * Close / Deinitialize the ICS Interface.
 * Mainly closes the USB device and deletes the allocated data.
 * Returns 0 if successful, < 0 otherwise
 */
int ics_close(ICSData * r)
{
	assert(r);

	// close usb device
	if (ftdi_usb_close(&r->ftdic) < 0)
		ics_ftdi_error(r);

	// deinit
	ftdi_deinit(&r->ftdic);

	return 0;
}

/*-----------------------------------------------------------------------------
 * Write n bytes from the swap to the Kondo.
 * Returns >0 number of bytes written, < 0 if error
 */
int ics_write(ICSData * r, int n)
{
	assert(r);
	int i;
	if ((i = ftdi_write_data(&r->ftdic, r->swap, n)) < 0)
		ics_ftdi_error(r);
	return i;
}
/*-----------------------------------------------------------------------------
 * Read n bytes from ICS. Reads immediately from the serial buffer.
 * See ics_read_timeout for a version that blocks waiting for the data.
 * Returns < 0: error
 * Returns >= 0: number of bytes read
 */
int ics_read(ICSData * r, int n)
{
	assert(r);
	int i;
	if ((i = ftdi_write_data(&r->ftdic, r->swap, n)) < 0)
		ics_ftdi_error(r);
	return i;
}
/*-----------------------------------------------------------------------------
 * Read n bytes from the ICS, waiting for at most usecs for the n bytes.
 * Performs this by continuously polling the serial buffer until either
 * all of the bytes are read or the timeout has been reached.
 * Returns < 0: error
 * Returns >= 0: number of bytes read
 */
int ics_read_timeout(ICSData * r, int n, long usecs)
{
	assert(r);
	static struct timeval tv, end;
	int i = 0, bytes_read = 0;
	gettimeofday(&tv, NULL);
	// determine end time
	end.tv_sec = tv.tv_sec + usecs / 1000000;
	end.tv_usec = tv.tv_usec + usecs % 1000000;
	if (end.tv_usec > 1000000) {
		end.tv_sec += 1;
		end.tv_usec -= 1000000;
	}
	// spam the read until data arrives
	do {
		if ((i = ftdi_read_data(&r->ftdic, r->swap, n - bytes_read)) < 0)
			ics_ftdi_error(r);
		bytes_read += i;
		gettimeofday(&tv, NULL);
	} while (bytes_read < n && (tv.tv_sec < end.tv_sec || tv.tv_usec
			< end.tv_usec));
	return bytes_read;
}

/*-----------------------------------------------------------------------------
 * Purge the serial buffers.
 * Returns 0 if successful, < 0 if error
 */
int ics_purge(ICSData * r)
{
	assert(r);
	if (ftdi_usb_purge_buffers(&r->ftdic) < 0)
		ics_ftdi_error(r);
	return 0;
}

/*-----------------------------------------------------------------------------
 * Transaction template: Purge, then send out_bytes, then receive in_bytes
 * Blocks for ICS_RX_TIMEOUT microseconds (default value)
 * Returns < 0: error
 * Returns >= 0: number of bytes read
 */
int ics_trx(ICSData * r, UINT bytes_out, UINT bytes_in)
{
	return ics_trx_timeout(r, bytes_out, bytes_in, ICS_RX_TIMEOUT);
}

/*-----------------------------------------------------------------------------
 * Transaction template: Purge, then send out_bytes, then receive in_bytes
 * On RX, blocks for at most timeout microseconds before giving up.
 * Returns < 0: error
 * Returns >= 0: number of bytes read
 */
int ics_trx_timeout(ICSData * r, UINT bytes_out, UINT bytes_in, long timeout)
{
	assert(r);
	int i;
	int j;

	if ((i = ics_purge(r)) < 0)
		return i;
	if ((i = ics_write(r, bytes_out)) < 0)
		return i;

	// debug printing
	if (r->debug) {
		printf("send %d bytes: ", i);
		for (j = 0; j < i; j++)
			printf("%x ", r->swap[j]);
		printf("\n");
	}

	// clear swap
	for (i = 0; i < bytes_in; i++)
		r->swap[i] = 0;

	// read the return data
	i = ics_read_timeout(r, bytes_in, timeout);

	// debug printing
	if (r->debug) {
		printf("recv %d bytes: ", i);
		for (j = 0; j < i; j++)
			printf("%x ", r->swap[j]);
		printf("\n");
	}

	return i;
}

/*-----------------------------------------------------------------------------
 * Set servo position
 * id: the servo id, 0-31
 * pos: the position to set, 0-16383
 * to free the servo, pos = 0
 * to hold the servo, pos = 16383
 * Returns current position >= 0, or < 0 if error.
 */
int ics_pos(ICSData * r, UINT id, UINT pos)
{
	assert(r);
	int i;

	// check for valid data
	if (id > 31)
		ics_error(r, "Invalid servo ID > 31.");
	if (pos > 16383)
		ics_error(r, "Invalid servo position > 16383.");

	// build command
	r->swap[0] = id | ICS_CMD_POS; // id and command
	r->swap[1] = (pos >> 7) & 0x7F; // high 7 bits of pos
	r->swap[2] = pos & 0x7F; // low 7 bits of pos

	// synchronize with servo
	if ((i = ics_trx_timeout(r, 3, 3, ICS_POS_TIMEOUT)) < 0)
		return i;

	// return the position
	return ((r->swap[1] & 0x7F) << 7) | (r->swap[2] & 0x7F);
}

/*-----------------------------------------------------------------------------
 * Hold servo position
 * This tells the servo to stop and maintain the current position.
 * id: the servo id, 0-31
 * Returns current position >= 0, or < 0 if error.
 */
int ics_hold(ICSData * r, UINT id)
{
	return ics_pos(r, id, 16383);
}

/*-----------------------------------------------------------------------------
 * Free servo position
 * This tells the servo to stop and relax (power off motor)
 * id: the servo id, 0-31
 * Returns the current position >= 0, or < 0 if error.
 */
int ics_free(ICSData * r, UINT id)
{
	return ics_pos(r, id, 0);
}

/*-----------------------------------------------------------------------------
 * Get servo stretch
 * id: the servo id, 0-31
 * Returns: Value of stretch (>= 0) if successful, < 0 if error
 */
int ics_get_stretch(ICSData * r, UINT id)
{
	assert(r);
	int i;

	// check valid id
	if (id > 31)
		ics_error(r, "Invalid servo ID > 31.");

	// build command
	r->swap[0] = id | ICS_CMD_GET; // id and command
	r->swap[1] = ICS_SC_STRETCH; // subcommand

	// synchronize
	if ((i = ics_trx_timeout(r, 2, 3, ICS_GET_TIMEOUT)) < 0)
		return i;

	// return stretch
	return r->swap[2];
}

/*-----------------------------------------------------------------------------
 * Get servo speed
 * id: the servo id, 0-31
 * Returns: Value of speed (>= 0) if successful, < 0 if error
 */
int ics_get_speed(ICSData * r, UINT id)
{
	assert(r);
	int i;

	// check valid id
	if (id > 31)
		ics_error(r, "Invalid servo ID > 31.");

	// build command
	r->swap[0] = id | ICS_CMD_GET; // id and command
	r->swap[1] = ICS_SC_SPEED; // subcommand

	// synchronize
	if ((i = ics_trx_timeout(r, 2, 3, ICS_GET_TIMEOUT)) < 0)
		return i;

	// return stretch
	return r->swap[2];
}

/*-----------------------------------------------------------------------------
 * Get servo current
 * id: the servo id, 0-31
 * Returns: Value of current (>= 0) if successful, < 0 if error
 */
int ics_get_current(ICSData * r, UINT id)
{
	assert(r);
	int i;

	// check valid id
	if (id > 31)
		ics_error(r, "Invalid servo ID > 31.");

	// build command
	r->swap[0] = id | ICS_CMD_GET; // id and command
	r->swap[1] = ICS_SC_CURRENT; // subcommand

	// synchronize
	if ((i = ics_trx_timeout(r, 2, 3, ICS_GET_TIMEOUT)) < 0)
		return i;

	// return stretch
	return r->swap[2];
}

/*-----------------------------------------------------------------------------
 * Set stretch parameter of the servo
 * id: the id of the servo 0-31
 * stretch: the desired stretch 1(2)-127(254)
 * Returns: the stretch as reported by the servo (>= 0), or < 0 if error
 */
int ics_set_stretch(ICSData * r, UINT id, UCHAR stretch)
{
	assert(r);
	int i;

	// check valid stuff
	if (id > 31)
		ics_error(r, "Invalid servo ID > 31.");
	if (stretch < 1 || stretch > 127)
		ics_error(r, "Invalid stretch, not [1-127]");

	// build command
	r->swap[0] = id | ICS_CMD_SET; // id and command
	r->swap[1] = ICS_SC_STRETCH; // subcommand
	r->swap[2] = stretch;

	// synchronize
	if ((i = ics_trx_timeout(r, 3, 3, ICS_SET_TIMEOUT)) < 0)
		return i;

	return r->swap[2];
}

/*-----------------------------------------------------------------------------
 * Set speed parameter of the servo
 * id: the id of the servo 0-31
 * stretch: the desired speed 1(2)-127(254)
 * Returns: the speed as reported by the servo (>= 0), or < 0 if error
 */
int ics_set_speed(ICSData * r, UINT id, UCHAR speed)
{
	assert(r);
	int i;

	// check valid stuff
	if (id > 31)
		ics_error(r, "Invalid servo ID > 31.");
	if (speed < 1 || speed > 127)
		ics_error(r, "Invalid speed, not [1-127]");

	// build command
	r->swap[0] = id | ICS_CMD_SET; // id and command
	r->swap[1] = ICS_SC_SPEED; // subcommand
	r->swap[2] = speed;

	// synchronize
	if ((i = ics_trx_timeout(r, 3, 3, ICS_SET_TIMEOUT)) < 0)
		return i;

	return r->swap[2];
}
/*-----------------------------------------------------------------------------
 * Set current limit parameter of the servo
 * id: the id of the servo 0-31
 * curlim: the desired current limit 1-63
 * Returns: the current limit as reported by the servo (>= 0), or < 0 if error
 */
int ics_set_current_limit(ICSData * r, UINT id, UCHAR curlim)
{
	assert(r);
	int i;

	// check valid stuff
	if (id > 31)
		ics_error(r, "Invalid servo ID > 31.");
	if (curlim > 1 || curlim > 63)
		ics_error(r, "Invalid current, not [1-63]");

	// build command
	r->swap[0] = id | ICS_CMD_SET; // id and command
	r->swap[1] = ICS_SC_CURRENT; // subcommand
	r->swap[2] = curlim;

	// synchronize
	if ((i = ics_trx_timeout(r, 3, 3, ICS_SET_TIMEOUT)) < 0)
		return i;

	return r->swap[2];
}
/*-----------------------------------------------------------------------------
 * Get the ID of the connected servo
 * This command should be used with only one servo attached to the bus.
 * Returns: ID (0-31), or < 0 if error
 */
int ics_get_id(ICSData * r)
{
	assert(r);
	int i;

	// build command
	r->swap[0] = 0xFF; // command (0xFF for read)
	r->swap[1] = ICS_SC_READ; // subcommand (read)
	r->swap[2] = ICS_SC_READ; // subcommand (read)
	r->swap[3] = ICS_SC_READ; // subcommand (read)

	// synchronize
	if ((i = ics_trx_timeout(r, 4, 1, ICS_ID_TIMEOUT)) < 0)
		return i;

	// return the ID
	return r->swap[0] & 0x1F;
}

/*-----------------------------------------------------------------------------
 * Set the ID of the connected servo
 * This command should be used with only one servo attached to the bus.
 * id: the desired ID of the servo 0-31
 * Returns: ID (0-31), or < 0 if error.
 */
int ics_set_id(ICSData * r, UINT id)
{
	assert(r);
	int i;

	// check id
	if (id > 31)
		ics_error(r, "Invalid servo ID, > 31.");

	// build command
	r->swap[0] = id | ICS_CMD_ID;
	r->swap[1] = ICS_SC_WRITE;
	r->swap[2] = ICS_SC_WRITE;
	r->swap[3] = ICS_SC_WRITE;

	// synchronize
	if ((i = ics_trx_timeout(r, 4, 1, ICS_ID_TIMEOUT)) < 0)
		return i;

	// return the ID
	return r->swap[0] & 0x1F;
}