group_sync_write.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2017 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Author: zerom, Ryu Woon Jung (Leon) */
18 
19 #include <algorithm>
20 
21 #if defined(__linux__)
22 #include "group_sync_write.h"
23 #elif defined(__APPLE__)
24 #include "group_sync_write.h"
25 #elif defined(_WIN32) || defined(_WIN64)
26 #define WINDLLEXPORT
27 #include "group_sync_write.h"
28 #elif defined(ARDUINO) || defined(__OPENCR__) || defined(__OPENCM904__)
29 #include "../../include/dynamixel_sdk/group_sync_write.h"
30 #endif
31 
32 using namespace dynamixel;
33 
34 GroupSyncWrite::GroupSyncWrite(PortHandler *port, PacketHandler *ph, uint16_t start_address, uint16_t data_length)
35  : port_(port),
36  ph_(ph),
37  is_param_changed_(false),
38  param_(0),
39  start_address_(start_address),
40  data_length_(data_length)
41 {
42  clearParam();
43 }
44 
46 {
47  if (id_list_.size() == 0) return;
48 
49  if (param_ != 0)
50  delete[] param_;
51  param_ = 0;
52 
53  param_ = new uint8_t[id_list_.size() * (1 + data_length_)]; // ID(1) + DATA(data_length)
54 
55  int idx = 0;
56  for (unsigned int i = 0; i < id_list_.size(); i++)
57  {
58  uint8_t id = id_list_[i];
59  if (data_list_[id] == 0)
60  return;
61 
62  param_[idx++] = id;
63  for (int c = 0; c < data_length_; c++)
64  param_[idx++] = (data_list_[id])[c];
65  }
66 }
67 
68 bool GroupSyncWrite::addParam(uint8_t id, uint8_t *data)
69 {
70  if (std::find(id_list_.begin(), id_list_.end(), id) != id_list_.end()) // id already exist
71  return false;
72 
73  id_list_.push_back(id);
74  data_list_[id] = new uint8_t[data_length_];
75  for (int c = 0; c < data_length_; c++)
76  data_list_[id][c] = data[c];
77 
78  is_param_changed_ = true;
79  return true;
80 }
81 
83 {
84  std::vector<uint8_t>::iterator it = std::find(id_list_.begin(), id_list_.end(), id);
85  if (it == id_list_.end()) // NOT exist
86  return;
87 
88  id_list_.erase(it);
89  delete[] data_list_[id];
90  data_list_.erase(id);
91 
92  is_param_changed_ = true;
93 }
94 
95 bool GroupSyncWrite::changeParam(uint8_t id, uint8_t *data)
96 {
97  std::vector<uint8_t>::iterator it = std::find(id_list_.begin(), id_list_.end(), id);
98  if (it == id_list_.end()) // NOT exist
99  return false;
100 
101  delete[] data_list_[id];
102  data_list_[id] = new uint8_t[data_length_];
103  for (int c = 0; c < data_length_; c++)
104  data_list_[id][c] = data[c];
105 
106  is_param_changed_ = true;
107  return true;
108 }
109 
111 {
112  if (id_list_.size() == 0)
113  return;
114 
115  for (unsigned int i = 0; i < id_list_.size(); i++)
116  delete[] data_list_[id_list_[i]];
117 
118  id_list_.clear();
119  data_list_.clear();
120  if (param_ != 0)
121  delete[] param_;
122  param_ = 0;
123 }
124 
126 {
127  if (id_list_.size() == 0)
128  return COMM_NOT_AVAILABLE;
129 
130  if (is_param_changed_ == true || param_ == 0)
131  makeParam();
132 
134 }
void clearParam()
The function that clears the Sync Write list.
virtual int syncWriteTxOnly(PortHandler *port, uint16_t start_address, uint16_t data_length, uint8_t *param, uint16_t param_length)=0
The function that transmits INST_SYNC_WRITE instruction packet The function makes an instruction pac...
bool addParam(uint8_t id, uint8_t *data)
The function that adds id, start_address, data_length to the Sync Write list.
std::map< uint8_t, uint8_t * > data_list_
The class that inherits Protocol1PacketHandler class or Protocol2PacketHandler class.
The class for port control that inherits PortHandlerLinux, PortHandlerWindows, PortHandlerMac, or PortHandlerArduino.
Definition: port_handler.h:56
void removeParam(uint8_t id)
The function that removes id from the Sync Write list.
bool changeParam(uint8_t id, uint8_t *data)
The function that changes the data for write in id -> start_address -> data_length to the Sync Write ...
GroupSyncWrite(PortHandler *port, PacketHandler *ph, uint16_t start_address, uint16_t data_length)
The function that Initializes instance for Sync Write.
#define COMM_NOT_AVAILABLE
int txPacket()
The function that transmits the Sync Write instruction packet which might be constructed by GroupSync...
std::vector< uint8_t > id_list_


dynamixel_sdk
Author(s): Gilbert , Zerom , Darby Lim , Leon
autogenerated on Fri Apr 16 2021 02:25:55