Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. from Robot import *
  2. from Path import *
  3. from math import *
  4. import json
  5.  
  6. robot = Robot()
  7.  
  8. # load a path file
  9. p = Path("Path-around-table-and-back.json")
  10. path = p.getPath()
  11.  
  12. newPath = []
  13.  
  14. #skapar en lista med heltal
  15. for i in range(len(path)):
  16.     newPath.append(i)
  17.  
  18. #stor forloop som räknar alla koordinater
  19. for i in newPath[::4]:
  20.    
  21.     increaseSpeed = 0
  22.    
  23.     robotRunning = True
  24.    
  25.     while robotRunning is True:
  26.        
  27.         #koordinats position, lista av dictionaries
  28.         coordinate_x = path[i]['X']
  29.         coordinate_y = path[i]['Y']        
  30.  
  31.         #Räkna ut robotens vinkel
  32.         robotHeading = robot.getHeading()
  33.  
  34.         #robotens position, returnerar två värden
  35.         robotPos = robot.getPosition()
  36.  
  37.         dx = coordinate_x - robotPos["X"]
  38.         dy = coordinate_y - robotPos["Y"]
  39.        
  40.         #Räknar ut vinkeln från roboten till carrot
  41.         angle = atan2(dy, dx)
  42.        
  43.         speed = 0.35
  44.    
  45.         #räknar ut avstånd till carrot
  46.         distanceToCarrot = sqrt(dx**2 + dy**2)
  47.        
  48.         timeToCarrot = distanceToCarrot/speed
  49.        
  50.         #räknar ut anglevi
  51.         robotAngle = (angle) - (robotHeading) / timeToCarrot
  52.        
  53.         #Robotens hastighet + rotationshastighet
  54.         robot.setMotion(speed, robotAngle)  
  55.        
  56.        
  57.         print("Robot heading" + str(robotHeading))
  58.         print("Robotens hastighet" + str(speed))
  59.         print("Distance to Carrot" + str(distanceToCarrot))
  60.        
  61.         time.sleep(0.05)
  62.        
  63.         if increaseSpeed == 5:
  64.             print("Speed speed")
  65.             speed + = 0,01
  66.             increaseSpeed = 0
  67.        
  68.         if distanceToCarrot < 0.4:
  69.             print("Carrot found!")
  70.             speed = 0.35
  71.             robotRunning = False
  72.             time.sleep(0.05)
  73.            
  74.  
  75. print("Path complete")
  76. robot.setMotion(0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement