|
31 | 31 | SmartServoClass::SmartServoClass(RS485Class & RS485)
|
32 | 32 | : _RS485{RS485}
|
33 | 33 | , _errors{0}
|
| 34 | +, _angular_velocity_deg_per_sec{DEFAULT_ANGULAR_VELOCITY_deg_per_sec} |
34 | 35 | , _onError{}
|
35 | 36 | , _mtx{}
|
36 | 37 | {
|
@@ -198,27 +199,43 @@ void SmartServoClass::begin()
|
198 | 199 | _targetSpeed[idToArrayIndex(i)] = 1000;
|
199 | 200 | }
|
200 | 201 |
|
201 |
| -void SmartServoClass::setPosition(uint8_t const id, float const angle) |
| 202 | +void SmartServoClass::setPosition(uint8_t const id, float const angle_deg) |
202 | 203 | {
|
203 |
| - if (!isValidAngle(angle)) |
| 204 | + if (!isValidAngle(angle_deg)) |
204 | 205 | return;
|
205 | 206 |
|
206 |
| - mbed::ScopedLock<rtos::Mutex> lock(_mtx); |
207 |
| - if (isValidId(id)) |
| 207 | + if (!isValidId(id)) |
| 208 | + return; |
| 209 | + |
| 210 | + float const target_position_deg = angle_deg; |
| 211 | + float const actual_position_deg = getPosition(id); |
| 212 | + if (actual_position_deg < 0.0f) |
| 213 | + return; |
| 214 | + |
| 215 | + float const abs_position_diff_deg = fabs(target_position_deg - actual_position_deg); |
| 216 | + float const limited_runtime_sec = abs_position_diff_deg / _angular_velocity_deg_per_sec; |
| 217 | + uint16_t const limited_runtime_ms = static_cast<uint16_t>(limited_runtime_sec * 1000.f); |
| 218 | + |
| 219 | + if (_positionMode == PositionMode::IMMEDIATE) |
208 | 220 | {
|
209 |
| - _targetPosition[idToArrayIndex(id)] = angleToPosition(angle); |
210 |
| - if (_positionMode==PositionMode::IMMEDIATE) { |
211 |
| - writeWordCmd(id, REG(SmartServoRegister::TARGET_POSITION_H), angleToPosition(angle)); |
212 |
| - } |
213 |
| - } |
| 221 | + mbed::ScopedLock<rtos::Mutex> lock(_mtx); |
| 222 | + writeWordCmd(id, REG(SmartServoRegister::RUN_TIME_H), limited_runtime_ms); |
| 223 | + writeWordCmd(id, REG(SmartServoRegister::TARGET_POSITION_H), angleToPosition(target_position_deg)); |
| 224 | + } |
| 225 | + else if (_positionMode == PositionMode::SYNC) |
| 226 | + { |
| 227 | + _targetSpeed[idToArrayIndex(id)] = limited_runtime_ms; |
| 228 | + _targetPosition[idToArrayIndex(id)] = angleToPosition(target_position_deg); |
| 229 | + } |
214 | 230 | }
|
215 | 231 |
|
216 | 232 | float SmartServoClass::getPosition(uint8_t const id)
|
217 | 233 | {
|
| 234 | + if (!isValidId(id)) |
| 235 | + return -1.0f; |
| 236 | + |
218 | 237 | mbed::ScopedLock<rtos::Mutex> lock(_mtx);
|
219 |
| - float ret = -1; |
220 |
| - if (isValidId(id)) |
221 |
| - return positionToAngle(readWordCmd(id, REG(SmartServoRegister::POSITION_H))); |
| 238 | + return positionToAngle(readWordCmd(id, REG(SmartServoRegister::POSITION_H))); |
222 | 239 | }
|
223 | 240 |
|
224 | 241 | void SmartServoClass::center(uint8_t const id, uint16_t const position)
|
@@ -261,16 +278,24 @@ void SmartServoClass::setTorque(uint8_t const id, bool const torque)
|
261 | 278 |
|
262 | 279 | void SmartServoClass::setTime(uint16_t const time)
|
263 | 280 | {
|
264 |
| - mbed::ScopedLock<rtos::Mutex> lock(_mtx); |
265 | 281 | for (int i = MIN_MOTOR_ID; i <= MAX_MOTOR_ID; i++)
|
266 | 282 | _targetSpeed[idToArrayIndex(i)] = time;
|
| 283 | + |
| 284 | + mbed::ScopedLock<rtos::Mutex> lock(_mtx); |
267 | 285 | writeWordCmd(BROADCAST, REG(SmartServoRegister::RUN_TIME_H), time);
|
268 | 286 | }
|
269 | 287 |
|
270 | 288 | void SmartServoClass::setTime(uint8_t const id, uint16_t const time)
|
271 | 289 | {
|
| 290 | + if (!isValidId(id)) |
| 291 | + return; |
| 292 | + |
| 293 | + if (id == BROADCAST) |
| 294 | + return; |
| 295 | + |
| 296 | + _targetSpeed[idToArrayIndex(id)] = time; |
| 297 | + |
272 | 298 | mbed::ScopedLock<rtos::Mutex> lock(_mtx);
|
273 |
| - if ((id >= MIN_MOTOR_ID) && (id <= MAX_MOTOR_ID)) _targetSpeed[idToArrayIndex(id)] = time; |
274 | 299 | writeWordCmd(id, REG(SmartServoRegister::RUN_TIME_H), time);
|
275 | 300 | }
|
276 | 301 |
|
|
0 commit comments