summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/trap.c
blob: b359b8af501b9830fc488c6955e0570af82bd363 (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
/* trap.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 "trap.h"

/**
 * @todo
 *   - how to manage traps collision for the 3 and 4.
 *   - who map servo/pin to trap?
 */

/**
 * @defgroup TrapPrivate Trap module private variables and functions
 * declarations
 * @{
 */

/**
 * Possible positions of a trap.
 * It is used for the index of the table @see trap_high_time_pos.
 */
typedef enum trap_position_e
{
    /** Horizontal. */
    horizontal = 0,
    /** Vertical. */
    vertical,
    
    /** Length of the enum, always left it at the end. */
    lenght
} trap_position_e;

/**
 * Trap high time values for the different positions (horizontal and vertical).
 * In this two dimensions table, the first index represent horizontal or
 * vertical values.
 */
uint8_t trap_high_time_pos[lenght][SERVO_NUMBER];

/** @} */

/* Trap module initialization. */
void
trap_init (void)
{
    /* Initialize servo module */
    servo_init ();
}

/* Configure traps to open a path to a box. */
void
trap_setup_path_to_box (trap_box_id_e box)
{
    switch (box)
    {
    case garbage:
      /* 0:H, 1:H, 2:H, 3:V, 4:? */
      servo_set_high_time (0, trap_high_time_pos[horizontal][0]);
      servo_set_high_time (1, trap_high_time_pos[horizontal][1]);
      servo_set_high_time (2, trap_high_time_pos[horizontal][2]);
      servo_set_high_time (3, trap_high_time_pos[vertical][3]);
      break;
    case out_left_box:
      /* 0:V, 1:H, 2:H, 3:V, 4:? */
      servo_set_high_time (0, trap_high_time_pos[vertical][0]);
      servo_set_high_time (1, trap_high_time_pos[horizontal][1]);
      servo_set_high_time (2, trap_high_time_pos[horizontal][2]);
      servo_set_high_time (3, trap_high_time_pos[vertical][3]);
      break;
    case middle_left_box:
      /* 0:?, 1:V, 2:H, 3:V, 4:? */
      servo_set_high_time (1, trap_high_time_pos[vertical][1]);
      servo_set_high_time (2, trap_high_time_pos[horizontal][2]);
      servo_set_high_time (3, trap_high_time_pos[vertical][3]);
      break;
    case middle_box:
      /* 0:?, 1:?, 2:V, 3:V, 4:? */
      servo_set_high_time (2, trap_high_time_pos[vertical][2]);
      servo_set_high_time (3, trap_high_time_pos[vertical][3]);
      break;
    case middle_right_box:
      /* 0:?, 1:?, 2:V, 3:H, 4:V */
      servo_set_high_time (2, trap_high_time_pos[vertical][2]);
      servo_set_high_time (3, trap_high_time_pos[horizontal][3]);
      servo_set_high_time (4, trap_high_time_pos[vertical][4]);
      break;
    case out_right_box:
      /* 0:?, 1:?, 2:V, 3:H, 4:H */
      servo_set_high_time (2, trap_high_time_pos[vertical][2]);
      servo_set_high_time (3, trap_high_time_pos[horizontal][3]);
      servo_set_high_time (4, trap_high_time_pos[horizontal][4]);
      break;
    default:
      return;
    }
}

/* Set high time value for horizontal and vertical position of a trap. */
void
trap_set_high_time (uint8_t servo_id, uint8_t horizontal, uint8_t vertical)
{
    if (servo_id < SERVO_NUMBER)
      {
	trap_high_time_pos[horizontal][servo_id] = horizontal;
	trap_high_time_pos[vertical][servo_id] = vertical;
      }
}

/* Open the rear pannel to eject the balls. */
void
trap_open_rear_panel (void)
{
    servo_set_high_time (5, trap_high_time_pos[horizontal][5]);
}

/* Close the rear pannel to eject the balls. */
void
trap_close_rear_panel (void)
{
    servo_set_high_time (5, trap_high_time_pos[vertical][5]);
}