@@ -12,6 +12,7 @@ SmartServoClass::SmartServoClass(RS485Class & RS485)
12
12
: _RS485{RS485}
13
13
, _errors{0 }
14
14
, _onError{}
15
+ , _mtx{}
15
16
{
16
17
17
18
}
@@ -129,8 +130,9 @@ int SmartServoClass::readByteCmd(uint8_t const id, uint8_t const address) {
129
130
* PUBLIC MEMBER FUNCTIONS
130
131
**************************************************************************************/
131
132
132
- int SmartServoClass::ping (uint8_t const id) {
133
- mutex.lock ();
133
+ int SmartServoClass::ping (uint8_t const id)
134
+ {
135
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
134
136
writeCmd (id, SmartServoOperation::PING);
135
137
// TODO: check return
136
138
receiveResponse (6 );
@@ -139,11 +141,8 @@ int SmartServoClass::ping(uint8_t const id) {
139
141
_rxBuf[1 ]==0xf5 &&
140
142
_rxBuf[2 ]==id &&
141
143
_rxBuf[3 ]==2 ) {
142
-
143
- mutex.unlock ();
144
144
return _rxBuf[4 ];
145
145
}
146
- mutex.unlock ();
147
146
_errors++;
148
147
if (_onError) _onError ();
149
148
return -1 ;
@@ -152,68 +151,63 @@ int SmartServoClass::ping(uint8_t const id) {
152
151
/*
153
152
// ATTENTION: RESET also changes the ID of the motor
154
153
155
- void SmartServoClass::reset(uint8_t const id) {
156
- mutex.lock();
154
+ void SmartServoClass::reset(uint8_t const id)
155
+ {
156
+ mbed::ScopedLock<rtos::Mutex> lock(_mtx);
157
157
writeCmd(id, SmartServoOperation::RESET);
158
- mutex.unlock();
159
158
}
160
159
*/
161
160
162
- void SmartServoClass::action (uint8_t const id) {
163
- mutex.lock ();
161
+ void SmartServoClass::action (uint8_t const id)
162
+ {
163
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
164
164
writeCmd (id, SmartServoOperation::ACTION);
165
- mutex.unlock ();
166
- }
167
-
168
- int SmartServoClass::begin () {
169
- if (_RS485) {
170
- _txPacket.header [0 ] = 0xff ;
171
- _txPacket.header [1 ] = 0xff ;
172
- _RS485.begin (115200 , 0 , 90 );
173
- _RS485.receive ();
174
- writeByteCmd (BROADCAST, REG (SmartServoRegister::SERVO_MOTOR_MODE), 1 );
175
- writeByteCmd (BROADCAST, REG (SmartServoRegister::TORQUE_SWITCH) ,1 );
176
- _positionMode = PositionMode::IMMEDIATE;
177
- return 0 ;
178
- } else {
179
- return -1 ;
180
- }
165
+ }
166
+
167
+ void SmartServoClass::begin ()
168
+ {
169
+ _txPacket.header [0 ] = 0xff ;
170
+ _txPacket.header [1 ] = 0xff ;
171
+ _RS485.begin (115200 , 0 , 90 );
172
+ _RS485.receive ();
173
+ writeByteCmd (BROADCAST, REG (SmartServoRegister::SERVO_MOTOR_MODE), 1 );
174
+ writeByteCmd (BROADCAST, REG (SmartServoRegister::TORQUE_SWITCH) ,1 );
175
+ _positionMode = PositionMode::IMMEDIATE;
181
176
}
182
177
183
178
void SmartServoClass::setPosition (uint8_t const id, float const angle, uint16_t const speed)
184
179
{
185
180
if (!isValidAngle (angle))
186
181
return ;
187
182
188
- mutex. lock ();
183
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
189
184
if (isValidId (id))
190
185
{
191
- _targetPosition[id- 1 ] = angleToPosition (angle);
192
- _targetSpeed[id- 1 ] = speed;
186
+ _targetPosition[idToArrayIndex (id) ] = angleToPosition (angle);
187
+ _targetSpeed[idToArrayIndex (id) ] = speed;
193
188
if (_positionMode==PositionMode::IMMEDIATE) {
194
189
writeWordCmd (id, REG (SmartServoRegister::TARGET_POSITION_H), angleToPosition (angle));
195
190
}
196
191
}
197
- mutex.unlock ();
198
192
}
199
193
200
- float SmartServoClass::getPosition (uint8_t const id) {
201
- mutex.lock ();
194
+ float SmartServoClass::getPosition (uint8_t const id)
195
+ {
196
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
202
197
float ret = -1 ;
203
198
if (isValidId (id))
204
- ret = positionToAngle (readWordCmd (id, REG (SmartServoRegister::POSITION_H)));
205
- mutex.unlock ();
206
- return ret;
199
+ return positionToAngle (readWordCmd (id, REG (SmartServoRegister::POSITION_H)));
207
200
}
208
201
209
- void SmartServoClass::center (uint8_t const id, uint16_t const position) {
210
- mutex.lock ();
202
+ void SmartServoClass::center (uint8_t const id, uint16_t const position)
203
+ {
204
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
211
205
writeWordCmd (id, REG (SmartServoRegister::CENTER_POINT_ADJ_H), position);
212
- mutex.unlock ();
213
206
}
214
207
215
- void SmartServoClass::synchronize () {
216
- mutex.lock ();
208
+ void SmartServoClass::synchronize ()
209
+ {
210
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
217
211
_txPacket.id = 0xFE ;
218
212
_txPacket.length = MAX_TX_PAYLOAD_LEN;
219
213
_txPacket.instruction = CMD (SmartServoOperation::SYNC_WRITE);
@@ -229,117 +223,116 @@ void SmartServoClass::synchronize() {
229
223
_txPacket.payload [index++] = _targetSpeed[idToArrayIndex (i)];
230
224
}
231
225
sendPacket ();
232
- mutex.unlock ();
233
226
}
234
227
235
- void SmartServoClass::setTorque (bool const torque) {
236
- mutex.lock ();
228
+ void SmartServoClass::setTorque (bool const torque)
229
+ {
230
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
237
231
writeByteCmd (BROADCAST, REG (SmartServoRegister::TORQUE_SWITCH), torque ? 1 : 0 );
238
- mutex.unlock ();
239
232
}
240
233
241
- void SmartServoClass::setTorque (uint8_t const id, bool const torque) {
242
- mutex.lock ();
234
+ void SmartServoClass::setTorque (uint8_t const id, bool const torque)
235
+ {
236
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
243
237
writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), torque ? 1 : 0 );
244
- mutex.unlock ();
245
238
}
246
239
247
- void SmartServoClass::setTime (uint8_t const id, uint16_t const time) {
248
- mutex.lock ();
240
+ void SmartServoClass::setTime (uint8_t const id, uint16_t const time)
241
+ {
242
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
249
243
writeWordCmd (id, REG (SmartServoRegister::RUN_TIME_H), time);
250
- mutex.unlock ();
251
244
}
252
245
253
- void SmartServoClass::setMaxTorque (uint16_t const torque) {
254
- mutex.lock ();
246
+ void SmartServoClass::setMaxTorque (uint16_t const torque)
247
+ {
248
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
255
249
writeWordCmd (BROADCAST, REG (SmartServoRegister::MAX_TORQUE_H), torque);
256
- mutex.unlock ();
257
250
}
258
251
259
- void SmartServoClass::setMaxTorque (uint8_t const id, uint16_t const torque) {
260
- mutex.lock ();
252
+ void SmartServoClass::setMaxTorque (uint8_t const id, uint16_t const torque)
253
+ {
254
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
261
255
writeWordCmd (id+1 , REG (SmartServoRegister::MAX_TORQUE_H), torque);
262
- mutex.unlock ();
263
256
}
264
257
265
- void SmartServoClass::setID (uint8_t const id) {
266
- mutex.lock ();
258
+ void SmartServoClass::setID (uint8_t const id)
259
+ {
260
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
267
261
writeByteCmd (BROADCAST, REG (SmartServoRegister::ID), id);
268
- mutex.unlock ();
269
262
}
270
263
271
- void SmartServoClass::engage (uint8_t const id) {
272
- mutex.lock ();
264
+ void SmartServoClass::engage (uint8_t const id)
265
+ {
266
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
273
267
writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), 0x1 );
274
- mutex.unlock ();
275
268
}
276
269
277
- void SmartServoClass::disengage (uint8_t const id) {
278
- mutex.lock ();
270
+ void SmartServoClass::disengage (uint8_t const id)
271
+ {
272
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
279
273
writeByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH), 0 );
280
- mutex.unlock ();
281
274
}
282
275
283
- bool SmartServoClass::isEngaged (uint8_t const id) {
284
- mutex.lock ();
276
+ bool SmartServoClass::isEngaged (uint8_t const id)
277
+ {
278
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
285
279
int ret = readByteCmd (id, REG (SmartServoRegister::TORQUE_SWITCH));
286
- mutex.unlock ();
287
280
return ret != 0 ;
288
281
}
289
282
290
- void SmartServoClass::setStallProtectionTime (uint8_t const time) {
291
- mutex.lock ();
283
+ void SmartServoClass::setStallProtectionTime (uint8_t const time)
284
+ {
285
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
292
286
writeByteCmd (BROADCAST, REG (SmartServoRegister::STALL_PROTECTION_TIME), time);
293
- mutex.unlock ();
294
287
}
295
288
296
- void SmartServoClass::setStallProtectionTime (uint8_t const id, uint8_t const time) {
297
- mutex.lock ();
289
+ void SmartServoClass::setStallProtectionTime (uint8_t const id, uint8_t const time)
290
+ {
291
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
298
292
writeByteCmd (id, REG (SmartServoRegister::STALL_PROTECTION_TIME), time);
299
- mutex.unlock ();
300
293
}
301
294
302
295
void SmartServoClass::setMinAngle (uint16_t const min_angle)
303
296
{
304
- mutex. lock ();
297
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
305
298
writeWordCmd (BROADCAST, REG (SmartServoRegister::MIN_ANGLE_LIMIT_H), min_angle);
306
- mutex.unlock ();
307
299
}
308
300
309
301
void SmartServoClass::setMinAngle (uint8_t const id, uint16_t const min_angle)
310
302
{
311
- mutex. lock ();
303
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
312
304
writeWordCmd (id, REG (SmartServoRegister::MIN_ANGLE_LIMIT_H), min_angle);
313
- mutex.unlock ();
314
305
}
315
306
316
307
void SmartServoClass::setMaxAngle (uint16_t const max_angle)
317
308
{
318
- mutex. lock ();
309
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
319
310
writeWordCmd (BROADCAST, REG (SmartServoRegister::MAX_ANGLE_LIMIT_H), max_angle);
320
- mutex.unlock ();
321
311
}
322
312
323
313
void SmartServoClass::setMaxAngle (uint8_t const id, uint16_t const max_angle)
324
314
{
325
- mutex. lock ();
315
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx );
326
316
writeWordCmd (id, REG (SmartServoRegister::MAX_ANGLE_LIMIT_H), max_angle);
327
- mutex.unlock ();
328
317
}
329
318
330
- void SmartServoClass::getInfo (Stream & stream, uint8_t const id) {
319
+ void SmartServoClass::getInfo (Stream & stream, uint8_t const id)
320
+ {
331
321
uint8_t regs[65 ];
332
322
memset (regs, 0x55 , sizeof (regs));
333
323
int i = 0 ;
334
- mutex.lock ();
335
- while (i < sizeof (regs)) {
336
- if ((i > 29 && i < 40 ) || (i > 48 && i < 56 )) {
337
- i++;
338
- continue ;
324
+
325
+ {
326
+ mbed::ScopedLock<rtos::Mutex> lock (_mtx);
327
+ while (i < sizeof (regs)) {
328
+ if ((i > 29 && i < 40 ) || (i > 48 && i < 56 )) {
329
+ i++;
330
+ continue ;
331
+ }
332
+ regs[i++] = readByteCmd (id, i);
339
333
}
340
- regs[i++] = readByteCmd (id, i);
341
334
}
342
- mutex. unlock ();
335
+
343
336
stream.println (" regs map:" );
344
337
for (i = 0 ; i < sizeof (regs); i++) {
345
338
stream.println (String (i, HEX) + " : " + String (regs[i], HEX));
0 commit comments