EthercatMasterWithoutThread.hpp

Go to the documentation of this file.
00001 #ifndef YOUBOT_ETHERCATMASTERWITHOUTTHREAD_H
00002 #define YOUBOT_ETHERCATMASTERWITHOUTTHREAD_H
00003 
00004 /****************************************************************
00005  *
00006  * Copyright (c) 2011
00007  * All rights reserved.
00008  *
00009  * Hochschule Bonn-Rhein-Sieg
00010  * University of Applied Sciences
00011  * Computer Science Department
00012  *
00013  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00014  *
00015  * Author:
00016  * Jan Paulus, Nico Hochgeschwender, Michael Reckhaus, Azamat Shakhimardanov
00017  * Supervised by:
00018  * Gerhard K. Kraetzschmar
00019  *
00020  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00021  *
00022  * This sofware is published under a dual-license: GNU Lesser General Public 
00023  * License LGPL 2.1 and BSD license. The dual-license implies that users of this
00024  * code may choose which terms they prefer.
00025  *
00026  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00027  *
00028  * Redistribution and use in source and binary forms, with or without
00029  * modification, are permitted provided that the following conditions are met:
00030  *
00031  *     * Redistributions of source code must retain the above copyright
00032  *       notice, this list of conditions and the following disclaimer.
00033  *     * Redistributions in binary form must reproduce the above copyright
00034  *       notice, this list of conditions and the following disclaimer in the
00035  *       documentation and/or other materials provided with the distribution.
00036  *     * Neither the name of the Hochschule Bonn-Rhein-Sieg nor the names of its
00037  *       contributors may be used to endorse or promote products derived from
00038  *       this software without specific prior written permission.
00039  *
00040  * This program is free software: you can redistribute it and/or modify
00041  * it under the terms of the GNU Lesser General Public License LGPL as
00042  * published by the Free Software Foundation, either version 2.1 of the
00043  * License, or (at your option) any later version or the BSD license.
00044  *
00045  * This program is distributed in the hope that it will be useful,
00046  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00047  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00048  * GNU Lesser General Public License LGPL and the BSD license for more details.
00049  *
00050  * You should have received a copy of the GNU Lesser General Public
00051  * License LGPL and BSD license along with this program.
00052  *
00053  ****************************************************************/
00054 
00055 #include <vector>
00056 #include <sstream>
00057 #include <string>
00058 #include <cstdio>
00059 #include <stdexcept>
00060 #include <iostream>
00061 #include <boost/thread.hpp>
00062 #include <boost/date_time/posix_time/posix_time.hpp>
00063 #include "generic/Logger.hpp"
00064 #include "generic/Units.hpp"
00065 #include "generic/Time.hpp"
00066 #include "generic/Exceptions.hpp"
00067 #include "generic/ConfigFile.hpp"
00068 #include "youbot/ProtocolDefinitions.hpp"
00069 #include "youbot/YouBotSlaveMsg.hpp"
00070 #include "youbot/YouBotSlaveMailboxMsg.hpp"
00071 #include "youbot/EthercatMaster.hpp"
00072 #include "youbot/JointLimitMonitor.hpp"
00073 
00074 extern "C"{
00075 #include <ethercattype.h>
00076 #include <ethercatmain.h>
00077 }
00078 
00079 namespace youbot {
00080 
00081 ///////////////////////////////////////////////////////////////////////////////
00082 /// The Ethercat Master is managing the whole ethercat communication 
00083 /// It have to be a singleton in the system
00084 ///////////////////////////////////////////////////////////////////////////////
00085 class EthercatMasterWithoutThread : public EthercatMasterInterface {
00086 friend class EthercatMaster;
00087 friend class YouBotJoint;
00088 friend class YouBotGripper;
00089 friend class YouBotGripperBar;
00090   private:
00091     EthercatMasterWithoutThread(const std::string& configFile, const std::string& configFilePath);
00092 
00093     ~EthercatMasterWithoutThread();
00094 
00095 
00096   public:
00097     bool isThreadActive();
00098 
00099     ///return the quantity of ethercat slave which have an input/output buffer
00100     unsigned int getNumberOfSlaves() const;
00101 
00102     void AutomaticSendOn(const bool enableAutomaticSend);
00103 
00104     void AutomaticReceiveOn(const bool enableAutomaticReceive);
00105 
00106     ///provides all ethercat slave informations from the SOEM driver
00107     ///@param ethercatSlaveInfos ethercat slave informations
00108     void getEthercatDiagnosticInformation(std::vector<ec_slavet>& ethercatSlaveInfos);
00109 
00110     ///sends ethercat messages to the motor controllers
00111     /// returns a true if everything it OK and returns false if something fail
00112     bool sendProcessData();
00113 
00114     /// receives ethercat messages from the motor controllers
00115     /// returns a true if everything it OK and returns false if something fail
00116     bool receiveProcessData();
00117 
00118     /// checks if an error has occurred in the soem driver
00119     /// returns a true if an error has occurred
00120     bool isErrorInSoemDriver();
00121 
00122     bool isEtherCATConnectionEstablished();
00123 
00124     void registerJointLimitMonitor(JointLimitMonitor* object, const unsigned int JointNumber);
00125 
00126 
00127   private:
00128     ///establishes the ethercat connection
00129     void initializeEthercat();
00130 
00131     ///closes the ethercat connection
00132     bool closeEthercat();
00133 
00134     ///stores a ethercat message to the buffer
00135     ///@param msgBuffer ethercat message
00136     ///@param jointNumber joint number of the sender joint
00137     void setMsgBuffer(const YouBotSlaveMsg& msgBuffer, const unsigned int jointNumber);
00138 
00139     ///get a ethercat message form the buffer
00140     ///@param msgBuffer ethercat message
00141     ///@param jointNumber joint number of the receiver joint
00142     void getMsgBuffer(const unsigned int jointNumber, YouBotSlaveMsg& returnMsg);
00143 
00144     ///stores a mailbox message in a buffer which will be sent to the motor controllers
00145     ///@param msgBuffer ethercat mailbox message
00146     ///@param jointNumber joint number of the sender joint
00147     void setMailboxMsgBuffer(const YouBotSlaveMailboxMsg& msgBuffer, const unsigned int jointNumber);
00148 
00149     ///gets a mailbox message form the buffer which came form the motor controllers
00150     ///@param msgBuffer ethercat mailbox message
00151     ///@param jointNumber joint number of the receiver joint
00152     bool getMailboxMsgBuffer(YouBotSlaveMailboxMsg& mailboxMsg, const unsigned int jointNumber);
00153 
00154     ///sends the mailbox Messages which have been stored in the buffer
00155     ///@param mailboxMsg ethercat mailbox message
00156     bool sendMailboxMessage(const YouBotSlaveMailboxMsg& mailboxMsg);
00157 
00158     ///receives mailbox messages and stores them in the buffer
00159     ///@param mailboxMsg ethercat mailbox message
00160     bool receiveMailboxMessage(YouBotSlaveMailboxMsg& mailboxMsg);
00161 
00162     void parseYouBotErrorFlags(const YouBotSlaveMsg& messageBuffer);
00163 
00164     std::string ethernetDevice;
00165 
00166     std::vector<YouBotSlaveMsg> processDataBuffer;
00167 
00168     unsigned int nrOfSlaves;
00169 
00170     std::vector<SlaveMessageOutput*> ethercatOutputBufferVector;
00171 
00172     std::vector<SlaveMessageInput*> ethercatInputBufferVector;
00173 
00174     std::vector<YouBotSlaveMailboxMsg> firstMailboxBufferVector;
00175 
00176     ec_mbxbuft mailboxBufferSend;
00177 
00178     unsigned int mailboxTimeout;
00179 
00180     ec_mbxbuft mailboxBufferReceive;
00181 
00182     ConfigFile* configfile;
00183 
00184     std::vector<ec_slavet> ethercatSlaveInfo;
00185 
00186     char IOmap_[4096];
00187 
00188     unsigned int ethercatTimeout;
00189 
00190     static std::string configFileName;
00191 
00192     static std::string configFilepath;
00193 
00194     bool ethercatConnectionEstablished;
00195 
00196 };
00197 
00198 } // namespace youbot
00199 #endif
Generated by  doxygen 1.6.3