/* 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]; /** 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', 1): sniff_rvb_config (args[0]); 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; /* color mode game */ case c ('c', 0 ): proto_send1b('c', others_selectcoul()); break; /* Communication with the servo motor */ case c ('m', 2): servo_motor_set_pos (args[0], args[1]); break; // /* contact */ // case c ('TTTTTTTTTTt', 0 ): // proto_send1b('t', others_contact()); // break; // // /* jack */ // case c ('j', 0 ): // proto_send1b('j', others_jack()); // 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 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 (); // XXX // sniff_rvb_set_threshold (RVB_SNIFF_RED, -100, -85, 725, 0); // sniff_rvb_set_threshold (RVB_SNIFF_BLUE, 650, -140, -1050, -950); /* 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)); } /* Update all the asserv pin comunication */ sensor_rvb_update_asserv_pins (); /* Update RVB sensors data. */ sensor_rvb_start_capture (); /* gestion du barilet */ sequenceur_barillet(); /* Get data for serial port */ while (uart0_poll ()) proto_accept (uart0_getc ()); } }