abstract_ttl_driver.cpp
Go to the documentation of this file.
1 /*
2  abstract_ttl_driver.cpp
3  Copyright (C) 2020 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 
18 
19 #include <memory>
20 #include <sstream>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 using ::std::ostringstream;
26 using ::std::shared_ptr;
27 using ::std::string;
28 using ::std::vector;
29 
30 namespace ttl_driver
31 {
32 
38 AbstractTtlDriver::AbstractTtlDriver(std::shared_ptr<dynamixel::PortHandler> portHandler,
39  std::shared_ptr<dynamixel::PacketHandler> packetHandler)
40  : _dxlPortHandler(std::move(portHandler)), _dxlPacketHandler(std::move(packetHandler))
41 {
42 }
43 
49 int AbstractTtlDriver::ping(uint8_t id)
50 {
51  uint8_t dxl_error = 0;
52 
53  int result = _dxlPacketHandler->ping(_dxlPortHandler.get(), id, &dxl_error);
54 
55  return result;
56 }
57 
64 int AbstractTtlDriver::getModelNumber(uint8_t id, uint16_t &model_number)
65 {
66  uint8_t dxl_error = 0;
67 
68  int result = _dxlPacketHandler->ping(_dxlPortHandler.get(), id, &model_number, &dxl_error);
69 
70  return result;
71 }
72 
78 int AbstractTtlDriver::scan(vector<uint8_t> &id_list)
79 {
80  return _dxlPacketHandler->broadcastPing(_dxlPortHandler.get(), id_list);
81 }
82 
89 {
90  int result = -1;
91  uint8_t dxl_error = 0;
92 
93  result = _dxlPacketHandler->reboot(_dxlPortHandler.get(), id, &dxl_error);
94 
95  return result;
96 }
97 
102 std::string AbstractTtlDriver::str() const
103 {
104  ostringstream ss;
105 
106  ss << "TTL Driver : "
107  << "packet handler " << (_dxlPacketHandler ? "OK" : "Not Ok") << ","
108  << "port handler " << (_dxlPortHandler ? "OK" : "Not Ok");
109 
110  return ss.str();
111 }
112 
113 /*
114  * ----------------- Read Write operations --------------------
115  */
116 
125 int AbstractTtlDriver::readCustom(uint16_t address, uint8_t data_len, uint8_t id, uint32_t &data)
126 {
127  // clean output data first
128  data = 0;
129  int dxl_comm_result = COMM_TX_FAIL;
130 
131  switch (data_len)
132  {
133  case DXL_LEN_ONE_BYTE: {
134  uint8_t read_data;
135  dxl_comm_result = read<uint8_t>(address, id, read_data);
136  data = read_data;
137  }
138  break;
139  case DXL_LEN_TWO_BYTES: {
140  uint16_t read_data;
141  dxl_comm_result = read<uint16_t>(address, id, read_data);
142  data = read_data;
143  }
144  break;
145  case DXL_LEN_FOUR_BYTES: {
146  uint32_t read_data;
147  dxl_comm_result = read<uint32_t>(address, id, read_data);
148  data = read_data;
149  }
150  break;
151  default:
152  printf("AbstractTtlDriver::read ERROR: Size param must be 1, 2 or 4 bytes\n");
153  break;
154  }
155 
156  return dxl_comm_result;
157 }
158 
167 int AbstractTtlDriver::writeCustom(uint16_t address, uint8_t data_len, uint8_t id, uint32_t data)
168 {
169  int dxl_comm_result = COMM_TX_FAIL;
170  uint8_t error = 0;
171 
172  switch (data_len)
173  {
174  case DXL_LEN_ONE_BYTE:
175  dxl_comm_result = write<uint8_t>(address, id, static_cast<uint8_t>(data));
176  break;
177  case DXL_LEN_TWO_BYTES:
178  dxl_comm_result = write<uint16_t>(address, id, static_cast<uint16_t>(data));
179  break;
180  case DXL_LEN_FOUR_BYTES:
181  dxl_comm_result = write<uint32_t>(address, id, data);
182  break;
183  default:
184  printf("AbstractTtlDriver::write ERROR: Size param must be 1, 2 or 4 bytes\n");
185  break;
186  }
187 
188  if (error != 0)
189  {
190  printf("AbstractTtlDriver::write ERROR: device return error: id=%d, addr=%d, len=%d, err=0x%02x\n", id, address,
191  data_len, error);
192  }
193 
194  return dxl_comm_result;
195 }
196 
197 } // namespace ttl_driver
ttl_driver::AbstractTtlDriver::str
virtual std::string str() const
AbstractTtlDriver::str : build a string describing the object. For debug purpose only.
Definition: abstract_ttl_driver.cpp:102
ttl_driver::AbstractTtlDriver::DXL_LEN_TWO_BYTES
static constexpr uint8_t DXL_LEN_TWO_BYTES
Definition: abstract_ttl_driver.hpp:109
ttl_driver::AbstractTtlDriver::_dxlPortHandler
std::shared_ptr< dynamixel::PortHandler > _dxlPortHandler
Definition: abstract_ttl_driver.hpp:105
ttl_driver::AbstractTtlDriver::ping
virtual int ping(uint8_t id)
AbstractTtlDriver::ping.
Definition: abstract_ttl_driver.cpp:49
ttl_driver::AbstractTtlDriver::reboot
virtual int reboot(uint8_t id)
AbstractTtlDriver::reboot.
Definition: abstract_ttl_driver.cpp:88
ttl_driver::AbstractTtlDriver::AbstractTtlDriver
AbstractTtlDriver()=default
ttl_driver::AbstractTtlDriver::readCustom
virtual int readCustom(uint16_t address, uint8_t data_len, uint8_t id, uint32_t &data)
AbstractTtlDriver::readCustom.
Definition: abstract_ttl_driver.cpp:125
ttl_driver::AbstractTtlDriver::scan
virtual int scan(std::vector< uint8_t > &id_list)
AbstractTtlDriver::scan.
Definition: abstract_ttl_driver.cpp:78
ttl_driver::AbstractTtlDriver::getModelNumber
virtual int getModelNumber(uint8_t id, uint16_t &model_number)
AbstractTtlDriver::getModelNumber.
Definition: abstract_ttl_driver.cpp:64
ttl_driver
Definition: abstract_dxl_driver.hpp:30
abstract_ttl_driver.hpp
ttl_driver::AbstractTtlDriver::_dxlPacketHandler
std::shared_ptr< dynamixel::PacketHandler > _dxlPacketHandler
Definition: abstract_ttl_driver.hpp:106
ttl_driver::AbstractTtlDriver::DXL_LEN_FOUR_BYTES
static constexpr uint8_t DXL_LEN_FOUR_BYTES
Definition: abstract_ttl_driver.hpp:110
ttl_driver::AbstractTtlDriver::DXL_LEN_ONE_BYTE
static constexpr uint8_t DXL_LEN_ONE_BYTE
Definition: abstract_ttl_driver.hpp:108
ttl_driver::AbstractTtlDriver::writeCustom
virtual int writeCustom(uint16_t address, uint8_t data_len, uint8_t id, uint32_t data)
AbstractTtlDriver::writeCustom.
Definition: abstract_ttl_driver.cpp:167


ttl_driver
Author(s): Clement Cocquempot
autogenerated on Fri Mar 6 2026 15:24:15