summaryrefslogtreecommitdiffhomepage
path: root/digital/io-hub/src/robospierre/main.c
diff options
context:
space:
mode:
authorNicolas Schodet2011-05-05 23:11:38 +0200
committerNicolas Schodet2011-05-05 23:11:38 +0200
commitff7505c0a9d6b5332db32ae9b23326bc466f0b01 (patch)
tree59cffcb5f2a39defd342aa5afe556499c356f301 /digital/io-hub/src/robospierre/main.c
parentd32d259cbd5fcb8994d9f555a74a3fc2640d8ebe (diff)
digital/io-hub: add base robospierre io-hub program
Diffstat (limited to 'digital/io-hub/src/robospierre/main.c')
-rw-r--r--digital/io-hub/src/robospierre/main.c142
1 files changed, 142 insertions, 0 deletions
diff --git a/digital/io-hub/src/robospierre/main.c b/digital/io-hub/src/robospierre/main.c
new file mode 100644
index 00000000..5a987240
--- /dev/null
+++ b/digital/io-hub/src/robospierre/main.c
@@ -0,0 +1,142 @@
+/* main.c */
+/* robospierre - Eurobot 2011 AI. {{{
+ *
+ * Copyright (C) 2011 Nicolas Schodet
+ *
+ * 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 "modules/utils/utils.h"
+#include "modules/uart/uart.h"
+#include "modules/proto/proto.h"
+
+#include "timer.h"
+#include "chrono.h"
+#include "simu.host.h"
+
+#include "asserv.h"
+#include "mimot.h"
+#include "twi_master.h"
+
+#include "io.h"
+
+/** Our color. */
+enum team_color_e team_color;
+
+/** Asserv stats counters. */
+static uint8_t main_stats_asserv_, main_stats_asserv_cpt_;
+
+/** Main initialisation. */
+static void
+main_init (void)
+{
+ /* Serial port */
+ uart0_init ();
+ /* Enable interrupts */
+ sei ();
+ /* Main timer */
+ timer_init ();
+ /* TWI communications */
+ asserv_init ();
+ mimot_init ();
+ twi_master_init ();
+ /* Initialization done. */
+ proto_send0 ('z');
+}
+
+/** Main events management. */
+void
+main_event_to_fsm (void)
+{
+}
+
+/** Main (and infinite) loop. */
+static void
+main_loop (void)
+{
+ while (1)
+ {
+ /* Wait until next cycle. */
+ timer_wait ();
+ /* Update chrono. */
+ chrono_update ();
+ /* Is match over? */
+ if (chrono_is_match_over ())
+ {
+ /* End it and block here indefinitely. */
+ chrono_end_match (42);
+ return;
+ }
+ /* Handle commands from UART. */
+ while (uart0_poll ())
+ proto_accept (uart0_getc ());
+ /* Only manage events if slaves are synchronised. */
+ if (twi_master_sync ())
+ main_event_to_fsm ();
+ /* Send stats if requested. */
+ if (main_stats_asserv_ && !--main_stats_asserv_cpt_)
+ {
+ /* Get current position */
+ position_t cur_pos;
+ asserv_get_position (&cur_pos);
+ /* Send stats */
+ proto_send3w ('A', cur_pos.v.x, cur_pos.v.y, cur_pos.a);
+ /* Reset stats counter */
+ main_stats_asserv_cpt_ = main_stats_asserv_;
+ }
+ }
+}
+
+/** Handle received commands. */
+void
+proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
+{
+#define c(cmd, size) (cmd << 8 | size)
+ switch (c (cmd, size))
+ {
+ case c ('z', 0):
+ /* Reset */
+ utils_reset ();
+ break;
+ /* Stats commands.
+ * - b: interval between stats. */
+ case c ('A', 1):
+ /* Position stats. */
+ main_stats_asserv_ = main_stats_asserv_cpt_ = args[0];
+ break;
+ default:
+ /* Unknown commands */
+ proto_send0 ('?');
+ return;
+ }
+ /* When no error, acknowledge commands */
+ proto_send (cmd, size, args);
+#undef c
+}
+
+int
+main (int argc, char **argv)
+{
+ avr_init (argc, argv);
+ main_init ();
+ main_loop ();
+ return 0;
+}