Advertisement
firtel

Untitled

Jan 22nd, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. import board
  2. import digitalio
  3. import simpleio
  4. import time
  5. import pwmio
  6. from adafruit_motor import servo, motor
  7. import busio
  8. import adafruit_vl53l0x
  9.  
  10. # Initialize DC motors
  11. m1a = pwmio.PWMOut(board.GP8, frequency=50)
  12. m1b = pwmio.PWMOut(board.GP9, frequency=50)
  13. motor1 = motor.DCMotor(m1a, m1b)
  14. m2a = pwmio.PWMOut(board.GP10, frequency=50)
  15. m2b = pwmio.PWMOut(board.GP11, frequency=50)
  16. motor2 = motor.DCMotor(m2a, m2b)
  17.  
  18. #Initialize UART0(Bluetooth)
  19. uart0 = busio.UART(tx=board.GP16, rx=board.GP17, baudrate=9600)
  20.  
  21. #Initialize Sensor
  22. i2c = busio.I2C(scl=board.GP3, sda=board.GP2)
  23. sensor = adafruit_vl53l0x.VL53L0X(i2c)
  24.  
  25.  
  26. #モーターどっちも止める
  27. motor1.throttle = 0
  28. motor2.throttle = 0
  29.  
  30. #起動したことを知らせるためにBluetoothでなんかしゃべる
  31. txData = b'hello world\n\r'
  32. uart0.write(txData)
  33.  
  34.  
  35. #Joystick用関数
  36. #Initialize UART1(ESP-WROOM-02)
  37. uart1 = busio.UART(tx=board.GP4, rx=board.GP5, baudrate=9600)
  38. #WiFiモジュール通信用関数
  39. def get_joystick():
  40.     uart1.write(b'c')
  41.     data = uart1.read(3)
  42.     try:
  43.         if data[0] == 99:#99 = c
  44.             x = int.from_bytes(data[1:2],'big')
  45.             if x > 127:
  46.                 x = (256-x) * (-1)
  47.             y = int.from_bytes(data[2:3],'big')
  48.             if y > 127:
  49.                 y = (256-y) * (-1)
  50.             return x,y
  51.         else:
  52.             while(1):
  53.                 if uart1.read() == None:
  54.                     break
  55.             return 0,0
  56.     except:
  57.         return 0,0
  58. #ESP-WROOM-02起動時に出るゴミデータを消去    
  59. while(1):
  60.     if uart1.read() == None:
  61.         break
  62. #Joystick用関数 終わり
  63.  
  64. #txData = b'Range: {}mm'.format(sensor.range)
  65.  
  66. #print(b'Range: {}mm'.format(sensor.range))
  67.  
  68. #ポーリング用の変数初期化(前回実行した時間を格納
  69. now = 0#センサー用
  70. joytime = 0#ジョイスティック用
  71.  
  72.  
  73. #以下無限ループ
  74. while True:
  75.     #モーター制御値ポーリング
  76.     if time.monotonic() > (serialtime + 0.2):#ポーリング時間設定
  77.         joytime = time.monotonic()#今の時間をポーリング用の変数に記録して次回実行の判断に使う。
  78.         joy = get_joystick()#joyに最新のJoystick状態を取得(xとyの2つの値が来る)
  79.         #print("X:" + str(joy[0]))#joystickのX座標(-100~100)
  80.         #print("Y:" + str(joy[1]))#joystickのY座標(-100~100)
  81.         inputX = joy[0]/100.00#わかりやすいようにしてみたつもり。joyの0にXが入って、1にYが入っている。
  82.         inputY = joy[1]/100.00
  83.         motorA = inputY + inputX
  84.         motorB = inputY - inputX
  85.         #print(motorA)
  86.         #print(motorB)
  87.         motor2.throttle = motorA
  88.         motor1.throttle = motorB
  89.    
  90.     #センサー用ポーリング
  91.     if time.monotonic() > (now + 5):
  92.         now = time.monotonic()
  93.         dist = sensor.range
  94.         #print(dist)
  95.         if dist < 200:
  96.             motor1.throttle = 0
  97.             motor2.throttle = 0
  98.             time.sleep(1)
  99.             uart0.read(128)
  100.             motor2.throttle = -1
  101.             motor1.throttle = -1
  102.             time.sleep(1)
  103.         uart0.write(b'Range: {}mm'.format(dist))
  104.         uart0.write(b'\n\r')
  105.        
  106.    
  107.    
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement