From 5f511221ebe3991fc1084eb979474857e45df2b1 Mon Sep 17 00:00:00 2001 From: schodet Date: Mon, 28 Jun 2004 10:18:48 +0000 Subject: Ajout de ai. --- 2004/i/nono/src/GNUmakefile | 2 +- 2004/i/nono/src/ai/Makefile.defs | 7 + 2004/i/nono/src/ai/ai.cc | 268 +++++++++++++++++++++++++++++++++++++++ 2004/i/nono/src/ai/ai.h | 98 ++++++++++++++ 4 files changed, 374 insertions(+), 1 deletion(-) create mode 100644 2004/i/nono/src/ai/Makefile.defs create mode 100644 2004/i/nono/src/ai/ai.cc create mode 100644 2004/i/nono/src/ai/ai.h (limited to '2004/i/nono/src') diff --git a/2004/i/nono/src/GNUmakefile b/2004/i/nono/src/GNUmakefile index d887290..64f1492 100644 --- a/2004/i/nono/src/GNUmakefile +++ b/2004/i/nono/src/GNUmakefile @@ -1,5 +1,5 @@ SUBDIRS = config date motor serial fake_serial utils \ - logger video4linux image ovision qdai io path + logger video4linux image ovision qdai ai io path TARGETS = LIBS = -lm diff --git a/2004/i/nono/src/ai/Makefile.defs b/2004/i/nono/src/ai/Makefile.defs new file mode 100644 index 0000000..330a818 --- /dev/null +++ b/2004/i/nono/src/ai/Makefile.defs @@ -0,0 +1,7 @@ +TARGETS += ai +ai_SOURCES = ai.cc date.a io.a image.a logger.a motor.a \ + movement_reposition.cc ovision.a serial.a utils.a \ + video4linux.a config.a + +ai: $(ai_SOURCES:%.cc=%.o) + diff --git a/2004/i/nono/src/ai/ai.cc b/2004/i/nono/src/ai/ai.cc new file mode 100644 index 0000000..5531805 --- /dev/null +++ b/2004/i/nono/src/ai/ai.cc @@ -0,0 +1,268 @@ +// ai.cc +// nono - programme du robot 2004. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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 "ai.h" +#include "motor/movement_types.h" +#include "motor/movement_reposition.h" +#include "date/date.h" + +#include +#include + +/// Constructeur. +Ai::Ai (void) + : ana_ (gspp_), + ga_ (motor_.getAsserv ()), + gc_ (ga_, gspp_, ga_.getNbIo ()), + gp_ (gc_), + v4l_ ("/dev/video", ImageLoader::yuv), + lastCapture_ (0), + front_ (135), bigFront_ (182), width_ (260), rear_ (135), + roulOn_ (false), roulRev_ (false) +{ + gpioJack_ = gp_.getByName ("jack"); + gpioRoul_ = gp_.getByName ("roul"); + gpioRoulS_ = gp_.getByName ("rouls"); + gpioContactL_ = gp_.getByName ("contactl"); + gpioContactR_ = gp_.getByName ("contactr"); + anaSharpL_ = ana_.getByName ("ana1"); + anaSharpR_ = ana_.getByName ("ana0"); +} + +/// Initialise le robot. +void +Ai::init (void) +{ + wait (500); + benneUp (false); + rouleau (false, false); + motor_.go (); + //v4l_.calibrate (); +} + +/// Arrète le robot. +void +Ai::stop (void) +{ + /// Attend l'arret complet de l'appareil. + motor_.waitStopped (); +} + +/// Lance le robot. +void +Ai::run (void) +{ + try + { + waitJack (false); + rouleau (true, false); + wait (500); + rouleau (false, false); + waitJack (true); + Date::getInstance ().startRound (); + rouleau (true, false); + goTo (900, 1500); + goTo (900, 1800); + goTo (2400, 1800); + benneUp (true); + recaleX (9 * 300 - front_, 0); + decharge (1000); + basic (-0.5, 150); + benneUp (false); + rotation (-M_PI / 2); + goTo (2400, 300); + rotation (0); + benneUp (true); + recaleX (9 * 300 - front_, 0); + decharge (1000); + basic (-0.5, 150); + benneUp (false); + rotation (M_PI); + goTo (600, 300); + benneUp (true); + recaleX (1 * 300 + front_, M_PI); + basic (-0.5, 150); + benneUp (false); + rotation (0); + basic (0.5, 1800); + benneUp (true); + recaleX (9 * 300 - front_, 0); + decharge (3000); + basic (-0.5, 150); + benneUp (false); + } + catch (FinDeMatch &e) + { + } +} + +/// Mises à jour périodiques. +void +Ai::update (void) +{ + // Mise à jour moteur. + motor_.waitOk (); + // Mise à jour capteurs. + gspp_.update (); + // Mise à jour camera. + if (lastCapture_ + captureInterval_ < Date::getInstance ().start ()) + { + } +} + +/// Mises à jour jusqu'à fin du mouvement en cours. +void +Ai::updateToEnd (void) +{ + while (!motor_.end ()) + { + if (Date::getInstance ().round () >= roundDuration_) + throw FinDeMatch (); + update (); + } +} + +/// Mises à jour jusqu'à timeout. +void +Ai::updateWait (int t) +{ + int start; + start = Date::getInstance ().start (); + while (Date::getInstance ().start () < start + t) + { + if (Date::getInstance ().round () >= roundDuration_) + throw FinDeMatch (); + update (); + } +} + +/// Lève (true) ou baisse (false) la benne. +void +Ai::benneUp (bool f) +{ + gspp_.moveTo (1, f ? 5 : 57); +} + +/// Fait tourner le rouleau. +void +Ai::rouleau (bool on, bool rev) +{ + if (rev != roulRev_) + { + if (roulOn_) + { + gp_.set (gpioRoul_, 0); + roulOn_ = false; + updateWait (200); + } + gp_.set (gpioRoulS_, rev ? 1 : 0); + roulRev_ = rev; + if (on) + updateWait (200); + } + if (on != roulOn_) + { + gp_.set (gpioRoul_, on ? 1 : 0); + roulOn_ = on; + } +} + +/// Attend. +void +Ai::wait (int t) +{ + updateWait (t); +} + +/// Attend le jack entré (false) ou sorti (true). +void +Ai::waitJack (bool out) +{ + while (gp_.get (gpioJack_) != (out ? 1u : 0u)) + updateWait (50); +} + +/// Rejoint un point. +void +Ai::goTo (double x, double y) +{ + Goto *g = new GotoSimple (x, y); + Movement *m = new MovementGoto (g); + motor_.addMovement (m); + updateToEnd (); +} + +/// Recale contre une bordure. +void +Ai::recale (void) +{ + Movement *m = new MovementReposition (0.2, gp_, gpioContactL_, + gpioContactR_, ana_, anaSharpL_, + anaSharpR_); + motor_.addMovement (m); + updateToEnd (); +} + +/// Recale et ajuste en X contre une bordure. +void +Ai::recaleX (double x, double a) +{ + recale (); +} + +/// Mouvement basic. +void +Ai::basic (double v, double d) +{ + Movement *m = new MovementBasic (v, d); + motor_.addMovement (m); + updateToEnd (); +} + +/// Rotation. +void +Ai::rotation (double a) +{ + Movement *m = new MovementRotation (a); + motor_.addMovement (m); + updateToEnd (); +} + +/// Décharge la benne. +void +Ai::decharge (int t) +{ + rouleau (true, true); + wait (t); + rouleau (true, false); +} + +int +main (int argc, char **argv) +{ + Ai qdai_; + qdai_.init (); + qdai_.run (); + qdai_.stop (); +} diff --git a/2004/i/nono/src/ai/ai.h b/2004/i/nono/src/ai/ai.h new file mode 100644 index 0000000..0cacf23 --- /dev/null +++ b/2004/i/nono/src/ai/ai.h @@ -0,0 +1,98 @@ +#ifndef ai_h +#define ai_h +// ai.h +// nono - programme du robot 2004. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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 "motor/motor.h" +#include "io/analog_servo_pp.h" +#include "io/gpio_asserv.h" +#include "io/gpio_concat.h" +#include "io/gpio_servo_pp.h" +#include "io/gpio_param.h" +#include "video4linux/video4linux.h" + +/// Intelligence (heu...) artificielle. +class Ai +{ + class FinDeMatch + { + }; + Motor motor_; + GpioServoPp gspp_; + AnalogServoPp ana_; + GpioAsserv ga_; + GpioConcat gc_; + GpioParam gp_; + Video4Linux v4l_; + static const int roundDuration_ = 90000; + static const int captureInterval_ = 500; + int lastCapture_; + double front_, bigFront_, width_, rear_; + int gpioJack_; + int gpioRoul_; + int gpioRoulS_; + int gpioContactL_; + int gpioContactR_; + int anaSharpL_; + int anaSharpR_; + bool roulOn_, roulRev_; + public: + /// Constructeur. + Ai (void); + /// Initialise le robot. + void init (void); + /// Arrète le robot. + void stop (void); + /// Lance le robot. + void run (void); + /// Mises à jour périodiques. + void update (void); + /// Mises à jour jusqu'à fin du mouvement en cours. + void updateToEnd (void); + /// Mises à jour jusqu'à timeout. + void updateWait (int t); + protected: + /// Lève (true) ou baisse (false) la benne. + void benneUp (bool f); + /// Fait tourner le rouleau. + void rouleau (bool on, bool rev); + /// Attend. + void wait (int t); + /// Attend le jack entré (false) ou sorti (true). + void waitJack (bool out); + /// Rejoint un point. + void goTo (double x, double y); + /// Recale contre une bordure. + void recale (void); + /// Recale et ajuste en X contre une bordure. + void recaleX (double x, double a); + /// Mouvement basic. + void basic (double v, double d); + /// Rotation. + void rotation (double a); + /// Décharge la benne. + void decharge (int t); +}; + +#endif // ai_h -- cgit v1.2.3