@@ -130,8 +130,9 @@ int SmartServoClass::readByteCmd(uint8_t const id, uint8_t const address) {
130130 * PUBLIC MEMBER FUNCTIONS
131131 **************************************************************************************/
132132
133- int SmartServoClass::ping (uint8_t const id) {
134- _mtx.lock ();
133+ int SmartServoClass::ping (uint8_t const id)
134+ {
135+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
135136 writeCmd (id, SmartServoOperation::PING);
136137 // TODO: check return
137138 receiveResponse (6 );
@@ -140,11 +141,8 @@ int SmartServoClass::ping(uint8_t const id) {
140141 _rxBuf[1 ]==0xf5 &&
141142 _rxBuf[2 ]==id &&
142143 _rxBuf[3 ]==2 ) {
143-
144- _mtx.unlock ();
145144 return _rxBuf[4 ];
146145 }
147- _mtx.unlock ();
148146 _errors++;
149147 if (_onError) _onError ();
150148 return -1 ;
@@ -153,20 +151,21 @@ int SmartServoClass::ping(uint8_t const id) {
153151/*
154152// ATTENTION: RESET also changes the ID of the motor
155153
156- void SmartServoClass::reset(uint8_t const id) {
157- _mtx.lock();
154+ void SmartServoClass::reset(uint8_t const id)
155+ {
156+ mbed::ScopedLock<rtos::Mutex> lock(_mtx);
158157 writeCmd(id, SmartServoOperation::RESET);
159- _mtx.unlock();
160158}
161159*/
162160
163- void SmartServoClass::action (uint8_t const id) {
164- _mtx.lock ();
161+ void SmartServoClass::action (uint8_t const id)
162+ {
163+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
165164 writeCmd (id, SmartServoOperation::ACTION);
166- _mtx.unlock ();
167165}
168166
169- int SmartServoClass::begin () {
167+ int SmartServoClass::begin ()
168+ {
170169 if (_RS485) {
171170 _txPacket.header [0 ] = 0xff ;
172171 _txPacket.header [1 ] = 0xff ;
@@ -186,7 +185,7 @@ void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t
186185 if (!isValidAngle (angle))
187186 return ;
188187
189- _mtx. lock ();
188+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
190189 if (isValidId (id))
191190 {
192191 _targetPosition[id-1 ] = angleToPosition (angle);
@@ -195,26 +194,25 @@ void SmartServoClass::setPosition(uint8_t const id, float const angle, uint16_t
195194 writeWordCmd (id, REG (SmartServoRegister::TARGET_POSITION_H), angleToPosition (angle));
196195 }
197196 }
198- _mtx.unlock ();
199197}
200198
201- float SmartServoClass::getPosition (uint8_t const id) {
202- _mtx.lock ();
199+ float SmartServoClass::getPosition (uint8_t const id)
200+ {
201+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
203202 float ret = -1 ;
204203 if (isValidId (id))
205- ret = positionToAngle (readWordCmd (id, REG (SmartServoRegister::POSITION_H)));
206- _mtx.unlock ();
207- return ret;
204+ return positionToAngle (readWordCmd (id, REG (SmartServoRegister::POSITION_H)));
208205}
209206
210- void SmartServoClass::center (uint8_t const id, uint16_t const position) {
211- _mtx.lock ();
207+ void SmartServoClass::center (uint8_t const id, uint16_t const position)
208+ {
209+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
212210 writeWordCmd (id, REG (SmartServoRegister::CENTER_POINT_ADJ_H), position);
213- _mtx.unlock ();
214211}
215212
216- void SmartServoClass::synchronize () {
217- _mtx.lock ();
213+ void SmartServoClass::synchronize ()
214+ {
215+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
218216 _txPacket.id = 0xFE ;
219217 _txPacket.length = MAX_TX_PAYLOAD_LEN;
220218 _txPacket.instruction = CMD (SmartServoOperation::SYNC_WRITE);
@@ -230,117 +228,116 @@ void SmartServoClass::synchronize() {
230228 _txPacket.payload [index++] = _targetSpeed[idToArrayIndex (i)];
231229 }
232230 sendPacket ();
233- _mtx.unlock ();
234231}
235232
236- void SmartServoClass::setTorque (bool const torque) {
237- _mtx.lock ();
233+ void SmartServoClass::setTorque (bool const torque)
234+ {
235+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
238236 writeByteCmd (BROADCAST, REG (SmartServoRegister::TORQUE_SWITCH), torque ? 1 : 0 );
239- _mtx.unlock ();
240237}
241238
242- void SmartServoClass::setTorque (uint8_t const id, bool const torque) {
243- _mtx.lock ();
239+ void SmartServoClass::setTorque (uint8_t const id, bool const torque)
240+ {
241+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
244242 writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), torque ? 1 : 0 );
245- _mtx.unlock ();
246243}
247244
248- void SmartServoClass::setTime (uint8_t const id, uint16_t const time) {
249- _mtx.lock ();
245+ void SmartServoClass::setTime (uint8_t const id, uint16_t const time)
246+ {
247+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
250248 writeWordCmd (id, REG (SmartServoRegister::RUN_TIME_H), time);
251- _mtx.unlock ();
252249}
253250
254- void SmartServoClass::setMaxTorque (uint16_t const torque) {
255- _mtx.lock ();
251+ void SmartServoClass::setMaxTorque (uint16_t const torque)
252+ {
253+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
256254 writeWordCmd (BROADCAST, REG (SmartServoRegister::MAX_TORQUE_H), torque);
257- _mtx.unlock ();
258255}
259256
260- void SmartServoClass::setMaxTorque (uint8_t const id, uint16_t const torque) {
261- _mtx.lock ();
257+ void SmartServoClass::setMaxTorque (uint8_t const id, uint16_t const torque)
258+ {
259+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
262260 writeWordCmd (id+1 , REG (SmartServoRegister::MAX_TORQUE_H), torque);
263- _mtx.unlock ();
264261}
265262
266- void SmartServoClass::setID (uint8_t const id) {
267- _mtx.lock ();
263+ void SmartServoClass::setID (uint8_t const id)
264+ {
265+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
268266 writeByteCmd (BROADCAST, REG (SmartServoRegister::ID), id);
269- _mtx.unlock ();
270267}
271268
272- void SmartServoClass::engage (uint8_t const id) {
273- _mtx.lock ();
269+ void SmartServoClass::engage (uint8_t const id)
270+ {
271+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
274272 writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), 0x1 );
275- _mtx.unlock ();
276273}
277274
278- void SmartServoClass::disengage (uint8_t const id) {
279- _mtx.lock ();
275+ void SmartServoClass::disengage (uint8_t const id)
276+ {
277+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
280278 writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), 0 );
281- _mtx.unlock ();
282279}
283280
284- bool SmartServoClass::isEngaged (uint8_t const id) {
285- _mtx.lock ();
281+ bool SmartServoClass::isEngaged (uint8_t const id)
282+ {
283+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
286284 int ret = readByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH));
287- _mtx.unlock ();
288285 return ret != 0 ;
289286}
290287
291- void SmartServoClass::setStallProtectionTime (uint8_t const time) {
292- _mtx.lock ();
288+ void SmartServoClass::setStallProtectionTime (uint8_t const time)
289+ {
290+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
293291 writeByteCmd (BROADCAST, REG (SmartServoRegister::STALL_PROTECTION_TIME), time);
294- _mtx.unlock ();
295292}
296293
297- void SmartServoClass::setStallProtectionTime (uint8_t const id, uint8_t const time) {
298- _mtx.lock ();
294+ void SmartServoClass::setStallProtectionTime (uint8_t const id, uint8_t const time)
295+ {
296+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
299297 writeByteCmd (id, REG (SmartServoRegister::STALL_PROTECTION_TIME), time);
300- _mtx.unlock ();
301298}
302299
303300void SmartServoClass::setMinAngle (uint16_t const min_angle)
304301{
305- _mtx. lock ();
302+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
306303 writeWordCmd (BROADCAST, REG (SmartServoRegister::MIN_ANGLE_LIMIT_H), min_angle);
307- _mtx.unlock ();
308304}
309305
310306void SmartServoClass::setMinAngle (uint8_t const id, uint16_t const min_angle)
311307{
312- _mtx. lock ();
308+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
313309 writeWordCmd (id, REG (SmartServoRegister::MIN_ANGLE_LIMIT_H), min_angle);
314- _mtx.unlock ();
315310}
316311
317312void SmartServoClass::setMaxAngle (uint16_t const max_angle)
318313{
319- _mtx. lock ();
314+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
320315 writeWordCmd (BROADCAST, REG (SmartServoRegister::MAX_ANGLE_LIMIT_H), max_angle);
321- _mtx.unlock ();
322316}
323317
324318void SmartServoClass::setMaxAngle (uint8_t const id, uint16_t const max_angle)
325319{
326- _mtx. lock ();
320+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
327321 writeWordCmd (id, REG (SmartServoRegister::MAX_ANGLE_LIMIT_H), max_angle);
328- _mtx.unlock ();
329322}
330323
331- void SmartServoClass::getInfo (Stream & stream, uint8_t const id) {
324+ void SmartServoClass::getInfo (Stream & stream, uint8_t const id)
325+ {
332326 uint8_t regs[65 ];
333327 memset (regs, 0x55 , sizeof (regs));
334328 int i = 0 ;
335- _mtx.lock ();
336- while (i < sizeof (regs)) {
337- if ((i > 29 && i < 40 ) || (i > 48 && i < 56 )) {
338- i++;
339- continue ;
329+
330+ {
331+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
332+ while (i < sizeof (regs)) {
333+ if ((i > 29 && i < 40 ) || (i > 48 && i < 56 )) {
334+ i++;
335+ continue ;
336+ }
337+ regs[i++] = readByteCmd (id, i);
340338 }
341- regs[i++] = readByteCmd (id, i);
342339 }
343- _mtx. unlock ();
340+
344341 stream.println (" regs map:" );
345342 for (i = 0 ; i < sizeof (regs); i++) {
346343 stream.println (String (i, HEX) + " : " + String (regs[i], HEX));
0 commit comments