# Tactiverge Academy — companion code for "Velocity kinematics and the Jacobian"
# Lesson: https://tactiverge.win/academy/robotics-foundations/mathematical-foundations-of-movement/velocity-and-the-jacobian/
# Licence: MIT (see https://tactiverge.win/open-engineering/). Examples appear in lesson order.

# ── Example 1 ────────────────────────────────────────
import math

L1, L2 = 10, 8

def fk(t1, t2):
    return (L1 * math.cos(t1) + L2 * math.cos(t1 + t2),
            L1 * math.sin(t1) + L2 * math.sin(t1 + t2))

def jacobian(t1, t2):
    s1, c1 = math.sin(t1), math.cos(t1)
    s12, c12 = math.sin(t1 + t2), math.cos(t1 + t2)
    return [[-L1 * s1 - L2 * s12, -L2 * s12],
            [ L1 * c1 + L2 * c12,  L2 * c12]]

t1, t2 = math.radians(30), math.radians(45)
rate1, rate2 = 0.5, 0.0                  # joint speeds in rad/s

J = jacobian(t1, t2)
vx = J[0][0] * rate1 + J[0][1] * rate2
vy = J[1][0] * rate1 + J[1][1] * rate2
print(f"Jacobian:      ({vx:.3f}, {vy:.3f}) cm/s")

dt = 0.0001                              # a tiny time step, in seconds
x0, y0 = fk(t1, t2)
x1, y1 = fk(t1 + rate1 * dt, t2 + rate2 * dt)
print(f"tiny step:     ({(x1 - x0) / dt:.3f}, {(y1 - y0) / dt:.3f}) cm/s")
