ros2_canopen  master
C++ ROS CANopen Library
mode_target_helper.hpp
Go to the documentation of this file.
1 #ifndef MODE_TARGET_HELPER_HPP
2 #define MODE_TARGET_HELPER_HPP
3 
4 #include <atomic>
5 #include <boost/numeric/conversion/cast.hpp>
6 #include <cmath>
7 #include <iostream>
8 #include <limits>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
14 
15 namespace ros2_canopen
16 {
17 
18 template <typename T>
19 class ModeTargetHelper : public Mode
20 {
21  T target_;
22  std::atomic<bool> has_target_;
23 
24 public:
25  ModeTargetHelper(uint16_t mode) : Mode(mode) {}
26  bool hasTarget() { return has_target_; }
27  T getTarget() { return target_; }
28  virtual bool setTarget(const double & val)
29  {
30  if (std::isnan(val))
31  {
32  // std::cout << "canopen_402 target command is not a number" << std::endl;
33  RCLCPP_DEBUG(rclcpp::get_logger("canopen_402_target"), "Target command is not a number");
34  return false;
35  }
36 
37  using boost::numeric_cast;
38  using boost::numeric::negative_overflow;
39  using boost::numeric::positive_overflow;
40 
41  try
42  {
43  target_ = numeric_cast<T>(val);
44  }
45  catch (negative_overflow &)
46  {
47  std::cout << "canopen_402 Command " << val
48  << " does not fit into target, clamping to min limit" << std::endl;
49  target_ = std::numeric_limits<T>::min();
50  }
51  catch (positive_overflow &)
52  {
53  std::cout << "canopen_402 Command " << val
54  << " does not fit into target, clamping to max limit" << std::endl;
55  target_ = std::numeric_limits<T>::max();
56  }
57  catch (...)
58  {
59  std::cout << "canopen_402 Was not able to cast command " << val << std::endl;
60  return false;
61  }
62 
63  has_target_ = true;
64  return true;
65  }
66  virtual bool start()
67  {
68  has_target_ = false;
69  return true;
70  }
71 };
72 } // namespace ros2_canopen
73 
74 #endif // MODE_TARGET_HELPER_HPP
Definition: mode_target_helper.hpp:20
virtual bool setTarget(const double &val)
Definition: mode_target_helper.hpp:28
virtual bool start()
Definition: mode_target_helper.hpp:66
bool hasTarget()
Definition: mode_target_helper.hpp:26
T getTarget()
Definition: mode_target_helper.hpp:27
ModeTargetHelper(uint16_t mode)
Definition: mode_target_helper.hpp:25
Definition: mode.hpp:13
Definition: configuration_manager.hpp:28