libcpp4ec
C++ for EtherCat Library
 All Classes Functions Variables Typedefs Enumerations Enumerator
EcMaster.h
1 #ifndef ECMASTER_H
2 #define ECMASTER_H
3 
4 #include "EcSlave.h"
5 #include "EcSlaveFactory.h"
6 #include "EcError.h"
7 
8 
9 extern "C"
10 {
11 #include <soem/ethercattype.h>
12 #include <soem/ethercatbase.h>
13 #include <soem/ethercatmain.h>
14 #include <soem/ethercatconfig.h>
15 #include <soem/ethercatdc.h>
16 #include <soem/ethercatcoe.h>
17 #include <soem/ethercatsoe.h>
18 #include <soem/ethercatprint.h>
19 #include <soem/nicdrv.h>
20 }
21 
22 #include <vector>
23 #include <string>
24 #include <sstream>
25 #include <sys/time.h>
26 #include <time.h>
27 #include <cstdio>
28 #include <unistd.h>
29 #include <stdint.h>
30 #include <mutex>
31 #include <thread>
32 
33 #define NSEC_PER_SEC 1000000000
34 
35 
36 
37 namespace cpp4ec
38 {
46 class EcMaster
47 {
48 
49 public:
50 
54  EcMaster();
55 
64  EcMaster(std::string ecPort, unsigned long cycleTime = 1000000, bool useDC = false);
65 
69  ~EcMaster();
70 
76  void setPort(std::string ecPort) {ethPort=ecPort;}
77 
83  void setCycleTime(unsigned long cycleTime) {m_cycleTime = cycleTime;}
84 
90  void setDC( bool useDC) {m_useDC = useDC;}
91 
97  void setSync0Shift( int Shift) {sync0Shift = Shift;}
98 
104  void setSlaveInfo(bool slaveInfo) {slaveInformation = slaveInfo;}
105 
112  bool preconfigure() throw(EcError);
113 
120  bool configure() throw(EcError);
121 
127  bool start() throw(EcError);
128 
134  bool stop() throw(EcError);
135 
141  bool reset() throw(EcError);
142 
148  std::vector<EcSlave*> getSlaves() {return m_drivers;}
149 
155  void update(void) throw(EcError);
156 
171 private:
178  //Master variables
179  std::string ethPort;
180  char * m_ecPort;
181  char m_IOmap[4096];
182  unsigned long m_cycleTime; //the periodicity of ethercatLoop ("PDOs period")
183  int sync0Shift;
184  bool m_useDC;
185  std::vector<EcSlave*> m_drivers;
186 #if defined(HRT) || defined(NRT)
187  char * inputBuf;
188  char * outputBuf;
189  int inputSize, outputSize;
190 #endif
191  bool SGDVconnected;
192 
193  //ethercat switchState function
194  bool switchState (ec_state state); //switch the state of state machine--returns true if the state is reached
195 
196 #ifdef HRT
197  //Sockect stuff
198  char * devnameOutput;
199  char * devnameInput;
200  int fdOutput,fdInput;
201 #endif
202 #if defined(HRT) || defined(NRT)
203  bool threadFinished;
204  std::thread updateThread;
205  int NRTtaskFinished;
206  void update_EcSlaves(void) throw(EcError);
207 #endif
208 
209  //Ethercat slaveinfo stuff
210  bool slaveInformation;
211  FILE * pFile;
212  ec_ODlistt ODlist;
213  ec_OElistt OElist;
214  char usdo[128];
215  char hstr[1024];
216  bool printSDO;
217  bool printMAP;
218  void slaveInfo(void);
219  char* dtype2string(uint16 dtype);
220  char* SDO2string(uint16 slave, uint16 index, uint8 subidx, uint16 dtype);
221  int si_PDOassign(uint16 slave, uint16 PDOassign, int mapoffset, int bitoffset);
222  int si_siiPDO(uint16 slave, uint8 t, int mapoffset, int bitoffset);
223  int si_map_sdo(int slave);
224  int si_map_sii(int slave);
225  void si_sdo(int cnt);
226 };
227 }
228 #endif
229 
EcMaster()
Default Constructor.
Definition: EcNRTMaster.cpp:24
Class EcSlave.
Definition: EcSlave.h:53
bool start()
Starts comunication.
Definition: EcNRTMaster.cpp:186
void update(void)
Update the outputs.
Definition: EcNRTMaster.cpp:200
void setSync0Shift(int Shift)
Set Sync0 Shift.
Definition: EcMaster.h:97
std::vector< EcSlave * > getSlaves()
Get slaves vector.
Definition: EcMaster.h:148
Class EcError.
Definition: EcError.h:13
void setPort(std::string ecPort)
Set the ethercat port.
Definition: EcMaster.h:76
bool preconfigure()
Preconfiguration.
Definition: EcNRTMaster.cpp:63
void setSlaveInfo(bool slaveInfo)
Set slave information.
Definition: EcMaster.h:104
bool configure()
Configuration.
Definition: EcNRTMaster.cpp:125
void setCycleTime(unsigned long cycleTime)
Set the cycle time of the PDO thread.
Definition: EcMaster.h:83
bool stop()
Stops communication.
Definition: EcNRTMaster.cpp:236
~EcMaster()
Destructor.
Definition: EcNRTMaster.cpp:58
bool reset()
Resets configuration.
Definition: EcNRTMaster.cpp:248
Class EcMaster.
Definition: EcMaster.h:46
void setDC(bool useDC)
Set Distributed clocks.
Definition: EcMaster.h:90
Definition: EcMaster.h:37