MegiRicky

Z-10Rotor

Jul 4th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.49 KB | None | 0 0
  1. local blade1 = peripheral.wrap("front")
  2. local blade2 = peripheral.wrap("left")
  3. local blade3 = peripheral.wrap("back")
  4. local blade4 = peripheral.wrap("right")
  5. local modem = peripheral.find("modem")
  6.  
  7. blade1.assemble()
  8. blade2.assemble()
  9. blade3.assemble()
  10. blade4.assemble()
  11.  
  12. print("Input the controlChannel, default = 1100")
  13. local controlChannel = io.read()
  14. if controlChannel == "" then
  15. controlChannel = 1100
  16. end
  17. controlChannel = tonumber(controlChannel)
  18. modem.open(controlChannel) -- Open a channel to communicate
  19. AuxControlChannel = controlChannel + 1
  20. flightInfoChannel = controlChannel + 2
  21. modem.open(AuxControlChannel)
  22. modem.open(flightInfoChannel)
  23.  
  24. local controls = {}
  25. local AuxControls = {}
  26. local desiredYVel,desiredPitch
  27. local currentRotorSpeed = 0
  28. local currentTailRotorSpeed = 0
  29. local newRotorSpeed = 0
  30. local speed = 0
  31.  
  32. local Kp_rotor, Ki_rotor, Kd_rotor = 0.08, 0, 0.05
  33. local Kp_yaw, Ki_yaw, Kd_yaw = 0.2, 0, 0.05 -- Yaw PID values
  34. local yVelError = 0
  35. local yVelIntegral = 0
  36. local yVelPrevError = 0
  37. local yawError = 0
  38. local yawIntegral = 0
  39. local yawPrevError = 0
  40. local prevYaw = 0 -- To store the previous yaw value
  41. local currentYaw = 0
  42. local Kp_pitch, Ki_pitch, Kd_pitch = 0.2, 0, 0 -- Pitch PID values
  43. local pitchError = 0
  44. local pitchIntegral = 0
  45. local pitchPrevError = 0
  46. local currentPitch = 0
  47. local lastTime = os.clock()
  48. local dt = 0.1 -- inistial time step
  49.  
  50. local flightInfo
  51.  
  52. -- Normalize a vector
  53. local function normalizeVector(v)
  54. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  55. if length == 0 then
  56. return {0, 0, 0}
  57. end
  58. return {v[1] / length, v[2] / length, v[3] / length}
  59. end
  60.  
  61. -- Normalize the rotation matrix
  62. local function normalizeRotationMatrix(rotMatrix)
  63. local normalizedMatrix = {}
  64. for i = 1, #rotMatrix do
  65. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  66. end
  67. return normalizedMatrix
  68. end
  69.  
  70. -- Get the pitch of the ship
  71. local function getPitch()
  72. local rotMatrix = ship.getTransformationMatrix()
  73. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  74. return -math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  75. end
  76. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  77. -- Get the yaw of the ship
  78. local function getYaw()
  79. local rotMatrix = ship.getTransformationMatrix()
  80. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  81. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) + math.pi -- Extract yaw from the matrix
  82. end
  83.  
  84. -- Get the roll of the ship
  85. local function getRoll()
  86. local rotMatrix = ship.getTransformationMatrix()
  87. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  88. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  89. end
  90.  
  91. local function PIDController(Kp, Ki, Kd, error, integral, derivative, prevError, dt)
  92. -- Calculate the proportional, integral, and derivative components
  93. local proportional = Kp * error
  94. integral = integral + error * dt
  95. derivative = (error - prevError) / dt
  96.  
  97. -- Calculate output
  98. local output = proportional + (Ki * integral) + (Kd * derivative)
  99.  
  100. -- Return the PID output and updated integral and previous error
  101. return output, integral, error
  102. end
  103.  
  104. local function modemMessage()
  105. while true do
  106. if modem then
  107. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  108. if channel == cannonHitPosChannel then
  109. cannonHitPos = message
  110. elseif channel == controlChannel then
  111. controls = message
  112. elseif channel == flightInfoChannel then
  113. flightInfo = message
  114. end
  115. else
  116. sleep()
  117. end
  118. end
  119. end
  120.  
  121. local bladeCol = 0
  122. local bladeColAdjustment = 0
  123. -- Y Velocity stabilization using blade collective angle (bladeCol)
  124. local function yVelStabilize(desiredYVel)
  125. local currentVel = flightInfo.velocity
  126.  
  127. -- Calculate the error between desired and current Y velocity
  128. local yVelError = desiredYVel - currentVel.y
  129.  
  130. -- Use PID controller to calculate bladeCol (collective pitch) adjustment
  131. bladeColAdjustment, yVelIntegral, yVelPrevError = PIDController(
  132. Kp_rotor, Ki_rotor, Kd_rotor,
  133. yVelError, yVelIntegral,
  134. (yVelError - yVelPrevError), yVelPrevError,
  135. dt
  136. )
  137. bladeCol = bladeCol + bladeColAdjustment
  138. bladeCol = math.max(-5, math.min(20, bladeCol))
  139.  
  140. -- Clamp bladeCol between -10 (negative lift) and 30 (max positive lift)
  141. --local bladeCol = math.max(-10, math.min(30, bladeColAdjustment))
  142. --return bladeCol
  143. end
  144.  
  145. local function pitchStabilizer(desiredPitch)
  146. -- Get the current pitch from the helicopter
  147. currentPitch = math.deg(flightInfo.pitch)
  148.  
  149. -- Calculate the error between the desired pitch and the current pitch
  150. pitchError = desiredPitch - currentPitch
  151.  
  152. pitchAdjustment, pitchIntegral, pitchPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchError, pitchIntegral, (pitchError - pitchPrevError), pitchPrevError, dt)
  153. pitchAdjustment = pitchAdjustment
  154. -- Adjust the rotor speed or control surfaces based on the PID output
  155. return pitchAdjustment
  156. end
  157.  
  158.  
  159. local Kp_roll, Ki_roll, Kd_roll = 0.2, 0, 0
  160. local rollError = 0
  161. local rollIntegral = 0
  162. local rollPrevError = 0
  163. local function rollStabilizer(desiredRoll)
  164. -- Get the current pitch from the helicopter
  165. currentRoll = math.deg(flightInfo.roll)
  166.  
  167. -- Calculate the error between the desired pitch and the current pitch
  168. rollError = desiredRoll - currentRoll
  169.  
  170. rollAdjustment, rollIntegral, rollPrevError = PIDController(Kp_roll, Ki_roll, Kd_roll, rollError, rollIntegral, (rollError - rollPrevError), rollPrevError, dt)
  171. -- Adjust the rotor speed or control surfaces based on the PID output
  172. return rollAdjustment
  173. end
  174.  
  175. -- Normalize angle to 0–360 degrees
  176. local function normalizeAngleRad(rad)
  177. local deg = math.deg(rad) % 360
  178. if deg < 0 then deg = deg + 360 end
  179. return deg
  180. end
  181.  
  182. local function getBladeQuadrant(bladeYawDeg, shipYawDeg)
  183. local relativeYaw = (bladeYawDeg - shipYawDeg + 360) % 360
  184. if relativeYaw >= 0 and relativeYaw < 90 then
  185. return 1 -- Front
  186. elseif relativeYaw >= 90 and relativeYaw < 180 then
  187. return 2 -- Right
  188. elseif relativeYaw >= 180 and relativeYaw < 270 then
  189. return 3 -- Back
  190. elseif relativeYaw >= 270 and relativeYaw < 360 then
  191. return 4 -- Left
  192. end
  193. end
  194.  
  195. local shipYaw = 0
  196. --[[ Inside main loop, after computing bladeCol and pitchAdjustment
  197. local function applyBladeControl(blade, bladeYaw, baseCol, pitchAdj, rollAdj, bladeName)
  198. local bladeYawDeg = normalizeAngleRad(bladeYaw)
  199. if bladeName == "1" then
  200. bladeYawDeg = bladeYawDeg
  201. elseif bladeName == "2" then
  202. bladeYawDeg = bladeYawDeg + 90
  203. elseif bladeName == "3" then
  204. bladeYawDeg = bladeYawDeg + 180
  205. elseif bladeName == "4" then
  206. bladeYawDeg = bladeYawDeg + 270
  207. end
  208. bladeYawDeg = bladeYawDeg % 360
  209. local quadrant = getBladeQuadrant(bladeYawDeg, shipYaw)
  210.  
  211. print(string.format("Blade %s Yaw: %.1f | ShipYaw: %.1f | Quadrant: %d", bladeName, bladeYawDeg, shipYaw, quadrant))
  212.  
  213. local finalCol = baseCol
  214. if quadrant == 2 then
  215. finalCol = baseCol - pitchAdj
  216. elseif quadrant == 4 then
  217. finalCol = baseCol + pitchAdj
  218. end
  219. if quadrant == 1 then
  220. finalCol = finalCol - rollAdj
  221. elseif quadrant == 3 then
  222. finalCol = finalCol + rollAdj
  223. end
  224. -- 1 3: roll
  225. --print("finalCol: "..finalCol)
  226. if bladeName == "1" or bladeName == "4" then
  227. flapAdjustedCol = -finalCol
  228. else
  229. flapAdjustedCol = finalCol
  230. end
  231. --print("flapAdjustedCol: "..flapAdjustedCol)
  232. blade.setAngle(flapAdjustedCol)
  233. end]]
  234.  
  235. --------------------------------------------------------------------
  236. -- 1. 预先给每片桨叶一个 0-360° 的固定相位(以机头方向 = 0° 为基准)
  237. --------------------------------------------------------------------
  238. local bladePhaseDeg = { -- bladeName → 角度
  239. ["1"] = 0, -- front
  240. ["2"] = 90, -- left
  241. ["3"] = 180, -- back
  242. ["4"] = 270 -- right
  243. }
  244.  
  245. --------------------------------------------------------------------
  246. -- 2. 连续循环控制函数
  247. --------------------------------------------------------------------
  248. local function applyBladeControl(blade, bladeYawRad, baseCol, pitchAdj, rollAdj, bladeName)
  249. ------------------------------------------------------------
  250. -- (a) 计算该桨叶的绝对方位角(世界坐标,°)
  251. ------------------------------------------------------------
  252. local shipYawDeg = math.deg(flightInfo.yaw)
  253. local bladeYawDeg = (math.deg(bladeYawRad) + bladePhaseDeg[bladeName]) % 360
  254.  
  255. ------------------------------------------------------------
  256. -- (b) 相对相位 rel = bladeYaw – shipYaw (-180°~+180°)
  257. ------------------------------------------------------------
  258. local relDeg = ((bladeYawDeg - shipYawDeg - 60) % 360) - 180
  259. local relRad = math.rad(relDeg)
  260.  
  261. ------------------------------------------------------------
  262. -- (c) 计算连续攻角
  263. ------------------------------------------------------------
  264. local finalCol =
  265. baseCol
  266. + pitchAdj * math.sin(relRad) -- 纵向 (sin)
  267. + rollAdj * math.cos(relRad) -- 横向 (cos)
  268.  
  269. -- 安全限幅
  270. --finalCol = math.max(-10, math.min(30, finalCol))
  271.  
  272. if bladeName == "4" or bladeName == "1" then
  273. flapAdjustedCol = -finalCol
  274. else
  275. flapAdjustedCol = finalCol
  276. end
  277.  
  278. blade.setAngle(flapAdjustedCol)
  279. end
  280.  
  281. local function main()
  282. while true do
  283. if flightInfo and flightInfo.velocity then --and controls.engine == "on"
  284. local now = os.clock()
  285. dt = now - lastTime
  286. lastTime = now
  287.  
  288. if controls.rotorUp then
  289. bladeCol = 15
  290. elseif controls.rotorDown then
  291. yVelStabilize(-10)
  292. else
  293. yVelStabilize(0)
  294. end
  295.  
  296.  
  297. if math.abs(newRotorSpeed) < 128 then
  298. AuxControls.tailSpeed = newRotorSpeed * 1
  299. else
  300. AuxControls.tailSpeed = newRotorSpeed * 0.085
  301. end
  302.  
  303. if controls.yawLeft then
  304. AuxControls.tailSpeed = AuxControls.tailSpeed + 256
  305. elseif controls.yawRight then
  306. AuxControls.tailSpeed = AuxControls.tailSpeed - 256
  307. end
  308.  
  309. AuxControls.tailSpeed = math.max(-256, math.min(256, AuxControls.tailSpeed))
  310.  
  311. local velocity = ship.getVelocity()
  312. local speed = math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2)
  313. if controls.pitchUp then
  314. desiredPitch = -30
  315. elseif controls.pitchDown then
  316. desiredPitch = 30
  317. else
  318. desiredPitch = 0
  319. end
  320.  
  321. if controls.rollLeft then
  322. desiredRoll = 20
  323. elseif controls.rollRight then
  324. desiredRoll = -20
  325. else
  326. desiredRoll = 0
  327. end
  328.  
  329. --local pitchAdjustment = pitchStabilizer(desiredPitch)
  330.  
  331. if controls.pitchUp then
  332. pitchAdjustment = -20
  333. elseif controls.pitchDown then
  334. pitchAdjustment = 20
  335. else
  336. pitchAdjustment = 0
  337. end
  338. if controls.rollLeft then
  339. rollAdjustment = 15
  340. elseif controls.rollRight then
  341. rollAdjustment = -15
  342. else
  343. rollAdjustment = rollStabilizer(desiredRoll)
  344. end
  345.  
  346.  
  347. if controls.rotorDown then
  348. pitchAdjustment = -pitchAdjustment
  349. rollAdjustment = -rollAdjustment
  350. end
  351.  
  352. shipYaw = normalizeAngleRad(flightInfo.yaw)
  353.  
  354. applyBladeControl(blade1, getYaw(), bladeCol, pitchAdjustment, rollAdjustment, "1")
  355. applyBladeControl(blade2, getYaw(), bladeCol, pitchAdjustment, rollAdjustment, "2")
  356. applyBladeControl(blade3, getYaw(), bladeCol, pitchAdjustment, rollAdjustment, "3")
  357. applyBladeControl(blade4, getYaw(), bladeCol, pitchAdjustment, rollAdjustment, "4")
  358. end
  359. sleep()
  360. end
  361. end
  362.  
  363. parallel.waitForAny(
  364. main,
  365. modemMessage
  366. )
Advertisement
Add Comment
Please, Sign In to add comment