@@ -12,6 +12,7 @@ SmartServoClass::SmartServoClass(RS485Class & RS485)
1212: _RS485{RS485}
1313, _errors{0 }
1414, _onError{}
15+ , _mtx{}
1516{
1617
1718}
@@ -130,7 +131,7 @@ int SmartServoClass::readByteCmd(uint8_t const id, uint8_t const address) {
130131 **************************************************************************************/
131132
132133int SmartServoClass::ping (uint8_t const id) {
133- mutex .lock ();
134+ _mtx .lock ();
134135 writeCmd (id, SmartServoOperation::PING);
135136 // TODO: check return
136137 receiveResponse (6 );
@@ -140,10 +141,10 @@ int SmartServoClass::ping(uint8_t const id) {
140141 _rxBuf[2 ]==id &&
141142 _rxBuf[3 ]==2 ) {
142143
143- mutex .unlock ();
144+ _mtx .unlock ();
144145 return _rxBuf[4 ];
145146 }
146- mutex .unlock ();
147+ _mtx .unlock ();
147148 _errors++;
148149 if (_onError) _onError ();
149150 return -1 ;
@@ -153,16 +154,16 @@ int SmartServoClass::ping(uint8_t const id) {
153154// ATTENTION: RESET also changes the ID of the motor
154155
155156void SmartServoClass::reset(uint8_t const id) {
156- mutex .lock();
157+ _mtx .lock();
157158 writeCmd(id, SmartServoOperation::RESET);
158- mutex .unlock();
159+ _mtx .unlock();
159160}
160161*/
161162
162163void SmartServoClass::action (uint8_t const id) {
163- mutex .lock ();
164+ _mtx .lock ();
164165 writeCmd (id, SmartServoOperation::ACTION);
165- mutex .unlock ();
166+ _mtx .unlock ();
166167}
167168
168169int SmartServoClass::begin () {
@@ -185,7 +186,7 @@ void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t
185186 if (!isValidAngle (angle))
186187 return ;
187188
188- mutex .lock ();
189+ _mtx .lock ();
189190 if (isValidId (id))
190191 {
191192 _targetPosition[id-1 ] = angleToPosition (angle);
@@ -194,26 +195,26 @@ void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t
194195 writeWordCmd (id, REG (SmartServoRegister::TARGET_POSITION_H), angleToPosition (angle));
195196 }
196197 }
197- mutex .unlock ();
198+ _mtx .unlock ();
198199}
199200
200201float SmartServoClass::getPosition (uint8_t const id) {
201- mutex .lock ();
202+ _mtx .lock ();
202203 float ret = -1 ;
203204 if (isValidId (id))
204205 ret = positionToAngle (readWordCmd (id, REG (SmartServoRegister::POSITION_H)));
205- mutex .unlock ();
206+ _mtx .unlock ();
206207 return ret;
207208}
208209
209210void SmartServoClass::center (uint8_t const id, uint16_t const position) {
210- mutex .lock ();
211+ _mtx .lock ();
211212 writeWordCmd (id, REG (SmartServoRegister::CENTER_POINT_ADJ_H), position);
212- mutex .unlock ();
213+ _mtx .unlock ();
213214}
214215
215216void SmartServoClass::synchronize () {
216- mutex .lock ();
217+ _mtx .lock ();
217218 _txPacket.id = 0xFE ;
218219 _txPacket.length = MAX_TX_PAYLOAD_LEN;
219220 _txPacket.instruction = CMD (SmartServoOperation::SYNC_WRITE);
@@ -229,117 +230,117 @@ void SmartServoClass::synchronize() {
229230 _txPacket.payload [index++] = _targetSpeed[idToArrayIndex (i)];
230231 }
231232 sendPacket ();
232- mutex .unlock ();
233+ _mtx .unlock ();
233234}
234235
235236void SmartServoClass::setTorque (bool const torque) {
236- mutex .lock ();
237+ _mtx .lock ();
237238 writeByteCmd (BROADCAST, REG (SmartServoRegister::TORQUE_SWITCH), torque ? 1 : 0 );
238- mutex .unlock ();
239+ _mtx .unlock ();
239240}
240241
241242void SmartServoClass::setTorque (uint8_t const id, bool const torque) {
242- mutex .lock ();
243+ _mtx .lock ();
243244 writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), torque ? 1 : 0 );
244- mutex .unlock ();
245+ _mtx .unlock ();
245246}
246247
247248void SmartServoClass::setTime (uint8_t const id, uint16_t const time) {
248- mutex .lock ();
249+ _mtx .lock ();
249250 writeWordCmd (id, REG (SmartServoRegister::RUN_TIME_H), time);
250- mutex .unlock ();
251+ _mtx .unlock ();
251252}
252253
253254void SmartServoClass::setMaxTorque (uint16_t const torque) {
254- mutex .lock ();
255+ _mtx .lock ();
255256 writeWordCmd (BROADCAST, REG (SmartServoRegister::MAX_TORQUE_H), torque);
256- mutex .unlock ();
257+ _mtx .unlock ();
257258}
258259
259260void SmartServoClass::setMaxTorque (uint8_t const id, uint16_t const torque) {
260- mutex .lock ();
261+ _mtx .lock ();
261262 writeWordCmd (id+1 , REG (SmartServoRegister::MAX_TORQUE_H), torque);
262- mutex .unlock ();
263+ _mtx .unlock ();
263264}
264265
265266void SmartServoClass::setID (uint8_t const id) {
266- mutex .lock ();
267+ _mtx .lock ();
267268 writeByteCmd (BROADCAST, REG (SmartServoRegister::ID), id);
268- mutex .unlock ();
269+ _mtx .unlock ();
269270}
270271
271272void SmartServoClass::engage (uint8_t const id) {
272- mutex .lock ();
273+ _mtx .lock ();
273274 writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), 0x1 );
274- mutex .unlock ();
275+ _mtx .unlock ();
275276}
276277
277278void SmartServoClass::disengage (uint8_t const id) {
278- mutex .lock ();
279+ _mtx .lock ();
279280 writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), 0 );
280- mutex .unlock ();
281+ _mtx .unlock ();
281282}
282283
283284bool SmartServoClass::isEngaged (uint8_t const id) {
284- mutex .lock ();
285+ _mtx .lock ();
285286 int ret = readByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH));
286- mutex .unlock ();
287+ _mtx .unlock ();
287288 return ret != 0 ;
288289}
289290
290291void SmartServoClass::setStallProtectionTime (uint8_t const time) {
291- mutex .lock ();
292+ _mtx .lock ();
292293 writeByteCmd (BROADCAST, REG (SmartServoRegister::STALL_PROTECTION_TIME), time);
293- mutex .unlock ();
294+ _mtx .unlock ();
294295}
295296
296297void SmartServoClass::setStallProtectionTime (uint8_t const id, uint8_t const time) {
297- mutex .lock ();
298+ _mtx .lock ();
298299 writeByteCmd (id, REG (SmartServoRegister::STALL_PROTECTION_TIME), time);
299- mutex .unlock ();
300+ _mtx .unlock ();
300301}
301302
302303void SmartServoClass::setMinAngle (uint16_t const min_angle)
303304{
304- mutex .lock ();
305+ _mtx .lock ();
305306 writeWordCmd (BROADCAST, REG (SmartServoRegister::MIN_ANGLE_LIMIT_H), min_angle);
306- mutex .unlock ();
307+ _mtx .unlock ();
307308}
308309
309310void SmartServoClass::setMinAngle (uint8_t const id, uint16_t const min_angle)
310311{
311- mutex .lock ();
312+ _mtx .lock ();
312313 writeWordCmd (id, REG (SmartServoRegister::MIN_ANGLE_LIMIT_H), min_angle);
313- mutex .unlock ();
314+ _mtx .unlock ();
314315}
315316
316317void SmartServoClass::setMaxAngle (uint16_t const max_angle)
317318{
318- mutex .lock ();
319+ _mtx .lock ();
319320 writeWordCmd (BROADCAST, REG (SmartServoRegister::MAX_ANGLE_LIMIT_H), max_angle);
320- mutex .unlock ();
321+ _mtx .unlock ();
321322}
322323
323324void SmartServoClass::setMaxAngle (uint8_t const id, uint16_t const max_angle)
324325{
325- mutex .lock ();
326+ _mtx .lock ();
326327 writeWordCmd (id, REG (SmartServoRegister::MAX_ANGLE_LIMIT_H), max_angle);
327- mutex .unlock ();
328+ _mtx .unlock ();
328329}
329330
330331void SmartServoClass::getInfo (Stream & stream, uint8_t const id) {
331332 uint8_t regs[65 ];
332333 memset (regs, 0x55 , sizeof (regs));
333334 int i = 0 ;
334- mutex .lock ();
335+ _mtx .lock ();
335336 while (i < sizeof (regs)) {
336337 if ((i > 29 && i < 40 ) || (i > 48 && i < 56 )) {
337338 i++;
338339 continue ;
339340 }
340341 regs[i++] = readByteCmd (id, i);
341342 }
342- mutex .unlock ();
343+ _mtx .unlock ();
343344 stream.println (" regs map:" );
344345 for (i = 0 ; i < sizeof (regs); i++) {
345346 stream.println (String (i, HEX) + " : " + String (regs[i], HEX));
0 commit comments