File size: 7,887 Bytes
9897e20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #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);
};
// TODO(samk): hoist into Vector2::PolarToVector2D
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);
// Average speed
const float v{clipSpeed(speed + 0.5f * action.classic.acceleration * dt)};
const float tanDelta{tanf(action.classic.steering)};
// Assume center of mass lies at the middle of length, then l / L == 0.5.
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};
// model.position += d * dt;
// model.heading = utils::AngleAdd(model.heading, w * dt);
// model.speed = clipSpeed(model.speed + action.acceleration * dt);
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)
{
// Clip acceleration and steering
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();
//new_x = x + vel_x * t + 0.5 * accel * jnp.cos(yaw) * t**2
position.x = position.x + velocity.linear.x * dt + 0.5 * action.classic.acceleration * cosf(yaw) * dt * dt;
// new_y = y + vel_y * t + 0.5 * accel * jnp.sin(yaw) * t**2
position.y = position.y + velocity.linear.y * dt + 0.5 * action.classic.acceleration * sinf(yaw) * dt * dt;
// delta_yaw = steering * (speed * t + 0.5 * accel * t**2)
float delta_yaw = action.classic.steering * (speed * dt + 0.5 * action.classic.acceleration * dt * dt);
// new_yaw = geometry.wrap_yaws(yaw + delta_yaw)
float new_yaw = utils::AngleAdd(yaw, delta_yaw);
// new_vel = speed + accel * t
float new_speed = speed + action.classic.acceleration * dt;
// new_vel_x = new_vel * jnp.cos(new_yaw)
velocity.linear.x = new_speed * cosf(new_yaw);
// new_vel_y = new_vel * jnp.sin(new_yaw)
velocity.linear.y = new_speed * sinf(new_yaw);
velocity.linear.z = 0;
velocity.angular = Vector3::zero();
velocity.angular.z = delta_yaw / dt; // Is this correct ?
rotation = Quat::angleAxis(new_yaw, madrona::math::up);
}
inline void forwardDeltaModel(Action &action, Rotation &rotation, Position &position, Velocity &velocity)
{
// start delta model
// start DeltaLocal
const float dt{0.1};
float yaw = utils::quatToYaw(rotation);
// rotated_xy = jnp.matmul(rotation_mat, action.data[..., :2, jnp.newaxis], precision='float32')[..., 0]
// From https://en.wikipedia.org/wiki/Rotation_matrix
float cos = std::cos(yaw);
float sin = std::sin(yaw);
// x = c * x - s * y
// y = s * x + c * y
float dx = action.delta.dx * cos - action.delta.dy * sin;
float dy = action.delta.dx * sin + action.delta.dy * cos;
// end DeltaLocal
// start DeltaGlobal
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; // Is this correct ?
// end DeltaGlobal
float new_yaw = utils::AngleAdd(yaw, action.delta.dyaw);
// end delta model
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();
// accel = (new_vel - vel) / dt
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);
}
// steering = (new_yaw - yaw) / (speed * dt + 1/2 * accel * dt ** 2)
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);
// start delta model
// start DeltaGlobal
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));
// end DeltaGlobal
// start DeltaLocal
// rotated_xy = jnp.matmul(rotation_mat, action.data[..., :2, jnp.newaxis], precision='float32')[..., 0]
// From https://en.wikipedia.org/wiki/Rotation_matrix
float cos = std::cos(-yaw);
float sin = std::sin(-yaw);
// x = c * x - s * y
// y = s * x + c * y
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);
// end DeltaLocal
// end delta model
return action;
}
inline void forwardStateModel(Action &action, Rotation &rotation, Position &position, Velocity &velocity)
{
// No clipping happening here.
// This can go out of bounds with invalid actions
position = action.state.position;
velocity = action.state.velocity;
rotation = Quat::angleAxis(action.state.yaw, madrona::math::up);
}
} |