-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrbcompass.lua
More file actions
214 lines (198 loc) · 5.79 KB
/
rbcompass.lua
File metadata and controls
214 lines (198 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
------------------------------------------
-- Robot Compass
------------------------------------------
--関数宣言--------------------------------
main={} --mainメソッド
setTateGamen={} --縦向きに変更
print={} --スクロールするテキスト表示
sendcmd={} --コマンドを送信します
iniRngBuf={} --リングバッファの初期化
getKaku={} --角度の取得
--グローバル変数宣言----------------------
Cmps={ mRngBuf={}, cnt=1, cntmax=5 }
------------------------------------------
mt={}
mt.__newindex=function(mtt,mtn,mtv)
dialog( "Error Message", "宣言していない変数 "..mtn.." に値を入れようとしています", 0 )
toast("画面タッチで実行を続けます", 1)
touch(3)
end
mt.__index=function(mtt,mtn)
dialog( "Error Message", "変数 "..mtn.." は宣言されていません", 0 )
toast("画面タッチで実行を続けます", 1)
touch(3)
end
setmetatable(_G,mt)
--------以下が実プログラム----------------
------------------------------------------
--縦向きに変更
------------------------------------------
function setTateGamen()
system.setScreen(1)
--内部グラフィック画面設定の変更
local w,h = canvas.getviewSize()
canvas.setMainBmp( w, h )
end
------------------------------------------
--スクロールするテキスト表示
-- 文字, サイズ, 色, 背景色
------------------------------------------
function print(...)
local w, h = canvas.getviewSize() --画面サイズ取得
local t={...}
local str = t[1]
if( str==nil )then str = "" end
local fontsize = t[2]
if( fontsize==nil )then fontsize = 18 end
local fcolor = t[3]
if( fcolor==nil )then fcolor = color(0,0,0) end
local bcolor = t[4]
if( bcolor==nil )then bcolor = color(255,255,255) end
--一度、見えないところにテキストを書いて、改行数を求める
local sc = canvas.putTextBox( str, 0, h+1, fontsize, fcolor, w )
--画面の絵をワークエリアに取り込みます
canvas.getg( 0, fontsize*sc, w-1, h-1, 0, fontsize*sc, w-1, h-1 )
--取り込んだ画面をスクロールさせて描きます
canvas.putg( 0, 0, w-1, h-fontsize*sc-1, 0, fontsize*sc, w-1, h-1 )
--書き出す部分をバックカラーで塗り潰します
canvas.putRect( 0, h-fontsize*sc-1, w, h, bcolor, 1 )
--スクロールしたところにテキストを書きます
canvas.drawTextBox( str, 0, h-fontsize*sc, fontsize, fcolor, w )
end
------------------------------------------
--コマンドを送信します
------------------------------------------
function sendcmd( cmd, sec )
local txt, ret,psec
while( true )do
--print( cmd:sub(1,1) )
sock.nsend( 1, cmd, 2 )
if( cmd:sub(1,1)=="Q" )then break end
txt, ret = sock.nrecv( 1 )
--print( txt..","..ret )
if( txt==cmd:sub(1,1) )then break end
print( "コマンド送信に失敗しました" )
--dialog( "","コマンド送信に失敗しました",1 )
end
psec=system.getSec() + sec -0.4
while psec>system.getSec() do end
end
------------------------------------------
--リングバッファの初期化
------------------------------------------
function iniRngBuf()
local i
for i=1, Cmps.cntmax do
Cmps.mRngBuf[i] = sensor.getOrient()
end
Cmps.cnt = 1
end
------------------------------------------
--角度の取得
------------------------------------------
function getKaku()
local i
Cmps.mRngBuf[Cmps.cnt] = sensor.getOrient()
Cmps.cnt = Cmps.cnt + 1
if( Cmps.cnt>Cmps.cntmax )then Cmps.cnt = 1 end
local kaku = 0
for i=1, Cmps.cntmax do
kaku = kaku + Cmps.mRngBuf[i]
end
return kaku/Cmps.cntmax
end
------------------------------------------
--メインプログラム
------------------------------------------
function main()
--画面を縦向きに変更
setTateGamen()
canvas.drawCls( color(255,255,255) ) --背景を白色に
system.setSleep(0) --スリープしない
--60101ポートへの接続を10秒間待つ
local ret = sock.nlistenOpen( 1, 60101, 10 )
if( ret~=1 )then
dialog( "adb接続に失敗しました", "終了します" , 1 )
system.exit()
end
--接続できたことを表示する
print( "MicroBridgeと接続しました")
--方位センサ起動
sensor.setdevOrient(1)
--リングバッファの初期化
iniRngBuf()
local cmd
--ギアを回転に入れる間を取る
cmd = "L"..string.char(25)
sendcmd( cmd, 0.2 )
local gosa = 5
local spd = 64
local tim = 0.5
local kaku = 0
while(true)do
kaku = 0
while(kaku==0)do
kaku = sensor.getOrient()
end
if( kaku>90 and kaku<270 )then
spd = 75
tim = 1.0
elseif( (kaku>45 and kaku<=90) or (kaku>=270 and kaku<315) )then
spd = 60
tim = 0.4
else
spd = 38
tim = 0.2
end
if( kaku<=180 and kaku>gosa )then
--左回り
cmd = "L"..string.char(spd)
sendcmd( cmd, tim )
elseif( kaku>180 and kaku<360-gosa )then
--右回り
cmd = "R"..string.char(spd)
sendcmd( cmd, tim )
end
if( kaku>350 or kaku<10 )then
--止める
cmd = "S"..string.char(32)
sendcmd( cmd, 0.2 )
if( kaku<=gosa or kaku>=360-gosa )then
local w,h = canvas.getviewSize()
canvas.putCls( color(255,255,255) )
local fs = 55
local y = fs/1.5
canvas.putTextRotate( "こ", w/2, y, 0, fs, color(0,0,0) )
y = y + fs
canvas.putTextRotate( "っ", w/2, y, 0, fs, color(0,0,0) )
y = y + fs
canvas.putTextRotate( "ち", w/2, y, 0, fs, color(0,0,0) )
y = y + fs
canvas.putTextRotate( "が", w/2, y, 0, fs, color(0,0,0) )
y = y + fs
canvas.putTextRotate( "北", w/2, y, 0, fs, color(0,0,0) )
y = y + fs
canvas.putTextRotate( "だ", w/2, y, 0, fs, color(0,0,0) )
y = y + fs
canvas.putTextRotate( "よ", w/2, y, 0, fs, color(0,0,0) )
y = y + fs
canvas.putTextRotate( "!", w/2, y, 0, fs, color(0,0,0) )
canvas.putflush()
end
else
--canvas.drawCls( color(255,255,255) )
print( kaku )
end
local x,y,s = touch()
if( s~=1 )then break end
end
--PIC動作終了
cmd = "C"..string.char(40)
sendcmd( cmd, 0.2 )
--傾斜センサ終了
sensor.setdevOrient(0)
sock.nclose( 1 )
--dialog( "", "電源を切ってください" , 1 )
end
main()
system.exit()