• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# gps_controller

a guest Mar 10th, 2017 123 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local component=require("component")
2. local event=require("event")
3. local computer=require("computer")
4.
5. --(детерминант для метода Крамера)
6. function det(a1,a2,a3,a4,a5,a6,a7,a8,a9)
7.   return a1*a5*a9+a2*a6*a7+a3*a4*a8-a1*a6*a8-a2*a4*a9-a3*a5*a7
8. end
9.
10. --(Сам метод Крамера)
11. function cram(a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4)
12.   local det_coeff=det(a1,a2,a3,b1,b2,b3,c1,c2,c3)
13.   if not det_coeff then return nil end
14.   local det1=det(a4,a2,a3,b4,b2,b3,c4,c2,c3)
15.   local det2=det(a1,a4,a3,b1,b4,b3,c1,c4,c3)
16.   local det3=det(a1,a2,a4,b1,b2,b4,c1,c2,c4)
17.   return det1/det_coeff,det2/det_coeff,det3/det_coeff
18. end
19.
20. --(Применение к нашей задаче)
21. function triangulate(x1,y1,z1,d1,x2,y2,z2,d2,x3,y3,z3,d3,x4,y4,z4,d4)
22.   return cram(
23. 2*(x2-x1),2*(y2-y1),2*(z2-z1),d1^2-d2^2-x1^2-y1^2-z1^2+x2^2+y2^2+z2^2,
24. 2*(x3-x2),2*(y3-y2),2*(z3-z2),d2^2-d3^2-x2^2-y2^2-z2^2+x3^2+y3^2+z3^2,
25. 2*(x4-x3),2*(y4-y3),2*(z4-z3),d3^2-d4^2-x3^2-y3^2-z3^2+x4^2+y4^2+z4^2)
26. end
27.
28. local wired_modem
29. local wireless_modem
30. for i in component.list("modem")
31. do
32.   if component.proxy(i).isWireless()
33.   then
34.     wireless_modem=component.proxy(i)
35.   else
36.     wired_modem=component.proxy(i)
37.   end
38. end
39.
41. local requesttable={}
42.
43. function wake_relays()
45. end
46.
48.   print("Caught")
49.   if(eventname=="modem_message" and message=="gps_response" and distance==0)
50.   then
51.     local id,cx,cy,cz,cd,ra=...
52.     print("Got response from '"..id.."'.")
53.     if requesttable[ra]
54.     then
55.       if not requesttable[ra][id]
56.       then
57.         requesttable[ra].hits=requesttable[ra].hits+1
58.         requesttable[ra][id]=true
59.         requesttable[ra][requesttable[ra].hits]={cx,cy,cz,cd}
60.       end
61.     else
63.       requesttable[ra][id]=true
64.       requesttable[ra][1]={cx,cy,cz,cd}
65.       requesttable[ra].hits=1
66.     end
67.     if requesttable[ra].hits>=4 then respond(ra) end
68.   end
69. end
70.
71. function respond(ra)
72.   local rt=requesttable[ra]
73.   local x,y,z=triangulate(
74.     rt[1][1],rt[1][2],rt[1][3],rt[1][4],
75.     rt[2][1],rt[2][2],rt[2][3],rt[2][4],
76.     rt[3][1],rt[3][2],rt[3][3],rt[3][4],
77.     rt[4][1],rt[4][2],rt[4][3],rt[4][4])
78.   requesttable[ra]=nil
79.   print(ra)
80.   print(x,y,z)
81.   if(wireless_modem)
82.   then
83.     wireless_modem.setStrength(400)
84.     wireless_modem.send(ra,312,"gps_response",x,y,z)
85.   end
86. end
87.
88. function handle_requests()
89.   local remt={}
90.   for k,v in pairs(requesttable)
91.   do
94.   end
95.   for _,i in pairs(remt) do requesttable[i]=nil; print("Removing request from "..i.." due to timeout") end
96. end
97.
98. --(Собственно, начало проги)
99. wake_relays()
100. wired_modem.open(312)
101. --event.listen("modem_message",handle_modem)
102. local last_check=computer.uptime()
103. while true do
104.   local tbl={event.pull(1,"modem_message")}
105.   if(#tbl>0) then handle_modem(table.unpack(tbl)) end
106.   if computer.uptime()-last_check >= 1 then last_check=computer.uptime();  handle_requests() end
107. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top