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, std::shared_ptr<dynamixel::PacketHandler> packetHandler)
39  : _dxlPortHandler(std::move(portHandler)), _dxlPacketHandler(std::move(packetHandler))
40 {
41 }
42 
48 int AbstractTtlDriver::ping(uint8_t id)
49 {
50  uint8_t dxl_error = 0;
51 
52  int result = _dxlPacketHandler->ping(_dxlPortHandler.get(), id, &dxl_error);
53 
54  return result;
55 }
56 
63 int AbstractTtlDriver::getModelNumber(uint8_t id, uint16_t &model_number)
64 {
65  uint8_t dxl_error = 0;
66 
67  int result = _dxlPacketHandler->ping(_dxlPortHandler.get(), id, &model_number, &dxl_error);
68 
69  return result;
70 }
71 
77 int AbstractTtlDriver::scan(vector<uint8_t> &id_list) { return _dxlPacketHandler->broadcastPing(_dxlPortHandler.get(), id_list); }
78 
85 {
86  int result = -1;
87  uint8_t dxl_error = 0;
88 
89  result = _dxlPacketHandler->reboot(_dxlPortHandler.get(), id, &dxl_error);
90 
91  return result;
92 }
93 
98 std::string AbstractTtlDriver::str() const
99 {
100  ostringstream ss;
101 
102  ss << "TTL Driver : "
103  << "packet handler " << (_dxlPacketHandler ? "OK" : "Not Ok") << ","
104  << "port handler " << (_dxlPortHandler ? "OK" : "Not Ok");
105 
106  return ss.str();
107 }
108 
109 /*
110  * ----------------- Read Write operations --------------------
111  */
112 
121 int AbstractTtlDriver::readCustom(uint16_t address, uint8_t data_len, uint8_t id, uint32_t &data)
122 {
123  // clean output data first
124  data = 0;
125  int dxl_comm_result = COMM_TX_FAIL;
126 
127  switch (data_len)
128  {
129  case DXL_LEN_ONE_BYTE:
130  {
131  uint8_t read_data;
132  dxl_comm_result = read<uint8_t>(address, id, read_data);
133  data = read_data;
134  }
135  break;
136  case DXL_LEN_TWO_BYTES:
137  {
138  uint16_t read_data;
139  dxl_comm_result = read<uint16_t>(address, id, read_data);
140  data = read_data;
141  }
142  break;
143  case DXL_LEN_FOUR_BYTES:
144  {
145  uint32_t read_data;
146  dxl_comm_result = read<uint32_t>(address, id, read_data);
147  data = read_data;
148  }
149  break;
150  default:
151  printf("AbstractTtlDriver::read ERROR: Size param must be 1, 2 or 4 bytes\n");
152  break;
153  }
154 
155  return dxl_comm_result;
156 }
157 
166 int AbstractTtlDriver::writeCustom(uint16_t address, uint8_t data_len, uint8_t id, uint32_t data)
167 {
168  int dxl_comm_result = COMM_TX_FAIL;
169  uint8_t error = 0;
170 
171  switch (data_len)
172  {
173  case DXL_LEN_ONE_BYTE:
174  dxl_comm_result = write<uint8_t>(address, id, static_cast<uint8_t>(data));
175  break;
176  case DXL_LEN_TWO_BYTES:
177  dxl_comm_result = write<uint16_t>(address, id, static_cast<uint16_t>(data));
178  break;
179  case DXL_LEN_FOUR_BYTES:
180  dxl_comm_result = write<uint32_t>(address, id, data);
181  break;
182  default:
183  printf("AbstractTtlDriver::write ERROR: Size param must be 1, 2 or 4 bytes\n");
184  break;
185  }
186 
187  if (error != 0)
188  {
189  printf("AbstractTtlDriver::write ERROR: device return error: id=%d, addr=%d, len=%d, err=0x%02x\n", id, address, data_len, error);
190  }
191 
192  return dxl_comm_result;
193 }
194 
195 } // 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:98
ttl_driver::AbstractTtlDriver::DXL_LEN_TWO_BYTES
static constexpr uint8_t DXL_LEN_TWO_BYTES
Definition: abstract_ttl_driver.hpp:110
ttl_driver::AbstractTtlDriver::_dxlPortHandler
std::shared_ptr< dynamixel::PortHandler > _dxlPortHandler
Definition: abstract_ttl_driver.hpp:106
ttl_driver::AbstractTtlDriver::ping
virtual int ping(uint8_t id)
AbstractTtlDriver::ping.
Definition: abstract_ttl_driver.cpp:48
ttl_driver::AbstractTtlDriver::reboot
virtual int reboot(uint8_t id)
AbstractTtlDriver::reboot.
Definition: abstract_ttl_driver.cpp:84
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:121
ttl_driver::AbstractTtlDriver::scan
virtual int scan(std::vector< uint8_t > &id_list)
AbstractTtlDriver::scan.
Definition: abstract_ttl_driver.cpp:77
ttl_driver::AbstractTtlDriver::getModelNumber
virtual int getModelNumber(uint8_t id, uint16_t &model_number)
AbstractTtlDriver::getModelNumber.
Definition: abstract_ttl_driver.cpp:63
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:107
ttl_driver::AbstractTtlDriver::DXL_LEN_FOUR_BYTES
static constexpr uint8_t DXL_LEN_FOUR_BYTES
Definition: abstract_ttl_driver.hpp:111
ttl_driver::AbstractTtlDriver::DXL_LEN_ONE_BYTE
static constexpr uint8_t DXL_LEN_ONE_BYTE
Definition: abstract_ttl_driver.hpp:109
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:166


ttl_driver
Author(s): Clement Cocquempot
autogenerated on Wed May 21 2025 08:30:14