🎉 Connects motion service with servo controller
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <EventSocket.h>
|
||||
#include <TaskManager.h>
|
||||
#include <Kinematics.h>
|
||||
#include <ServoController.h>
|
||||
#include <Gait/GaitState.h>
|
||||
#include <Timing.h>
|
||||
#include <MathUtils.h>
|
||||
@@ -15,13 +16,17 @@
|
||||
#define POSITION_EVENT "position"
|
||||
#define MODE_EVENT "mode"
|
||||
|
||||
enum class MOTION_STATE { IDLE, REST, STAND, WALK };
|
||||
enum class MOTION_STATE { DEACTIVATED, IDLE, CALIBRATION, REST, STAND, CRAWL, WALK };
|
||||
|
||||
class MotionService {
|
||||
public:
|
||||
MotionService(PsychicHttpServer *server, EventSocket *socket, SecurityManager *securityManager,
|
||||
TaskManager *taskManager)
|
||||
: _server(server), _socket(socket), _securityManager(securityManager), _taskManager(taskManager) {}
|
||||
ServoController *servoController, TaskManager *taskManager)
|
||||
: _server(server),
|
||||
_socket(socket),
|
||||
_securityManager(securityManager),
|
||||
_servoController(servoController),
|
||||
_taskManager(taskManager) {}
|
||||
|
||||
void begin() {
|
||||
_socket->onEvent(INPUT_EVENT, [&](JsonObject &root, int originId) { handleInput(root, originId); });
|
||||
@@ -36,6 +41,8 @@ class MotionService {
|
||||
std::bind(&MotionService::syncAngles, this, std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
body_state.updateFeet(default_feet_positions);
|
||||
|
||||
_taskManager->createTask(this->_loopImpl, "MotionService", 4096, this, 3);
|
||||
}
|
||||
|
||||
void anglesEvent(JsonObject &root, int originId) {
|
||||
@@ -58,53 +65,60 @@ class MotionService {
|
||||
|
||||
void handleInput(JsonObject &root, int originId) {
|
||||
JsonArray array = root["data"].as<JsonArray>();
|
||||
float lx = array[1];
|
||||
float ly = array[2];
|
||||
float rx = array[3];
|
||||
float ry = array[4];
|
||||
float h = array[5];
|
||||
float s = array[6];
|
||||
command.lx = array[1];
|
||||
command.lx = array[1];
|
||||
command.ly = array[2];
|
||||
command.rx = array[3];
|
||||
command.ry = array[4];
|
||||
command.h = array[5];
|
||||
command.s = array[6];
|
||||
|
||||
body_state.ym = (h + 128) * (float)0.7;
|
||||
body_state.ym = (command.h + 128.f) * 0.75f / 100;
|
||||
|
||||
switch (motionState) {
|
||||
case MOTION_STATE::STAND: {
|
||||
body_state.phi = rx / 4;
|
||||
body_state.psi = ry / 4;
|
||||
body_state.xm = ly / 2;
|
||||
body_state.zm = lx / 2;
|
||||
body_state.phi = command.rx / 4;
|
||||
body_state.psi = command.ry / 4;
|
||||
body_state.xm = command.ly / 2 / 100;
|
||||
body_state.zm = command.lx / 2 / 100;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handleMode(JsonObject &root, int originId) {
|
||||
ESP_LOGV("MotionService", "Mode %d", root["data"].as<int>());
|
||||
motionState = (MOTION_STATE)root["data"].as<int>();
|
||||
ESP_LOGV("MotionService", "Mode %d", motionState);
|
||||
char output[2];
|
||||
sprintf(output, "%d", (int)motionState);
|
||||
itoa((int)motionState, output, 10);
|
||||
motionState == MOTION_STATE::DEACTIVATED ? _servoController->deactivate() : _servoController->activate();
|
||||
_socket->emit(MODE_EVENT, output, String(originId).c_str());
|
||||
}
|
||||
|
||||
void syncAngles(const String &originId = "", bool sync = false) {
|
||||
char output[100];
|
||||
sprintf(output, "[%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f]", angles[0], angles[1],
|
||||
angles[2], angles[3], angles[4], angles[5], angles[6], angles[7], angles[8], angles[9], angles[10],
|
||||
angles[11]);
|
||||
snprintf(output, sizeof(output), "[%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f]", angles[0],
|
||||
angles[1], angles[2], angles[3], angles[4], angles[5], angles[6], angles[7], angles[8], angles[9],
|
||||
angles[10], angles[11]);
|
||||
_socket->emit(ANGLES_EVENT, output, originId.c_str());
|
||||
_servoController->setAngles(angles);
|
||||
}
|
||||
|
||||
bool updateMotion() {
|
||||
switch (motionState) {
|
||||
case MOTION_STATE::IDLE: return false; break;
|
||||
|
||||
case MOTION_STATE::DEACTIVATED: return false;
|
||||
case MOTION_STATE::IDLE: return false;
|
||||
case MOTION_STATE::CALIBRATION: update_angles(calibration_angles, new_angles, false); break;
|
||||
case MOTION_STATE::REST: update_angles(rest_angles, new_angles, false); break;
|
||||
|
||||
case MOTION_STATE::STAND: {
|
||||
case MOTION_STATE::STAND: kinematics.calculate_inverse_kinematics(body_state, new_angles); break;
|
||||
case MOTION_STATE::CRAWL:
|
||||
crawlGait->step(body_state, command);
|
||||
kinematics.calculate_inverse_kinematics(body_state, new_angles);
|
||||
break;
|
||||
case MOTION_STATE::WALK:
|
||||
walkGait->step(body_state, command);
|
||||
kinematics.calculate_inverse_kinematics(body_state, new_angles);
|
||||
break;
|
||||
}
|
||||
case MOTION_STATE::WALK: kinematics.calculate_inverse_kinematics(body_state, new_angles); break;
|
||||
}
|
||||
return update_angles(new_angles, angles);
|
||||
}
|
||||
@@ -121,38 +135,44 @@ class MotionService {
|
||||
return updated;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
EXECUTE_EVERY_N_MS(MotionInterval, {
|
||||
void _loop() {
|
||||
TickType_t xLastWakeTime = xTaskGetTickCount();
|
||||
for (;;) {
|
||||
if (updateMotion()) syncAngles();
|
||||
});
|
||||
_servoController->loop();
|
||||
vTaskDelayUntil(&xLastWakeTime, MotionInterval / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
static void _loopImpl(void *_this) { static_cast<MotionService *>(_this)->_loop(); }
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
EventSocket *_socket;
|
||||
SecurityManager *_securityManager;
|
||||
TaskManager *_taskManager;
|
||||
ServoController *_servoController;
|
||||
Kinematics kinematics;
|
||||
ControllerCommand command = {0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
MOTION_STATE motionState = MOTION_STATE::IDLE;
|
||||
friend class GaitState;
|
||||
|
||||
std::unique_ptr<GaitState> crawlGait = std::make_unique<EightPhaseWalkState>();
|
||||
std::unique_ptr<GaitState> walkGait = std::make_unique<FourPhaseWalkState>();
|
||||
|
||||
MOTION_STATE motionState = MOTION_STATE::DEACTIVATED;
|
||||
unsigned long _lastUpdate;
|
||||
constexpr static int MotionInterval = 100;
|
||||
constexpr static int MotionInterval = 15;
|
||||
|
||||
body_state_t body_state = {
|
||||
0,
|
||||
};
|
||||
float new_angles[12] = {
|
||||
0,
|
||||
};
|
||||
body_state_t body_state = {0, 0, 0, 0, 0, 0};
|
||||
float new_angles[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
float dir[12] = {1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1};
|
||||
float default_feet_positions[4][4] = {
|
||||
{100, -100, 100, 1}, {100, -100, -100, 1}, {-100, -100, 100, 1}, {-100, -100, -100, 1}};
|
||||
float default_feet_positions[4][4] = {{1, -1, 1, 1}, {1, -1, -1, 1}, {-1, -1, 1, 1}, {-1, -1, -1, 1}};
|
||||
|
||||
float angles[12] = {
|
||||
0,
|
||||
};
|
||||
float angles[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
float rest_angles[12] = {0, 90, -145, 0, 90, -145, 0, 90, -145, 0, 90, -145};
|
||||
float calibration_angles[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user