// es.cc // chuck - programme du robot 2007 {{{ // // Copyright (C) 2007 Romain Becquet // // Robot APB Team/Efrei 2007. // Web: http://assos.efrei.fr/si2e/ // Email: si2e 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 "es/es.hh" #include "config/config.hh" #include #include /// Constructeur Es::Es (const Config & config) : proto_ (*this), log_ ("Es"), lcdKeyPressed_ (-1), front_sensor_ (false), jackIn_ (false),colorModeBlue_(false),ackSharp_(false) { // Récupération des valeurs de configuration dans le fichier loadConfig (config); proto_.open (tty_); sharps_.resize (2); } bool Es::wait (int timeout /*-1*/) { return proto_.wait (timeout); } /// Récupère le File Descriptor int Es::getFd (void) { return proto_.getFd(); } bool Es::sync (void) { return proto_.sync (); } void Es::loadConfig (const Config & config) { tty_ = config.get("es.tty"); ackFreq_ = config.get ("es.ack_freq"); mainStat_ = config.get ("es.main_stat"); othersStat_ = config.get ("es.others_stat"); lcdKeyStat_ = config.get ("es.lcd_key_stat"); thresholdFrontSensors_ = config.get ("es.threshold_front_sensors"); thresholdHoleSensors_ = config.get ("es.threshold_hole_sensors"); thresholdSharp_ = config.get ("es.threshold_sharp"); freqSharp_ = config.get ("es.freq_sharp"); } void Es::reset (void) { // On reset l'AVR proto_.send ('z'); // Send configurations setMainStat (mainStat_); setAckStat (ackFreq_); setOthersStat (othersStat_); lcdGetKey (lcdKeyStat_); // XXX Hardcoded value ! setSharpThreshold (1, thresholdSharp_, thresholdSharp_); log_ ("Es", Log::debug) << "Reset Es done."; } /// Stat for the main () void Es::setMainStat (int freq) { proto_.send ('Z', "b", freq); } /// Shut up ! void Es::shutUp (void) { proto_.send ('f'); } /// Set frequency of ack void Es::setAckStat (int freq) { ackFreq_ = freq; proto_.send ('F', "b", ackFreq_); } /// Get the state of the jack bool Es::isJackOut (void) { return !jackIn_; } /// Get the color mode of the button bool Es::isColorModeBlue (void) { return colorModeBlue_; } /// Set frequency of jack, selectoul printed out function void Es::setOthersStat (int freq) { proto_.send ('O', "b", freq); } /// Set update frequency of sharps void Es::setSharpUpdate (int sharp_mask, int freq) { proto_.send ('h', "bb", sharp_mask, freq); } /// Set statistics frequency of sharps void Es::setSharpStat (int sharp_mask, int freq) { proto_.send ('H', "bb", sharp_mask, freq); } /// Set configuration of threshold sharps void Es::setSharpThreshold (int sharp_num, int threshold_high, int threshold_low) { proto_.send ('o', "bww", sharp_num, threshold_high, threshold_low); } /// Print something on the LCD (max 16 char) void Es::lcdPrint (const std::string &message) { std::memset (lcd_mess_char_, ' ', 16); int size = message.size (); std::memcpy (lcd_mess_char_, message.data (), size > 16 ? 16 : size); proto_.sendStr ('l', lcd_mess_char_, 16); } /// Get the current pressed keys of the LCD void Es::lcdGetKey (int freq) { proto_.send ('L', "b", freq); } void Es::receive(char command, const Proto::Frame & frame) { int errCode, clear, compt, value; int stat1, stat2, stat3, stat4; switch (command) { /* Main stat */ case 'Z': if (proto_.decode (frame, "bbbb", stat1, stat2, stat3, stat4)) { log_ ("Stats main", Log::debug) << stat1 << " " << stat2 << " " << stat3 << " " << stat4; } break; /* Ack */ case 'F': if (proto_.decode (frame, "b", errCode)) { log_ ("Ack", Log::debug) << "ErrCode :" << errCode; switch (errCode) { // XXX Yerka hardcoded case 1: log_ ("Ack", Log::debug) << "Ack sharp has seen something"; ackSharp_ = true; break; } shutUp (); } break; /* Others */ case 'O': if (proto_.decode (frame, "b", value)) { // XXX Use a decallage please if (value & 0x01) jackIn_ = true; else jackIn_ = false; if (value & 0x02) colorModeBlue_ = true; else colorModeBlue_ = false; // log_ ("Others", Log::debug) << "Color mode " << (colorModeBlue_ ? "Blue" : "Red") // << (jackIn_ ? ", jack in..." : ", jack out !!!"); } break; /* LCD */ case 'L': if (proto_.decode (frame, "b", value)) lcdKeyPressed_ = value; break; /* Sharps */ case 'H': if (proto_.decode (frame, "ww", compt, value)) { std::cout << "[" << compt << "]" << " = " << value << std::endl; sharps_[compt] = value; } break; } } //Activation du truk ki monte et descend. void Es::bouge_ascenceur(void) { proto_.send('a',"bbb",0x70,0xde,0x00); /*roulo_roule(0x00);*/ } void Es::bougeascDown(void) { proto_.send('a',"bbb",0x60,0xf0,0x01); /* roulo_roule(0x00);*/ } //le roulo qui roule void Es::roulo_roule(uint8_t vit) { proto_.send('R',"b",vit); } //Euh kel est mon facteur de symetrie pour l'asserv?? int Es::setcolorsens(void) { if (colorModeBlue_) { return 1; } else { return -1; } } void Es::initservo(void) { proto_.send('m',"bb",0x02,0x00); } //Le roulo tombe grace au servo void Es::roulo_tombe(void) { proto_.send('m',"bb",0x02,0x7b); } /// Restart the detection of something front void Es::launchSharpDetection (void) { ackSharp_ = false; setSharpUpdate (2, freqSharp_); } /// Move ascenseur void Es::moveAsc (int speed, int time, bool up) { proto_.send ('a', "bbb", speed, time, up ? 0x0 : 0x1); }