16
Projects 3&4: PyRobot & Picobot

Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Projects 3&4:PyRobot & Picobot

Page 2: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Project 3: PyRobot

Goal: get from Pt A to Pt B

2d Roomba simulator

Pt B

Pt A

Page 3: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Project 3: PyRobot

Pt B

Pt A IMPORTANT:ROBOT CAN START ANYWHERE!

IMPORTANT:GOAL CAN BE ANYWHERE

Page 4: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Robot control continuously runs three things:

SENSE PLAN

[x,y,thd], bump = self.getData()

if bump[0] == True or bump[1] == True: print 'BUMP!', print ' [Left bump sensor:', bump[0], '] ', print ' [Right bump sensor:', bump[1], '] ' robotTask = STOP

STOP is one of the robot's states. Every 40th of a second, the robot runs through this loop, sets the robot's state and sets the velocities accordingly. Don't sleep!

while True:

Project 3: PyRobot

ACT

if robotTask == STOP: self.setVels(0,0) robotTask = KBD

Page 5: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

BASIC ROBOT COMMANDS:STOP:self.setVels(0,0)

GO FORWARD:self.setVels(FV,0)

GO BACKWARD:self.setVels(-FV,0)

GO CLOCKWISE:self.setVels(0,RV)

GO COUNTERCLOCKWISE:self.setVels(0,-RV)

Project 3: PyRobot

Page 6: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Project 3: PyRobot

To make the robot go forward a set amount useThe max forward velocity: FVExample...

TIME_ONE_CIRCLE_OVER = RADIUS*2 / FV

if state==GO_LEFT_LITTLE:#FIGURE OUT HOW TO TRAVELpause_stop = time.time() + TIME_ONE_CIRCLE_OVERState = NOW_GOING_LEFT_LITTLE

if pause_stop > time.time() and state==NOW_GOING_LEFT_LITTLE:self.setVels(0,0) #STOP!

elif state==NOW_GOING_LEFT_LITTLE:self.setVels(0,FV) #KEEP GOING!

Page 7: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

To rotate the robot use the Max Rotational Velocity: RV

Example...

TIME_ROTATE_90_DEGREES = 90.0 / RV

if state==ROTATE_LEFT_DOWN:#FIGURE OUT HOW LONG TO ROTATEpause_stop = time.time() + TIME_ROTATE_90_DEGREESState = NOW_ROTATING_DOWN

if pause_stop > time.time() and state==NOW_ROTATING_DOWN:self.setVels(0,0) #STOP!

elif state==NOW_ROTATING_DOWN:self.setVels(0,-RV) #KEEP GOING!

Project 3: PyRobot

Page 8: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

One way to traverse the space isGO DOWN UNTIL BUMP SOMETHING,GO LEFT A LITTLEGO UP UNTIL BUMP SOMETHINGGO RIGHT A LITTLE

DO THIS UNTIL HIT CORNER THEN REVERSE....

Project 3: PyRobot

Page 9: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Required

We may test on any map with rectangular objects

Project 3: PyRobot

Page 10: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Project 4: Picobot

Your goal is to….

1) Create a maze with

Go and Stop buttons

2) Read in commands that will guide the robot through the maze

0 xxxx -> N 1 10 Nxxx -> S 2 20 xExx -> W 3

Page 11: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Go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0,0,1,0,1,0,1,1,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,1],[1,0,1,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1],[1,0,1,0,0,1,0,1,0,0,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1],[1,0,1,1,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1],[1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,1,0,0,0,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

 

Your program should read maze description from a file. If you mark blue by 1, and white by 0, then the maze could be represented as a 2D list:

Page 12: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Creating squares….

Project 4: Picobot

#square in mazewin = GraphWin("MyWindow",

400, 400)p1 = Point(0,355)p2 = Point(400,400)square5 = Rectangle(p1,p2)square5.setFill(“blue")square5.setOutline(“blue")square5.draw(win)

#Exit buttonp1 = Point(122,360)p2 = Point(198,390)square1 =

Rectangle(p1,p2)square1.setFill("gray")square1.draw(win)p = square1.getCenter()t = Text(p, "Exit")t.draw(win) 

Page 13: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Project 4: Picobot

Figuring out when user clicks on a “button” K = win.getMouse()

GoStop = 0

if K.getX() > LEFT_X and K.getX() < RIGHT_X and K.getY() > TOP_Y and K.getY() < BOTTOM_Y: GoStop = 1

Page 14: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Project 4: Picobot

Remember to have the trail…

Page 15: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Reading in directions from a text file

Project 4: Picobot

0 xxxx -> N 1 10 Nxxx -> S 2 20 xExx -> W 3

Page 16: Projects 3&4: PyRobot & Picobot. Project 3: PyRobot Goal: get from Pt A to Pt B 2d Roomba simulator Pt B Pt A

Reading in directions from text file

Project 4: Picobot

f = open('map3.txt', 'r')#read in all commandstext = f.read()#split all commandsL = text.split()f.close()#everytime you get to an arrow grab the statefor i in range(len(L)): if L[i] == '->':

if L[i-1] == 'xxxx':

Map3.txt…0 xxxx -> N 1 10 Nxxx -> S 2 20 xExx -> W 3