| #pragma once |
|
|
| #include <madrona/math.hpp> |
| #include <madrona/types.hpp> |
|
|
| using namespace madrona; |
| using namespace madrona::math; |
| namespace madrona_gpudrive |
| { |
|
|
| inline void forwardKinematics(const Action &action, VehicleSize &size, Rotation &rotation, Position &position, Velocity &velocity) |
| { |
| const float maxSpeed{std::numeric_limits<float>::max()}; |
| const float dt{0.1}; |
|
|
| auto clipSpeed = [maxSpeed](float speed) |
| { |
| return std::max(std::min(speed, maxSpeed), -maxSpeed); |
| }; |
| |
| auto polarToVector2D = [](float r, float theta) |
| { |
| return math::Vector2{r * cosf(theta), r * sinf(theta)}; |
| }; |
|
|
| float speed = velocity.linear.length(); |
| float yaw = utils::quatToYaw(rotation); |
| |
| const float v{clipSpeed(speed + 0.5f * action.classic.acceleration * dt)}; |
| const float tanDelta{tanf(action.classic.steering)}; |
| |
| const float beta{std::atan(0.5f * tanDelta)}; |
| const math::Vector2 d{polarToVector2D(v, yaw + beta)}; |
| const float w{v * std::cos(beta) * tanDelta / size.length}; |
|
|
| |
| |
| |
| float new_yaw = utils::AngleAdd(yaw, w * dt); |
| float new_speed = clipSpeed(speed + action.classic.acceleration * dt); |
| position.x += d.x * dt; |
| position.y += d.y * dt; |
| position.z = 1; |
| rotation = Quat::angleAxis(new_yaw, madrona::math::up); |
| velocity.linear.x = new_speed * cosf(new_yaw); |
| velocity.linear.y = new_speed * sinf(new_yaw); |
| velocity.linear.z = 0; |
| velocity.angular = Vector3::zero(); |
| velocity.angular.z = w; |
| } |
|
|
| inline void forwardBicycleModel(Action &action, Rotation &rotation, Position &position, Velocity &velocity) |
| { |
| |
| action.classic.acceleration = fmaxf(-6.0, fminf(action.classic.acceleration, 6.0)); |
| action.classic.steering = fmaxf(-3.0, fminf(action.classic.steering, 3.0)); |
|
|
| const float dt{0.1}; |
| float yaw = utils::quatToYaw(rotation); |
| float speed = velocity.linear.length(); |
| |
| position.x = position.x + velocity.linear.x * dt + 0.5 * action.classic.acceleration * cosf(yaw) * dt * dt; |
| |
| position.y = position.y + velocity.linear.y * dt + 0.5 * action.classic.acceleration * sinf(yaw) * dt * dt; |
| |
| float delta_yaw = action.classic.steering * (speed * dt + 0.5 * action.classic.acceleration * dt * dt); |
| |
| float new_yaw = utils::AngleAdd(yaw, delta_yaw); |
| |
| float new_speed = speed + action.classic.acceleration * dt; |
| |
| velocity.linear.x = new_speed * cosf(new_yaw); |
| |
| velocity.linear.y = new_speed * sinf(new_yaw); |
| velocity.linear.z = 0; |
|
|
| velocity.angular = Vector3::zero(); |
| velocity.angular.z = delta_yaw / dt; |
|
|
| rotation = Quat::angleAxis(new_yaw, madrona::math::up); |
| } |
|
|
| inline void forwardDeltaModel(Action &action, Rotation &rotation, Position &position, Velocity &velocity) |
| { |
| |
| |
| const float dt{0.1}; |
| float yaw = utils::quatToYaw(rotation); |
| |
| |
| float cos = std::cos(yaw); |
| float sin = std::sin(yaw); |
|
|
| |
| |
| float dx = action.delta.dx * cos - action.delta.dy * sin; |
| float dy = action.delta.dx * sin + action.delta.dy * cos; |
| |
| |
| position.x = position.x + dx; |
| position.y = position.y + dy; |
|
|
|
|
| velocity.linear.x = dx / dt; |
| velocity.linear.y = dy / dt; |
| velocity.linear.z = 0; |
| velocity.angular = Vector3::zero(); |
| velocity.angular.z = action.delta.dyaw / dt; |
| |
| float new_yaw = utils::AngleAdd(yaw, action.delta.dyaw); |
| |
|
|
| rotation = Quat::angleAxis(new_yaw, madrona::math::up); |
|
|
| } |
|
|
| inline Action inverseBicycleModel(const Rotation &rotation, const Velocity &velocity, const Rotation &targetRotation, const Velocity &targetVelocity) |
| { |
| const float dt{0.1}; |
|
|
| Action action = {.classic = {0, 0, 0}}; |
| float speed = velocity.linear.length(); |
| float target_speed = targetVelocity.linear.length(); |
|
|
| |
| action.classic.acceleration = (target_speed - speed) / dt; |
|
|
| float yaw = utils::NormalizeAngle<float>(utils::quatToYaw(rotation)); |
| float target_yaw = utils::NormalizeAngle<float>(utils::quatToYaw(targetRotation)); |
|
|
| if(consts::useEstimatedYaw) |
| { |
| target_yaw = atan2f(targetVelocity.linear.y, targetVelocity.linear.x); |
| } |
|
|
| |
| float denominator = speed * dt + 0.5 * action.classic.acceleration * dt * dt; |
| if (denominator != 0) |
| { |
| action.classic.steering = (target_yaw - yaw) / denominator; |
| } |
| else |
| { |
| action.classic.steering = 0; |
| } |
| |
| return action; |
|
|
| } |
|
|
| inline Action inverseDeltaModel(const Rotation &rotation, const Position &position, const Rotation &targetRotation, const Position &targetPosition) |
| { |
| Action action{.delta = {0, 0, 0}}; |
| float yaw = utils::quatToYaw(rotation); |
| float target_yaw = utils::quatToYaw(targetRotation); |
| |
| |
| action.delta.dx = targetPosition.x - position.x; |
| action.delta.dy = targetPosition.y - position.y; |
| action.delta.dyaw = target_yaw - yaw; |
|
|
| action.delta.dx = fmaxf(-6.0, fminf(action.delta.dx, 6.0)); |
| action.delta.dy = fmaxf(-6.0, fminf(action.delta.dy, 6.0)); |
| |
| |
|
|
| |
| |
| float cos = std::cos(-yaw); |
| float sin = std::sin(-yaw); |
| |
| |
| float local_dx= action.delta.dx * cos - action.delta.dy * sin; |
| float local_dy = action.delta.dx * sin + action.delta.dy * cos; |
|
|
| action.delta.dx = fmaxf(-6.0, fminf(local_dx, 6.0)); |
| action.delta.dy = fmaxf(-6.0, fminf(local_dy, 6.0)); |
| action.delta.dyaw = utils::NormalizeAngle<float>(action.delta.dyaw); |
| |
| |
|
|
| return action; |
|
|
| } |
|
|
| inline void forwardStateModel(Action &action, Rotation &rotation, Position &position, Velocity &velocity) |
| { |
| |
| |
| position = action.state.position; |
| velocity = action.state.velocity; |
|
|
| rotation = Quat::angleAxis(action.state.yaw, madrona::math::up); |
| } |
| } |