私的AI研究会? > AIbot Proj.1
#ref(): File not found: "20200408_142232_001s.jpg" at page "画像一覧"
#ref(): File not found: "20200303_080643_001s.jpg" at page "画像一覧"
#ref(): File not found: "20200303_080517_001s.jpg" at page "画像一覧"
#ref(): File not found: "20200310_160731_001s.jpg" at page "画像一覧"
#ref(): File not found: "20200326_132657_001s.jpg" at page "画像一覧"
#ref(): File not found: "raspi_box_s.jpg" at page "画像一覧"
#ref(): File not found: "raspi_case2s.jpg" at page "画像一覧"
#ref(): File not found: "20200308_085842_001s.jpg" at page "画像一覧"
#ref(): File not found: "20200319_081123_001s.jpg" at page "画像一覧"
#ref(): File not found: "20200407_164609_001s.jpg" at page "画像一覧"
#ref(): File not found: "20200407_164632_001s.jpg" at page "画像一覧"
#ref(): File not found: "fig6s.jpg" at page "画像一覧"
#ref(): File not found: "fig9m.jpg" at page "画像一覧"
● fig.6
LED・スイッチ・スピーカー・オーディオアンプ
→ ケースの作成・基板実装・配線?
● fig.9
LED・DCモーター
→ キャタピラーロボットの組み立て?
aibot.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # aibot.py # AI Robot Controle Library on Raspberry Pi4 # # DC motor DRV8835 controle # A-IN1 : GPIO 14 (BCM) B-IN1 : GPIO 15 (BCM) # A-IN2 : 23 B-IN2 : 24 # LED-S : 16 LED-L : 17 # LED-R : 18 # # Tact switch # ext pullup : GPIO 20 # import wiringpi as pi import sys import time class MotorCtrl_base: def __init__(self, a_phase, a_enbl, led): pi.wiringPiSetupGpio() if a_phase > 0: self.a_phase = a_phase pi.pinMode(self.a_phase, pi.OUTPUT) if a_enbl > 0: self.a_enbl = a_enbl pi.pinMode(self.a_enbl, pi.OUTPUT) if led > 0: self.led = led pi.pinMode(self.led, pi.OUTPUT) def fwd(self): # 回転(正転) pi.digitalWrite(self.a_phase, 1) pi.digitalWrite(self.a_enbl, 1) def back(self): # 回転(逆転) pi.digitalWrite(self.a_phase, 0) pi.digitalWrite(self.a_enbl, 1) def stop(self): # 停止 pi.digitalWrite(self.a_phase, 0) pi.digitalWrite(self.a_enbl, 0) def led_on(self): # ON pi.digitalWrite(self.led, 1) def led_off(self): # OFF pi.digitalWrite(self.led, 0) def reset(self): pi.digitalWrite(self.a_phase, 0) pi.digitalWrite(self.b_phase, 0) pi.digitalWrite(self.led, 0) class MotorCtrl: def __init__(self): self.dcmotor1 = MotorCtrl_base(a_phase=14, a_enbl=23, led=0) # 右モーター self.dcmotor2 = MotorCtrl_base(a_phase=15, a_enbl=24, led=0) # 左モーター self.led_sw = MotorCtrl_base(a_phase=0, a_enbl=0, led=16) # switch LED self.led_right = MotorCtrl_base(a_phase=0, a_enbl=0, led=18) # 右LED self.led_left = MotorCtrl_base(a_phase=0, a_enbl=0, led=17) # 左LED def stop(self): self.dcmotor1.stop() self.dcmotor2.stop() self.led_off() self.led_sw.led_off() def forword(self): self.dcmotor1.fwd() self.dcmotor2.fwd() def wait_forword(self, cn): for i in range(cn): self.fwd_led() def fwd_led(self): self.led_right.led_on() self.led_left.led_on() time.sleep(0.5) def back(self): self.dcmotor1.back() self.dcmotor2.back() def wait_back(self, cn): for i in range(cn): self.back_led() def back_led(self): self.led_right.led_on() self.led_left.led_on() time.sleep(0.25) self.led_right.led_off() self.led_left.led_off() time.sleep(0.25) def turn_left(self): self.dcmotor1.fwd() self.dcmotor2.back() def wait_turn_left(self, cn): for i in range(cn): self.turn_left_led() def turn_left_led(self): self.led_left.led_on() self.led_right.led_on() time.sleep(0.25) self.led_right.led_off() time.sleep(0.25) def turn_right(self): self.dcmotor1.back() self.dcmotor2.fwd() def wait_turn_right(self, cn): for i in range(cn): self.turn_right_led() def turn_right_led(self): self.led_right.led_on() self.led_left.led_on() time.sleep(0.25) self.led_left.led_off() time.sleep(0.25) def left(self): self.dcmotor1.fwd() self.dcmotor2.stop() def wait_left(self, cn): for i in range(cn): self.left_led() def left_led(self): self.led_left.led_off() self.led_right.led_on() time.sleep(0.25) self.led_right.led_off() time.sleep(0.25) def right(self): self.dcmotor1.stop() self.dcmotor2.fwd() def wait_right(self, cn): for i in range(cn): self.right_led() def right_led(self): self.led_right.led_off() self.led_left.led_on() time.sleep(0.25) self.led_left.led_off() time.sleep(0.25) def led_off(self): self.led_left.led_off() self.led_right.led_off() if __name__ == '__main__': motor = MotorCtrl() motor.stop() motor.led_off() args = sys.argv if 2 <= len(args): duration = sys.argv[2] if sys.argv[1] in {"stop"}: motor.stop() elif sys.argv[1] in {"forword"}: motor.forword() motor.wait_forword(int(duration)) motor.stop() elif sys.argv[1] in {"back"}: motor.back() motor.wait_back(int(duration)) motor.stop() elif sys.argv[1] in {"turn-left"}: motor.turn_left() motor.wait_turn_left(int(duration)) motor.stop() elif sys.argv[1] in {"turn-right"}: motor.turn_right() motor.wait_turn_right(int(duration)) motor.stop() elif sys.argv[1] in {"left"}: motor.left() motor.wait_left(int(duration)) motor.stop() elif sys.argv[1] in {"right"}: motor.right() motor.wait_right(int(duration)) motor.stop() else: print("Need Argument;forword, right, left, turn-right, turn-left, back or stop") else: print("Command Format: aibot [forword|right|left|turn-right|turn-left|back|stop] [count(0.5sec)]")
#!/usr/bin/env python # -*- coding: utf-8 -*- # aibotrm.py <<このソースコードは IDE デバッグはできない>> # AI Robot Remort Program on Raspberry Pi4 # # DC motor DRV8835 controle # A-IN1 : GPIO 14 (BCM) B-IN1 : GPIO 15 (BCM) # A-IN2 : 23 B-IN2 : 24 # LED-S : 16 LED-L : 17 # LED-R : 18 # # Tact switch # ext pullup : GPIO 20 # import wiringpi as pi from kbhit import * import aibot import sys if __name__ == '__main__': atexit.register(set_normal_term) set_curses_term() motor = aibot.MotorCtrl() motor.stop() motor.led_off() ledmod = 0 kb = ' ' print("AIbot Control Ok! 'i,m,j,l,k,,u,o' or 'q'") while 1: # key push check & Motor control if kbhit(): kb = getch() if kb == 'q': print(" ## Break ##") motor.stop() motor.led_off() ledmod = 9 elif kb == 'A' or kb == 'i': print(" ** Forword **") motor.forword() ledmod = 1 elif kb == 'B' or kb == 'm': print(" ** Bsck **") motor.back() ledmod = 2 elif kb == 'C' or kb == 'j': print(" ** Left **") motor.left() ledmod = 3 elif kb == 'D' or kb == 'l': print(" ** Right **") motor.right() ledmod = 4 elif kb == '2' or kb == 'o': print(" ** Right turn **") motor.turn_right() ledmod = 5 elif kb == '1' or kb == 'u': print(" ** Left turn **") motor.turn_left() ledmod = 6 elif kb == 's' or kb == 'k' or kb == ' ': print(" !! Stop !!") motor.stop() ledmod = 0 else: print("AiBot Standby OK !!!!") # LED mode set & wait 0.5sec if ledmod == 0: motor.led_off() elif ledmod == 1: motor.fwd_led() elif ledmod == 2: motor.back_led() elif ledmod == 3: motor.left_led() elif ledmod == 4: motor.right_led() elif ledmod == 5: motor.turn_right_led() elif ledmod == 6: motor.turn_left_led() elif ledmod == 9: break else: pass ledmod = 0 print("Program Finished !!!")
# -*- cording: utf-8 -*- import time import RPi.GPIO as GPIO LED = 16 BUTTON = 20 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(LED, GPIO.OUT) GPIO.setup(BUTTON, GPIO.IN) GPIO.add_event_detect(BUTTON, GPIO.FALLING) while True: if GPIO.event_detected(BUTTON): print ("Raspberry Pi Switch ON !!") break else: GPIO.output(LED, GPIO.HIGH) time.sleep(0.5) GPIO.output(LED, GPIO.LOW) time.sleep(0.5) GPIO.cleanup() print ("Start OK !!!")
#!/bin/bash cd ~/Programs/aibot python wait.py echo "Motor Test Program Start..." python aibot.py forword 6 python aibot.py back 6 python aibot.py left 4 python aibot.py right 4 python aibot.py turn-left 3 python aibot.py turn-right 3 python motor3.py stop 1 echo "Aibot Remote Start..."
Description=Aibot boot [Service] ExecStart=/bin/bash /home/pi/Programs/aibot/aibot_boot.sh WorkingDirectory=/home/pi/Programs/aibot/ Restart=always User=pi [Install] WantedBy=multi-user.target
Pythonではreadcharというライブラリを使う。
$ pip install readchar
#!/usr/bin/env python import readchar import sys while 1: kb = readchar.readchar() # sys.stdout.write(kb) print(ord(kb)) if kb == 'q': print("") break
Python error: (25, 'Inappropriate ioctl for device')