Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cfg = require("config")
- rednet.open(cfg.modem)
- rednet.host("leg_controller", cfg.prefix)
- -- Speed Controllers
- local speed = {
- hip1 = peripheral.wrap(cfg.speedcontrollers.hip1),
- hip2 = peripheral.wrap(cfg.speedcontrollers.hip2),
- knee = peripheral.wrap(cfg.speedcontrollers.knee)
- }
- local joints = {
- hip1 = {
- hostname = cfg.joints.hip1,
- id = nil,
- busy = false,
- commandedAngle = 0,
- rpm = speed.hip1,
- invert = false
- },
- hip2 = {
- hostname = cfg.joints.hip2,
- id = nil,
- busy = false,
- commandedAngle = 0,
- rpm = speed.hip2,
- invert = true
- },
- knee = {
- hostname = cfg.joints.knee,
- id = nil,
- busy = false,
- commandedAngle = 0,
- rpm = speed.knee,
- invert = false
- }
- }
- --------------------------------------------------
- -- Discover Joints
- --------------------------------------------------
- for _, joint in pairs(joints) do
- repeat
- joint.id = rednet.lookup(
- cfg.prefix .. "joint",
- cfg.prefix .. joint.hostname
- )
- if not joint.id then
- sleep(0.25)
- end
- until joint.id
- end
- local function legIdle()
- for _, joint in pairs(joints) do
- if joint.busy then
- return false
- end
- end
- return true
- end
- ------------------------------------
- -- Foot State
- --------------------------------------------------
- local current = {
- x = cfg.rest.x,
- y = cfg.rest.y,
- z = cfg.rest.z
- }
- local start = {
- x = current.x,
- y = current.y,
- z = current.z
- }
- local target = {
- x = current.x,
- y = current.y,
- z = current.z
- }
- local progress = 1
- local elapsed = 0
- local duration = cfg.step_time or 0.40
- --------------------------------------------------
- -- Scheduler Helpers
- --------------------------------------------------
- local function setRPM(joint, rpm)
- if joint.invert then
- rpm = -rpm
- end
- joint.rpm.setTargetSpeed(rpm)
- end
- local function sendDelta(joint, delta, rpm)
- if math.abs(delta) < 0.2 then
- return
- end
- setRPM(joint, rpm)
- rednet.send(
- joint.id,
- {
- type = "turn",
- delta = delta
- },
- cfg.prefix .. ".joint.command"
- )
- joint.busy = true
- end
- --------------------------------------------------
- -- Gait Commands
- --------------------------------------------------
- local function beginMove(x, y, z)
- start.x = current.x
- start.y = current.y
- start.z = current.z
- target.x = x or target.x
- target.y = y or target.y
- target.z = z or target.z
- local dx = target.x - start.x
- local dy = target.y - start.y
- local dz = target.z - start.z
- local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
- duration = math.max(
- 0.12,
- distance / cfg.footSpeed
- )
- progress = 0
- elapsed = 0
- end
- --------------------------------------------------
- -- Helpers
- --------------------------------------------------
- local function clamp(v, lo, hi)
- return math.max(lo, math.min(hi, v))
- end
- local function shortestDiff(target, current)
- return (target - current + 180) % 360 - 180
- end
- --------------------------------------------------
- -- Inverse Kinematics
- --------------------------------------------------
- local function solveIK(x, y, z)
- local t1 = math.atan2(x, y)
- local r = math.sqrt(x * x + y * y)
- local px = r - cfg.lengths.l1
- local pz = z
- local d = math.sqrt(px * px + pz * pz)
- local maxReach = cfg.lengths.l2 + cfg.lengths.l3 - 0.001
- local minReach = math.abs(cfg.lengths.l2 - cfg.lengths.l3) + 0.001
- if d > maxReach then
- local s = maxReach / d
- px = px * s
- pz = pz * s
- d = maxReach
- elseif d < minReach then
- local s = minReach / d
- px = px * s
- pz = pz * s
- d = minReach
- end
- local c3 = clamp(
- (px * px + pz * pz - cfg.lengths.l2 ^ 2 - cfg.lengths.l3 ^ 2) /
- (2 * cfg.lengths.l2 * cfg.lengths.l3),
- -1,
- 1
- )
- local s3 = math.sqrt(1 - c3 * c3)
- local t3 = math.atan2(s3, c3)
- local k1 = cfg.lengths.l2 + cfg.lengths.l3 * c3
- local k2 = cfg.lengths.l3 * s3
- local t2 = math.atan2(pz, px) - math.atan2(k2, k1)
- return
- -math.deg(t1),
- math.deg(t2),
- -math.deg(t3 - t2)
- end
- --------------------------------------------------
- -- Trajectory
- --------------------------------------------------
- local function updateTrajectory(dt)
- if progress >= 1 then
- return
- end
- elapsed = elapsed + dt
- progress = math.min(1, elapsed / duration)
- local s = (1 - math.cos(progress * math.pi)) * 0.5
- current.x = start.x + (target.x - start.x) * s
- current.y = start.y + (target.y - start.y) * s
- current.z = start.z + (target.z - start.z) * s
- end
- --------------------------------------------------
- -- RPM Calculation
- --------------------------------------------------
- local function calculateRPMs(desired)
- local maxDelta = 0
- for name, angle in pairs(desired) do
- local delta = math.abs(shortestDiff(angle, joints[name].commandedAngle))
- if delta > maxDelta then
- maxDelta = delta
- end
- end
- if maxDelta < 0.2 then
- return {
- hip1 = 0,
- hip2 = 0,
- knee = 0
- }
- end
- local master = math.min(maxDelta * 4.5, 24)
- local rpm = {}
- for name, angle in pairs(desired) do
- local delta = math.abs(shortestDiff(angle, joints[name].commandedAngle))
- rpm[name] = clamp(
- (delta / maxDelta) * master,
- 2,
- 24
- )
- end
- return rpm
- end
- --------------------------------------------------
- -- Desired Joint Angles
- --------------------------------------------------
- local function desiredAngles()
- local yaw, hip, knee =
- solveIK(
- current.x,
- current.y,
- current.z
- )
- return {
- hip1 = yaw,
- hip2 = hip,
- knee = knee
- }
- end
- --------------------------------------------------
- -- Main Loop
- --------------------------------------------------
- local last = os.clock()
- while true do
- -- Drain inbox
- while true do
- local sender, msg, protocol = rednet.receive(nil, 0)
- if not sender then
- break
- end
- if protocol == cfg.prefix .. "gait.command" then
- if msg.type == "move_to" then
- beginMove(msg.x, msg.y, msg.z)
- elseif msg.type == "home" then
- beginMove(
- cfg.rest.x,
- cfg.rest.y,
- cfg.rest.z
- )
- end
- elseif protocol == cfg.prefix .. ".joint.done" then
- local joint = joints[msg.joint]
- if joint and joint.busy then
- joint.busy = false
- joint.commandedAngle =
- joint.commandedAngle + msg.delta
- end
- end
- end
- --------------------------------------------------
- -- Update Trajectory
- --------------------------------------------------
- local now = os.clock()
- local dt = now - last
- last = now
- if legIdle() then
- updateTrajectory(dt)
- local desired = desiredAngles()
- local rpms = calculateRPMs(desired)
- for name, joint in pairs(joints) do
- if not joint.busy then
- local delta =
- shortestDiff(
- desired[name],
- joint.commandedAngle
- )
- if math.abs(delta) >= 0.2 then
- sendDelta(
- joint,
- delta,
- rpms[name]
- )
- else
- setRPM(joint, 0)
- end
- end
- end
- end
- sleep(0)
- end
Advertisement
Add Comment
Please, Sign In to add comment