sr_hand_detector.cpp
Go to the documentation of this file.
1 /*
2 * Copyright 2020 Shadow Robot Company Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 
17 #include <iostream>
18 #include <string>
19 #include <map>
20 #include <utility>
21 #include <algorithm>
22 #include <ifaddrs.h>
23 #include <unistd.h>
25 
26 
27 namespace sr_hand_detector
28 {
30 {
31  std::cout << "Starting hand detector..." << std::endl;
32 }
33 
35 {
36 }
37 
39 {
42 }
43 
45 {
46  struct ifaddrs *ifaddr, *ifa;
47  int n;
48 
49  if (-1 == getifaddrs(&ifaddr))
50  {
51  perror("getifaddrs");
52  exit(EXIT_FAILURE);
53  }
54 
55  for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++)
56  {
57  if (ifa->ifa_addr == NULL || count(available_port_names_.begin(),
59  ifa->ifa_name))
60  {
61  continue;
62  }
63  available_port_names_.push_back(ifa->ifa_name);
64  }
65 
66  freeifaddrs(ifaddr);
67 }
68 
70 {
71  for (auto const& port_name : available_port_names_)
72  {
74  {
75  int hand_serial = get_hand_serial(port_name);
76  hand_serial_to_port_map_.insert(std::pair<int, std::string>(hand_serial, port_name));
77  }
78  }
79 }
80 
81 int SrHandDetector::count_slaves_on_port(std::string port_name)
82 {
83  int w;
84  char *port_name_c_str = &port_name[0];
85  if (ec_init(port_name_c_str))
86  {
87  return ec_BRD(0x0000, ECT_REG_TYPE, sizeof(w), &w, EC_TIMEOUTSAFE);
88  }
89  else
90  {
91  return 0;
92  }
93 }
94 
95 int SrHandDetector::get_hand_serial(std::string port_name)
96 {
97  uint16 *wbuf;
98  int hand_serial;
99  char *port_name_c_str = &port_name[0];
100  if (ec_init(port_name_c_str))
101  {
102  read_eeprom(SLAVE_WITH_HAND_SERIAL_, 0x0000, 128);
103 
104  wbuf = reinterpret_cast<uint16 *>(&ebuf_[0]);
105  hand_serial = *reinterpret_cast<uint32 *>(wbuf + 0x0E);
106 
107  ec_close();
108  return hand_serial;
109  }
110  else
111  {
112  std::cout << "No socket connection on " << port_name_c_str << ". Excecute as root.";
113  return 0;
114  }
115 }
116 
117 int SrHandDetector::read_eeprom(int slave, int start, int length)
118 {
119  int i, wkc, ainc = 4;
120  uint16 estat, aiadr;
121  uint32 b4;
122  uint64 b8;
123  uint8 eepctl;
124 
125  aiadr = 1 - slave;
126  eepctl = 2;
127  // force Eeprom from PDI
128  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET);
129  eepctl = 0;
130  // set Eeprom to master
131  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET);
132 
133  estat = 0x0000;
134  aiadr = 1 - slave;
135  // read eeprom status
136  wkc = ec_APRD(aiadr, ECT_REG_EEPSTAT, sizeof(estat), &estat, EC_TIMEOUTRET);
137  estat = etohs(estat);
138  if (estat & EC_ESTAT_R64)
139  {
140  ainc = 8;
141  for (i = start ; i < (start + length) ; i+=ainc)
142  {
143  b8 = ec_readeepromAP(aiadr, i >> 1 , EC_TIMEOUTEEP);
144  ebuf_[i] = b8;
145  ebuf_[i+1] = b8 >> 8;
146  ebuf_[i+2] = b8 >> 16;
147  ebuf_[i+3] = b8 >> 24;
148  ebuf_[i+4] = b8 >> 32;
149  ebuf_[i+5] = b8 >> 40;
150  ebuf_[i+6] = b8 >> 48;
151  ebuf_[i+7] = b8 >> 56;
152  }
153  }
154  else
155  {
156  for (i = start ; i < (start + length) ; i+=ainc)
157  {
158  b4 = ec_readeepromAP(aiadr, i >> 1 , EC_TIMEOUTEEP);
159  ebuf_[i] = b4;
160  ebuf_[i+1] = b4 >> 8;
161  ebuf_[i+2] = b4 >> 16;
162  ebuf_[i+3] = b4 >> 24;
163  }
164  }
165 
166  return 1;
167 }
168 
169 std::map<int, std::string> SrHandDetector::get_hand_serial_to_port()
170 {
172 }
173 } // namespace sr_hand_detector
ECT_REG_TYPE
@ ECT_REG_TYPE
Definition: ethercattype.h:415
EC_TIMEOUTSAFE
#define EC_TIMEOUTSAFE
Definition: ethercattype.h:94
sr_hand_detector::SrHandDetector::available_port_names_
std::vector< std::string > available_port_names_
Definition: sr_hand_detector.h:48
sr_hand_detector::SrHandDetector::NUM_OF_SLAVES_EXPECTED_FOR_HAND_
const int NUM_OF_SLAVES_EXPECTED_FOR_HAND_
Definition: sr_hand_detector.h:51
etohs
#define etohs(A)
Definition: ethercattype.h:550
sr_hand_detector::SrHandDetector::SrHandDetector
SrHandDetector()
Definition: sr_hand_detector.cpp:29
sr_hand_detector::SrHandDetector::get_hand_serial
int get_hand_serial(std::string)
Definition: sr_hand_detector.cpp:95
uint32
uint32_t uint32
Definition: osal.h:35
sr_hand_detector::SrHandDetector::get_available_port_names
void get_available_port_names()
Definition: sr_hand_detector.cpp:44
sr_hand_detector::SrHandDetector::get_hands_ports_and_serials
void get_hands_ports_and_serials()
Definition: sr_hand_detector.cpp:69
sr_hand_detector::SrHandDetector::hand_serial_to_port_map_
std::map< int, std::string > hand_serial_to_port_map_
Definition: sr_hand_detector.h:47
uint8
uint8_t uint8
Definition: osal.h:33
uint64
uint64_t uint64
Definition: osal.h:37
EC_TIMEOUTRET
#define EC_TIMEOUTRET
Definition: ethercattype.h:90
sr_hand_detector
Definition: sr_hand_autodetect.h:27
sr_hand_detector::SrHandDetector::SLAVE_WITH_HAND_SERIAL_
const int SLAVE_WITH_HAND_SERIAL_
Definition: sr_hand_detector.h:50
sr_hand_detector::SrHandDetector::get_hand_serial_to_port
std::map< int, std::string > get_hand_serial_to_port()
Definition: sr_hand_detector.cpp:169
ECT_REG_EEPCFG
@ ECT_REG_EEPCFG
Definition: ethercattype.h:430
sr_hand_detector::SrHandDetector::~SrHandDetector
~SrHandDetector()
Definition: sr_hand_detector.cpp:34
sr_hand_detector.h
uint16
uint16_t uint16
Definition: osal.h:34
sr_hand_detector::SrHandDetector::read_eeprom
int read_eeprom(int, int, int)
Definition: sr_hand_detector.cpp:117
EC_ESTAT_R64
#define EC_ESTAT_R64
Definition: ethercattype.h:293
sr_hand_detector::SrHandDetector::count_slaves_on_port
int count_slaves_on_port(std::string)
Definition: sr_hand_detector.cpp:81
start
ROSCPP_DECL void start()
sr_hand_detector::SrHandDetector::run
void run()
Definition: sr_hand_detector.cpp:38
EC_TIMEOUTEEP
#define EC_TIMEOUTEEP
Definition: ethercattype.h:96
ECT_REG_EEPSTAT
@ ECT_REG_EEPSTAT
Definition: ethercattype.h:432
sr_hand_detector::SrHandDetector::ebuf_
uint8 ebuf_[MAXBUF]
Definition: sr_hand_detector.h:49


sr_hand_detector
Author(s):
autogenerated on Sat Sep 24 2022 02:26:10