Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit ab42904

Browse files
authored
A SyncGroup is a convenient simplification on-top of the sync API. (#17)
1 parent 74683d6 commit ab42904

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

include/dynamixel++/SyncGroup.hpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* This software is distributed under the terms of the MIT License.
3+
* Copyright (c) 2022 LXRobotics GmbH.
4+
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com>
5+
* Contributors: https://github.com/107-systems/libdynamixelplusplus/graphs/contributors.
6+
*/
7+
8+
#ifndef DYNAMIXEL_SYNCGROUP_H
9+
#define DYNAMIXEL_SYNCGROUP_H
10+
11+
/**************************************************************************************
12+
* INCLUDE
13+
**************************************************************************************/
14+
15+
#include "Dynamixel.hpp"
16+
17+
/**************************************************************************************
18+
* NAMESPACE
19+
**************************************************************************************/
20+
21+
namespace dynamixelplusplus
22+
{
23+
24+
/**************************************************************************************
25+
* CLASS DECLARATION
26+
**************************************************************************************/
27+
28+
class SyncGroup
29+
{
30+
public:
31+
SyncGroup(SharedDynamixel dyn_ctrl, Dynamixel::IdVect const & id_vect)
32+
: _dyn_ctrl{dyn_ctrl}
33+
, _id_vect{id_vect}
34+
{ }
35+
36+
37+
template<typename T> std::vector<T> read (uint16_t const start_address);
38+
template<typename T> void write(uint16_t const start_address, T const val);
39+
template<typename T> void write(uint16_t const start_address, std::vector<T> const & val_vect);
40+
41+
42+
private:
43+
SharedDynamixel _dyn_ctrl;
44+
Dynamixel::IdVect const _id_vect;
45+
};
46+
47+
/**************************************************************************************
48+
* NAMESPACE
49+
**************************************************************************************/
50+
51+
} /* dynamixelplusplus */
52+
53+
/**************************************************************************************
54+
* TEMPLATE IMPLEMENTATION
55+
**************************************************************************************/
56+
57+
#include "SyncGroup.ipp"
58+
59+
#endif /* DYNAMIXEL_SYNCGROUP_H */

include/dynamixel++/SyncGroup.ipp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* This software is distributed under the terms of the MIT License.
3+
* Copyright (c) 2022 LXRobotics GmbH.
4+
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com>
5+
* Contributors: https://github.com/107-systems/libdynamixelplusplus/graphs/contributors.
6+
*/
7+
8+
/**************************************************************************************
9+
* NAMESPACE
10+
**************************************************************************************/
11+
12+
namespace dynamixelplusplus
13+
{
14+
15+
/**************************************************************************************
16+
* PUBLIC MEMBER FUNCTIONS
17+
**************************************************************************************/
18+
19+
template<typename T> std::vector<T> SyncGroup::read(uint16_t const start_address)
20+
{
21+
std::vector<T> data_vect;
22+
std::map<Dynamixel::Id, T> const data_map = _dyn_ctrl->syncRead<T>(start_address, _id_vect);
23+
24+
for (auto [id, val] : data_map)
25+
data_vect.push_back(val);
26+
27+
return data_vect;
28+
}
29+
30+
template<typename T> void SyncGroup::write(uint16_t const start_address, T const val)
31+
{
32+
std::map<Dynamixel::Id, T> data_map;
33+
34+
for (auto id : _id_vect)
35+
data_map[id] = val;
36+
37+
_dyn_ctrl->syncWrite(start_address, data_map);
38+
}
39+
40+
template<typename T> void SyncGroup::write(uint16_t const start_address, std::vector<T> const & val_vect)
41+
{
42+
assert(val_vect.size() == _id_vect.size());
43+
44+
std::map<Dynamixel::Id, T> data_map;
45+
46+
auto id_citer = _id_vect.cbegin();
47+
auto val_citer = val_vect.cbegin();
48+
49+
for (; id_citer != _id_vect.cend(); id_citer++, val_citer++)
50+
data_map[*id_citer] = *val_citer;
51+
52+
_dyn_ctrl->syncWrite(start_address, data_map);
53+
}
54+
55+
/**************************************************************************************
56+
* NAMESPACE
57+
**************************************************************************************/
58+
59+
} /* dynamixelplusplus */

include/dynamixel++/dynamixel++.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
**************************************************************************************/
1414

1515
#include "Dynamixel.hpp"
16+
#include "SyncGroup.hpp"
1617

1718
#endif /* DYNAMIXEL_DYNAMIXELPLUSPLUS_H */

0 commit comments

Comments
 (0)