summaryrefslogtreecommitdiff
path: root/digital/io/src/asserv.c
blob: a60ca55ba7ed488a222a517a007eb35d257321b9 (plain)
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
/* asserv.c */
/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
 *
 * Copyright (C) 2008 Dufour Jérémy
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "asserv.h"

#include "twi_master.h"

#include "modules/utils/byte.h"	/* v*_to_v* */
#include "modules/math/fixed/fixed.h"
#include "bot.h"
#include "io.h"

/**
 * @defgroup AsservPrivate Asserv module private variables and functions
 * declarations and definitions
 * @{
 */

/**
 * Flag bit position value for the status byte of the asserv.
 */
enum asserv_status_flag_e
{
    /** Bot movement finished with success. */
    asserv_status_flag_move_succeed = 0,
    /** Bot movement finished with failure: the bot is blocked. */
    asserv_status_flag_move_failed = 1,
    /** Bot is moving forward (linear speed greater than 0). */
    asserv_status_flag_move_forward = 2,
    /** Bot is moving backward (linear speed smaller than 0). */
    asserv_status_flag_move_backward = 3,
    /** Motor0 movement finished with success. */
    asserv_status_flag_motor0_succeed = 4,
    /** Motor0 movement finished with failure. */
    asserv_status_flag_motor0_failed = 5,
    /** Motor1 movement finished with success. */
    asserv_status_flag_motor1_succeed = 6,
    /** Motor1 movement finished with failure. */
    asserv_status_flag_motor1_failed = 7,
};
typedef enum asserv_status_flag_e asserv_status_flag_e;

/** Scaling factor. */
static uint32_t asserv_scale;

/** Scaling factor inverse. */
static uint32_t asserv_scale_inv;

/** Last moving direction. */
static uint8_t asserv_last_moving_direction;

/**
 * Structure for storing a position for the bot using asserv units.
 */
typedef struct asserv_position_t
{
    /** X position. */
    uint32_t x;
    /** Y position. */
    uint32_t y;
    /** Angle. */
    uint16_t a;
} asserv_position_t;

/**
 * Status structure maintains by the update command.
 */
typedef struct asserv_struct_s
{
    /** Status flags. */
    uint8_t status;
    /** Asserv board input port. */
    uint8_t input_port;
    /** Bot position. */
    asserv_position_t position;
    /** Motor0 position. */
    uint16_t motor0_position;
    /** Motor1 position. */
    uint16_t motor1_position;
} asserv_struct_s;

/**
 * Status variable.
 */
asserv_struct_s asserv_status;

void
asserv_init (void)
{
    asserv_set_scale (BOT_SCALE * (1L << 24));
}

void
asserv_status_cb (uint8_t *status)
{
    /* Parse received data and store them. */
    asserv_status.status = status[0];
    asserv_status.input_port = status[1];
    asserv_status.position.x = v8_to_v32 (0, status[3], status[4], status[5]);
    asserv_status.position.y = v8_to_v32 (0, status[6], status[7], status[8]);
    asserv_status.position.a = v8_to_v16 (status[9], status[10]);
    asserv_status.motor0_position = v8_to_v16 (status[11], status[12]);
    asserv_status.motor1_position = v8_to_v16 (status[13], status[14]);
    /* Update moving direction. */
    if (asserv_get_moving_direction () != 0)
	asserv_last_moving_direction = asserv_get_moving_direction ();
}

asserv_status_e
asserv_move_cmd_status (void)
{
    /* Check Motor Finished flag */
    if (asserv_status.status & _BV (asserv_status_flag_move_succeed))
	return success;
    /* Check Motor Blocked flag */
    else if (asserv_status.status & _BV (asserv_status_flag_move_failed))
	return failure;
    /* Otherwise, not finished nor failure */
    return none;
}

asserv_status_e
asserv_motor0_cmd_status (void)
{
    /* Check Motor0 Finished flag */
    if (asserv_status.status & _BV (asserv_status_flag_motor0_succeed))
	return success;
    /* Check Motor0 Blocked flag */
    else if (asserv_status.status & _BV (asserv_status_flag_motor0_failed))
	return failure;
    /* Otherwise, not finished nor failure */
    return none;
}

asserv_status_e
asserv_motor1_cmd_status (void)
{
    /* Check Motor1 Finished flag */
    if (asserv_status.status & _BV (asserv_status_flag_motor1_succeed))
	return success;
    /* Check Motor1 Blocked flag */
    else if (asserv_status.status & _BV (asserv_status_flag_motor1_failed))
	return failure;
    /* Otherwise, not finished nor failure */
    return none;
}

void
asserv_get_position (position_t *current_position)
{
    assert (current_position);
    /* Copy last received status buffer information to current position */
    current_position->v.x = fixed_mul_f824 (asserv_status.position.x,
					    asserv_scale);
    current_position->v.y = fixed_mul_f824 (asserv_status.position.y,
					    asserv_scale);
    current_position->a = asserv_status.position.a;
}

uint16_t
asserv_get_motor0_position (void)
{
    /* Return the position of the motor0 of the current status buffer */
    return asserv_status.motor0_position;
}

uint16_t
asserv_get_motor1_position (void)
{
    /* Return the position of the motor1 of the current status buffer */
    return asserv_status.motor1_position;
}

uint8_t
asserv_get_moving_direction (void)
{
    /* Foward move? */
    if (asserv_status.status & _BV (asserv_status_flag_move_forward))
	return 1;
    /* Backward move? */
    if (asserv_status.status & _BV (asserv_status_flag_move_backward))
	return 2;
    /* Not moving */
    return 0;
}

uint8_t
asserv_get_last_moving_direction (void)
{
    return asserv_last_moving_direction;
}

void
asserv_reset (void)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'z';
    twi_master_send (1);
}

void
asserv_free_motor (void)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'w';
    twi_master_send (1);
}

void
asserv_stop_motor (void)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 's';
    twi_master_send (1);
}

void
asserv_move_linearly (int32_t distance)
{
    distance = fixed_mul_f824 (distance, asserv_scale_inv);
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'l';
    buffer[1] = v32_to_v8 (distance, 2);
    buffer[2] = v32_to_v8 (distance, 1);
    buffer[3] = v32_to_v8 (distance, 0);
    twi_master_send (4);
}

void
asserv_move_angularly (int16_t angle)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'a';
    buffer[1] = v16_to_v8 (angle, 1);
    buffer[2] = v16_to_v8 (angle, 0);
    twi_master_send (3);
}

void
asserv_goto_angle (int16_t angle)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'y';
    buffer[1] = v16_to_v8 (angle, 1);
    buffer[2] = v16_to_v8 (angle, 0);
    twi_master_send (3);
}

void
asserv_goto_xya (uint32_t x, uint32_t y, int16_t a, uint8_t backward)
{
    x = fixed_mul_f824 (x, asserv_scale_inv);
    y = fixed_mul_f824 (y, asserv_scale_inv);
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'X';
    buffer[1] = v32_to_v8 (x, 2);
    buffer[2] = v32_to_v8 (x, 1);
    buffer[3] = v32_to_v8 (x, 0);
    buffer[4] = v32_to_v8 (y, 2);
    buffer[5] = v32_to_v8 (y, 1);
    buffer[6] = v32_to_v8 (y, 0);
    buffer[7] = v16_to_v8 (a, 1);
    buffer[8] = v16_to_v8 (a, 0);
    buffer[9] = backward;
    twi_master_send (10);
}

void
asserv_go_to_the_wall (uint8_t backward)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'f';
    buffer[1] = backward;
    twi_master_send (2);
}

void
asserv_move_motor0_absolute (uint16_t position, uint8_t speed)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'b';
    buffer[1] = v16_to_v8 (position, 1);
    buffer[2] = v16_to_v8 (position, 0);
    buffer[3] = speed;
    twi_master_send (4);
}

void
asserv_move_motor1_absolute (uint16_t position, uint8_t speed)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'c';
    buffer[1] = v16_to_v8 (position, 1);
    buffer[2] = v16_to_v8 (position, 0);
    buffer[3] = speed;
    twi_master_send (4);
}

void
asserv_set_x_position (int32_t x)
{
    x = fixed_mul_f824 (x, asserv_scale_inv);
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'p';
    buffer[1] = 'X';
    buffer[2] = v32_to_v8 (x, 2);
    buffer[3] = v32_to_v8 (x, 1);
    buffer[4] = v32_to_v8 (x, 0);
    twi_master_send (5);
}

void
asserv_set_y_position (int32_t y)
{
    y = fixed_mul_f824 (y, asserv_scale_inv);
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'p';
    buffer[1] = 'Y';
    buffer[2] = v32_to_v8 (y, 2);
    buffer[3] = v32_to_v8 (y, 1);
    buffer[4] = v32_to_v8 (y, 0);
    twi_master_send (5);
}

void
asserv_set_angle_position (int16_t angle)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'p';
    buffer[1] = 'A';
    buffer[2] = v32_to_v8 (angle, 1);
    buffer[3] = v32_to_v8 (angle, 0);
    twi_master_send (4);
}

void
asserv_set_speed (uint8_t linear_high, uint8_t angular_high,
		  uint8_t linear_low, uint8_t angular_low)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'p';
    buffer[1] = 's';
    buffer[2] = linear_high;
    buffer[3] = angular_high;
    buffer[4] = linear_low;
    buffer[5] = angular_low;
    twi_master_send (6);
}

void
asserv_set_position (int32_t x, int32_t y, int16_t angle)
{
    x = fixed_mul_f824 (x, asserv_scale_inv);
    y = fixed_mul_f824 (y, asserv_scale_inv);
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'p';
    buffer[1] = 'X';
    buffer[2] = v32_to_v8 (x, 2);
    buffer[3] = v32_to_v8 (x, 1);
    buffer[4] = v32_to_v8 (x, 0);
    buffer[5] = 'Y';
    buffer[6] = v32_to_v8 (y, 2);
    buffer[7] = v32_to_v8 (y, 1);
    buffer[8] = v32_to_v8 (y, 0);
    buffer[9] = 'A';
    buffer[10] = v32_to_v8 (angle, 1);
    buffer[11] = v32_to_v8 (angle, 0);
    twi_master_send (12);
}

void
asserv_goto (uint32_t x, uint32_t y, uint8_t backward)
{
    x = fixed_mul_f824 (x, asserv_scale_inv);
    y = fixed_mul_f824 (y, asserv_scale_inv);
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'x';
    buffer[1] = v32_to_v8 (x, 2);
    buffer[2] = v32_to_v8 (x, 1);
    buffer[3] = v32_to_v8 (x, 0);
    buffer[4] = v32_to_v8 (y, 2);
    buffer[5] = v32_to_v8 (y, 1);
    buffer[6] = v32_to_v8 (y, 0);
    buffer[7] = backward;
    twi_master_send (8);
}

void
asserv_set_scale (uint32_t scale)
{
    asserv_scale = scale;
    asserv_scale_inv = fixed_div_f824 (1L << 24, scale);
}

void
asserv_motor0_zero_position (int8_t speed)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'B';
    buffer[1] = speed;
    twi_master_send (2);
}

void
asserv_motor1_zero_position (int8_t speed)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'C';
    buffer[1] = speed;
    twi_master_send (2);
}

void
asserv_motor0_free (void)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'r';
    buffer[1] = 0;
    twi_master_send (2);
}

void
asserv_motor1_free (void)
{
    uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
    buffer[0] = 'r';
    buffer[1] = 1;
    twi_master_send (2);
}