Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. #!/usr/bin/env pybricks-micropython
  2.  
  3. #Team Members:
  4. #Matthew Zheng, 730092528
  5. #Hyrum Draughon, 730089364
  6.  
  7. from pybricks import ev3brick as brick
  8. from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
  9.                                  InfraredSensor, UltrasonicSensor, GyroSensor)
  10. from pybricks.parameters import (Port, Stop, Direction, Button, Color,
  11.                                  SoundFile, ImageFile, Align)
  12. from pybricks.tools import print, wait, StopWatch
  13. from math import pi
  14.  
  15. def findSpeed(distance, time):
  16.     """
  17.    distance: distance in meters to travel
  18.    time: travel time in seconds
  19.    returns speed in degress/second
  20.    """
  21.     wheelCircumference = DIAMETER*pi
  22.     numRotations = (distance*100)/wheelCircumference
  23.     degrees = numRotations*360
  24.     rotSpeed = degrees/time
  25.     return rotSpeed
  26.  
  27. left = Motor(Port.B)
  28. right = Motor(Port.C)
  29.  
  30. touch = TouchSensor(Port.S3)
  31. ultra = UltrasonicSensor(Port.S4)
  32.  
  33. DIAMETER = 5.3975 #centimeters
  34.  
  35. while not 32 in brick.buttons():
  36.     wait(10)
  37. #Move forward 60-120 cm to wall
  38.  
  39. seconds = 10
  40. speed = findSpeed(1.289, seconds)
  41.  
  42. left.run(speed)
  43. right.run(speed)
  44.  
  45. while (not touch.pressed()):
  46.     continue
  47.  
  48. left.stop(Stop.BRAKE)
  49. right.stop(Stop.BRAKE)
  50.  
  51. #Back up and turn right, go forward
  52.  
  53. #back up and turn right
  54.  
  55. #Keep running and stay within 30cm, until past the wall
  56.  
  57. speed2 = findSpeed(3, 30) #Estimate 30 seconds to move at most 3 meters
  58.  
  59. def runOnWall():
  60.     right.run(speed2)
  61.     left.run(speed2)
  62.  
  63.     while ((ultra.distance() < 20) and (not touch.pressed())):
  64.         continue
  65.     if ((ultra.distance() >= 20) and (ultra.distance() <= 30)):
  66.         #stop and turn left
  67.     elif (touch.pressed()):
  68.         #stop and turn right (45 degrees)
  69.    
  70.     if (ultra.distance() > 30):
  71.         #stop and turn left to original orientation (about 90 degrees)  
  72.     else:
  73.         runOnWall()
  74.  
  75. runOnWall()
  76.    
  77. #Turn left and go straight 0.7 meters
  78. seconds
  79. speed3 = findSpeed(0.7, seconds)
  80.  
  81. left.run_time(speed3, seconds*1000, Stop.COAST, False)
  82. right.run_time(speed3, seconds*1000, Stop.COAST, True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement