Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A SyncGroup is a convenient simplification on-top of the sync API. #17

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions include/dynamixel++/SyncGroup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2022 LXRobotics GmbH.
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com>
* Contributors: https://github.com/107-systems/libdynamixelplusplus/graphs/contributors.
*/

#ifndef DYNAMIXEL_SYNCGROUP_H
#define DYNAMIXEL_SYNCGROUP_H

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include "Dynamixel.hpp"

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace dynamixelplusplus
{

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/

class SyncGroup
{
public:
SyncGroup(SharedDynamixel dyn_ctrl, Dynamixel::IdVect const & id_vect)
: _dyn_ctrl{dyn_ctrl}
, _id_vect{id_vect}
{ }


template<typename T> std::vector<T> read (uint16_t const start_address);
template<typename T> void write(uint16_t const start_address, T const val);
template<typename T> void write(uint16_t const start_address, std::vector<T> const & val_vect);


private:
SharedDynamixel _dyn_ctrl;
Dynamixel::IdVect const _id_vect;
};

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* dynamixelplusplus */

/**************************************************************************************
* TEMPLATE IMPLEMENTATION
**************************************************************************************/

#include "SyncGroup.ipp"

#endif /* DYNAMIXEL_SYNCGROUP_H */
59 changes: 59 additions & 0 deletions include/dynamixel++/SyncGroup.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2022 LXRobotics GmbH.
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com>
* Contributors: https://github.com/107-systems/libdynamixelplusplus/graphs/contributors.
*/

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace dynamixelplusplus
{

/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

template<typename T> std::vector<T> SyncGroup::read(uint16_t const start_address)
{
std::vector<T> data_vect;
std::map<Dynamixel::Id, T> const data_map = _dyn_ctrl->syncRead<T>(start_address, _id_vect);

for (auto [id, val] : data_map)
data_vect.push_back(val);

return data_vect;
}

template<typename T> void SyncGroup::write(uint16_t const start_address, T const val)
{
std::map<Dynamixel::Id, T> data_map;

for (auto id : _id_vect)
data_map[id] = val;

_dyn_ctrl->syncWrite(start_address, data_map);
}

template<typename T> void SyncGroup::write(uint16_t const start_address, std::vector<T> const & val_vect)
{
assert(val_vect.size() == _id_vect.size());

std::map<Dynamixel::Id, T> data_map;

auto id_citer = _id_vect.cbegin();
auto val_citer = val_vect.cbegin();

for (; id_citer != _id_vect.cend(); id_citer++, val_citer++)
data_map[*id_citer] = *val_citer;

_dyn_ctrl->syncWrite(start_address, data_map);
}

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* dynamixelplusplus */
1 change: 1 addition & 0 deletions include/dynamixel++/dynamixel++.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
**************************************************************************************/

#include "Dynamixel.hpp"
#include "SyncGroup.hpp"

#endif /* DYNAMIXEL_DYNAMIXELPLUSPLUS_H */