summaryrefslogtreecommitdiff
path: root/n/asserv/src/taz.c
blob: 533f6341bbfcc66b666e23f5eca9ad57ec8ce21e (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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/* taz.c - Automate pour Taz, robot 2005. */
/* asserv - Position & speed motor control on a ATmega128. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * 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.
 *
 * Contact :
 *        Web: http://perso.efrei.fr/~schodet/
 *      Email: <contact@ni.fr.eu.org>
 * }}} */

/** FSM state & substate. */
uint8_t taz_state;
uint8_t taz_substate;
/** FSM tempo. */
uint16_t taz_tempo;
#define taz_1s 900
/** FSM max state & substate. */
uint8_t taz_max_state = 0xff;
uint8_t taz_max_substate;

/** Min et max pour les socles. */
uint16_t taz_socles_left_min = 0x220, taz_socles_left_max = 0x270;
uint16_t taz_socles_right_min = 0x290, taz_socles_right_max = 0x2d8;
/** Min pour un amas de quilles. */
uint16_t taz_quilles_min = 0x160;
/** Min pour un socle vu de haut. */
uint16_t taz_socle_min = 0x171;

/** Anti blocage. */
uint8_t taz_bloc = 0;

/** Positions. */
/********************************
 *                              *
 *  !!! LES Y SONT N�GATIFS !!! *
 *                              *
 ********************************/
#define taz_scale (1.0/.05077874252508530693)
#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
static const uint32_t taz_rear_angle = 0x00ff90bf;
static const uint16_t taz_rear_16 = taz_scale * 270 / 2;
static const uint32_t taz_rear_32 = taz_scale * 270 / 2 * 256;
static const uint16_t taz_front_16 = taz_scale * 270 / 2;
static const uint32_t taz_front_32 = taz_scale * 270 / 2 * 256;
static const uint16_t taz_side_16 = taz_scale * 340 / 2;
static const uint32_t taz_side_32 = taz_scale * 340 / 2 * 256;

static const uint16_t taz_start_y_16 = taz_scale * 450 / 2;

static const uint16_t taz_before_bridge_16 = taz_scale * 1200;
static const uint16_t taz_brige_interval_16 = taz_scale * 150;
#define OR2 (1500 + 22 + 600 + 22)
//#define OR2 (1500 + 600 + 22)
static const uint16_t taz_after_bridge_16[4] =
{
    taz_scale * (OR2),
    taz_scale * (OR2 + 150),
    taz_scale * (OR2 + 150),
    taz_scale * (OR2 + 150)
};
static const uint8_t taz_ang[4] =
{
    -19,
    -12,
    10,
    -10
};
static const uint16_t taz_ang_dist_16[4] =
{
    taz_scale * (503),
    taz_scale * (309),
    taz_scale * (309),
    taz_scale * (309)
};

/** Bridge position. */
uint8_t taz_pont;
/** Timer. */
uint32_t taz_timer = 90L * taz_1s;

/* +AutoDec */
/* -AutoDec */

/** Initialise behavior. */
static inline void
taz_init (void)
{
}

/** Mise en place avant le d�part. */
static void
taz_state_0 (void)
{
    switch (taz_substate)
      {
      case 0:
	/* Attend que l'on enfonce le jack. */
	if (!(PINA & _BV (6)))
	    taz_substate = 1;
	break;
      case 1:
	/* Attend que l'on enl�ve le jack. */
	if (PINA & _BV (6))
	  {
	    //taz_substate = 2;
	    taz_substate = 5;
	    /* FTW. */
	    speed_restart ();
	    goto_ftw (-3);
	  }
	break;
      case 2:
	if (goto_finish)
	  {
	    taz_substate = 3;
	    /* Recalage. */
	    speed_restart ();
	    postrack_y = -taz_rear_32;
	    postrack_a = 0x00bf0000;
	    /* On avance juste qu'� l'y de d�part. */
	    goto_linear (taz_start_y_16 - taz_rear_16);
	  }
	break;
      case 3:
	if (goto_finish)
	  {
	    taz_substate = 4;
	    /* Direction l'axe des x. */
	    goto_angular (0);
	  }
	break;
      case 4:
	if (goto_finish)
	  {
	    taz_substate = 5;
	    /* FTW. */
	    goto_ftw (-3);
	  }
	break;
      case 5:
	if (goto_finish)
	  {
	    taz_substate = 6;
	    /* Recalage. */
	    speed_restart ();
	    postrack_x = taz_rear_32;
	    postrack_a = 0x00ff0000;
	    postrack_y = -taz_start_y_16; //
	  }
	break;
      case 6:
	if (!(PINA & _BV (6)))
	  {
	    taz_substate = 7;
	  }
	break;
      case 7:
	if (PINA & _BV (6))
	  {
	    taz_state = 1;
	    taz_substate = 0;
	  }
	break;
      }
}

/** Avanc�e vers le pont. */
static void
taz_state_1 (void)
{
    switch (taz_substate)
      {
      case 0:
	taz_substate = 1;
	/* On avance juste qu'au pont. */
	goto_linear (taz_before_bridge_16 - taz_rear_16);
	break;
      case 1:
	if (goto_finish)
	  {
	    taz_substate = 2;
	    taz_tempo = taz_1s;
	  }
	break;
      case 2:
	taz_substate = 3;
	/* Lance la capture. */
	twi_master_read (0x02, (uint8_t *) motor_sharps, 12);
	break;
      case 3:
	if (twi_master_is_finished ())
	  {
	    proto_send3w ('H', motor_sharps[0], motor_sharps[1],
			  motor_sharps[2]);
	    /* Calcule la valeur du pont. */
	    if (motor_sharps[0] > 0x108 && motor_sharps[1] > 0x108
		&& motor_sharps[2] > 0x108)
		taz_pont = 0;
	    else if (motor_sharps[0] < 0x108 && motor_sharps[1] > 0x108
		     && motor_sharps[2] > 0x108)
		taz_pont = 1;
	    else if (motor_sharps[0] < 0x108 && motor_sharps[1] < 0x108
		     && motor_sharps[2] > 0x108)
		taz_pont = 2;
	    else if (motor_sharps[0] < 0x108 && motor_sharps[1] < 0x108
		     && motor_sharps[2] < 0x108)
		taz_pont = 3;
	    else
	      {
		taz_substate = 2;
		break;
	      }
	    taz_substate = 4;
	    /* Direction l'axe des y. */
	    goto_angular (0xc0);
	  }
	break;
      case 4:
	if (goto_finish)
	  {
	    taz_substate = 5;
	    /* FTW. */
	    goto_ftw (-3);
	  }
	break;
      case 5:
	if (goto_finish)
	  {
	    taz_substate = 6;
	    /* Recalage. */
	    speed_restart ();
	    postrack_y = -taz_rear_32;
	    postrack_a = 0x00be0000;
	  }
	// no break;
      case 6:
	if (goto_finish)
	  {
	    taz_substate = 7;
	    /* On avance juste qu'� l'y de travers�e de pont. */
	    goto_linear (taz_start_y_16
			 + taz_pont * taz_brige_interval_16
			 + (postrack_y >> 8)
			 + (taz_pont < 2 ? 15 * taz_scale : 0));
	  }
	break;
      case 7:
	if (goto_finish)
	  {
	    taz_substate = 8;
	    /* Direction l'axe des x. */
	    goto_angular (0);
	  }
	break;
      case 8:
	if (goto_finish)
	  {
	    taz_substate = 85;
	    taz_tempo = taz_1s;
	  }
	break;
      case 85:
	taz_substate = 9;
	/* Reprend une mesure. */
	twi_master_read (0x02, (uint8_t *) motor_sharps, 12);
	break;
      case 9:
	if (twi_master_is_finished ())
	  {
	    proto_send3w ('H', motor_sharps[0], motor_sharps[1],
			  motor_sharps[2]);
	    /* Calcule la valeur du pont. */
	    if (motor_sharps[0] > 0x108 && motor_sharps[1] > 0x108
		&& motor_sharps[2] > 0x108)
	      {
		taz_substate = 10;
		break;
	      }
	    else if (motor_sharps[0] < 0x108 && motor_sharps[1] > 0x108
		&& motor_sharps[2] > 0x108 && taz_pont < 3)
	      {
		taz_pont++;
	      }
	    else if (motor_sharps[0] < 0x108 && motor_sharps[1] < 0x108
		&& motor_sharps[2] > 0x108 && taz_pont < 2)
	      {
		taz_pont += 2;
	      }
	    else if (motor_sharps[0] < 0x108 && motor_sharps[1] < 0x108
		&& motor_sharps[2] < 0x108 && taz_pont < 1)
	      {
		taz_pont += 3;
	      }
	    else if (motor_sharps[0] > 0x108 && motor_sharps[1] < 0x108
		&& motor_sharps[2] < 0x108 && taz_pont > 1)
	      {
		taz_pont -= 2;
	      }
	    else if (motor_sharps[0] > 0x108 && motor_sharps[1] > 0x108
		&& motor_sharps[2] < 0x108 && taz_pont > 0)
	      {
		taz_pont--;
	      }
	    else
	      {
		taz_pont = 0;
	      }
	    taz_substate = 4;
	    /* Direction l'axe des y. */
	    goto_angular (0xc0);
	  }
	break;
      case 10:
	if (goto_finish)
	  {
	    taz_substate = 11;
	    /* Travers�e du pont. */
	    goto_linear (taz_after_bridge_16[taz_pont] - (postrack_x >> 8));
	  }
	break;
      case 11:
	if (goto_finish)
	  {
	    taz_substate = 12;
	    /* Tourne vers les quilles. */
	    goto_angular (taz_ang[taz_pont]);
	  }
	break;
      case 12:
	if (goto_finish)
	  {
	    taz_substate = 13;
	    /* Avance la petite distance, pour arriver sur la premi�re
	     * quille. */
	    goto_linear (taz_ang_dist_16[taz_pont]);
	  }
	break;
      case 13:
	if (goto_finish)
	  {
	    taz_substate = 14;
	    /* Tourne vers les quilles. */
	    goto_angular (0);
	    /* D�mare les sharps. */
	    twi_master_read (0x02, (uint8_t *) motor_sharps, 12);
	    motor_print_sharps_cpt = 1;
	    motor_print_sharps = 8;
	    motor_print_sharps_no_send = 1;
	  }
	break;
      case 14:
	if (goto_finish)
	  {
	    taz_state = 2;
	    taz_substate = 0;
	  }
	break;
      }
}

/** Trouve un amas de quilles sur un certain angle et sens, retourne 1 si
 * ok, 2 si angle fini. */
static uint8_t
taz_find_quilles (uint8_t a, uint8_t dir)
{
    uint32_t angle_diff;
    /* Compute angle diff. */
    angle_diff = v8_to_v32 (0, goto_a, 0, 0) - postrack_a;
    angle_diff <<= 8;
    angle_diff >>= 8;
    motor_mode = 1;
    /* Small angles. */
    if (0x10000L > angle_diff && angle_diff > -0x10000L)
      {
	speed_left = 0;
	speed_right = 0;
	speed_left_aim = 0;
	speed_right_aim = 0;
	return 2;
      }
    else if (motor_sharps[1] > taz_quilles_min)
      {
	speed_left_aim = 0;
	speed_right_aim = 0;
	return 1;
      }
    else
      {
	if (dir)
	  {
	    speed_left_aim = 2;
	    speed_right_aim = -2;
	  }
	else
	  {
	    speed_left_aim = -2;
	    speed_right_aim = 2;
	  }
	return 0;
      }
}

/** Se tourne vers un socle, retourne 1 si ok. */
static uint8_t
taz_dir_socle (void)
{
    uint8_t left, right;
    left = motor_sharps[3] > taz_socles_left_min
	&& motor_sharps[3] < taz_socles_left_max;
    right = motor_sharps[4] > taz_socles_right_min
	&& motor_sharps[4] < taz_socles_right_max;
    motor_mode = 1;
    if (left == right)
      {
	if (speed_left == 0 && speed_right == 0)
	  {
	    return 1;
	  }
	else
	  {
	    speed_left_aim = 0;
	    speed_right_aim = 0;
	  }
      }
    else
      {
	if (left)
	  {
	    speed_left_aim = -1;
	    speed_right_aim = 1;
	  }
	else
	  {
	    speed_left_aim = 1;
	    speed_right_aim = -1;
	  }
      }
    return 0;
}

/** Test si socles. */
static uint8_t
taz_test_socles (void)
{
    return (motor_sharps[3] > taz_socles_left_min
	    && motor_sharps[3] < taz_socles_left_max)
	|| (motor_sharps[4] > taz_socles_right_min
	    && motor_sharps[4] < taz_socles_right_max);
}

/** O� sont les quilles !. */
static void
taz_state_2 (void)
{
    uint8_t r;
    switch (taz_substate)
      {
      case 0:
	taz_substate = 1;
	/* Tout droit. */
	goto_linear (600 * taz_scale);
	break;
      case 1:
	if (goto_finish)
	    taz_substate = 3;
	else if (taz_test_socles ())
	    taz_substate = 2;
	break;
      case 2:
	if (taz_dir_socle ())
	  {
	    taz_substate = 1;
	    motor_mode = 2;
	  }
	break;
      case 3:
	/* Sur un socle ? */
	proto_send1w ('H', motor_sharps[5]);
	if (motor_sharps[5] > taz_socle_min)
	    taz_substate = 4;
	else
	    taz_substate = 5;
	break;
      case 4:
	taz_substate = 9;
	/* Tour simple sur place. */
	goto_angular (0xc0);
	break;
      case 5:
	taz_substate++;
	/* Sp�cial move sans socle. */
	goto_linear (150 * taz_scale);
	break;
      case 6:
	if (goto_finish)
	  {
	    taz_substate++;
	    goto_angular (0xe0);
	  }
	break;
      case 7:
	if (goto_finish)
	  {
	    taz_substate++;
	    goto_linear (-150 * M_SQRT2 * taz_scale);
	  }
	break;
      case 8:
	if (goto_finish)
	  {
	    taz_substate = 9;
	    goto_angular (0xc0);
	  }
	break;
      case 9:
	if (goto_finish)
	  {
	    taz_substate = 10;
	    /* Tout droit sur la rang�e du fond. */
	    goto_linear ((1500 - 150) * taz_scale + (postrack_y >> 8));
	  }
	break;
      case 10:
	if (goto_finish)
	    taz_substate = 20;
	else if (taz_test_socles ())
	    taz_substate = 11;
	break;
      case 11:
	if (taz_dir_socle ())
	  {
	    taz_substate = 10;
	    motor_mode = 2;
	  }
	break;

      case 20:
	/* Direction en point de charge !!! */
	if (motor_sharps[5] > taz_socle_min)
	  {
	    taz_substate = 21;
	    goto_angular (115);
	  }
	else
	  {
	    taz_substate = 22;
	    goto_linear (150 * taz_scale);
	  }
	break;
      case 21:
	if (goto_finish)
	  {
	    taz_substate = 24;
	    goto_linear (474 * taz_scale);
	  }
	break;
      case 22:
	if (goto_finish)
	  {
	    taz_substate = 23;
	    goto_angular (0x80);
	  }
	break;
      case 23:
	if (goto_finish)
	  {
	    taz_substate = 24;
	    goto_linear (450 * taz_scale);
	  }
	break;
      case 24:
	if (goto_finish)
	  {
	    taz_substate = 25;
	    goto_angular (0x40);
	  }
	break;
      case 25:
	if (goto_finish)
	  {
	    taz_substate = 26;
	    goto_linear (1200 * taz_scale);
	  }
	break;
      case 26:
	if (goto_finish)
	    taz_substate = 28;
	else if (taz_test_socles ())
	    taz_substate = 27;
	break;
      case 27:
	if (taz_dir_socle ())
	  {
	    taz_substate = 28;
	    goto_linear (450 * taz_scale);
	  }
	break;
      case 28:
	if (goto_finish)
	  {
	    taz_substate = 29;
	    goto_angular (0x60);
	  }
	break;
      case 29:
	if (goto_finish)
	  {
	    uint8_t a = v32_to_v8 (postrack_a, 2) + 0xf0;
	    r = taz_find_quilles (a, 0);
	    if (r == 1)
	      {
		taz_substate = 26;
		goto_linear (600 * taz_scale);
	      }
	  }
	break;
      }
}

/** . */
static void
taz_state_3 (void)
{
    switch (taz_substate)
      {
      case 0:
	break;
      }
}

/** . */
static void
taz_state_4 (void)
{
    switch (taz_substate)
      {
      case 0:
	break;
      }
}

/** . */
static void
taz_state_5 (void)
{
    switch (taz_substate)
      {
      case 0:
	break;
      }
}

/** FSM. */
void
taz_update (void)
{
    uint8_t old_state, old_substate;
    old_state = taz_state;
    old_substate = taz_substate;
    /* Game timer. */
    if (taz_state)
      {
	taz_timer--;
	if (!taz_timer)
	  {
	    taz_state = 0xff;
	    taz_substate = 0xff;
	    speed_restart ();
	    motor_mode = 0;
	    pwm_left = 0;
	    pwm_right = 0;
	    proto_send2b ('t', 0xff, 0xff);
	  }
      }
    /* Blocages. */
    if (taz_bloc)
      {
	if (!goto_finish)
	  {
	    return;
	  }
	proto_send1b ('b', 0);
	taz_bloc = 0;
      }
    else if ((speed_left_e_old == speed_e_sat || speed_left_e_old == -speed_e_sat)
	     && (speed_right_e_old == speed_e_sat || speed_right_e_old == -speed_e_sat))
      {
	speed_restart ();
	goto_counter (-70 * taz_scale, -100 * taz_scale);
	taz_bloc = 1;
	proto_send1b ('b', 1);
	return;
      }
    /* Max state. */
    if (taz_state > taz_max_state
	|| (taz_state == taz_max_state && taz_substate > taz_max_substate))
	return;
    /* Pause timer. */
    if (taz_tempo)
      {
	taz_tempo--;
	return;
      }
    switch (taz_state)
      {
      case 0:
	taz_state_0 ();
	break;
      case 1:
	taz_state_1 ();
	break;
      case 2:
	speed_max_l = 5;
	taz_state_2 ();
	break;
      case 3:
	taz_state_3 ();
	break;
      case 4:
	taz_state_4 ();
	break;
      case 5:
	taz_state_5 ();
	break;
      }
    if (old_state != taz_state || old_substate != taz_substate)
	proto_send2b ('t', taz_state, taz_substate);
}