summaryrefslogtreecommitdiff
path: root/2004/n/asserv/src/motor.c
blob: aab34cf378d9973df573d3e1ad07aa1e24dfe81c (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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* motor.c */
/* APBTasserv - asservissement Robot 2004 {{{
 *
 * Copyright (C) 2003 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2004.
 *	Web: http://assos.efrei.fr/robot/
 *	Mail: robot AT efrei DOT fr
 *
 * 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 "motor.h"
#include "serial.h"

/* Variables globales. */
short motor_int_recv;		/* Mis � 1 lors d'une interruption. */
short motor_asservi;		/* Asservissement activ� ? */
short motor_pos_asserv;		/* Si vrai, la carte d'asservissement est en
				   mode d'asservissement en position. C'est �
				   dire, l'ordinateur connect� � la carte
				   d'asservissement g�re lui m�me la vitesse
				   afin d'asservir la position. En pratique,
				    - pas d'acquitement lors d'un !v
				    - arr�t automatique si aucune commande !v
				    re�ue apr�s n cycles. Voir motor_ttl. */
unsigned int motor_ttl;		/* Time to live : arr�te le robot si arrive �
				   0 en mode asservissement position. */
unsigned int motor_kp, motor_ki, motor_kd;	/* Coefficients du PID. */
unsigned int motor_a;		/* Acceleration. */
unsigned int motor_a_cpt;	/* Compteur d'acceleration. */
signed long motor_int_max;	/* Terme int�gral maximum. */
unsigned int motor_pid_int;	/* Compteur d'interruptions timer2 entre deux
				   calculs de PID. */

/* Statistiques, etc... */
unsigned int motor_stat_delay;		/* Delay between stats. */
unsigned int motor_stat_delay_cpt;	/* Delay counter. */
short motor_cpt;			/* Report motors counters. */

/* Entr�es. */
unsigned int motor_gpi_delay;		/* Delais entre deux envois. */
unsigned int motor_gpi_delay_cpt;	/* Compteur. */

/* Moteurs. */
signed int motor_g_vdes, motor_g_vacc;		/* Vitesse d�sir�e, actuelle. */
signed int motor_d_vdes, motor_d_vacc;
unsigned int motor_g_cpt_av, motor_g_cpt_ar;	/* Compteurs avant/arri�re. */
unsigned int motor_d_cpt_av, motor_d_cpt_ar;
signed long motor_g_cpt, motor_d_cpt;		/* Compteurs. */
signed long motor_g_e_old, motor_d_e_old;	/* Derni�re erreur, pour le
						   calcul de la d�riv�e. */
signed long motor_g_pwm_old, motor_d_pwm_old;	/* Derni�re pwm, pour les
						   stats. */
signed long motor_g_int, motor_d_int;		/* Valeur integrale. */
signed long motor_g_der, motor_d_der;		/* Valeur d�riv�e. */

/* Comptage hard. */
unsigned int motor_timer0_old, motor_timer3_old;	/* Ancienne valeur du
							   compteur hard. */

/* Initialise le module moteur. */
void
motor_init (void)
{
    motor_int_recv = 0;
    motor_asservi = 0;
    motor_pos_asserv = 0;
    motor_ttl = 10;
    motor_kp = 10; motor_ki = 5; motor_kd = 0;
    motor_a = 8;
    motor_a_cpt = 8;
    motor_int_max = 1000;
    motor_pid_int = 0;
    motor_stat_delay = 0;
    motor_stat_delay_cpt = 0;
    motor_cpt = 0;
    motor_gpi_delay = 0;
    motor_gpi_delay_cpt = 0;
    motor_g_vdes = 0; motor_g_vacc = 0;
    motor_d_vdes = 0; motor_d_vacc = 0;
    motor_g_cpt_av = 0; motor_g_cpt_ar = 0;
    motor_d_cpt_av = 0; motor_d_cpt_ar = 0;
    motor_g_cpt = 0; motor_d_cpt = 0;
    motor_g_e_old = 0; motor_d_e_old = 0;
    motor_g_pwm_old = 0; motor_d_pwm_old = 0;
    motor_g_int = 0; motor_d_int = 0;
    motor_g_der = 0; motor_d_der = 0;
    motor_timer0_old = get_timer0 ();
    motor_timer3_old = get_timer3 ();
}

/* Addition limit�e � 32000. */
signed long
safe_add_long (signed long &a, signed long &b)
{
    signed long ret;
    /* Additionne. */
    ret = a + b;
    /* V�rifie les d�bordements par coh�rence des signes. */
    if (a > 0 && b > 0 && ret <= 0)
	return 32000;
    else if (a < 0 && b < 0 && ret >= 0)
	return -32000;
    else
	return ret;
}

/* Soustraction limit�e � 32000. */
signed long
safe_sub_long (signed long &a, signed long &b)
{
    signed long ret;
    /* Soustrait. */
    ret = a - b;
    /* V�rifie les d�bordements par coh�rence des signes. */
    if (a > 0 && b < 0 && ret <= 0)
	return 32000;
    else if (a < 0 && b > 0 && ret >= 0)
	return -32000;
    else
	return ret;
}

/* Limite un entier entre -MAX et MAX. */
signed long
boundary (signed long &n, signed long &max)
{
    if (n > max)
	return max;
    if (n < -max)
	return -max;
    return n;
}

/* Multiplication par les coef. */
signed long
safe_mul_long (signed long &a, unsigned int &k)
{
    unsigned long ua;
    short sa;
    signed long temp;
    /* S�pare le signe de la valeur absolue. */
    if (a >= 0)
      {
	sa = 1;
	ua = a;
      }
    else
      {
	sa = 0;
	ua = -a;
      }
    /* Multiplie le poid fort. */
    temp = make8 (ua, 1) * k;
    /* Test un futur d�bordement. */
    if (temp > 255)
      {
	if (sa) return 32760;
	else return -32760;
      }
    else
      {
	temp = make8 (temp, 1);
	temp += ((long) make8 (ua, 0)) * k ;
	if (sa) return temp;
	else return -temp;
      }
}

/* Met � jour la vitesse du moteur gauche. */
void
motor_update_left_speed (void)
{
    if (motor_g_vacc != motor_g_vdes)
      {
	if (motor_g_vacc < motor_g_vdes)
	  {
	    /* Acc�l�re. */
	    motor_g_vacc++;
	  }
	else
	  {
	    /* Freine. */
	    motor_g_vacc--;
	  }
      }
}

/* Met � jour la vitesse du moteur droit. */
void
motor_update_right_speed (void)
{
    if (motor_d_vacc != motor_d_vdes)
      {
	if (motor_d_vacc < motor_d_vdes)
	  {
	    /* Acc�l�re. */
	    motor_d_vacc++;
	  }
	else
	  {
	    /* Freine. */
	    motor_d_vacc--;
	  }
      }
}

/* Calcule le PID associ� au moteur gauche. */
void
motor_compute_left_pid (void)
{
    signed long e;
    signed long diff;
    signed long pwm;
    signed long temp;
    /* R�cup�re les compteurs et calcule l'erreur. */
    diff = motor_g_cpt_av;
    motor_g_cpt_av = 0;
    diff -= motor_g_cpt_ar;
    motor_g_cpt_ar = 0;
    motor_g_cpt += diff;
    e = motor_g_vacc - diff;
    /* Calcul de l'integrale. */
    motor_g_int = safe_add_long (motor_g_int, e);
    motor_g_int = boundary (motor_g_int, motor_int_max);
    /* Calcul de la d�riv�e. */
    motor_g_der = safe_sub_long (e, motor_g_e_old);
    /* Calcul du PID. P... */
    pwm = safe_mul_long (e, motor_kp);
    /* I... */
    temp = safe_mul_long (motor_g_int, motor_ki);
    pwm = safe_add_long (pwm, temp);
    /* D... */
    temp = safe_mul_long (motor_g_der, motor_kd);
    pwm = safe_add_long (pwm, temp);
    /* Limite la valeur. */
    temp = 1000;
    pwm = boundary (pwm, temp);
    /* Envois la sauce. */
    motor_g_pwm (pwm);
    /* Conserve l'ancienne erreur pour le terme d�riv�. */
    motor_g_e_old = e;
    /* Conserve l'ancienne pwm pour les stats. */
    motor_g_pwm_old = pwm;
}

/* Calcule le PID associ� au moteur droit. */
void
motor_compute_right_pid (void)
{
    signed long e;
    signed long diff;
    signed long pwm;
    signed long temp;
    /* R�cup�re les compteurs et calcule l'erreur. */
    diff = motor_d_cpt_av;
    motor_d_cpt_av = 0;
    diff -= motor_d_cpt_ar;
    motor_d_cpt_ar = 0;
    motor_d_cpt += diff;
    e = motor_d_vacc - diff;
    /* Calcul de l'integrale. */
    motor_d_int = safe_add_long (motor_d_int, e);
    motor_d_int = boundary (motor_d_int, motor_int_max);
    /* Calcul de la d�riv�e. */
    motor_d_der = safe_sub_long (e, motor_d_e_old);
    /* Calcul du PID. P... */
    pwm = safe_mul_long (e, motor_kp);
    /* I... */
    temp = safe_mul_long (motor_d_int, motor_ki);
    pwm = safe_add_long (pwm, temp);
    /* D... */
    temp = safe_mul_long (motor_d_der, motor_kd);
    pwm = safe_add_long (pwm, temp);
    /* Limite la valeur. */
    temp = 1000;
    pwm = boundary (pwm, temp);
    /* Envois la sauce. */
    motor_d_pwm (pwm);
    /* Conserve l'ancienne erreur pour le terme d�riv�. */
    motor_d_e_old = e;
    /* Conserve l'ancienne pwm pour les stats. */
    motor_d_pwm_old = pwm;
}

/* Param�tre la PWM pour le moteur gauche. */
void
motor_g_pwm (signed long &pwm)
{
    if (!motor_asservi) pwm = 0;
    if (pwm >= 0)
      {
	output_high (PIN_D1);
	set_pwm2_duty(pwm);
      }
    else
      {
	output_low (PIN_D1);
	set_pwm2_duty (-pwm);
      }
}

/* Param�tre la PWM pour le moteur droit. */
void
motor_d_pwm (signed long &pwm)
{
    if (!motor_asservi) pwm = 0;
    if (pwm >= 0)
      {
	output_high (PIN_D0);
	set_pwm1_duty(pwm);
      }
    else
      {
	output_low (PIN_D0);
	set_pwm1_duty (-pwm);
      }
}

/* Met � jour les compteurs. */
inline void
motor_update_cpt (void)
{
    unsigned int timer;
    /* R�cup�re le compteur gauche. */
    timer = get_timer0 ();
    if (input_b () & 0x10)
      {
	motor_g_cpt_ar += timer - motor_timer0_old;
      }
    else
      {
	motor_g_cpt_av += timer - motor_timer0_old;
      }
    motor_timer0_old = timer;
    /* R�cup�re le compteur droit. */
    timer = get_timer3 ();
    if (input_b () & 0x80)
      {
	motor_d_cpt_av += timer - motor_timer3_old;
      }
    else
      {
	motor_d_cpt_ar += timer - motor_timer3_old;
      }
    motor_timer3_old = timer;
}

/* Interruption de PID. */
#int_TIMER2
TIMER2_isr ()
{
    motor_int_recv = 1;
}

/* Boucle principale. */
void
motor_main (void)
{
    while (1)
      {
	/* Ne fait le traitement que s'il y a eu une interruption. */
	while (!motor_int_recv)
	    motor_update_cpt ();
	motor_int_recv = 0;
	/* Calcul du PID. */
	if ((motor_pid_int & 7) == 0)
	  {
	    motor_compute_left_pid ();
	    motor_compute_right_pid ();
	    /* Information de PWM. */
	    if (motor_stat_delay)
	      {
		if (!--motor_stat_delay_cpt)
		  {
		    serial_send_motor_stat ('l', motor_g_vacc, motor_g_e_old,
					    motor_g_pwm_old);
		    serial_send_motor_stat ('r', motor_d_vacc, motor_d_e_old,
					    motor_d_pwm_old);
		    serial_unreliable_send_eob ();
		    motor_stat_delay_cpt = motor_stat_delay;
		  }
	      }
	    /* Acc�l�re. */
	    if (motor_a)
	      {
		if (!--motor_a_cpt)
		  {
		    motor_update_left_speed ();
		    motor_update_right_speed ();
		    motor_a_cpt = motor_a;
		  }
	      }
	    else
	      {
		motor_g_vacc = motor_g_vdes;
		motor_d_vacc = motor_d_vdes;
	      }
	  }
	/* Le reste. */
	if ((motor_pid_int & 63) == 0)
	  {
	    /* Gestion du ttl. */
	    if (motor_pos_asserv && (motor_g_vdes || motor_d_vdes) &&
		--motor_ttl == 0)
	      {
		motor_g_vdes = 0;
		motor_d_vdes = 0;
		serial_send_motor_ttl ();
	      }
	    serial_parse ();
	    if (motor_cpt)
	      {
		serial_send_motor_cpt (motor_g_cpt, motor_d_cpt);
		serial_unreliable_send_eob ();
	      }
	    /* GPI. */
	    if (motor_gpi_delay)
	      {
		if (!--motor_gpi_delay_cpt)
		  {
		    serial_send_gpi (input_d ());
		    motor_gpi_delay_cpt = motor_gpi_delay;
		  }
	      }
	  }
	motor_pid_int++;
      }
}

/* Traite une entr�e s�rie. */
short
motor_parse (void)
{
    int temp;
    switch (serial_recv_com ())
      {
      case 'v':
	serial_recv_int (motor_g_vdes);
	serial_recv_int1 (motor_d_vdes);
	if (!motor_pos_asserv)
	    serial_send_ok ();
	else
	    motor_ttl = 10;
	return 1;
      case 'V':
	serial_recv_bool (motor_pos_asserv);
	serial_send_ok ();
	return 1;
      case 's':
	motor_g_vdes = 0;
	motor_d_vdes = 0;
	serial_send_ok ();
	return 1;
      case 'a':
	serial_recv_int (motor_a);
	motor_a_cpt = motor_a;
	serial_send_ok ();
	return 1;
      case 'p':
	serial_recv_int (motor_kp);
	serial_send_ok ();
	return 1;
      case 'i':
	serial_recv_int (motor_ki);
	serial_send_ok ();
	return 1;
      case 'd':
	serial_recv_int (motor_kd);
	serial_send_ok ();
	return 1;
      case 'm':
	serial_recv_int (motor_stat_delay);
	motor_stat_delay_cpt = motor_stat_delay;
	serial_send_ok ();
	return 1;
      case 'c':
	serial_recv_bool (motor_cpt);
	serial_send_ok ();
	return 1;
      case 'g':
	serial_recv_bool (motor_asservi);
	motor_toggle_asservi ();
	serial_send_ok ();
	return 1;
      case 'h':
	serial_recv_int (motor_gpi_delay);
	motor_gpi_delay_cpt = motor_gpi_delay;
	serial_send_ok ();
	return 1;
      case 'k':
	serial_recv_int (temp);
	output_b (temp);
	serial_send_ok ();
	return 1;
      case 'D':
	serial_recv_int (temp);
	if (temp == 0x42)
	    enable_interrupts (INT_EXT);
	serial_send_ok ();
	return 1;
      default:
	return 0;
      }
}

/* D�marre l'asservissement. */
void
motor_toggle_asservi (void)
{
    if (motor_asservi)
      {
	motor_g_cpt_av = 0;
	motor_g_cpt_ar = 0;
	motor_d_cpt_av = 0;
	motor_d_cpt_ar = 0;
	motor_timer0_old = get_timer0 ();
	motor_timer3_old = get_timer3 ();
	motor_g_vacc = 0;
	motor_d_vdes = 0;
	motor_g_vdes = 0;
	motor_d_vdes = 0;
	motor_g_int = 0;
	motor_d_int = 0;
      }
}