Skip to content

Add configuration of joint angular velocity and replace the outdated speed API. #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ RecordAndReplayApp app;

void setup()
{
if (Braccio.begin(custom_main_menu)) {
if (Braccio.begin(custom_main_menu))
{
app.enableButtons();
/* Allow greater angular velocity than the default one. */
Braccio.setAngularVelocity(45.0f);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ void setup()

if (Braccio.begin(directionScreen))
{
/* Configure Braccio. */
Braccio.speed(speed_grade_t(120)/*MEDIUM*/);
/* Move to home position. */
Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]);
delay(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ void setup() {
Braccio.begin();
delay(500); // Waits for the Braccio initialization

// You can choose the speed beforehand with
Braccio.speed(SLOW); // could be FAST or MEDIUM or SLOW

// Send motors initial angle
gripper.move().to(initialGripper);
delay(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ void setup() {
Braccio.begin();
delay(500); // Waits for the Braccio initialization

// You can choose the speed beforehand with
Braccio.speed(SLOW); // could be FAST or MEDIUM or SLOW

// Set motors initial angle
// Should move all the motors at once
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ void setup() {
Braccio.begin();
delay(500); // Waits for the Braccio initialization

// You can choose the speed beforehand with
Braccio.speed(SLOW); // could be FAST or MEDIUM or SLOW

// Set motors initial angle
// Should move all the motors at once
Braccio.moveTo(homePos[0], homePos[1], homePos[2], homePos[3], homePos[4], homePos[5]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ void setup()

if (Braccio.begin(directionScreen))
{
/* Configure Braccio. */
Braccio.speed(speed_grade_t(120)/*MEDIUM*/);
/* Move to home position. */
Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]);
delay(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ void setup()

if (Braccio.begin(directionScreen))
{
/* Configure Braccio. */
Braccio.speed(speed_grade_t(120)/*MEDIUM*/);
/* Move to home position. */
Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]);
delay(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ void setup()
{
if (Braccio.begin(custom_main_menu)) {
app.enableButtons();
/* Allow greater angular velocity than the default one. */
Braccio.setAngularVelocity(45.0f);
}
}

Expand Down
9 changes: 5 additions & 4 deletions examples/Tools/Braccio_Basic/Braccio_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ void setup()

Braccio.moveTo(home_position[0], home_position[1], home_position[2], home_position[3], home_position[4], home_position[5]);
delay(1000);
Braccio.setAngularVelocity(45.0f); /* 45 deg/sec */
}

void loop()
{
if (move_joint)
{
Braccio.move(4).to((SmartServoClass::MAX_ANGLE / 2.0f) - 45.0f).in(1s);
delay(1000);
Braccio.move(4).to((SmartServoClass::MAX_ANGLE / 2.0f) + 45.0f).in(1s);
delay(1000);
Braccio.move(4).to((SmartServoClass::MAX_ANGLE / 2.0f) - 45.0f);
delay(2000);
Braccio.move(4).to((SmartServoClass::MAX_ANGLE / 2.0f) + 45.0f);
delay(2000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool set_initial_servo_position(int const id, float const target_angle)
for ( float current_angle = Braccio.get(id).position();
!isTargetAngleReached(EPSILON) && !isTimeout(start);)
{
Braccio.get(id).move().to(target_angle).in(200ms);
Braccio.get(id).move().to(target_angle);
delay(250);

char msg[64] = {0};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ void test_motor(int const id)
delay(500);

Serial.print("Drive to start . ");
Braccio.get(id).move().to(0.0f).in(1s);
Braccio.get(id).move().to(0.0f);
Serial.println("OK.");
delay(1500);

for (float target_angle = 0.0f; target_angle < SmartServoClass::MAX_ANGLE; target_angle += 1.0f)
{
Braccio.get(id).move().to(target_angle).in(200ms);
Braccio.get(id).move().to(target_angle);
delay(250);

char msg[64] = {0};
Expand Down
3 changes: 2 additions & 1 deletion src/Braccio++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ bool BraccioClass::begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_
lvgl_defaultMenu();

_servos.begin();
_servos.setTime(SLOW);
_servos.setMaxTorque(SmartServoClass::TORQUE_MAX);
_servos.setAngularVelocity(SmartServoClass::DEFAULT_ANGULAR_VELOCITY_deg_per_sec);
_servos.setPositionMode(PositionMode::IMMEDIATE);

_motors_connected_thd.start(mbed::callback(this, &BraccioClass::motorConnectedThreadFunc));
Expand Down
21 changes: 4 additions & 17 deletions src/Braccio++.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@
#include <chrono>
using namespace std::chrono;

/**************************************************************************************
* TYPEDEF
**************************************************************************************/

enum speed_grade_t {
FAST = 10,
MEDIUM = 100,
SLOW = 1000,
};

/**************************************************************************************
* FORWARD DECLARATION
**************************************************************************************/
Expand Down Expand Up @@ -83,8 +73,8 @@ class BraccioClass
inline void setMaxTorque(uint16_t const max_torque) { _servos.setMaxTorque(max_torque); }
inline void setMaxTorque(int const id, uint16_t const max_torque) { _servos.setMaxTorque(id, max_torque); }

inline void speed(speed_grade_t const speed_grade) { _servos.setTime(speed_grade); }
inline void speed(int const id, speed_grade_t const speed_grade) { _servos.setTime(id, speed_grade); }
inline void setAngularVelocity(float const angular_velocity_deg_per_sec) { _servos.setAngularVelocity(angular_velocity_deg_per_sec); }
inline float getAngularVelocity() const { return _servos.getAngularVelocity(); }

inline void disengage(int const id = SmartServoClass::BROADCAST) { _servos.disengage(id); }
inline void engage (int const id = SmartServoClass::BROADCAST) { _servos.engage(id); }
Expand Down Expand Up @@ -189,15 +179,12 @@ class Servo
inline bool engaged() { return _servos.isEngaged(_id); }
inline void setMaxTorque(uint16_t const max_torque) { _servos.setMaxTorque(_id, max_torque); }

inline Servo & move() { return *this; }
inline Servo & to (float const angle) { _servos.setPosition(_id, angle); return *this; }
inline Servo & in (std::chrono::milliseconds const len) { _servos.setTime(_id, len.count()); return *this; }
inline Servo & move() { return *this; }
inline Servo & to (float const angle) { _servos.setPosition(_id, angle); return *this; }

inline float position() { return _servos.getPosition(_id); }
inline bool connected() { return Braccio.connected(_id); }
inline void info(Stream & stream) { _servos.getInfo(stream, _id); }
inline uint16_t runtime() { return _servos.getTime(_id); }
inline void setRuntime(uint16_t const time) { _servos.setTime(_id, time); }

operator bool() { return connected(); }

Expand Down
53 changes: 39 additions & 14 deletions src/lib/motors/SmartServo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
SmartServoClass::SmartServoClass(RS485Class & RS485)
: _RS485{RS485}
, _errors{0}
, _angular_velocity_deg_per_sec{DEFAULT_ANGULAR_VELOCITY_deg_per_sec}
, _onError{}
, _mtx{}
{
Expand Down Expand Up @@ -198,27 +199,43 @@ void SmartServoClass::begin()
_targetSpeed[idToArrayIndex(i)] = 1000;
}

void SmartServoClass::setPosition(uint8_t const id, float const angle)
void SmartServoClass::setPosition(uint8_t const id, float const angle_deg)
{
if (!isValidAngle(angle))
if (!isValidAngle(angle_deg))
return;

mbed::ScopedLock<rtos::Mutex> lock(_mtx);
if (isValidId(id))
if (!isValidId(id))
return;

float const target_position_deg = angle_deg;
float const actual_position_deg = getPosition(id);
if (actual_position_deg < 0.0f)
return;

float const abs_position_diff_deg = fabs(target_position_deg - actual_position_deg);
float const limited_runtime_sec = abs_position_diff_deg / _angular_velocity_deg_per_sec;
uint16_t const limited_runtime_ms = static_cast<uint16_t>(limited_runtime_sec * 1000.f);

if (_positionMode == PositionMode::IMMEDIATE)
{
_targetPosition[idToArrayIndex(id)] = angleToPosition(angle);
if (_positionMode==PositionMode::IMMEDIATE) {
writeWordCmd(id, REG(SmartServoRegister::TARGET_POSITION_H), angleToPosition(angle));
}
}
mbed::ScopedLock<rtos::Mutex> lock(_mtx);
writeWordCmd(id, REG(SmartServoRegister::RUN_TIME_H), limited_runtime_ms);
writeWordCmd(id, REG(SmartServoRegister::TARGET_POSITION_H), angleToPosition(target_position_deg));
}
else if (_positionMode == PositionMode::SYNC)
{
_targetSpeed[idToArrayIndex(id)] = limited_runtime_ms;
_targetPosition[idToArrayIndex(id)] = angleToPosition(target_position_deg);
}
}

float SmartServoClass::getPosition(uint8_t const id)
{
if (!isValidId(id))
return -1.0f;

mbed::ScopedLock<rtos::Mutex> lock(_mtx);
float ret = -1;
if (isValidId(id))
return positionToAngle(readWordCmd(id, REG(SmartServoRegister::POSITION_H)));
return positionToAngle(readWordCmd(id, REG(SmartServoRegister::POSITION_H)));
}

void SmartServoClass::center(uint8_t const id, uint16_t const position)
Expand Down Expand Up @@ -261,16 +278,24 @@ void SmartServoClass::setTorque(uint8_t const id, bool const torque)

void SmartServoClass::setTime(uint16_t const time)
{
mbed::ScopedLock<rtos::Mutex> lock(_mtx);
for (int i = MIN_MOTOR_ID; i <= MAX_MOTOR_ID; i++)
_targetSpeed[idToArrayIndex(i)] = time;

mbed::ScopedLock<rtos::Mutex> lock(_mtx);
writeWordCmd(BROADCAST, REG(SmartServoRegister::RUN_TIME_H), time);
}

void SmartServoClass::setTime(uint8_t const id, uint16_t const time)
{
if (!isValidId(id))
return;

if (id == BROADCAST)
return;

_targetSpeed[idToArrayIndex(id)] = time;

mbed::ScopedLock<rtos::Mutex> lock(_mtx);
if ((id >= MIN_MOTOR_ID) && (id <= MAX_MOTOR_ID)) _targetSpeed[idToArrayIndex(id)] = time;
writeWordCmd(id, REG(SmartServoRegister::RUN_TIME_H), time);
}

Expand Down
9 changes: 6 additions & 3 deletions src/lib/motors/SmartServo.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ class SmartServoClass

inline void setPositionMode(PositionMode const mode) { _positionMode = mode; }

void setPosition(uint8_t const id, float const angle);

void setPosition(uint8_t const id, float const angle_deg);
float getPosition(uint8_t const id);

inline void setAngularVelocity(float const angular_velocity_deg_per_sec) { _angular_velocity_deg_per_sec = angular_velocity_deg_per_sec; }
inline float getAngularVelocity() const { return _angular_velocity_deg_per_sec; }

void synchronize();

void setTorque(bool const torque);

void setTorque(uint8_t const id, bool const torque);

void setMaxTorque(uint8_t const id, uint16_t const max_torque);
Expand Down Expand Up @@ -97,6 +98,7 @@ class SmartServoClass
static int constexpr MAX_MOTOR_ID = 6;
static int constexpr NUM_MOTORS = 6;
static float constexpr MAX_ANGLE = 315.0f;
static float constexpr DEFAULT_ANGULAR_VELOCITY_deg_per_sec = 20.0f;

static uint16_t constexpr TORQUE_MIN = 0;
static uint16_t constexpr TORQUE_MAX = 1000;
Expand Down Expand Up @@ -129,6 +131,7 @@ class SmartServoClass

RS485Class& _RS485;
int _errors;
float _angular_velocity_deg_per_sec;
mbed::Callback<void()> _onError;
rtos::Mutex _mtx;

Expand Down