32 #include "transmit_pdo_mapping.h"
36 #include "dictionary_error.h"
43 uint16_t cob_id_, TransmissionType transmission_type_, std::chrono::milliseconds repeat_time_,
const std::vector<Mapping>& mappings_)
45 transmission_type(transmission_type_),
46 repeat_time(repeat_time_),
49 m_dictionary(dictionary),
50 m_name_to_address(name_to_address)
64 std::vector<uint8_t> data(8,0);
67 DEBUG_LOG(
"[TransmitPDOMapping::send] Sending transmit PDO with cob_id 0x"<<std::hex<<
cob_id);
71 const std::string entry_name =
Utils::escape(mapping.entry_name);
72 const Entry& entry = m_dictionary.at(m_name_to_address.at(entry_name));
74 const std::vector<uint8_t> bytes = value.
get_bytes();
75 assert(mapping.offset+bytes.size() <= 8);
77 DEBUG_LOG(
"[TransmitPDOMapping::send] Mapping:");
78 DEBUG_DUMP(mapping.offset);
79 DEBUG_DUMP(entry_name);
80 DEBUG_DUMP_HEX(value);
83 for (uint8_t i=mapping.offset; i<mapping.offset+bytes.size(); ++i) {
84 data[i] = bytes[count++];
87 max_byte = std::max(max_byte, mapping.offset+bytes.size());
91 data.resize(max_byte+1);
96 void TransmitPDOMapping::check_correctness()
const {
99 std::vector<bool> byte_mapped(8,
false);
103 const std::string entry_name =
Utils::escape(mapping.entry_name);
105 if (m_name_to_address.count(entry_name) == 0) {
109 const Entry& entry = m_dictionary.at(m_name_to_address.at(entry_name));
112 if (mapping.offset+type_size > 8) {
113 throw dictionary_error(dictionary_error::type::mapping_size, entry_name,
114 "mapping.offset ("+std::to_string(mapping.offset)+
") + type_size ("+std::to_string(type_size)+
") > 8.");
117 for (uint8_t i=mapping.offset; i<mapping.offset+type_size; ++i) {
118 if (byte_mapped[i]) {
119 throw dictionary_error(dictionary_error::type::mapping_overlap, entry_name,
"Byte index: "+std::to_string(i));
121 byte_mapped[i] =
true;
129 throw dictionary_error(dictionary_error::type::mapping_size,
"",
130 "total size ("+std::to_string(size)+
") > 8.");
This type of exception is thrown if there are problems accessing the object dictionary or arguments d...
PDO pdo
The PDO sub-protocol.
const Value & get_value() const
Returns the value.
std::vector< uint8_t > get_bytes() const
Returns the byte representation (little-endian) as a vector.
void send(uint16_t cob_id, const std::vector< uint8_t > &data)
Sends a PDO message.
~TransmitPDOMapping()
Stops the transmitter thread if there is one.
static std::string escape(const std::string &str)
Converts entry names to lower case and replaces all spaces and '-' by underscores.
std::unique_ptr< std::thread > transmitter
The transmitter thread.
void send() const
Sends the PDO.
This class implements the Core of KaCanOpen It communicates with the CAN driver, sends CAN messages a...
uint16_t cob_id
COB-ID of the PDO.
This class represents an entry in the object dictionary of a device.
std::vector< Mapping > mappings
Mapped entries with offset (see Mapping class)
TransmitPDOMapping(Core &core, const std::unordered_map< Address, Entry > &dictionary, const std::unordered_map< std::string, Address > &name_to_address, uint16_t cob_id_, TransmissionType transmission_type_, std::chrono::milliseconds repeat_time_, const std::vector< Mapping > &mappings_)
Constructor.
This struct represents one mapped entry inside a TransmitPDOMapping.
static uint8_t get_type_size(Type type)
Returns the size of a data type in bytes.
This class contains a value to be stored in the object dictionary. The value can have one of the type...