Kinematics and frames · University · about 30 min
Turning a vector from one frame into the other, in two dimensions, by hand and in code.
[1 mark]A robot at heading 60 measures (0, 10) in its own frame. What does this print?
import math
def body_to_world(vx, vy, h_deg):
a = math.radians(h_deg)
return (vx * math.cos(a) + vy * math.sin(a),
-vx * math.sin(a) + vy * math.cos(a))
wx, wy = body_to_world(0, 10, 60)
print("wx:", round(wx, 2))
print("wy:", round(wy, 2))wx: 8.66 wy: 5.0
wx = 10 sin 60 = 8.66 and wy = 10 cos 60 = 5.0. Ten centimetres ahead of a robot turned 60 degrees clockwise is mostly to the right and partly away.
[1 mark]A robot at heading 90 slides 10 cm to its own right, the body vector (10, 0). What does this print?
import math a = math.radians(90) vx, vy = 10.0, 0.0 wx = vx * math.cos(a) + vy * math.sin(a) wy = -vx * math.sin(a) + vy * math.cos(a) print(round(wx, 2), round(wy, 2))
0.0 -10.0
wx = 10 cos 90 = 0 and wy = -10 sin 90 = -10. A robot facing +x has its right hand pointing along -y, towards you.
[1 mark]A student computes 10 * math.sin(90) for a robot facing +x and gets 8.94 instead of 10. What is wrong?
[1 mark]Which matrix undoes R(h), turning a world vector back into the body frame?
[1 mark]In R(h) as the lesson writes it, the minus sign sits on -sin h below the diagonal. Why there?
[1 mark]This rotates the body vector (3, 4) into the world at heading 37 and back again. What does it print?
import math
def body_to_world(vx, vy, h):
a = math.radians(h)
return vx * math.cos(a) + vy * math.sin(a), -vx * math.sin(a) + vy * math.cos(a)
def world_to_body(wx, wy, h):
a = math.radians(h)
return wx * math.cos(a) - wy * math.sin(a), wx * math.sin(a) + wy * math.cos(a)
wx, wy = body_to_world(3, 4, 37)
bx, by = world_to_body(wx, wy, 37)
print(round(math.hypot(wx, wy), 6), round(bx, 6), round(by, 6))5.0 3.0 4.0
A rotation never changes length, so the world vector is still 5 long, and rotating back recovers (3, 4). Both are cheap checks that the maths is right.
A robot at heading 60 measures (0, 10) in its own frame. Print the same vector in the world frame, as wx: and wy:, worked out with the rotation matrix.
from bugbot import * import math connect() h = 60 vx, vy = 0.0, 10.0
The hint students can ask for: A robot at heading 60 measures (0, 10) in its own frame: ten centimetres straight ahead. World x is vx*cos(h) + vy*sin(h); world y is -vx*sin(h) + vy*cos(h). The angle is in degrees.
from bugbot import *
import math
connect()
h = 60
vx, vy = 0.0, 10.0
a = math.radians(h)
wx = vx * math.cos(a) + vy * math.sin(a)
wy = -vx * math.sin(a) + vy * math.cos(a)
print("wx:", round(wx, 3))
print("wy:", round(wy, 3))
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.