summaryrefslogtreecommitdiff
path: root/n/es-2006/src/main.c
blob: 5d79a4f692016bacb6e0efa20d195e1c37c548dd (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
/* main.c */
/* es - Input/Output general purpose board. {{{
 *
 * Copyright (C) 2006 Dufour J�r�my
 *
 * Robot APB Team/Efrei 2004.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: 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 "common.h"
#include "io.h"
#include "modules/uart/uart.h"
#include "modules/proto/proto.h"
#include "modules/utils/utils.h"
#include "modules/utils/byte.h"

#include "timer_main.h"	/* main timer */
#include "sensor_rvb.h" /* RVB sensors management */
#include "sniff_rvb.h"	/* RVB sensors analysis */
#include "timer_1.h"	/* timer/counter 1 */
#include "others.h"	/* define game color mode + jack + frontal sensor */
#include "barillet.h"
#include "servo_motor.h"


/* Statistics for RVB sensors */
uint8_t sensor_rvb_stat_enable[RVB_MAX_SENSOR], sensor_rvb_stats[RVB_MAX_SENSOR];
/* Statistics for RVB analysis. */
uint8_t sniff_rvb_stat_enable[RVB_MAX_SENSOR], sniff_rvb_stats[RVB_MAX_SENSOR];

/* Statistics for RVB ball sensors. */
uint8_t sniff_ball_stat_enable[2], sniff_ball_stats[2];

/** Call when we receive some data from proto (uart) */
void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
    /* This macro combine command and size in one integer. */
#define c(cmd, size) (cmd << 8 | size)

    uint8_t compt;
    uint16_t temp_16b;

    switch (c (cmd, size))
      {
	/* Reset */
      case c ('z', 0):
	utils_reset ();
	break;

	/*** RVB ***/
	/* RVB sensors configuration */
      case c ('p', 3):
	temp_16b = v8_to_v16 (args[1], args[2]);
	sensor_rvb_config (args[0], temp_16b);
	break;
	/* RVB sniff configuration */
      case c ('x', 5):
	sniff_rvb_config (args[0], v8_to_v16 (args[1], args[2]),
			  v8_to_v16 (args[3], args[4]));
	break;
	/* RVB sensors stats */
      case c ('S', 3):
	temp_16b = v8_to_v16 (args[0], args[1]);
	for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
	    if (temp_16b & _BV(compt))
		sensor_rvb_stat_enable[compt] = sensor_rvb_stats[compt] =
		    args[2];
	break;
	/* Set current color as reference */
      case c ('r', 3):
	temp_16b = v8_to_v16 (args[0], args[1]);
	for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
	    if (temp_16b & _BV(compt))
		sniff_rvb_set_ref_color (compt, args[2]);
	break;
	/* Print stats if the sensor is seen */
      case c ('A', 3):
	temp_16b = v8_to_v16 (args[0], args[1]);
	for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
	    if (temp_16b & _BV(compt))
		sniff_rvb_stat_enable[compt] = sniff_rvb_stats[compt] =
		    args[2];
	break;
	/* Print stats for ball */
      case c ('B', 2):
	for (compt = 0; compt < 2; compt++)
	    if (args[0] & _BV(compt))
		sniff_ball_stats[compt] = sniff_ball_stat_enable[compt] = args[1];
	break;
      case c ('u', 1):
	sensor_rvb_upper_sensors (args[0]);
	break;
	/* contact */
      case c ('i', 0 ):
	proto_send1b ('i', others_contact());
	break;
	/* jack */
      case c ('j', 0 ):
	proto_send1b ('j', others_jack());
	break;
	/* color mode game */
      case c ('k', 0 ):
	proto_send1b ('k', others_selectcoul());
	break;
	/* Communication with the servo motor */
      case c ('m', 2):
	servo_motor_set_pos (args[0], args[1]);
	break;

	
/* tests primaires pour le barillet */
      case c('v',3):
	temp_16b = v8_to_v16 (args[1], args[2]);
	vitesse_turbine( args[0], temp_16b);
	break;

      case c('w',1):
	sens(args[0]);
	break;
	
/* tests plus complexes de la gestion du barillet */

	/* bon on commence - le jack est parti */
      case c ('g', 0):
	init_2_barillet();	// le nom est mal choisi, mais cette fonction sera gerer autrement quand le main sera fini
	break;

	/* deposer une balle du barillet */
      case c ('d', 0):
	depot_balle();
	break;
	
	/* extraire une balle */
      case c ('e', 0):
	extraction();
	break;

	/* rotation du barillet */
      case c ('t', 1):
	rotation(args[0]);
	break;
	
	/* Unknown commands */
      default:
	proto_send0 ('?');
	return;
      }
    /* When no error acknoledge. */
    proto_send (cmd, size, args);
#undef c
}

int
main (void)
{
    uint8_t init_rvb = 200;
    uint8_t compt;

    /* Serial port */
    uart0_init ();
    /* Main timer init */
    timer_main_init ();
    /* Init timer/counter 1 for RVB & PWM
     * /!\ Must be called before RVB/PWM init ! */
    timer1_init ();
    /* Init RVB sensors system */
    sensor_rvb_init ();
    /* Init Color button init  */
    others_init();
    /* barillet init */
    barillet_init();
    /* Servo motor init */
    servo_motor_init ();
    /* Enable interrupts */
    sei ();

    /* We are ready ! */
    proto_send0 ('z');

    while (1)
      {
	/* Wait 4.44 ms */
	// FIXME Try to put this down and make some test before if it
	// possible.
	timer_main_wait ();

	/* RVB Sensors stats */
	for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
	    if (sensor_rvb_stat_enable[compt] && !--sensor_rvb_stats[compt])
	      {
		/* Re init stats system for this sensor */
		sensor_rvb_stats[compt] = sensor_rvb_stat_enable[compt];
		// FIXME useless 16 bit value.
		proto_send5w ('S', compt, sensor_rvb_values[compt][0],
			      sensor_rvb_values[compt][1],
			      sensor_rvb_values[compt][2],
			      sensor_rvb_values[compt][3]);
	      }

	/* RVB Sensors analysis stats */
	for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
	    if (sniff_rvb_stat_enable[compt] && !--sniff_rvb_stats[compt])
	      {
		/* Re init stats system for this sensor */
		sniff_rvb_stats[compt] = sniff_rvb_stat_enable[compt];
		proto_send2b ('A', compt, sniff_rvb_analysis_color (compt, RVB_SNIFF_ALL_COLORS));
	      }

	/* Print stats for ball */
	for (compt = 0; compt < 2; compt++)
	    if (sniff_ball_stat_enable[compt] && !--sniff_ball_stats[compt])
	      {
		/* Re init stats system for this sensor */
		sniff_ball_stats[compt] = sniff_ball_stat_enable[compt];
		proto_send2b ('B', compt, sniff_rvb_analysis_ball (compt + 7));
	      }

	/* Update all the asserv pin comunication */
	sensor_rvb_update_asserv_pins ();

	// FIXME, if we can put some of this values into the eeprom, it will
	// be better...
	if (init_rvb > 0)
	  {
	    init_rvb--;
	    if ((init_rvb == 1) || (init_rvb == 2))
	      {
		sniff_rvb_set_ref_color (0, 0);
		sniff_rvb_set_ref_color (1, 0);
		sniff_rvb_set_ref_color (2, 0);
		sniff_rvb_set_ref_color (3, 0);
	      }
	  }

	/* gestion du barilet */
	sequenceur_barillet ();

	/* Get data for serial port */
	while (uart0_poll ())
	    proto_accept (uart0_getc ());

	/* Update RVB sensors data.
	 * I want this to be done as later as possible, and espceically after
	 * commands has been parsed ! */
	sensor_rvb_start_capture ();
      }
}