ned3pro_end_effector_driver.hpp
Go to the documentation of this file.
1 /*
2 ned3pro_end_effector_driver.hpp
3 Copyright (C) 2024 Niryo
4 All rights reserved.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http:// www.gnu.org/licenses/>.
15 */
16 
17 #ifndef NED3PRO_END_EFFECTOR_DRIVER_HPP
18 #define NED3PRO_END_EFFECTOR_DRIVER_HPP
19 
20 #include <memory>
21 #include <vector>
22 #include <string>
23 #include <iostream>
24 #include <sstream>
25 #include <cassert>
26 
28 
30 #include "common/model/end_effector_command_type_enum.hpp"
31 #include "common/model/end_effector_state.hpp"
32 
33 using ::common::model::EEndEffectorCommandType;
34 
35 namespace ttl_driver
36 {
37 
41  template <typename reg_type = Ned3ProEndEffectorReg>
43  {
44  public:
45  Ned3ProEndEffectorDriver(std::shared_ptr<dynamixel::PortHandler> portHandler,
46  std::shared_ptr<dynamixel::PacketHandler> packetHandler);
47 
48  public:
49  // AbstractTtlDriver interface : we cannot define them globally in AbstractTtlDriver
50  // as it is needed here for polymorphism (AbstractTtlDriver cannot be a template class and does not
51  // have access to reg_type). So it seems like a duplicate of StepperDriver
52  std::string str() const override;
53 
54  int checkModelNumber(uint8_t id) override;
55  int readFirmwareVersion(uint8_t id, std::string &version) override;
56 
57  int readTemperature(uint8_t id, uint8_t &temperature) override;
58  int readVoltage(uint8_t id, double &voltage) override;
59  int readHwErrorStatus(uint8_t id, uint8_t &hardware_error_status) override;
60 
61  int syncReadFirmwareVersion(const std::vector<uint8_t> &id_list, std::vector<std::string> &firmware_list) override;
62  int syncReadTemperature(const std::vector<uint8_t> &id_list, std::vector<uint8_t> &temperature_list) override;
63  int syncReadVoltage(const std::vector<uint8_t> &id_list, std::vector<double> &voltage_list) override;
64  int syncReadRawVoltage(const std::vector<uint8_t> &id_list, std::vector<double> &voltage_list) override;
65  int syncReadHwStatus(const std::vector<uint8_t> &id_list, std::vector<std::pair<double, uint8_t>> &data_list) override;
66 
67  int syncReadHwErrorStatus(const std::vector<uint8_t> &id_list, std::vector<uint8_t> &hw_error_list) override;
68 
69  public:
70  // AbstractEndEffectorDriver
71 
72  int readButton0Status(uint8_t id, common::model::EActionType &action) override;
73  int readButton1Status(uint8_t id, common::model::EActionType &action) override;
74  int readButton2Status(uint8_t id, common::model::EActionType &action) override;
75  int syncReadButtonsStatus(const uint8_t &id, std::vector<common::model::EActionType> &action_list) override;
76 
77  int readAccelerometerXValue(uint8_t id, uint32_t &x_value) override;
78  int readAccelerometerYValue(uint8_t id, uint32_t &y_value) override;
79  int readAccelerometerZValue(uint8_t id, uint32_t &z_value) override;
80 
81  int readCollisionStatus(uint8_t id, bool &status) override;
82 
83  int readDigitalInput(uint8_t id, bool &in) override;
84  int writeDigitalInput(uint8_t id, bool in) override;
85  int writeDigitalOutput(uint8_t id, bool out) override;
86 
87  int writeCollisionThresh(uint8_t id, int thresh) override;
88  int readCollisionThresh(uint8_t id, uint32_t &thresh);
89 
90  int writeCollisionThreshAlgo2(uint8_t id, int thresh) override;
91  int readCollisionThreshAlgo2(uint8_t id, uint32_t &thresh);
92  };
93 
94  // definition of methods
95 
99  template <typename reg_type>
100  Ned3ProEndEffectorDriver<reg_type>::Ned3ProEndEffectorDriver(std::shared_ptr<dynamixel::PortHandler> portHandler,
101  std::shared_ptr<dynamixel::PacketHandler> packetHandler) : AbstractEndEffectorDriver(std::move(portHandler),
102  std::move(packetHandler))
103  {
104  }
105 
106  //*****************************
107  // AbstractTtlDriver interface
108  //*****************************
109 
114  template <typename reg_type>
116  {
117  return common::model::HardwareTypeEnum(reg_type::motor_type).toString() + " : " + ttl_driver::AbstractEndEffectorDriver::str();
118  }
119 
125  template <typename reg_type>
127  {
128  uint16_t model_number = 0;
129  int ping_result = getModelNumber(id, model_number);
130 
131  if (ping_result == COMM_SUCCESS)
132  {
133  if (model_number && model_number != reg_type::MODEL_NUMBER)
134  {
135  return PING_WRONG_MODEL_NUMBER;
136  }
137  }
138 
139  return ping_result;
140  }
141 
148  template <typename reg_type>
149  int Ned3ProEndEffectorDriver<reg_type>::readFirmwareVersion(uint8_t id, std::string &version)
150  {
151  int res = COMM_RX_FAIL;
152  uint32_t data{};
153  res = read<typename reg_type::TYPE_FIRMWARE_VERSION>(reg_type::ADDR_FIRMWARE_VERSION, id, data);
154  version = interpretFirmwareVersion(data);
155  return res;
156  }
157 
158  // ram read
159 
166  template <typename reg_type>
167  int Ned3ProEndEffectorDriver<reg_type>::readTemperature(uint8_t id, uint8_t &temperature)
168  {
169  return read<typename reg_type::TYPE_PRESENT_TEMPERATURE>(reg_type::ADDR_PRESENT_TEMPERATURE, id, temperature);
170  }
171 
178  template <typename reg_type>
179  int Ned3ProEndEffectorDriver<reg_type>::readVoltage(uint8_t id, double &voltage)
180  {
181  uint16_t voltage_mV = 0;
182  int res = read<typename reg_type::TYPE_PRESENT_VOLTAGE>(reg_type::ADDR_PRESENT_VOLTAGE, id, voltage_mV);
183  voltage = static_cast<double>(voltage_mV) / reg_type::VOLTAGE_CONVERSION;
184  return res;
185  }
186 
193  template <typename reg_type>
194  int Ned3ProEndEffectorDriver<reg_type>::readHwErrorStatus(uint8_t id, uint8_t &hardware_error_status)
195  {
196  hardware_error_status = 0;
197  return read<typename reg_type::TYPE_HW_ERROR_STATUS>(reg_type::ADDR_HW_ERROR_STATUS, id, hardware_error_status);
198  }
199 
206  template <typename reg_type>
207  int Ned3ProEndEffectorDriver<reg_type>::syncReadFirmwareVersion(const std::vector<uint8_t> &id_list, std::vector<std::string> &firmware_list)
208  {
209  int res = 0;
210  firmware_list.clear();
211  std::vector<uint32_t> data_list{};
212  res = syncRead<typename reg_type::TYPE_FIRMWARE_VERSION>(reg_type::ADDR_FIRMWARE_VERSION, id_list, data_list);
213  for (auto const &data : data_list)
214  firmware_list.emplace_back(interpretFirmwareVersion(data));
215  return res;
216  }
217 
224  template <typename reg_type>
225  int Ned3ProEndEffectorDriver<reg_type>::syncReadTemperature(const std::vector<uint8_t> &id_list, std::vector<uint8_t> &temperature_list)
226  {
227  return syncRead<typename reg_type::TYPE_PRESENT_TEMPERATURE>(reg_type::ADDR_PRESENT_TEMPERATURE, id_list, temperature_list);
228  }
229 
236  template <typename reg_type>
237  int Ned3ProEndEffectorDriver<reg_type>::syncReadVoltage(const std::vector<uint8_t> &id_list, std::vector<double> &voltage_list)
238  {
239  voltage_list.clear();
240  std::vector<uint16_t> v_read;
241  int res = syncRead<typename reg_type::TYPE_PRESENT_VOLTAGE>(reg_type::ADDR_PRESENT_VOLTAGE, id_list, v_read);
242  for (auto const &v : v_read)
243  voltage_list.emplace_back(static_cast<double>(v) / reg_type::VOLTAGE_CONVERSION);
244  return res;
245  }
246 
253  template <typename reg_type>
254  int Ned3ProEndEffectorDriver<reg_type>::syncReadRawVoltage(const std::vector<uint8_t> &id_list, std::vector<double> &voltage_list)
255  {
256  voltage_list.clear();
257  std::vector<uint16_t> v_read;
258  int res = syncRead<typename reg_type::TYPE_PRESENT_VOLTAGE>(reg_type::ADDR_PRESENT_VOLTAGE, id_list, v_read);
259  for (auto const &v : v_read)
260  voltage_list.emplace_back(static_cast<double>(v));
261  return res;
262  }
263 
270  template <typename reg_type>
271  int Ned3ProEndEffectorDriver<reg_type>::syncReadHwStatus(const std::vector<uint8_t> &id_list,
272  std::vector<std::pair<double, uint8_t>> &data_list)
273  {
274  data_list.clear();
275 
276  std::vector<std::array<uint8_t, 3>> raw_data;
277  int res = syncReadConsecutiveBytes<uint8_t, 3>(reg_type::ADDR_PRESENT_VOLTAGE, id_list, raw_data);
278 
279  for (auto const &data : raw_data)
280  {
281  // Voltage is first reg, uint16
282  auto voltage = static_cast<double>((static_cast<uint16_t>(data.at(1)) << 8) | data.at(0));
283 
284  // Temperature is second reg, uint8
285  uint8_t temperature = data.at(2);
286 
287  data_list.emplace_back(std::make_pair(voltage, temperature));
288  }
289 
290  return res;
291  }
292 
299  template <typename reg_type>
300  int Ned3ProEndEffectorDriver<reg_type>::syncReadHwErrorStatus(const std::vector<uint8_t> &id_list, std::vector<uint8_t> &hw_error_list)
301  {
302  return syncRead<typename reg_type::TYPE_HW_ERROR_STATUS>(reg_type::ADDR_HW_ERROR_STATUS, id_list, hw_error_list);
303  }
304 
305  // buttons status
306 
313  template <typename reg_type>
315  common::model::EActionType &action)
316  {
317  uint8_t status;
318  int res = read<typename reg_type::TYPE_BUTTON_STATUS>(reg_type::ADDR_STATE_BUTTON_CUSTOM, id, status);
319  action = interpretActionValue(status);
320  return res;
321  }
322 
329  template <typename reg_type>
330  int Ned3ProEndEffectorDriver<reg_type>::readButton1Status(uint8_t id, common::model::EActionType &action)
331  {
332  uint8_t status;
333  int res = read<typename reg_type::TYPE_BUTTON_STATUS>(reg_type::ADDR_STATE_BUTTON_SAVE, id, status);
334  action = interpretActionValue(status);
335  return res;
336  }
337 
344  template <typename reg_type>
345  int Ned3ProEndEffectorDriver<reg_type>::readButton2Status(uint8_t id, common::model::EActionType &action)
346  {
347  uint8_t status;
348  int res = read<typename reg_type::TYPE_BUTTON_STATUS>(reg_type::ADDR_STATE_BUTTON_FREEDRIVE, id, status);
349  action = interpretActionValue(status);
350  return res;
351  }
352 
359  template <typename reg_type>
361  std::vector<common::model::EActionType> &action_list)
362  {
363  std::vector<std::array<typename reg_type::TYPE_BUTTON_STATUS, 3>> data_array_list;
364  int res;
365  res = syncReadConsecutiveBytes<typename reg_type::TYPE_BUTTON_STATUS, 3>(reg_type::ADDR_STATE_BUTTON_FREEDRIVE, {id}, data_array_list);
366  if (res == COMM_SUCCESS && data_array_list.size() == 1)
367  {
368  for (auto data : data_array_list.at(0))
369  {
370  action_list.push_back(interpretActionValue(data));
371  }
372  }
373  return res;
374  }
375  // accelerometers and collision
376 
383  template <typename reg_type>
385  {
386  return read<typename reg_type::TYPE_ACCELERO_VALUE_X>(reg_type::ADDR_ACCELERO_VALUE_X, id, x_value);
387  }
388 
395  template <typename reg_type>
397  {
398  return read<typename reg_type::TYPE_ACCELERO_VALUE_Y>(reg_type::ADDR_ACCELERO_VALUE_Y, id, y_value);
399  }
400 
407  template <typename reg_type>
409  {
410  return read<typename reg_type::TYPE_ACCELERO_VALUE_Z>(reg_type::ADDR_ACCELERO_VALUE_Z, id, z_value);
411  }
412 
419  template <typename reg_type>
421  {
422  uint8_t value = 0;
423  status = false;
424  int res = read<typename reg_type::TYPE_COLLISION_STATUS>(reg_type::ADDR_COLLISION_STATUS, id, value);
425  if (COMM_SUCCESS == res)
426  {
427  status = (value > 0) ? true : false;
428  }
429  return res;
430  }
431 
438  template <typename reg_type>
440  {
441  uint8_t value;
442  int res = read<typename reg_type::TYPE_DIGITAL_IN>(reg_type::ADDR_DIGITAL_INPUT, id, value);
443  in = (value > 0) ? true : false;
444  return res;
445  }
446 
453  template <typename reg_type>
455  {
456  return COMM_TX_ERROR;
457  }
458 
465  template <typename reg_type>
467  {
468  return write<typename reg_type::TYPE_DIGITAL_OUT>(reg_type::ADDR_DIGITAL_OUTPUT, id, static_cast<uint8_t>(out));
469  }
470 
477  template <typename reg_type>
479  {
480  return write<typename reg_type::TYPE_COLLISION_THRESHOLD_ALGO_1>(reg_type::ADDR_COLLISION_DETECT_THRESH_ALGO_1, id, static_cast<uint32_t>(thresh));
481  }
482 
489  template <typename reg_type>
491  {
492  return read<typename reg_type::TYPE_COLLISION_THRESHOLD_ALGO_1>(reg_type::ADDR_COLLISION_DETECT_THRESH_ALGO_1, id, thresh);
493  }
494 
501  template <typename reg_type>
503  {
504  return write<typename reg_type::TYPE_COLLISION_THRESHOLD_ALGO_2>(reg_type::ADDR_COLLISION_DETECT_THRESH_ALGO_2, id, static_cast<uint32_t>(thresh));
505  }
506 
513  template <typename reg_type>
515  {
516  return read<typename reg_type::TYPE_COLLISION_THRESHOLD_ALGO_2>(reg_type::ADDR_COLLISION_DETECT_THRESH_ALGO_2, id, thresh);
517  }
518 
519 } // ttl_driver
520 
521 #endif // Ned3ProEndEffectorDriver
ned3pro_end_effector_reg.hpp
ttl_driver::Ned3ProEndEffectorDriver::syncReadHwErrorStatus
int syncReadHwErrorStatus(const std::vector< uint8_t > &id_list, std::vector< uint8_t > &hw_error_list) override
Ned3ProEndEffectorDriver<reg_type>::syncReadHwErrorStatus.
Definition: ned3pro_end_effector_driver.hpp:300
ttl_driver::Ned3ProEndEffectorDriver::writeCollisionThreshAlgo2
int writeCollisionThreshAlgo2(uint8_t id, int thresh) override
Ned3ProEndEffectorDriver<reg_type>::writeCollisionThreshAlgo2.
Definition: ned3pro_end_effector_driver.hpp:502
ttl_driver::Ned3ProEndEffectorDriver::readCollisionStatus
int readCollisionStatus(uint8_t id, bool &status) override
Ned3ProEndEffectorDriver<reg_type>::readCollisionStatus.
Definition: ned3pro_end_effector_driver.hpp:420
ttl_driver::Ned3ProEndEffectorDriver::readVoltage
int readVoltage(uint8_t id, double &voltage) override
Ned3ProEndEffectorDriver<reg_type>::readVoltage.
Definition: ned3pro_end_effector_driver.hpp:179
ttl_driver::Ned3ProEndEffectorDriver::syncReadButtonsStatus
int syncReadButtonsStatus(const uint8_t &id, std::vector< common::model::EActionType > &action_list) override
Ned3ProEndEffectorDriver<reg_type>::syncReadButtonsStatus.
Definition: ned3pro_end_effector_driver.hpp:360
ttl_driver::Ned3ProEndEffectorDriver::readHwErrorStatus
int readHwErrorStatus(uint8_t id, uint8_t &hardware_error_status) override
Ned3ProEndEffectorDriver<reg_type>::readHwErrorStatus.
Definition: ned3pro_end_effector_driver.hpp:194
ttl_driver::Ned3ProEndEffectorDriver::writeCollisionThresh
int writeCollisionThresh(uint8_t id, int thresh) override
Ned3ProEndEffectorDriver<reg_type>::writeCollisionThresh.
Definition: ned3pro_end_effector_driver.hpp:478
ttl_driver::Ned3ProEndEffectorDriver::checkModelNumber
int checkModelNumber(uint8_t id) override
Ned3ProEndEffectorDriver<reg_type>::checkModelNumber.
Definition: ned3pro_end_effector_driver.hpp:126
ttl_driver::Ned3ProEndEffectorDriver::syncReadVoltage
int syncReadVoltage(const std::vector< uint8_t > &id_list, std::vector< double > &voltage_list) override
Ned3ProEndEffectorDriver<reg_type>::syncReadVoltage.
Definition: ned3pro_end_effector_driver.hpp:237
ttl_driver::Ned3ProEndEffectorDriver::readAccelerometerZValue
int readAccelerometerZValue(uint8_t id, uint32_t &z_value) override
Ned3ProEndEffectorDriver<reg_type>::readAccelerometerZValue.
Definition: ned3pro_end_effector_driver.hpp:408
ttl_driver::Ned3ProEndEffectorDriver::writeDigitalInput
int writeDigitalInput(uint8_t id, bool in) override
Ned3ProEndEffectorDriver<reg_type>::writeDigitalInput not implemented for real end effectors.
Definition: ned3pro_end_effector_driver.hpp:454
ttl_driver::Ned3ProEndEffectorDriver::readTemperature
int readTemperature(uint8_t id, uint8_t &temperature) override
Ned3ProEndEffectorDriver<reg_type>::readTemperature.
Definition: ned3pro_end_effector_driver.hpp:167
ttl_driver::Ned3ProEndEffectorDriver::writeDigitalOutput
int writeDigitalOutput(uint8_t id, bool out) override
Ned3ProEndEffectorDriver<reg_type>::setDigitalOutput.
Definition: ned3pro_end_effector_driver.hpp:466
ttl_driver::Ned3ProEndEffectorDriver::readButton1Status
int readButton1Status(uint8_t id, common::model::EActionType &action) override
Ned3ProEndEffectorDriver<reg_type>::readButton1Status.
Definition: ned3pro_end_effector_driver.hpp:330
ttl_driver::Ned3ProEndEffectorDriver::Ned3ProEndEffectorDriver
Ned3ProEndEffectorDriver(std::shared_ptr< dynamixel::PortHandler > portHandler, std::shared_ptr< dynamixel::PacketHandler > packetHandler)
Ned3ProEndEffectorDriver<reg_type>::Ned3ProEndEffectorDriver.
Definition: ned3pro_end_effector_driver.hpp:100
ttl_driver::AbstractEndEffectorDriver::str
std::string str() const override
AbstractEndEffectorDriver::str : build a string describing the object. For debug purpose only.
Definition: abstract_end_effector_driver.cpp:48
abstract_end_effector_driver.hpp
ttl_driver::Ned3ProEndEffectorDriver::readCollisionThreshAlgo2
int readCollisionThreshAlgo2(uint8_t id, uint32_t &thresh)
Ned3ProEndEffectorDriver<reg_type>::readCollisionThreshAlgo2.
Definition: ned3pro_end_effector_driver.hpp:514
ttl_driver
Definition: abstract_dxl_driver.hpp:30
ttl_driver::Ned3ProEndEffectorDriver::readAccelerometerYValue
int readAccelerometerYValue(uint8_t id, uint32_t &y_value) override
Ned3ProEndEffectorDriver<reg_type>::readAccelerometerYValue.
Definition: ned3pro_end_effector_driver.hpp:396
ttl_driver::Ned3ProEndEffectorDriver::readCollisionThresh
int readCollisionThresh(uint8_t id, uint32_t &thresh)
Ned3ProEndEffectorDriver<reg_type>::readCollisionThresh.
Definition: ned3pro_end_effector_driver.hpp:490
ttl_driver::Ned3ProEndEffectorDriver::readButton0Status
int readButton0Status(uint8_t id, common::model::EActionType &action) override
Ned3ProEndEffectorDriver<reg_type>::readButton0Status.
Definition: ned3pro_end_effector_driver.hpp:314
ttl_driver::Ned3ProEndEffectorDriver::syncReadRawVoltage
int syncReadRawVoltage(const std::vector< uint8_t > &id_list, std::vector< double > &voltage_list) override
Ned3ProEndEffectorDriver<reg_type>::syncReadRawVoltage.
Definition: ned3pro_end_effector_driver.hpp:254
ttl_driver::AbstractEndEffectorDriver
The AbstractEndEffectorDriver class.
Definition: abstract_end_effector_driver.hpp:41
ttl_driver::Ned3ProEndEffectorDriver::readButton2Status
int readButton2Status(uint8_t id, common::model::EActionType &action) override
Ned3ProEndEffectorDriver<reg_type>::readButton2Status.
Definition: ned3pro_end_effector_driver.hpp:345
ttl_driver::Ned3ProEndEffectorDriver::readFirmwareVersion
int readFirmwareVersion(uint8_t id, std::string &version) override
Ned3ProEndEffectorDriver<reg_type>::readFirmwareVersion.
Definition: ned3pro_end_effector_driver.hpp:149
ttl_driver::Ned3ProEndEffectorDriver
The Ned3ProEndEffectorDriver class.
Definition: ned3pro_end_effector_driver.hpp:42
ttl_driver::Ned3ProEndEffectorDriver::syncReadTemperature
int syncReadTemperature(const std::vector< uint8_t > &id_list, std::vector< uint8_t > &temperature_list) override
Ned3ProEndEffectorDriver<reg_type>::syncReadTemperature.
Definition: ned3pro_end_effector_driver.hpp:225
ttl_driver::Ned3ProEndEffectorDriver::readDigitalInput
int readDigitalInput(uint8_t id, bool &in) override
Ned3ProEndEffectorDriver<reg_type>::readDigitalInput.
Definition: ned3pro_end_effector_driver.hpp:439
ttl_driver::Ned3ProEndEffectorDriver::syncReadHwStatus
int syncReadHwStatus(const std::vector< uint8_t > &id_list, std::vector< std::pair< double, uint8_t >> &data_list) override
Ned3ProEndEffectorDriver<reg_type>::syncReadHwStatus.
Definition: ned3pro_end_effector_driver.hpp:271
ttl_driver::Ned3ProEndEffectorDriver::readAccelerometerXValue
int readAccelerometerXValue(uint8_t id, uint32_t &x_value) override
Ned3ProEndEffectorDriver<reg_type>::readAccelerometerXValue.
Definition: ned3pro_end_effector_driver.hpp:384
ttl_driver::Ned3ProEndEffectorDriver::str
std::string str() const override
Ned3ProEndEffectorDriver<reg_type>::str.
Definition: ned3pro_end_effector_driver.hpp:115
ttl_driver::Ned3ProEndEffectorDriver::syncReadFirmwareVersion
int syncReadFirmwareVersion(const std::vector< uint8_t > &id_list, std::vector< std::string > &firmware_list) override
Ned3ProEndEffectorDriver<reg_type>::syncReadFirmwareVersion.
Definition: ned3pro_end_effector_driver.hpp:207


ttl_driver
Author(s): Clement Cocquempot
autogenerated on Thu Dec 11 2025 11:41:10