-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconectwifi
More file actions
204 lines (172 loc) · 5.55 KB
/
Copy pathconectwifi
File metadata and controls
204 lines (172 loc) · 5.55 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
class Ebichan
wlan = WLAN.new('STA')
wlan.connect("RubyCamp", "shimanekko")
attr_accessor :lux_right, :lux_left, :fieldout, :catched, :vl53l0x, :counter, :ball_find
def initialize
@motor1_pwm1 = PWM.new(25, timer:0, channel:1)
@motor1_pwm2 = PWM.new(26, timer:0, channel:2)
@motor2_pwm1 = PWM.new(32, timer:1, channel:3)
@motor2_pwm2 = PWM.new(33, timer:1, channel:4)
@lux_right = ADC.new(35)
@lux_left = ADC.new(2)
@servo1 = PWM.new(27, timer: 2, channel: 5, frequency: 50)
@servo2 = PWM.new(14, timer: 2, channel: 6, frequency: 50)
@i2c = I2C.new()
@vl53l0x = VL53L0X.new(@i2c)
@vl53l0x.set_timeout(2000)
@fieldout = false
@catched = false
@ball_find = false
@counter = 0
@vl53l0x.init
@vl53l0x.start_continuous(500)
end
def move(m1p1,m1p2,m2p1, m2p2)
@motor1_pwm1.duty(m1p1)
@motor1_pwm2.duty(m1p2)
@motor2_pwm1.duty(m2p1)
@motor2_pwm2.duty(m2p2)
end
def stop; move(50, 50, 50, 50); end
def back; move(50, 100, 50, 100); end
def turn_left; move(100, 50, 50, 50); end
def turn_right; move(50, 50, 100, 50 ); end
def turnslow_left; move(100, 77, 50, 50); end
def turnslow_right; move(50, 50, 100, 77); end
def dash; move(100, 70, 100, 67); end
def dash2; move(100, 60, 100, 57); end
def hand_open
@servo1.pulse_width_us( 1900 )
@servo2.pulse_width_us( 1100 )
end
def hand_close
@servo1.pulse_width_us( 1300 )
@servo2.pulse_width_us( 1700 )
end
def read_distance
@distance = @vl53l0x.read_range_continuous_millimeters
if @distance < 800
@ball_find = true
return true
else
return false
end
end
def send_data (now_pos_kani_x,now_pos_kani_y,now_ang_kani,now_pos_ball_x,now_pos_ball_y,now_ang_ball)
#if wlan.connected?
#192.168.6.31落合
HTTP.get( "http://192.168.6.31:3000/angle?op=abs&value=#{now_ang_kani}&target=Kani1")
HTTP.get( "http://192.168.6.31:3000/position?op=abs&x=#{now_pos_kani_x}&y=#{now_pos_kani_y}&target=Kani1")
HTTP.get( "http://192.168.6.31:3000/angle?op=abs&value=#{now_ang_ball}&target=Ball")
HTTP.get( "http://192.168.6.31:3000/position?op=abs&x=#{now_pos_ball_x}&y=#{now_pos_ball_y}&target=Ball")
#192.168.6.22藤田
#HTTP.get( "http://192.168.6.22:3000/angle?op=abs&value=#{now_ang_kani}&target=Kani1")
#HTTP.get( "http://192.168.6.22:3000/position?op=abs&x=#{now_pos_kani_x}&y=#{now_pos_kani_y}&target=Kani1")
#HTTP.get( "http://192.168.6.22:3000/angle?op=abs&value=#{now_ang_ball}&target=Ball")
#HTTP.get( "http://192.168.6.22:3000/position?op=abs&x=#{now_pos_ball_x}&y=#{now_pos_ball_y}&target=Ball")
sleep 0.1
#end
end
end
robotto = Ebichan.new
robotto.hand_open
first_loop = true
while true do
if robotto.lux_left.read_raw < 200
robotto.fieldout = true
robotto.hand_open
if robotto.lux_right.read_raw < 200
# 左右とも黒
robotto.stop
sleep 2
robotto.back
sleep 3
#一旦停止
robotto.turn_left
else
# 左が黒で右が白
robotto.stop
sleep 2
robotto.back
sleep 3
robotto.turn_right
end
else
if robotto.lux_right.read_raw < 200
robotto.fieldout = true
robotto.hand_open
# 左が黒ではない右が黒
robotto.stop
sleep 2
robotto.back
sleep 3
robotto.turn_left
else
# 左右とも白(前進)
robotto.fieldout = false
if !first_loop
robotto.dash
if robotto.counter > 4 && !robotto.catched && robotto.ball_find
robotto.counter = -1
robotto.ball_find = false
end
else
robotto.turnslow_right
sleep 2
first_loop = false
end
end
end
distance = robotto.vl53l0x.read_range_continuous_millimeters
if distance < 200 && !robotto.fieldout
robotto.hand_close
if !robotto.catched
robotto.dash2
sleep (1.5)
end
robotto.ball_find = true
robotto.catched = true
elsif robotto.ball_find == false
robotto.hand_open
#首振り
sleep 3
robotto.turnslow_left
5.times do
sleep 1
if robotto.read_distance
break
end
end
if robotto.ball_find
robotto.counter = -1
next
end
if robotto.lux_left.read_raw < 200
robotto.turnslow_right
sleep 1
end
robotto.turnslow_right
sleep 3
6.times do
sleep 1
if robotto.read_distance
break
end
end
if robotto.ball_find
robotto.counter = -1
next
end
if robotto.lux_right.read_raw < 200
robotto.turnslow_left
sleep 1
end
if robotto.read_distance
next
end
robotto.turnslow_left
sleep 2
end
sleep 1
robotto.counter += 1
end