From 9d3573134462335051e46b990e97e1d32bef4b29 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 31 Mar 2008 13:03:47 +0200 Subject: * digital/io/src: - adapted FSM to AVR program. --- digital/io/src/Makefile | 42 +++++++++++- digital/io/src/Makefile.getsamples | 21 ------ digital/io/src/Makefile.gutter | 21 ------ digital/io/src/fsm.c | 60 ++++++++++++++++ digital/io/src/fsm.conf | 8 +++ digital/io/src/fsm.h | 91 +++++++++++++++++++++++++ digital/io/src/fsm_templates/template_cb.h | 10 +++ digital/io/src/fsm_templates/template_cb_decl.h | 6 ++ digital/io/src/fsm_templates/template_cb_impl.c | 9 +++ digital/io/src/fsm_templates/template_cb_skel.c | 11 +++ digital/io/src/fsm_templates/template_fsm.c | 25 +++++++ digital/io/src/fsm_templates/template_fsm.h | 52 ++++++++++++++ digital/io/src/getSamplesFsm.conf | 5 -- digital/io/src/getSamplesFsm.fsm | 47 ------------- digital/io/src/getsamples.c | 44 ++++++++++++ digital/io/src/getsamples.fsm | 47 +++++++++++++ digital/io/src/getsamples.h | 47 +++++++++++++ digital/io/src/getsamples_cb.c | 75 ++++++++++++-------- digital/io/src/getsamples_robo.h | 42 ------------ digital/io/src/gutter.conf | 5 -- digital/io/src/gutter.fsm | 4 +- digital/io/src/gutter_cb.c | 52 ++++++++++---- digital/io/src/gutter_robo.h | 35 ---------- digital/io/src/test/gutter/Makefile | 17 +++-- digital/io/src/test/gutter/main.c | 26 ++++--- digital/io/src/test/testgetsamples/Makefile | 17 +++-- digital/io/src/test/testgetsamples/avrconfig.h | 64 +---------------- digital/io/src/test/testgetsamples/main.c | 48 ++++++------- 28 files changed, 591 insertions(+), 340 deletions(-) delete mode 100644 digital/io/src/Makefile.getsamples delete mode 100644 digital/io/src/Makefile.gutter create mode 100644 digital/io/src/fsm.c create mode 100644 digital/io/src/fsm.conf create mode 100644 digital/io/src/fsm.h create mode 100644 digital/io/src/fsm_templates/template_cb.h create mode 100644 digital/io/src/fsm_templates/template_cb_decl.h create mode 100644 digital/io/src/fsm_templates/template_cb_impl.c create mode 100644 digital/io/src/fsm_templates/template_cb_skel.c create mode 100644 digital/io/src/fsm_templates/template_fsm.c create mode 100644 digital/io/src/fsm_templates/template_fsm.h delete mode 100644 digital/io/src/getSamplesFsm.conf delete mode 100644 digital/io/src/getSamplesFsm.fsm create mode 100644 digital/io/src/getsamples.c create mode 100644 digital/io/src/getsamples.fsm create mode 100644 digital/io/src/getsamples.h delete mode 100644 digital/io/src/getsamples_robo.h delete mode 100644 digital/io/src/gutter.conf delete mode 100644 digital/io/src/gutter_robo.h (limited to 'digital/io') diff --git a/digital/io/src/Makefile b/digital/io/src/Makefile index 1ea16d6f..35a58a6a 100644 --- a/digital/io/src/Makefile +++ b/digital/io/src/Makefile @@ -1,6 +1,8 @@ BASE = ../../avr AVR_PROGS = io -io_SOURCES = main.c asserv.c servo.c eeprom.c trap.c sharp.c +io_SOURCES = main.c asserv.c servo.c eeprom.c trap.c sharp.c \ + fsm.c getsamples.c getsamples_fsm.c getsamples_cb.c \ + gutter_fsm.c gutter_cb.c MODULES = proto uart twi utils adc CONFIGFILE = avrconfig.h # atmega8, atmega8535, atmega128... @@ -10,3 +12,41 @@ AVR_MCU = atmega128 OPTIMIZE = -O2 include $(BASE)/make/Makefile.gen + +# FSM build: +# +# The %_cb.c file can not be auto-generated. When the %_cb_skel.c is changed, +# user should check if they still are synchronised using a merge program, like +# vimdiff. + +# If templates are modified, files should be rebuilt. +FSM_TEMPLATES := template_cb.h template_cb_decl.h template_cb_impl.c \ + template_cb_skel.c template_fsm.c template_fsm.h +FSM_TEMPLATES := $(FSM_TEMPLATES:%=fsm_templates/%) + +# Theses files are auto-generated, remove them on clean. +EXTRA_CLEAN_FILES = getsamples_fsm.h getsamples_fsm.c getsamples_cb.h \ + getsamples_cb_skel.c getsamples.png \ + gutter_fsm.h gutter_fsm.c gutter_cb.h \ + gutter_cb_skel.c gutter.png + +# Bootstrap, fsm should be generated before fsm.h is used. +fsm.c: fsm.h +fsm.h: getsamples_fsm.h gutter_fsm.h + +DFAGEN = python $(BASE)/../../tools/dfagen/dfagen.py + +%_fsm.h %_fsm.c %_cb.h %_cb_skel.c: %.fsm fsm.conf $(FSM_TEMPLATES) + $(DFAGEN) -o c -d $< -c fsm.conf -p $(<:%.fsm=%) + +%_cb.c: %_cb_skel.c + echo "Please update $@" + false + +png: getsamples.png gutter.png + +%.png: %.dot + dot -Tpng -o $@ $< + +%.dot: %.fsm + $(DFAGEN) -o dot -d $< -p $(<:%.fsm=%) diff --git a/digital/io/src/Makefile.getsamples b/digital/io/src/Makefile.getsamples deleted file mode 100644 index 9e3287f4..00000000 --- a/digital/io/src/Makefile.getsamples +++ /dev/null @@ -1,21 +0,0 @@ -CFLAGS = -O2 -Wall - -all: prgm exe dot png - -prgm: getSamplesFsm.fsm getSamplesFsm.conf - python ../../../tools/dfagen/dfagen.py -o c -d $< -c getSamplesFsm.conf -p getsamples - -exe: prgm - -dot: getSamplesFsm.fsm getSamplesFsm.conf - python ../../../tools/dfagen/dfagen.py -o dot -d $< -c getSamplesFsm.conf -p getsamples - -png: getsamples.dot - dot -Tpng -o getsamples.png getsamples.dot - -clean: - rm -f getsamples.h - rm -f getsamples_cb.h - rm -f getsamples.dot - rm -f getsamples.png - rm -f getsamples.c diff --git a/digital/io/src/Makefile.gutter b/digital/io/src/Makefile.gutter deleted file mode 100644 index 146835ed..00000000 --- a/digital/io/src/Makefile.gutter +++ /dev/null @@ -1,21 +0,0 @@ -CFLAGS = -O2 -Wall - -all: prgm exe dot png - -prgm: gutter.fsm gutter.conf - python ../../../tools/dfagen/dfagen.py -o c -d $< -c gutter.conf -p gutter - -exe: prgm - -dot: gutter.fsm gutter.conf - python ../../../tools/dfagen/dfagen.py -o dot -d $< -c gutter.conf -p gutter - -png: gutter.dot - dot -Tpng -o gutter.png gutter.dot - -clean: - rm -f gutter.h - rm -f gutter_cb.h - rm -f gutter.dot - rm -f gutter.png - rm -f gutter.c diff --git a/digital/io/src/fsm.c b/digital/io/src/fsm.c new file mode 100644 index 00000000..c19dd458 --- /dev/null +++ b/digital/io/src/fsm.c @@ -0,0 +1,60 @@ +/* fsm.c - Finite State Machine code. */ +/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ + * + * Copyright (C) 2008 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 "fsm.h" + +/** Reset a FSM. */ +void +fsm_init (fsm_t *fsm) +{ + assert (fsm); + fsm->state_current = fsm->state_init; +} + +/** Handle an event on the given FSM. */ +void +fsm_handle_event (fsm_t *fsm, u8 event) +{ + assert (fsm); + assert (event < fsm->events_nb); + /* Lookup transition. */ + fsm_transition_t tr = fsm->transition_table[ + fsm->state_current * fsm->events_nb + event]; + /* Ignore unhandled events. */ + if (tr) + { + /* Execute transition. */ + fsm_branch_t br = tr (); + /* Change state. */ +#ifdef HOST + assert (((br >> 16) & 0xff) == fsm->state_current); + assert (((br >> 8) & 0xff) == event); + fsm->state_current = br & 0xff; +#else + fsm->state_current = br; +#endif + } +} + diff --git a/digital/io/src/fsm.conf b/digital/io/src/fsm.conf new file mode 100644 index 00000000..6dce1c15 --- /dev/null +++ b/digital/io/src/fsm.conf @@ -0,0 +1,8 @@ +[user] +template-dir = fsm_templates + +[templates] +template_fsm.h = %_fsm.h +template_fsm.c = %_fsm.c +template_cb.h = %_cb.h +template_cb_skel.c = %_cb_skel.c diff --git a/digital/io/src/fsm.h b/digital/io/src/fsm.h new file mode 100644 index 00000000..bfdaf19b --- /dev/null +++ b/digital/io/src/fsm.h @@ -0,0 +1,91 @@ +#ifndef fsm_h +#define fsm_h +/* fsm.h - Finite State Machine code. */ +/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ + * + * Copyright (C) 2008 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. + * + * }}} */ + +/** + * The io board includes several concurrent FSM. The code to handle events is + * generic, but as events are not compatibles (they do not share the same + * identifier), events should be generated for each FSM separately. + * + * The main loop tests for each possible event and generate the corresponding + * FSM events. For example: + * + * if (asserv_move_cmd_status () == success) + * { + * fsm_handle_event (fsm_top, TOP_EVENT_position_reached); + * fsm_handle_event (fsm_getsamples, GETSAMPLES_EVENT_position_reached); + * } + * + * Any unhandled event will be ignored. + * + * To start a sub machine from the top FSM, call the sub machine start + * function (for example getsamples_start), which will set parameters, reset + * its fsm, then trigger its start event. + * + * To abandon a FSM, reset it using fsm_init or it will continue to run + * concurrently. + * + * The sub machine is expected to generate an event for the top machine to + * make it continue. + */ + +#ifdef HOST +/** On host, check branches. */ +typedef u32 fsm_branch_t; +#else +/** On AVR, no check. */ +typedef u8 fsm_branch_t; +#endif + +/** Transition function. */ +typedef fsm_branch_t (*fsm_transition_t) (void); + +/** Context of a FSM or sub-FSM. */ +struct fsm_t +{ + /** Transition table. */ + const fsm_transition_t *transition_table; + /** Number of events, used to index the right transition. */ + u8 events_nb; + /** Initial state. */ + u8 state_init; + /** Current active state. */ + u8 state_current; +}; +typedef struct fsm_t fsm_t; + +/** Reset a FSM. */ +void +fsm_init (fsm_t *fsm); + +/** Handle an event on the given FSM. */ +void +fsm_handle_event (fsm_t *fsm, u8 event); + +#include "getsamples_fsm.h" +#include "gutter_fsm.h" + +#endif /* fsm_h */ diff --git a/digital/io/src/fsm_templates/template_cb.h b/digital/io/src/fsm_templates/template_cb.h new file mode 100644 index 00000000..4f4deeec --- /dev/null +++ b/digital/io/src/fsm_templates/template_cb.h @@ -0,0 +1,10 @@ +#ifndef %(prefix)s_cb_h +#define %(prefix)s_cb_h +/* + * THIS IS AN AUTOMATICALLY GENERATED FILE, DO NOT EDIT! + * + * %(name)s callbacks declaration. + * +%(*comments)s */ + +%(states_template,template_cb_decl.h)s#endif /* %(prefix)s_cb_h */ diff --git a/digital/io/src/fsm_templates/template_cb_decl.h b/digital/io/src/fsm_templates/template_cb_decl.h new file mode 100644 index 00000000..fd72d678 --- /dev/null +++ b/digital/io/src/fsm_templates/template_cb_decl.h @@ -0,0 +1,6 @@ +/* + * %(state)s =%(event)s=> +%(*branches_to)s */ +fsm_branch_t +%(prefix)s__%(state)s__%(event)s (void); + diff --git a/digital/io/src/fsm_templates/template_cb_impl.c b/digital/io/src/fsm_templates/template_cb_impl.c new file mode 100644 index 00000000..91ff4fbb --- /dev/null +++ b/digital/io/src/fsm_templates/template_cb_impl.c @@ -0,0 +1,9 @@ +/* + * %(state)s =%(event)s=> +%(*branches_to)s */ +fsm_branch_t +%(prefix)s__%(state)s__%(event)s (void) +{ +%(returns)s +} + diff --git a/digital/io/src/fsm_templates/template_cb_skel.c b/digital/io/src/fsm_templates/template_cb_skel.c new file mode 100644 index 00000000..bf0a0639 --- /dev/null +++ b/digital/io/src/fsm_templates/template_cb_skel.c @@ -0,0 +1,11 @@ +/* + * THIS IS AN AUTOMATICALLY GENERATED FILE, DO NOT EDIT! + * + * Skeleton for %(name)s callbacks implementation. + * +%(*comments)s */ +#include "common.h" +#include "fsm.h" +#include "%(prefix)s_cb.h" + +%(states_template,template_cb_impl.c)s diff --git a/digital/io/src/fsm_templates/template_fsm.c b/digital/io/src/fsm_templates/template_fsm.c new file mode 100644 index 00000000..87e52f33 --- /dev/null +++ b/digital/io/src/fsm_templates/template_fsm.c @@ -0,0 +1,25 @@ +/* + * THIS IS AN AUTOMATICALLY GENERATED FILE, DO NOT EDIT! + * + * %(name)s + * +%(*comments)s */ +#include "common.h" +#include "fsm.h" +#include "%(prefix)s_cb.h" + +#define NULL ((void *)0L) + +/* %(name)s transition table. */ +static const fsm_transition_t +%(prefix)s_transition_table[%(PREFIX)s_STATE_NB][%(PREFIX)s_EVENT_NB] = { +%(transition_table)s}; + +/* %(name)s context. */ +fsm_t %(prefix)s_fsm = { + &%(prefix)s_transition_table[0][0], + %(PREFIX)s_EVENT_NB, + %(PREFIX)s_STATE_%(initial)s, + %(PREFIX)s_STATE_%(initial)s, +}; + diff --git a/digital/io/src/fsm_templates/template_fsm.h b/digital/io/src/fsm_templates/template_fsm.h new file mode 100644 index 00000000..9ac98895 --- /dev/null +++ b/digital/io/src/fsm_templates/template_fsm.h @@ -0,0 +1,52 @@ +#ifndef %(prefix)s_fsm_h +#define %(prefix)s_fsm_h +/* + * THIS IS AN AUTOMATICALLY GENERATED FILE, DO NOT EDIT! + * + * %(name)s + * +%(*comments)s */ + +/* %(name)s states. */ +enum %(prefix)s_state_t +{ +%(states)s %(PREFIX)s_STATE_NB +}; + +/* %(name)s events. */ +enum %(prefix)s_event_t +{ +%(events)s %(PREFIX)s_EVENT_NB +}; + +/* This macro enables checks for branches used in the wrong state/event + * combination. */ +#ifdef HOST +# define _BRANCH(state, event, to) \ + ((%(PREFIX)s_STATE_ ## state) << 16 \ + | (%(PREFIX)s_EVENT_ ## event) << 8 \ + | (%(PREFIX)s_STATE_ ## to)) +#else +# define _BRANCH(state, event, to) \ + ((%(PREFIX)s_STATE_ ## to)) +#endif + +/* %(name)s branches. */ +enum %(prefix)s_branch_t +{ +%(branches)s}; + +#undef _BRANCH + +/* Value to return to follow the only branch. */ +#define %(prefix)s_next(state, event) \ + %(PREFIX)s_BRANCH__ ## state ## __ ## event ## __ + +/* Value to return to follow a given branch. */ +#define %(prefix)s_next_branch(state, event, branch) \ + %(PREFIX)s_BRANCH__ ## state ## __ ## event ## __ ## branch + +/* %(name)s context. */ +extern fsm_t %(prefix)s_fsm; + +#endif /* %(prefix)s_fsm_h */ diff --git a/digital/io/src/getSamplesFsm.conf b/digital/io/src/getSamplesFsm.conf deleted file mode 100644 index 238c0f6f..00000000 --- a/digital/io/src/getSamplesFsm.conf +++ /dev/null @@ -1,5 +0,0 @@ -[user] -type = getsamples_t -type-forward-decl = typedef struct getsamples_t getsamples_t; -type-decl = #include "getsamples_robo.h" -field = fsm diff --git a/digital/io/src/getSamplesFsm.fsm b/digital/io/src/getSamplesFsm.fsm deleted file mode 100644 index 3f7266d0..00000000 --- a/digital/io/src/getSamplesFsm.fsm +++ /dev/null @@ -1,47 +0,0 @@ -#FSM get samples -get_samples - -States: - START - GO_TO_POSITION - PREPARE_ARM - FORWARD_CONTROL - TAKE_SAMPLES - BACKWARD - END - -Events: - ok - position_reached - position_failed - arm_moved - sample_took - classifier_ready - -START: - ok -> GO_TO_POSITION - Go to the distributor. The distributor to reach shall be setted in the fsm structure. - -GO_TO_POSITION: - position_reached -> PREPARE_ARM - Go to the position desired, it is very near the position of the distributor in case it is a ice distributor or sample distributor. - position_failed -> GO_TO_POSITION - Go to another point before trying to go to this one again. - -PREPARE_ARM: - arm_moved -> FORWARD_CONTROL - Prepare the arm to the correct position. - -FORWARD_CONTROL: - position_reached -> TAKE_SAMPLES - End the position to the distributor. - -TAKE_SAMPLES: - sample_took: more -> . - Continue to take samples and classify the next sample. - sample_took: no_more -> BACKWARD - If the quantity of samples are tooked, then go backeward and conitnue classifying the samples. - -BACKWARD: - position_reached -> END - Ending this state machine. diff --git a/digital/io/src/getsamples.c b/digital/io/src/getsamples.c new file mode 100644 index 00000000..73859fa3 --- /dev/null +++ b/digital/io/src/getsamples.c @@ -0,0 +1,44 @@ +/* getsamples.c */ +/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ + * + * Copyright (C) 2008 Nélio Laranjeiro + * + * 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 "getsamples.h" +#include "fsm.h" + +struct getsamples_data_t getsamples_data; + +/** Start a getsamples FSM. */ +void +getsamples_start (uint32_t distributor_x, uint32_t distributor_y, + uint8_t samples) +{ + /* Set parameters. */ + getsamples_data.distributor_x = distributor_x; + getsamples_data.distributor_y = distributor_y; + getsamples_data.samples = samples; + /* Start FSM. */ + fsm_init (&getsamples_fsm); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_ok); +} + diff --git a/digital/io/src/getsamples.fsm b/digital/io/src/getsamples.fsm new file mode 100644 index 00000000..3f7266d0 --- /dev/null +++ b/digital/io/src/getsamples.fsm @@ -0,0 +1,47 @@ +#FSM get samples +get_samples + +States: + START + GO_TO_POSITION + PREPARE_ARM + FORWARD_CONTROL + TAKE_SAMPLES + BACKWARD + END + +Events: + ok + position_reached + position_failed + arm_moved + sample_took + classifier_ready + +START: + ok -> GO_TO_POSITION + Go to the distributor. The distributor to reach shall be setted in the fsm structure. + +GO_TO_POSITION: + position_reached -> PREPARE_ARM + Go to the position desired, it is very near the position of the distributor in case it is a ice distributor or sample distributor. + position_failed -> GO_TO_POSITION + Go to another point before trying to go to this one again. + +PREPARE_ARM: + arm_moved -> FORWARD_CONTROL + Prepare the arm to the correct position. + +FORWARD_CONTROL: + position_reached -> TAKE_SAMPLES + End the position to the distributor. + +TAKE_SAMPLES: + sample_took: more -> . + Continue to take samples and classify the next sample. + sample_took: no_more -> BACKWARD + If the quantity of samples are tooked, then go backeward and conitnue classifying the samples. + +BACKWARD: + position_reached -> END + Ending this state machine. diff --git a/digital/io/src/getsamples.h b/digital/io/src/getsamples.h new file mode 100644 index 00000000..ac2b68f3 --- /dev/null +++ b/digital/io/src/getsamples.h @@ -0,0 +1,47 @@ +#ifndef getsamples_h +#define getsamples_h +/* getsamples.h */ +/* {{{ + * + * Copyright (C) 2008 Nélio Laranjeiro + * + * 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. + * + * }}} */ + +/** getsamples FSM associated data. */ +struct getsamples_data_t +{ + /* Distributor x position to get samples. */ + uint32_t distributor_x; + /* Distributor y position to get samples. */ + uint32_t distributor_y; + /* Samples to take. */ + uint8_t samples; +}; + +/** getsamples global. */ +extern struct getsamples_data_t getsamples_data; + +/** Start a getsamples FSM. */ +void +getsamples_start (uint32_t distributor_x, uint32_t distributor_y, + uint8_t samples); + +#endif /* getsamples_h */ diff --git a/digital/io/src/getsamples_cb.c b/digital/io/src/getsamples_cb.c index 27f0d9b0..aeb7f586 100644 --- a/digital/io/src/getsamples_cb.c +++ b/digital/io/src/getsamples_cb.c @@ -1,12 +1,31 @@ -/* - * THIS IS AN AUTOMATICALLY GENERATED FILE, DO NOT EDIT! +/* getsamples_cb.c - getsamples FSM callbacks. */ +/* {{{ * - * Skeleton for get_samples callbacks implementation. + * Copyright (C) 2008 Nélio Laranjeiro * - * - */ + * 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 "fsm.h" #include "getsamples_cb.h" -#include "getsamples_robo.h" +#include "getsamples.h" #include "asserv.h" /* @@ -14,13 +33,13 @@ * => FORWARD_CONTROL * Prepare the arm to the correct position. */ -getsamples_branch_t -getsamples__PREPARE_ARM__arm_moved (getsamples_t *user) +fsm_branch_t +getsamples__PREPARE_ARM__arm_moved (void) { // Try to end the position to the distributor. - asserv_set_x_position (user->distributor_x); + asserv_set_x_position (getsamples_data.distributor_x); // Go to the color distributor. - asserv_set_y_position (user->distributor_y); + asserv_set_y_position (getsamples_data.distributor_y); return getsamples_next (PREPARE_ARM, arm_moved); } @@ -29,8 +48,8 @@ getsamples__PREPARE_ARM__arm_moved (getsamples_t *user) * => TAKE_SAMPLES * End the position to the distributor. */ -getsamples_branch_t -getsamples__FORWARD_CONTROL__position_reached (getsamples_t *user) +fsm_branch_t +getsamples__FORWARD_CONTROL__position_reached (void) { // Take as many samples as necessary. This shall be updated to verify the asserv_move_arm (1666, 100); @@ -42,26 +61,26 @@ getsamples__FORWARD_CONTROL__position_reached (getsamples_t *user) * => GO_TO_POSITION * Go to the distributor. The distributor to reach shall be setted in the fsm structure. */ -getsamples_branch_t -getsamples__START__ok (getsamples_t *user) +fsm_branch_t +getsamples__START__ok (void) { - asserv_set_x_position (user->distributor_x - 20); - asserv_set_y_position (user->distributor_y - 20); + asserv_set_x_position (getsamples_data.distributor_x - 20); + asserv_set_y_position (getsamples_data.distributor_y - 20); return getsamples_next (START, ok); } -/** +/* * TAKE_SAMPLES =sample_took=> * no_more => BACKWARD * If the quantity of samples are tooked, then go backeward and conitnue classifying the samples. * more => TAKE_SAMPLES * Continue to take samples and classify the next sample. */ -getsamples_branch_t -getsamples__TAKE_SAMPLES__sample_took (getsamples_t *user) +fsm_branch_t +getsamples__TAKE_SAMPLES__sample_took (void) { // Decrement the samples counter. - if (user->samples) + if (getsamples_data.samples) { asserv_move_arm (1666, 100); return getsamples_next_branch (TAKE_SAMPLES, sample_took, more); @@ -69,9 +88,9 @@ getsamples__TAKE_SAMPLES__sample_took (getsamples_t *user) else { // Try to end the position to the distributor. - asserv_set_x_position (user->distributor_x - 20); + asserv_set_x_position (getsamples_data.distributor_x - 20); // Go to the color distributor. - asserv_set_y_position (user->distributor_y - 20); + asserv_set_y_position (getsamples_data.distributor_y - 20); return getsamples_next_branch (TAKE_SAMPLES, sample_took, no_more); } @@ -82,8 +101,8 @@ getsamples__TAKE_SAMPLES__sample_took (getsamples_t *user) * => END * Ending this state machine. */ -getsamples_branch_t -getsamples__BACKWARD__position_reached (getsamples_t *user) +fsm_branch_t +getsamples__BACKWARD__position_reached (void) { asserv_move_arm (5000, 100); return getsamples_next (BACKWARD, position_reached); @@ -94,8 +113,8 @@ getsamples__BACKWARD__position_reached (getsamples_t *user) * => GO_TO_POSITION * Go to another point before trying to go to this one again. */ -getsamples_branch_t -getsamples__GO_TO_POSITION__position_failed (getsamples_t *user) +fsm_branch_t +getsamples__GO_TO_POSITION__position_failed (void) { // TODO In this case i don't know what to do. return getsamples_next (GO_TO_POSITION, position_failed); @@ -106,8 +125,8 @@ getsamples__GO_TO_POSITION__position_failed (getsamples_t *user) * => PREPARE_ARM * Go to the position desired, it is very near the position of the distributor in case it is a ice distributor or sample distributor. */ -getsamples_branch_t -getsamples__GO_TO_POSITION__position_reached (getsamples_t *user) +fsm_branch_t +getsamples__GO_TO_POSITION__position_reached (void) { // Put the ARM in the position to allow the robot to take samples from the // distributor. diff --git a/digital/io/src/getsamples_robo.h b/digital/io/src/getsamples_robo.h deleted file mode 100644 index 87583b67..00000000 --- a/digital/io/src/getsamples_robo.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef getsamples_robo_h -#define getsamples_robo_h -/* getsamples_robo.h */ -/* {{{ - * - * Copyright (C) 2008 Nélio Laranjeiro - * - * 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" - -struct getsamples_t -{ - /* The FSM. */ - getsamples_state_t fsm; - /* Distributor x position to get samples. */ - uint32_t distributor_x; - /* Distributor y position to get samples. */ - uint32_t distributor_y; - /* Samples to take. */ - uint8_t samples; -}; - -#endif /* getsamples_robo_h */ diff --git a/digital/io/src/gutter.conf b/digital/io/src/gutter.conf deleted file mode 100644 index 2f21fd61..00000000 --- a/digital/io/src/gutter.conf +++ /dev/null @@ -1,5 +0,0 @@ -[user] -type = gutter_t -type-forward-decl = typedef struct gutter_t gutter_t; -type-decl = #include "gutter_robo.h" -field = fsm diff --git a/digital/io/src/gutter.fsm b/digital/io/src/gutter.fsm index 158c8d13..6faa340d 100644 --- a/digital/io/src/gutter.fsm +++ b/digital/io/src/gutter.fsm @@ -1,5 +1,5 @@ -#FSM get samples -get_samples +#FSM gutter +gutter States: START diff --git a/digital/io/src/gutter_cb.c b/digital/io/src/gutter_cb.c index 729c187f..30166929 100644 --- a/digital/io/src/gutter_cb.c +++ b/digital/io/src/gutter_cb.c @@ -1,19 +1,41 @@ -/* - * THIS IS AN AUTOMATICALLY GENERATED FILE, DO NOT EDIT! +/* gutter_cb.c - gutter FSM callbacks. */ +/* {{{ * - * Skeleton for get_samples callbacks implementation. + * Copyright (C) 2008 Nélio Laranjeiro * - * - */ + * 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 "fsm.h" #include "gutter_cb.h" +#include "asserv.h" +#include "trap.h" +#include "modules/utils/utils.h" /* * START =ok=> * => GO_TO_GUTTER * Go to the gutter. */ -gutter_branch_t -gutter__START__ok (gutter_t *user) +fsm_branch_t +gutter__START__ok (void) { asserv_go_to_gutter(); return gutter_next (START, ok); @@ -24,8 +46,8 @@ gutter__START__ok (gutter_t *user) * => END * The samples has been inserted in the gutter. */ -gutter_branch_t -gutter__CLOSE_COLLECTOR__collector_closed (gutter_t *user) +fsm_branch_t +gutter__CLOSE_COLLECTOR__collector_closed (void) { //Close the collector. trap_close_rear_panel(); @@ -37,8 +59,8 @@ gutter__CLOSE_COLLECTOR__collector_closed (gutter_t *user) * => GO_TO_GUTTER * The position failed, shall try another path. */ -gutter_branch_t -gutter__GO_TO_GUTTER__position_failed (gutter_t *user) +fsm_branch_t +gutter__GO_TO_GUTTER__position_failed (void) { return gutter_next (GO_TO_GUTTER, position_failed); } @@ -48,8 +70,8 @@ gutter__GO_TO_GUTTER__position_failed (gutter_t *user) * => OPEN_COLLECTOR * The robo is near the gutter and the door can be opened. */ -gutter_branch_t -gutter__GO_TO_GUTTER__position_reached (gutter_t *user) +fsm_branch_t +gutter__GO_TO_GUTTER__position_reached (void) { // Open the collector. trap_open_rear_panel(); @@ -61,8 +83,8 @@ gutter__GO_TO_GUTTER__position_reached (gutter_t *user) * => CLOSE_COLLECTOR * Wait some time and clse the door. */ -gutter_branch_t -gutter__OPEN_COLLECTOR__collector_opened (gutter_t *user) +fsm_branch_t +gutter__OPEN_COLLECTOR__collector_opened (void) { utils_delay_ms(4.4); return gutter_next (OPEN_COLLECTOR, collector_opened); diff --git a/digital/io/src/gutter_robo.h b/digital/io/src/gutter_robo.h deleted file mode 100644 index 251d6f4d..00000000 --- a/digital/io/src/gutter_robo.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef gutter_robo_h -#define gutter_robo_h -/* gutter_robo.h */ -/* {{{ - * - * Copyright (C) 2008 Nélio Laranjeiro - * - * 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 "gutter.h" - -struct gutter_t -{ - gutter_state_t fsm; -}; - -#endif /* gutter_robo_h */ diff --git a/digital/io/src/test/gutter/Makefile b/digital/io/src/test/gutter/Makefile index 21470810..cd355139 100644 --- a/digital/io/src/test/gutter/Makefile +++ b/digital/io/src/test/gutter/Makefile @@ -1,7 +1,12 @@ -BASE= .. +BASE= ../../../../avr +HOST_PROGS = fsm +IOBASE = ../.. +fsm_SOURCES = main.c $(IOBASE)/fsm.c \ + $(IOBASE)/gutter_cb.c $(IOBASE)/gutter_fsm.c +MODULES = +CONFIGFILE = +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 -all: main.c $(BASE)/gutter.c $(BASE)/gutter_cb.c - gcc -g -O2 -Wall main.c $(BASE)/gutter.c $(BASE)/gutter_cb.c -o fsm - -clean: fsm - rm -f fsm +include $(BASE)/make/Makefile.gen diff --git a/digital/io/src/test/gutter/main.c b/digital/io/src/test/gutter/main.c index 08be3342..41860461 100644 --- a/digital/io/src/test/gutter/main.c +++ b/digital/io/src/test/gutter/main.c @@ -22,17 +22,18 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ -#include "../gutter.h" -#include "../gutter_robo.h" +#include "common.h" + +#include "../../fsm.h" #include void -gutter_print_test (gutter_t *gutter) +gutter_print_test (fsm_t *gutter) { printf ("Machine state "); - switch (gutter->fsm) + switch (gutter->state_current) { case GUTTER_STATE_END: printf ("END"); @@ -55,28 +56,25 @@ gutter_print_test (gutter_t *gutter) int main (void) { - gutter_t gutter_fsm; + fsm_init (&gutter_fsm); + + fsm_handle_event (&gutter_fsm, GUTTER_EVENT_ok); - gutter_init (&gutter_fsm); gutter_print_test (&gutter_fsm); - gutter_handle_event (&gutter_fsm, - GUTTER_EVENT_position_failed); + fsm_handle_event (&gutter_fsm, GUTTER_EVENT_position_failed); gutter_print_test (&gutter_fsm); - gutter_handle_event (&gutter_fsm, - GUTTER_EVENT_position_reached); + fsm_handle_event (&gutter_fsm, GUTTER_EVENT_position_reached); gutter_print_test (&gutter_fsm); - gutter_handle_event (&gutter_fsm, - GUTTER_EVENT_collector_opened); + fsm_handle_event (&gutter_fsm, GUTTER_EVENT_collector_opened); gutter_print_test (&gutter_fsm); - gutter_handle_event (&gutter_fsm, - GUTTER_EVENT_collector_closed); + fsm_handle_event (&gutter_fsm, GUTTER_EVENT_collector_closed); gutter_print_test (&gutter_fsm); diff --git a/digital/io/src/test/testgetsamples/Makefile b/digital/io/src/test/testgetsamples/Makefile index b0a3a7a3..21313ff6 100644 --- a/digital/io/src/test/testgetsamples/Makefile +++ b/digital/io/src/test/testgetsamples/Makefile @@ -1,7 +1,12 @@ -BASE= ../.. +BASE= ../../../../avr +HOST_PROGS = fsm +IOBASE = ../.. +fsm_SOURCES = main.c $(IOBASE)/fsm.c $(IOBASE)/getsamples.c \ + $(IOBASE)/getsamples_cb.c $(IOBASE)/getsamples_fsm.c +MODULES = +CONFIGFILE = +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 -all: main.c $(BASE)/getsamples.c $(BASE)/getsamples_cb.c - gcc -g -O2 -Wall main.c $(BASE)/getsamples.c $(BASE)/getsamples_cb.c -o fsm - -clean: fsm - rm -f fsm +include $(BASE)/make/Makefile.gen diff --git a/digital/io/src/test/testgetsamples/avrconfig.h b/digital/io/src/test/testgetsamples/avrconfig.h index c4d9f159..d6afb3d3 100644 --- a/digital/io/src/test/testgetsamples/avrconfig.h +++ b/digital/io/src/test/testgetsamples/avrconfig.h @@ -1,7 +1,7 @@ #ifndef avrconfig_h #define avrconfig_h /* avrconfig.h */ -/* avr.spi - SPI AVR module. {{{ +/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ * * Copyright (C) 2008 Nélio Laranjeiro * @@ -30,66 +30,4 @@ * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ #define AC_FREQ 14745600 -/* uart - UART module. */ -/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ -#define AC_UART0_PORT 1 -/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, - * 115200, 230400, 250000, 500000, 1000000. */ -#define AC_UART0_BAUDRATE 38400 -/** Send mode: - * - POLLING: no interrupts. - * - RING: interrupts, ring buffer. */ -#define AC_UART0_SEND_MODE POLLING -/** Recv mode, same as send mode. */ -#define AC_UART0_RECV_MODE POLLING -/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */ -#define AC_UART0_CHAR_SIZE 8 -/** Parity : ODD, EVEN, NONE. */ -#define AC_UART0_PARITY EVEN -/** Stop bits : 1, 2. */ -#define AC_UART0_STOP_BITS 1 -/** Send buffer size, should be power of 2 for RING mode. */ -#define AC_UART0_SEND_BUFFER_SIZE 32 -/** Recv buffer size, should be power of 2 for RING mode. */ -#define AC_UART0_RECV_BUFFER_SIZE 32 -/** If the send buffer is full when putc: - * - DROP: drop the new byte. - * - WAIT: wait until there is room in the send buffer. */ -#define AC_UART0_SEND_BUFFER_FULL WAIT -/** In HOST compilation: - * - STDIO: use stdin/out. - * - PTS: use pseudo terminal. */ -#define AC_UART0_HOST_DRIVER STDIO -/** Same thing for secondary port. */ -#define AC_UART1_PORT -1 -#define AC_UART1_BAUDRATE 115200 -#define AC_UART1_SEND_MODE RING -#define AC_UART1_RECV_MODE RING -#define AC_UART1_CHAR_SIZE 8 -#define AC_UART1_PARITY EVEN -#define AC_UART1_STOP_BITS 1 -#define AC_UART1_SEND_BUFFER_SIZE 32 -#define AC_UART1_RECV_BUFFER_SIZE 32 -#define AC_UART1_SEND_BUFFER_FULL WAIT -#define AC_UART1_HOST_DRIVER PTS - -/* proto - Protocol module. */ -/** Maximum argument size. */ -#define AC_PROTO_ARGS_MAX_SIZE 8 -/** Callback function name. */ -#define AC_PROTO_CALLBACK proto_callback -/** Putchar function name. */ -#define AC_PROTO_PUTC uart0_putc -/** Support for quote parameter. */ -#define AC_PROTO_QUOTE 1 - -/* flash - Flash SPI AVR module. */ -/** Flash PORT used. */ -#define AC_FLASH_PORT PORTD -/** Flash DDR used. */ -#define AC_FLASH_DDR DDRD -/** Flash SS pin. */ -#define AC_FLASH_BIT_SS 5 - - #endif /* avrconfig_h */ diff --git a/digital/io/src/test/testgetsamples/main.c b/digital/io/src/test/testgetsamples/main.c index 4e156e05..4479b053 100644 --- a/digital/io/src/test/testgetsamples/main.c +++ b/digital/io/src/test/testgetsamples/main.c @@ -24,17 +24,16 @@ * }}} */ #include "common.h" -#include "modules/proto/proto.h" -#include "modules/utils/utils.h" - +#include "../../fsm.h" #include "../../getsamples.h" -#include "../../getsamples_robo.h" + +#include void -getsamples_print_test (getsamples_t *getsamples) +getsamples_print_test (fsm_t *getsamples) { printf ("STATE "); - switch (getsamples->fsm) + switch (getsamples->state_current) { case GETSAMPLES_STATE_START: printf ("START"); @@ -66,57 +65,48 @@ getsamples_print_test (getsamples_t *getsamples) int main (void) { - getsamples_t getsamples_fsm; - - getsamples_init (&getsamples_fsm); + fsm_init (&getsamples_fsm); getsamples_print_test (&getsamples_fsm); - getsamples_fsm.distributor_x = 700; - getsamples_fsm.distributor_y = 2100; - getsamples_fsm.samples = 3; + getsamples_start (700, 2100, 3); - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_ok); + /* Implicit: + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_ok); */ getsamples_print_test (&getsamples_fsm); - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_position_failed); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_position_failed); getsamples_print_test (&getsamples_fsm); - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_position_reached); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_position_reached); getsamples_print_test (&getsamples_fsm); - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_arm_moved); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_arm_moved); getsamples_print_test (&getsamples_fsm); - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_position_reached); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_position_reached); getsamples_print_test (&getsamples_fsm); - for (getsamples_fsm.samples--; getsamples_fsm.samples; getsamples_fsm.samples --) + //TODO: samples should be decremented by the FSM transitions. + for (getsamples_data.samples--; getsamples_data.samples; + getsamples_data.samples--) { - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_sample_took); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_sample_took); getsamples_print_test (&getsamples_fsm); } - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_sample_took); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_sample_took); getsamples_print_test (&getsamples_fsm); - getsamples_handle_event (&getsamples_fsm, - GETSAMPLES_EVENT_position_reached); + fsm_handle_event (&getsamples_fsm, GETSAMPLES_EVENT_position_reached); getsamples_print_test (&getsamples_fsm); -- cgit v1.2.3