Skip to content

Refactor/Cleanup MotorsWrapper #31

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 4 commits into from
Jan 20, 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
6 changes: 3 additions & 3 deletions src/Braccio++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ bool BraccioClass::connected(int const id)
return _is_motor_connected[SmartServoClass::idToArrayIndex(id)];
}

MotorsWrapper BraccioClass::move(int const id)
Servo BraccioClass::move(int const id)
{
MotorsWrapper wrapper(servos, id);
Servo wrapper(servos, id);
return wrapper;
}

MotorsWrapper BraccioClass::get(int const id)
Servo BraccioClass::get(int const id)
{
return move(id);
}
Expand Down
76 changes: 27 additions & 49 deletions src/Braccio++.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum speed_grade_t {
#include <chrono>
using namespace std::chrono;

class MotorsWrapper;
class Servo;

class BraccioClass
{
Expand All @@ -53,25 +53,16 @@ class BraccioClass
bool connected(int const id);


MotorsWrapper move(int const id);
MotorsWrapper get (int const id);
Servo move(int const id);
Servo get (int const id);

void moveTo(float const a1, float const a2, float const a3, float const a4, float const a5, float const a6);
void positions(float * buffer);
void positions(float & a1, float & a2, float & a3, float & a4, float & a5, float & a6);


void speed(speed_grade_t speed_grade) {
runTime = speed_grade;
}

void disengage(int id = SmartServoClass::BROADCAST) {
servos.disengage(id);
}

void engage(int id = SmartServoClass::BROADCAST) {
servos.engage(id);
}
inline void speed (speed_grade_t const speed_grade) { runTime = speed_grade; }
inline void disengage(int const id = SmartServoClass::BROADCAST) { servos.disengage(id); }
inline void engage (int const id = SmartServoClass::BROADCAST) { servos.engage(id); }

int getKey();
void connectJoystickTo(lv_obj_t* obj);
Expand Down Expand Up @@ -173,46 +164,33 @@ class BraccioClass

#define Braccio BraccioClass::get_default_instance()

class MotorsWrapper {
class Servo
{
public:
MotorsWrapper(SmartServoClass & servos, int idx) : _servos(servos), _idx(idx) {}
MotorsWrapper& to(float angle) {
_servos.setPosition(_idx, angle, _speed);
return *this;
}
MotorsWrapper& in(std::chrono::milliseconds len) {
_servos.setTime(_idx, len.count());
return *this;
}
MotorsWrapper& move() {
return *this;
}
float position() {
return _servos.getPosition(_idx);
}

inline bool connected() { return Braccio.connected(_idx); }
Servo(SmartServoClass & servos, int const id) : _servos(servos), _id(id) { }

inline void disengage() { _servos.disengage(_id); }
inline void engage() { _servos.engage(_id); }
inline bool engaged() { return _servos.isEngaged(_id); }

inline Servo & move() { return *this; }
inline Servo & to (float const angle) { _servos.setPosition(_id, angle, _speed); return *this; }
inline Servo & in (std::chrono::milliseconds const len) { _servos.setTime(_id, len.count()); 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); }

operator bool() { return connected(); }

operator bool() {
return connected();
}
void info(Stream& stream) {
_servos.getInfo(stream, _idx);
}
void disengage() {
_servos.disengage(_idx);
}
void engage() {
_servos.engage(_idx);
}
bool engaged() {
return _servos.isEngaged(_idx);
}

private:

SmartServoClass & _servos;
int _idx;
int _speed = 100;
int const _id;
int const _speed = 100;

};

struct __callback__container__ {
Expand Down