PDO.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of the SCHUNK Canopen Driver suite.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
12 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
23 //----------------------------------------------------------------------
24 
25 #include "ds301.h"
26 #include "PDO.h"
27 
28 #include "Logging.h"
29 #include "exceptions.h"
30 
31 #include <string.h>
32 
33 namespace icl_hardware {
34 namespace canopen_schunk {
35 
36 PDO::PDO(const uint8_t node_id,
37  const uint8_t pdo_nr,
39  : m_node_id(node_id),
40  m_pdo_nr(pdo_nr),
41  m_can_device(can_device)
42 {
43 }
44 
46  const MappingConfigurationList& mappings,
47  const eTransmissionType& transmission_type,
48  const uint16_t pdo_cob_id,
49  const uint16_t pdo_communication_parameter,
50  const uint16_t pdo_mapping_parameter,
51  const bool dummy_mapping,
52  const uint8_t cyclic_timeout_cycles
53  )
54 {
55  m_mapping_list.clear();
56  PDO::PDOStringMatchVec ret_vec;
57 
58  // if no mapping is given, we leave the mapping as is.
59  if (mappings.size() == 0)
60  {
61  LOGGING_INFO_C (CanOpen, PDO, "Remapping called with empty mapping. Not doing anything." << endl);
62  return PDO::PDOStringMatchVec();
63  }
64 
65  // A maximum of 64 mappings are supported.
66  // See DS301 Specification Section 7.5.2.36 (Object 0x1600 to 0x17FF). Sub Index ranges to 0x40 resulting in 64 maximum mappable values
67  if (mappings.size() > 64)
68  {
69  std::stringstream ss;
70  ss << "Illegal number of mappings given. A maximum of 64 mappings is allowed. " <<
71  "However, " << mappings.size() << " mappings were given.";
72  throw PDOException (ss.str());
73  }
74 
75  // set the full transmission type
76  uint8_t transmission_type_int = static_cast<uint8_t>(transmission_type);
77  if (transmission_type == SYNCHRONOUS_CYCLIC)
78  {
79  transmission_type_int += cyclic_timeout_cycles;
80  }
81 
82  // establish rpdo communication in index 0x1400 / 0x01
83  uint16_t index = pdo_communication_parameter;
84  uint8_t subindex = 0x01;
85  std::vector<uint8_t> data(4, 0);
86 
87  // disable this PDO
88  data[0] = pdo_cob_id & 0xff;
89  data[1] = pdo_cob_id >> 8;
90  data[2] = 0x00;
91  data[3] = 0x80;
92 
93  if (!dummy_mapping)
94  {
95  try
96  {
97  sdo.download(false, index, subindex, data);
98  }
99  catch (const std::exception& e)
100  {
101  std::stringstream ss;
102  ss << "Downloading PDO communication object failed! ";
103  ss << e.what();
104  throw PDOException (ss.str());
105  }
106  }
107 
108  // clear PDO
109  index = pdo_mapping_parameter;
110  subindex = 0x00;
111  uint8_t clear_data = 0;
112 
113  if (!dummy_mapping)
114  {
115  try
116  {
117  sdo.download(false, index, subindex, clear_data);
118  }
119  catch (const std::exception& e)
120  {
121  std::stringstream ss;
122  ss << "Clearing PDO mapping failed! ";
123  ss << e.what();
124  throw PDOException (ss.str());
125  }
126  }
127 
128 
129  // perform mapping
130  index = pdo_mapping_parameter;
131  subindex = 0;
132  uint8_t cumul_length = 0;
133  for (size_t i = 0; i < mappings.size(); ++i)
134  {
135  ++subindex;
136  data[0] = mappings[i].length;
137  data[1] = mappings[i].subindex;
138  data[2] = mappings[i].index & 0xff;
139  data[3] = mappings[i].index >> 8;
140  cumul_length += mappings[i].length;
141 
142  PDOStringMatch match;
143  match.name = mappings[i].name;
144  match.pdo_mapping_index = i;
145  ret_vec.push_back(match);
146 
147  if (cumul_length > 64)
148  {
149  throw PDOException ("The configured length of the PDO mapping is too big. To send a PDO in one CAN frame its size cannot be larger than 64 bit");
150 
151  //TODO: Currently this will leave a disabled PDO. We could as well use the PDO with the mappings so far and just drop a warning...
152  }
153 
154  LOGGING_DEBUG (CanOpen, "Mapping " << hexToString(mappings[i].index) << " / " << static_cast<int>(mappings[i].subindex) << endl );
155  if (!dummy_mapping)
156  {
157  sdo.download(false, index, subindex, data);
158  }
159 
160  m_mapping_list.push_back(Mapping(mappings[i]));
161  }
162 
163  index = pdo_mapping_parameter;
164  subindex = 0x00;
165  uint8_t num_mappings = mappings.size();
166 
167  if (!dummy_mapping)
168  {
169  try
170  {
171  sdo.download(false, index, subindex, num_mappings);
172  }
173  catch (const std::exception& e)
174  {
175  std::stringstream ss;
176  ss << "Setting number of mappings failed! ";
177  ss << e.what();
178  throw PDOException (ss.str());
179  }
180  }
181 
182  // set PDO's transmission type
183  index = pdo_communication_parameter;
184  subindex = 0x02;
185  if (!dummy_mapping)
186  {
187  try
188  {
189  sdo.download(false, index, subindex, transmission_type_int);
190  }
191  catch (const std::exception& e)
192  {
193  std::stringstream ss;
194  ss << "Downloading PDO communication object failed! ";
195  ss << e.what();
196  throw PDOException (ss.str());
197  }
198  }
199 
200  // enable this PDO
201  index = pdo_communication_parameter;
202  subindex = 0x01;
203  data[0] = pdo_cob_id & 0xff;
204  data[1] = pdo_cob_id >> 8;
205  data[2] = 0x00;
206  data[3] = 0x00;
207 
208  if (!dummy_mapping)
209  {
210  try
211  {
212  sdo.download(false, index, subindex, data);
213  }
214  catch (const std::exception& e)
215  {
216  std::stringstream ss;
217  ss << "Enabling PDO object failed! ";
218  ss << e.what();
219  throw PDOException (ss.str());
220  }
221  }
222 
223  LOGGING_DEBUG (CanOpen, "Remapping for node " << static_cast<int>(m_node_id) << " finished!" << endl);
224 
225  return ret_vec;
226 }
227 
228 
230  const MappingConfigurationList& mappings,
231  const eTransmissionType& transmission_type,
232  const uint16_t pdo_cob_id,
233  const uint16_t pdo_communication_parameter,
234  const uint16_t pdo_mapping_parameter,
235  const bool dummy_mapping,
236  const uint8_t cyclic_timeout_cycles)
237 {
238  // Create MappingConfiguration from m_mapping_list
239  MappingConfigurationList new_configuration;
240  uint8_t cum_length = 0;
241  for (MappingList::iterator it = m_mapping_list.begin(); it != m_mapping_list.end(); ++it)
242  {
243  new_configuration.push_back(it->getConfiguration());
244  cum_length += it->getConfiguration().length;
245  }
246 
247  // append new mapping to old mappingConfiguration
248  uint8_t new_cum_length = 0;
249  for (MappingConfigurationList::const_iterator it = mappings.begin(); it != mappings.end(); ++it)
250  {
251  new_cum_length += it->length;
252  new_configuration.push_back(*it);
253  }
254 
255  // Check, if there's enough space left in the PDO
256  if (cum_length + new_cum_length > 64)
257  {
258  std::stringstream ss;
259  ss << "The requested length of the PDO mapping is too big. "<<
260  "To send a PDO in one CAN frame its size cannot be larger than 64 bit." <<
261  "Please append this configuration to another PDO.";
262  throw PDOException (ss.str());
263  }
264 
265  // call remap
266  return remap(sdo,
267  new_configuration,
268  transmission_type,
269  pdo_cob_id,
270  pdo_communication_parameter,
271  pdo_mapping_parameter,
272  cyclic_timeout_cycles);
273 }
274 
275 
276 
277 }}//end of NS
#define LOGGING_INFO_C(streamname, classname, arg)
MappingList m_mapping_list
List of all mappings inside this PDO.
Definition: PDO.h:216
std::vector< MappingConfiguration > MappingConfigurationList
The MappingConfigurationList holds multiple Mapping configurations. The Mapping of a single PDO is de...
Definition: PDO.h:70
#define LOGGING_DEBUG(streamname, arg)
bool download(const bool normal_transfer, const uint16_t index, const uint8_t subindex, const std::vector< uint8_t > &usrdata)
Downloads SDO data from the master to the slave (From PC to node).
Definition: SDO.cpp:84
PDO related exceptions go here.
Definition: exceptions.h:114
uint8_t m_node_id
CANOPEN ID of the node this PDO belongs to.
Definition: PDO.h:220
PDOStringMatchVec remap(SDO &sdo, const MappingConfigurationList &mappings, const eTransmissionType &transmission_type, const uint16_t pdo_cob_id, const uint16_t pdo_communication_parameter, const uint16_t pdo_mapping_parameter, const bool dummy_mapping=false, const uint8_t cyclic_timeout_cycles=0)
Configure a PDO by sending some SDO packages. This can be either done during NMT state pre-operationa...
Definition: PDO.cpp:45
PDO(const uint8_t node_id, const uint8_t pdo_nr, const CanDevPtr &can_device)
Construct a new PDO.
Definition: PDO.cpp:36
eTransmissionType
Transmission types of a PDO, needed when mapping PDOs.
Definition: PDO.h:122
unsigned char uint8_t
ThreadStream & endl(ThreadStream &stream)
Unique index to find a mapped Object dictionary item in a PDO.
Definition: PDO.h:114
std::vector< PDOStringMatch > PDOStringMatchVec
Definition: PDO.h:119
PDOStringMatchVec appendMapping(SDO &sdo, const MappingConfigurationList &mappings, const eTransmissionType &transmission_type, const uint16_t pdo_cob_id, const uint16_t pdo_communication_parameter, const uint16_t pdo_mapping_parameter, const bool dummy_mapping=false, const uint8_t cyclic_timeout_cycles=0)
Appends one or more mapping parameters to the existing mapping. Note that the PDO will be disabled wh...
Definition: PDO.cpp:229
The PDO class provides access to one (of the possible multiple) Process Data Object of a canOpen node...
Definition: PDO.h:44
The SDO class represents Service Data Objects (SDO) that are used for slow access of the canOpen obje...
Definition: SDO.h:40
Holds the mapping parameter plus the actual data.
Definition: PDO.h:84
std::string hexToString(const uint64_t num)
Converts a hexadecimal number into its string representation 0xXX.
Definition: helper.cpp:33
unsigned short uint16_t


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Mon Jun 10 2019 15:07:49