1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*      File: BK1150.h
 *       This file is part of the program ethercatcpp-core
 *       Program description : EtherCAT driver libraries for UNIX
 *       Copyright (C) 2017-2024 -  Robin Passama (LIRMM / CNRS) Arnaud Meline
 * (LIRMM / CNRS) Benjamin Navarro (LIRMM / CNRS). All Right reserved.
 *
 *       This software is free software: you can redistribute it and/or modify
 *       it under the terms of the CeCILL-C license as published by
 *       the CEA CNRS INRIA, either version 1
 *       of the License, or (at your option) any later version.
 *       This software is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *       CeCILL-C License for more details.
 *
 *       You should have received a copy of the CeCILL-C License
 *       along with this software. If not, it can be found on the official
 * website of the CeCILL licenses family (http://www.cecill.info/index.en.html).
 */
/**
 * @file beckhoff_terminals/BK1150.h
 * @author Robin Passama
 * @brief EtherCAT driver for a generic beckhoff BK1150 device.
 * @date February 2020.
 * @ingroup ethercatcpp-core
 * @example beckhoff_BK1150_KL2284_example.cpp
 */
#pragma once

#include <ethercatcpp/beckhoff_terminals/KL_extensions.h>
#include <ethercatcpp/core.h>
#include <memory>

/*! \namespace ethercatcpp
 *
 */
namespace ethercatcpp {

/** @brief This class describe the EtherCAT driver for a beckhoff BK1150 slave
 *
 * @details This driver allows to communicate with a beckhoff BK1150 through an
 * EtherCAT bus. The BK1150 EtherCAT Terminal is a generic ethercat slave whose
 * capabilities depend on extensions plugged on its internal real-time BUS. It
 * can be specialized whenever a set of specific KL extensions are plugged on
 * its internal BUS or it can also be created dynamically by adding KL
 * extensions.
 * @see KLExtensionCard
 */
class BK1150 : public SlaveDevice {
public:
    /**
     * @brief Constructor of BK1150 class
     */
    BK1150();
    ~BK1150() = default;

    /**
     * @brief get the module present on BK1150 internal bus at given index
     *
     * @param index the index of the extension module
     * @return a shared pointer to the extension module
     */
    const std::shared_ptr<KLExtensionCard>& extension(unsigned int index) const;

    /**
     * @brief get the module present on BK1150 internal bus at given index
     * @tparam KLType the type of the module
     * @param index the index of the extension module
     * @return a shared pointer to the extension module with specialized type
     */
    template <typename KLType>
    const std::shared_ptr<KLType>& extension(unsigned int index) const {
        return (std::dynamic_pointer_cast<KLType>(extension(index)));
    }

    /**
     * @brief Adding an extension module to the internal bus
     *
     * @tparam Extension type of the extension module
     * @tparam Args types of arguments to pass at module construction time
     * @param args arguments to pass at module construction time
     * @return a specialized shared pointer to the extension created
     */
    template <typename Extension, typename... Args>
    std::shared_ptr<Extension> add_Extension(Args&&... args) {
        auto ext = std::make_shared<Extension>(args...);
        ext->set_holder(this, extensions_.size());
        if (ext->digital()) {
            digital_extensions_.push_back(ext);
        } else {
            non_digital_extensions_.push_back(ext);
        }
        extensions_.push_back(
            ext); // Note: usefull for end user to get extension
                  // in same order as in the physical K BUS
        return (ext);
    }

    /**
     * @brief initialize the internal bus.
     *
     */
    void init();

private:
    friend class KLExtensionCard;

    std::vector<std::shared_ptr<KLExtensionCard>> extensions_;
    std::vector<std::shared_ptr<KLExtensionCard>> digital_extensions_;
    std::vector<std::shared_ptr<KLExtensionCard>> non_digital_extensions_;

    void update_command_buffer();
    void unpack_status_buffer();

    uint8_t* output(uint8_t index, uint8_t& bit_shift);
    uint8_t* input(uint8_t index, uint8_t& bit_shift);
};

} // namespace ethercatcpp