[[2016a/Member]]

#contents
*内容 [#n9a47e72]
今回の課題はライントレースをすることである。~
今回自分がたどるルートは地点B→地点R→右折→地点Q→直進→地点Q→直進→地点P→右折→地点S→右折→地点Cです。
#ref(line01.jpg)
*ロボットについて [#eef3a4a8]

*プログラム [#o746260a]

 #!/usr/bin/python
 # -*- coding: utf-8 -*-

 import ev3dev.ev3 as ev3
**ロボットを動かすための設定をする。 [#t7a09902]
 MR = ev3.LargeMotor('outA') #右側のモーターを出力ポートAで制御
 ML = ev3.LargeMotor('outB')  #左側のモーターを出力ポートBで制御
 cs = ev3.ColorSensor('in1')  #光センサーのデータを入力ポート1で受信
 S = ev3.Sound()

 MR = ev3.LargeMotor('outA')
 ML = ev3.LargeMotor('outB')
 cs = ev3.ColorSensor('in1')
 t0 = time.time()
 S = ev3.Sound()
**定義する [#pc425198]
ロボットを停止させるプログラムを st() のみで表せるように設定する。
 def st():
      MR.stop()
      ML.stop()
      MR.stop()      #右側のモーターを停止
      ML.stop()   #左側のモーターを停止


ライントレースのプログラムは複雑なため、複数回書くと間違える確率が高くなるため、~
line(a)  で表すための設定をする。
 def line(a):
       t0 = time.time()
       while time.time() - t0 < a:
                if cs.value() > 30:
                if cs.value() > 30:  #センサーが感知した光の明るさが30以上の時
                         MR.run_forever(duty_cycle_sp=-30)
                         ML.run_forever(duty_cycle_sp=20)
                         ML.run_forever(duty_cycle_sp=20)  #車体を右に回転させる
                         t0 = time.time()
                elif cs.value() >= 10:
                elif cs.value() >= 10:  #光センサーが感知した光の明るさが10以上30以下の時
                         MR.run_forever(duty_cycle_sp=35)
                         ML.run_forever(duty_cycle_sp=35)
                         ML.run_forever(duty_cycle_sp=35)  #車体を直進させる
                         t0 = time.time()
                else:
                         MR.run_forever(duty_cycle_sp=20)
                         ML.run_forever(duty_cycle_sp=-30)
 
ロボットが前後左右に動くプログラムを run(r,l,t) で表すための設定する。 
 def run(r,l,t):
          MR.run_forever(duty_cycle_sp=r)
          ML.run_forever(duty_cycle_sp=l)
          time.sleep(t)
          MR.stop()
          ML.stop()
ロボットが交差点を右折するためのプログラムを R(b) で表すための設定をする。
 def R(b):
         MR.run_forever(duty_cycle_sp=-30)
         ML.run_forever(duty_cycle_sp=20)
         time.sleep(1)
         MR.stop()
         ML.stop()
         MR.run_forever(duty_cycle_sp=30)
         ML.run_forever(duty_cycle_sp=30)
         time.sleep(b)
         MR.stop()
         ML.stop() 
  
**実際に走行するためのプログラム [#fda4688e]

 line(0.22)
 S.beep()
 R(0.75)
 line(0.245)
 S.beep()
 run(-30,20,0.22)
 run(50,50,0.5)
 line(0.22)
 S.beep()
 run(-30,20,0.22)
 run(50,50,0.3)
 line(0.22)
 S.beep()
 R(0.75)
 line(0.22)
 S.beep()
 R(0.75)
 line(0.22)
 S.beep()
 run(-30,20,0.22)
 run(50,50,0.3)
 run(100,100,1)
 st()

*結果・考察 [#vd19bc09]


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS