Advertisement
igendel

DFRobot Macqueen MicroPython Basic Setup/Functions

Mar 30th, 2020
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. # Initialization and Commands for DFRobot Macqueen
  2. # Collected and inferred by Ido Gendel, 2020
  3. # MU Editor (tested on version 1.0.2)
  4. # Share and enjoy!
  5.  
  6. from microbit import *
  7. from utime import *
  8. from neopixel import *
  9.  
  10. # This is critical for IR reception!
  11. pin16.set_pull(pin16.NO_PULL)
  12. # Required for communication with motor chip
  13. i2c.init()
  14. # Pixel 0=Front/Left, 1=B/L, 2=B/R, 3=F/R
  15. # Usage: pixels[n] = (R, G, B) / pixels.clear(), pixels.show()
  16. pixels = NeoPixel(pin15, 4)
  17.  
  18. # Motor: 0 = left, 2 = right
  19. # Direction: 0 = Forward, 1 = Backward
  20. # Speed: 0 stop to 255 full
  21. def sendMotorCmd(motor, direction, speed):
  22.     i2c.write(0x10, bytes([motor, direction, speed]))
  23.  
  24.  
  25. # Servo number is 1 or 2 (as marked on robot)
  26. # Angle 0 - 180 inclusive
  27. def sendServoCmd(servo, angle):
  28.     addr = 0x13 + servo
  29.     i2c.write(0x10, bytes([addr, angle]))
  30.  
  31.  
  32. def frontLights(right, left):
  33.     pin12.write_digital(right)
  34.     pin8.write_digital(left)
  35.  
  36.  
  37. def horn(frequency):
  38.     if frequency == 0:
  39.         pin0.write_digital(0)
  40.         return
  41.     else:
  42.         pin0.set_analog_period_microseconds(1000000 // frequency)
  43.         pin0.write_analog(512)
  44.  
  45.  
  46. # Returns (right, left) reading
  47. def getLineSensorsReading():
  48.     return (pin14.read_digital(), pin13.read_digital())
  49.  
  50.  
  51. def getRange():
  52.     # Trigger
  53.     pin1.write_digital(1)
  54.     sleep_us(20)
  55.     pin1.write_digital(0)
  56.     # Wait for Echo start
  57.     while pin2.read_digital() == 0:
  58.         pass
  59.     # Measure Echo
  60.     t0 = ticks_us()
  61.     while pin2.read_digital() == 1:
  62.         pass
  63.     # Result is returned in cm
  64.     return (ticks_us() - t0) / 58.77
  65.  
  66.  
  67. def getRawIRReading():
  68.     return pin16.read_digital()
  69.  
  70. # Demo code from here on
  71.  
  72. display.show(Image.HAPPY)
  73. pixels[0] = (0, 30, 0)
  74. pixels[1] = (50, 0, 0)
  75. pixels[2] = (50, 0, 0)
  76. pixels[3] = (0, 30, 0)
  77. pixels.show()
  78.  
  79. while True:
  80.     if 0 == getRawIRReading():
  81.         frontLights(1, 1)
  82.         sleep(0.5)
  83.     else:
  84.         frontLights(0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement