Program Listing for File SVHControllerFeedback.h

Return to documentation for file (/tmp/ws/src/schunk_svh_library/include/schunk_svh_library/control/SVHControllerFeedback.h)

//
// © Copyright 2022 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
// © Copyright 2022 FZI Forschungszentrum Informatik, Karlsruhe, Germany
//
// This file is part of the Schunk SVH Library.
//
// The Schunk SVH Library is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// The Schunk SVH Library 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 GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// the Schunk SVH Library. If not, see <https://www.gnu.org/licenses/>.
//

//----------------------------------------------------------------------
//----------------------------------------------------------------------
#ifndef SVHCONTROLLERFEEDBACK_H
#define SVHCONTROLLERFEEDBACK_H

#include <schunk_svh_library/serial/ByteOrderConversion.h>

namespace driver_svh {

struct SVHControllerFeedback
{
  int32_t position;
  int16_t current;

  SVHControllerFeedback(const int32_t& position = 0, const int16_t& current = 0)
    : position(position)
    , current(current)
  {
  }

  bool operator==(const SVHControllerFeedback& other) const
  {
    return (position == other.position && current == other.current);
  }
};

struct SVHControllerFeedbackAllChannels
{
  std::vector<SVHControllerFeedback> feedbacks;

  SVHControllerFeedbackAllChannels(const SVHControllerFeedback& feedback0,
                                   const SVHControllerFeedback& feedback1,
                                   const SVHControllerFeedback& feedback2,
                                   const SVHControllerFeedback& feedback3,
                                   const SVHControllerFeedback& feedback4,
                                   const SVHControllerFeedback& feedback5,
                                   const SVHControllerFeedback& feedback6,
                                   const SVHControllerFeedback& feedback7,
                                   const SVHControllerFeedback& feedback8)
  {
    feedbacks.push_back(feedback0);
    feedbacks.push_back(feedback1);
    feedbacks.push_back(feedback2);
    feedbacks.push_back(feedback3);
    feedbacks.push_back(feedback4);
    feedbacks.push_back(feedback5);
    feedbacks.push_back(feedback6);
    feedbacks.push_back(feedback7);
    feedbacks.push_back(feedback8);
  }

  SVHControllerFeedbackAllChannels(std::vector<SVHControllerFeedback> feedbacks)
  {
    feedbacks.insert(feedbacks.begin(), feedbacks.begin(), feedbacks.end());
  }

  SVHControllerFeedbackAllChannels()
    : feedbacks(9)
  {
  }


  bool operator==(const SVHControllerFeedbackAllChannels& other) const
  {
    return (feedbacks == other.feedbacks);
  }
};

inline driver_svh::ArrayBuilder& operator<<(driver_svh::ArrayBuilder& ab,
                                            const SVHControllerFeedback& data)
{
  ab << data.position << data.current;
  return ab;
}


inline driver_svh::ArrayBuilder& operator>>(driver_svh::ArrayBuilder& ab,
                                            SVHControllerFeedback& data)
{
  ab >> data.position >> data.current;
  return ab;
}

inline std::ostream& operator<<(std::ostream& o, const SVHControllerFeedback& cf)
{
  o << "Pos: " << cf.position << " Cur: " << cf.current << std::endl;
  return o;
}


inline driver_svh::ArrayBuilder& operator<<(driver_svh::ArrayBuilder& ab,
                                            SVHControllerFeedbackAllChannels& data)
{
  // The Data is transmitted not channel by channel but rather position first, Currents afterwards
  // for all channels
  for (std::vector<SVHControllerFeedback>::iterator it = data.feedbacks.begin();
       it != data.feedbacks.end();
       ++it)
  {
    ab << it->position;
  }

  for (std::vector<SVHControllerFeedback>::iterator it = data.feedbacks.begin();
       it != data.feedbacks.end();
       ++it)
  {
    ab << it->current;
  }
  return ab;
}


inline driver_svh::ArrayBuilder& operator>>(driver_svh::ArrayBuilder& ab,
                                            SVHControllerFeedbackAllChannels& data)
{
  // The Data is transmitted not channel by channel but rather position first, Currents afterwards
  // for all channels
  for (std::vector<SVHControllerFeedback>::iterator it = data.feedbacks.begin();
       it != data.feedbacks.end();
       ++it)
  {
    ab >> it->position;
  }

  for (std::vector<SVHControllerFeedback>::iterator it = data.feedbacks.begin();
       it != data.feedbacks.end();
       ++it)
  {
    ab >> it->current;
  }
  return ab;
}

inline std::ostream& operator<<(std::ostream& o, const SVHControllerFeedbackAllChannels& cf)
{
  o << "Feedbacks: ";
  unsigned int i = 0;
  for (std::vector<SVHControllerFeedback>::const_iterator it = cf.feedbacks.begin();
       it != cf.feedbacks.end();
       ++it, ++i)
  {
    o << "Chan " << i << " : " << *it;
  }
  o << std::endl;
  return o;
}

} // namespace driver_svh

#endif // SVHCONTROLLERFEEDBACK_H