ksaw000

LegController

Jul 4th, 2026 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. local cfg = require("config")
  2.  
  3. rednet.open(cfg.modem)
  4. rednet.host("leg_controller", cfg.prefix)
  5.  
  6. -- Speed Controllers
  7. local speed = {
  8. hip1 = peripheral.wrap(cfg.speedcontrollers.hip1),
  9. hip2 = peripheral.wrap(cfg.speedcontrollers.hip2),
  10. knee = peripheral.wrap(cfg.speedcontrollers.knee)
  11. }
  12.  
  13. local joints = {
  14. hip1 = {
  15. hostname = cfg.joints.hip1,
  16. id = nil,
  17. busy = false,
  18. commandedAngle = 0,
  19. rpm = speed.hip1,
  20. invert = false
  21. },
  22.  
  23. hip2 = {
  24. hostname = cfg.joints.hip2,
  25. id = nil,
  26. busy = false,
  27. commandedAngle = 0,
  28. rpm = speed.hip2,
  29. invert = true
  30. },
  31.  
  32. knee = {
  33. hostname = cfg.joints.knee,
  34. id = nil,
  35. busy = false,
  36. commandedAngle = 0,
  37. rpm = speed.knee,
  38. invert = false
  39. }
  40. }
  41.  
  42. --------------------------------------------------
  43. -- Discover Joints
  44. --------------------------------------------------
  45.  
  46. for _, joint in pairs(joints) do
  47. repeat
  48. joint.id = rednet.lookup(
  49. cfg.prefix .. "joint",
  50. cfg.prefix .. joint.hostname
  51. )
  52.  
  53. if not joint.id then
  54. sleep(0.25)
  55. end
  56. until joint.id
  57. end
  58.  
  59.  
  60.  
  61. local function legIdle()
  62. for _, joint in pairs(joints) do
  63. if joint.busy then
  64. return false
  65. end
  66. end
  67. return true
  68. end
  69.  
  70. ------------------------------------
  71. -- Foot State
  72. --------------------------------------------------
  73.  
  74. local current = {
  75. x = cfg.rest.x,
  76. y = cfg.rest.y,
  77. z = cfg.rest.z
  78. }
  79.  
  80. local start = {
  81. x = current.x,
  82. y = current.y,
  83. z = current.z
  84. }
  85.  
  86. local target = {
  87. x = current.x,
  88. y = current.y,
  89. z = current.z
  90. }
  91.  
  92. local progress = 1
  93. local elapsed = 0
  94. local duration = cfg.step_time or 0.40
  95.  
  96. --------------------------------------------------
  97. -- Scheduler Helpers
  98. --------------------------------------------------
  99.  
  100. local function setRPM(joint, rpm)
  101. if joint.invert then
  102. rpm = -rpm
  103. end
  104.  
  105. joint.rpm.setTargetSpeed(rpm)
  106. end
  107.  
  108. local function sendDelta(joint, delta, rpm)
  109. if math.abs(delta) < 0.2 then
  110. return
  111. end
  112.  
  113. setRPM(joint, rpm)
  114.  
  115. rednet.send(
  116. joint.id,
  117. {
  118. type = "turn",
  119. delta = delta
  120. },
  121. cfg.prefix .. ".joint.command"
  122. )
  123.  
  124. joint.busy = true
  125. end
  126.  
  127. --------------------------------------------------
  128. -- Gait Commands
  129. --------------------------------------------------
  130.  
  131. local function beginMove(x, y, z)
  132. start.x = current.x
  133. start.y = current.y
  134. start.z = current.z
  135.  
  136. target.x = x or target.x
  137. target.y = y or target.y
  138. target.z = z or target.z
  139.  
  140. local dx = target.x - start.x
  141. local dy = target.y - start.y
  142. local dz = target.z - start.z
  143.  
  144. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  145.  
  146. duration = math.max(
  147. 0.12,
  148. distance / cfg.footSpeed
  149. )
  150.  
  151. progress = 0
  152. elapsed = 0
  153. end
  154.  
  155. --------------------------------------------------
  156. -- Helpers
  157. --------------------------------------------------
  158.  
  159. local function clamp(v, lo, hi)
  160. return math.max(lo, math.min(hi, v))
  161. end
  162.  
  163. local function shortestDiff(target, current)
  164. return (target - current + 180) % 360 - 180
  165. end
  166.  
  167. --------------------------------------------------
  168. -- Inverse Kinematics
  169. --------------------------------------------------
  170.  
  171. local function solveIK(x, y, z)
  172. local t1 = math.atan2(x, y)
  173. local r = math.sqrt(x * x + y * y)
  174.  
  175. local px = r - cfg.lengths.l1
  176. local pz = z
  177.  
  178. local d = math.sqrt(px * px + pz * pz)
  179.  
  180. local maxReach = cfg.lengths.l2 + cfg.lengths.l3 - 0.001
  181. local minReach = math.abs(cfg.lengths.l2 - cfg.lengths.l3) + 0.001
  182.  
  183. if d > maxReach then
  184. local s = maxReach / d
  185. px = px * s
  186. pz = pz * s
  187. d = maxReach
  188. elseif d < minReach then
  189. local s = minReach / d
  190. px = px * s
  191. pz = pz * s
  192. d = minReach
  193. end
  194.  
  195. local c3 = clamp(
  196. (px * px + pz * pz - cfg.lengths.l2 ^ 2 - cfg.lengths.l3 ^ 2) /
  197. (2 * cfg.lengths.l2 * cfg.lengths.l3),
  198. -1,
  199. 1
  200. )
  201.  
  202. local s3 = math.sqrt(1 - c3 * c3)
  203.  
  204. local t3 = math.atan2(s3, c3)
  205.  
  206. local k1 = cfg.lengths.l2 + cfg.lengths.l3 * c3
  207. local k2 = cfg.lengths.l3 * s3
  208.  
  209. local t2 = math.atan2(pz, px) - math.atan2(k2, k1)
  210.  
  211. return
  212. -math.deg(t1),
  213. math.deg(t2),
  214. -math.deg(t3 - t2)
  215. end
  216.  
  217. --------------------------------------------------
  218. -- Trajectory
  219. --------------------------------------------------
  220.  
  221. local function updateTrajectory(dt)
  222. if progress >= 1 then
  223. return
  224. end
  225.  
  226. elapsed = elapsed + dt
  227. progress = math.min(1, elapsed / duration)
  228.  
  229. local s = (1 - math.cos(progress * math.pi)) * 0.5
  230.  
  231. current.x = start.x + (target.x - start.x) * s
  232. current.y = start.y + (target.y - start.y) * s
  233. current.z = start.z + (target.z - start.z) * s
  234. end
  235.  
  236. --------------------------------------------------
  237. -- RPM Calculation
  238. --------------------------------------------------
  239.  
  240. local function calculateRPMs(desired)
  241. local maxDelta = 0
  242.  
  243. for name, angle in pairs(desired) do
  244. local delta = math.abs(shortestDiff(angle, joints[name].commandedAngle))
  245. if delta > maxDelta then
  246. maxDelta = delta
  247. end
  248. end
  249.  
  250. if maxDelta < 0.2 then
  251. return {
  252. hip1 = 0,
  253. hip2 = 0,
  254. knee = 0
  255. }
  256. end
  257.  
  258. local master = math.min(maxDelta * 4.5, 24)
  259.  
  260. local rpm = {}
  261.  
  262. for name, angle in pairs(desired) do
  263. local delta = math.abs(shortestDiff(angle, joints[name].commandedAngle))
  264.  
  265. rpm[name] = clamp(
  266. (delta / maxDelta) * master,
  267. 2,
  268. 24
  269. )
  270. end
  271.  
  272. return rpm
  273. end
  274.  
  275. --------------------------------------------------
  276. -- Desired Joint Angles
  277. --------------------------------------------------
  278.  
  279. local function desiredAngles()
  280. local yaw, hip, knee =
  281. solveIK(
  282. current.x,
  283. current.y,
  284. current.z
  285. )
  286.  
  287. return {
  288. hip1 = yaw,
  289. hip2 = hip,
  290. knee = knee
  291. }
  292. end
  293.  
  294. --------------------------------------------------
  295. -- Main Loop
  296. --------------------------------------------------
  297. local last = os.clock()
  298. while true do
  299. -- Drain inbox
  300. while true do
  301. local sender, msg, protocol = rednet.receive(nil, 0)
  302.  
  303. if not sender then
  304. break
  305. end
  306.  
  307. if protocol == cfg.prefix .. "gait.command" then
  308. if msg.type == "move_to" then
  309. beginMove(msg.x, msg.y, msg.z)
  310. elseif msg.type == "home" then
  311. beginMove(
  312. cfg.rest.x,
  313. cfg.rest.y,
  314. cfg.rest.z
  315. )
  316. end
  317. elseif protocol == cfg.prefix .. ".joint.done" then
  318. local joint = joints[msg.joint]
  319.  
  320. if joint and joint.busy then
  321. joint.busy = false
  322. joint.commandedAngle =
  323. joint.commandedAngle + msg.delta
  324. end
  325. end
  326. end
  327.  
  328. --------------------------------------------------
  329. -- Update Trajectory
  330. --------------------------------------------------
  331.  
  332. local now = os.clock()
  333. local dt = now - last
  334. last = now
  335.  
  336. if legIdle() then
  337. updateTrajectory(dt)
  338.  
  339. local desired = desiredAngles()
  340. local rpms = calculateRPMs(desired)
  341.  
  342. for name, joint in pairs(joints) do
  343. if not joint.busy then
  344. local delta =
  345. shortestDiff(
  346. desired[name],
  347. joint.commandedAngle
  348. )
  349.  
  350. if math.abs(delta) >= 0.2 then
  351. sendDelta(
  352. joint,
  353. delta,
  354. rpms[name]
  355. )
  356. else
  357. setRPM(joint, 0)
  358. end
  359. end
  360. end
  361. end
  362. sleep(0)
  363. end
  364.  
Advertisement
Add Comment
Please, Sign In to add comment