🏍️ Adds position event for updating kinematics

This commit is contained in:
Rune Harlyk
2024-05-23 17:29:38 +02:00
committed by Rune Harlyk
parent addf57b2a6
commit eb609e9873
4 changed files with 84 additions and 41 deletions
+56 -30
View File
@@ -9,6 +9,7 @@
#define LIGHT_SETTINGS_ENDPOINT_PATH "/api/input"
#define ANGLES_EVENT "angles"
#define INPUT_EVENT "input"
#define POSITION_EVENT "position"
#define MODE_EVENT "mode"
enum class MOTION_STATE
@@ -32,12 +33,16 @@ class MotionService
_socket->registerEvent(INPUT_EVENT);
_socket->registerEvent(ANGLES_EVENT);
_socket->registerEvent(MODE_EVENT);
_socket->registerEvent(POSITION_EVENT);
_socket->onEvent(INPUT_EVENT, [&](JsonObject &root, int originId) { handleInput(root, originId); });
_socket->onEvent(MODE_EVENT, [&](JsonObject &root, int originId) { handleMode(root, originId); });
_socket->onEvent(ANGLES_EVENT, [&](JsonObject &root, int originId) { anglesEvent(root, originId); });
_socket->onEvent(POSITION_EVENT, [&](JsonObject &root, int originId) { positionEvent(root, originId); });
_socket->onSubscribe(ANGLES_EVENT,
std::bind(&MotionService::syncAngles, this, std::placeholders::_1, std::placeholders::_2));
}
@@ -52,6 +57,18 @@ class MotionService
syncAngles(String(originId));
}
void positionEvent(JsonObject &root, int originId)
{
JsonArray array = root["data"].as<JsonArray>();
position.omega = array[0];
position.phi = array[1];
position.psi = array[2];
position.xm = array[3];
position.ym = array[4];
position.zm = array[5];
// syncAngles(String(originId));
}
void handleInput(JsonObject &root, int originId)
{
JsonArray array = root["data"].as<JsonArray>();
@@ -59,6 +76,20 @@ class MotionService
{
input[i] = array[i];
}
float lx = input[1];
float ly = input[2];
float rx = input[3];
float ry = input[4];
float h = input[5];
float s = input[6];
position = {
0,
rx / 4,
ry / 4,
ly / 2,
(h + 128) * (float)0.7,
lx / 2
};
ESP_LOGI("MotionService", "Input: %.0f %.0f %.0f %.0f %.0f %.0f %.0f", input[0], input[1], input[2], input[3], input[4], input[5], input[6]);
}
@@ -76,7 +107,6 @@ class MotionService
sprintf(output, "[%0.f,%0.f,%0.f,%0.f,%0.f,%0.f,%0.f,%0.f,%0.f,%0.f,%0.f,%0.f]", 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, String(originId).c_str());
}
float lerp(float start, float end, float t) {
@@ -99,31 +129,8 @@ class MotionService
break;
case MOTION_STATE::STAND: {
float lp[4][4] = {
{ 100, -100, 100, 1},
{ 100, -100, -100, 1},
{-100, -100, 100, 1},
{-100, -100, -100, 1}
};
float lx = input[1];
float ly = input[2];
float rx = input[3];
float ry = input[4];
float h = input[5];
float s = input[6];
position_t p = {
0,
rx / 4,
ry / 4,
ly / 2,
(h+128)*0.7,
lx / 2
};
float new_angles[12] = {0,};
float dir[12] = {-1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1};
kinematics.calculate_inverse_kinematics(lp, p, new_angles);
kinematics.calculate_inverse_kinematics(lp, position, new_angles);
for (int i = 0; i < 12; i++) {
float new_angle = lerp(angles[i], new_angles[i] * dir[i], 0.3);
@@ -138,10 +145,20 @@ class MotionService
break;
}
case MOTION_STATE::WALK:
angles[1] += dir;
if (angles[1] >= 90) dir = -1;
if (angles[1] <= 0) dir = 1;
updated = true;
float new_angles[12] = {0,};
lp[0][1] += walk_dir;
if (lp[0][1] >= 200) walk_dir = -1;
if (lp[0][1] <= 100) walk_dir = 1;
kinematics.calculate_inverse_kinematics(lp, position, new_angles);
for (int i = 0; i < 12; i++) {
float new_angle = lerp(angles[i], new_angles[i] * dir[i], 0.3);
if (new_angle != angles[i]) {
angles[i] = new_angle;
updated = true;
}
}
break;
}
return updated;
@@ -163,13 +180,22 @@ class MotionService
constexpr static int MotionInterval = 100;
position_t position = {0, 0, 0, 0, 0, 0};
float dir[12] = {-1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1};
float lp[4][4] = {
{ 100, -100, 100, 1},
{ 100, -100, -100, 1},
{-100, -100, 100, 1},
{-100, -100, -100, 1}
};
float input[7] = {0, 0, 0, 0, 0, 0, 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 stand_angles[12] = {0, 45, -90, 0, 45, -90, 0, 45, -90, 0, 45, -90};
MOTION_STATE motionState = MOTION_STATE::IDLE;
unsigned long _lastUpdate;
int dir = 2;
int walk_dir = 2;
};
#endif