I am trying to program a robot to move. The robot moves based on where currently is. There are four places where it can be:
LOCATION1 Motion Plan is like so,
5 6
3 4
1 2
Initial positon is (x1,y1)
This gets coded as (x1,y1)->(x1+dx,y1)->(x1,y1+dy)->(x1+dx,y1+dy) ... and so on
LOCATION2 Motion Plan is like so,
5 3 1
6 4 2
The initial position is (x1,y1)
This gets coded as (x1,y1)->(x1,y1-dy)->(x1-dx,y1)->(x1-dx,y1-dy) ... and so on
LOCATION3 Motion Plan is like so,
6 5
4 3
2 1
Initial positon is (x1,y1)
This gets coded as (x1,y1)->(x1-dx,y1)->(x1,y1+dy)->(x1-dx,y1+dy) ... and so on
LOCATION4 Motion Plan is like so,
6 4 2
5 3 1
The initial position is (x1,y1)
This gets coded as (x1,y1)->(x1,y1+dy)->(x1-dx,y1)->(x1-dx,y1+dy) ... and so on
I am struggling to come up with a good pythonic way to code this. I am thinking on the lines of defining 4 different next move rules and then having a bunch of if statements that choose the correct rules
Has someone done something similar... Is there a better way
I know that this could be made more elegant(and the names for my methods are awful!), but maybe something like this?
>>> import itertools
>>> def alternator(*values):
... return itertools.cycle(values)
...
>>> def increasor(value_1, dvalue_1, steps=2):
... counter = itertools.count(value_1, dvalue_1)
... while True:
... repeater = itertools.repeat(counter.next(), steps)
... for item in repeater:
... yield item
...
>>> def motion_plan(x_plan, y_plan, steps=6):
... while steps > 0:
... yield (x_plan.next(), y_plan.next())
... steps -= 1
...
>>> for pos in motion_plan(alternator('x1', 'x1+dx'), increaser('y1', '+dy'): #Location 1 motion plan
... print pos
...
('x1', 'y1')
('x1+dx', 'y1')
('x1', 'y1+dy')
('x1+dx', 'y1+dy')
('x1', 'y1+dy+dy')
('x1+dx', 'y1+dy+dy')
I was unsure how much flexibility you needed with this, you could reduce the complexity even further if you want to remove some flexibility. Additionally you almost certainly wouldn't be using strings for this, I just thought that it was the easiest way to demonstrate the idea. If you use numbers, then something like this:
>>> count = 0
>>> for pos in motion_plan(increaser(0, -1), alternator(0, 1)): #location 4 motion plan
... print "%d %r" % (count, pos)
... count += 1
1 (0, 0)
2 (0, 1)
3 (-1, 0)
4 (-1, 1)
5 (-2, 0)
6 (-2, 1)
Should correspond pretty clearly to this:
LOCATION4 Motion Plan is like so,
6 4 2
5 3 1
I believe that the motion plans would be as follows:
Location1 = motion_plan(alternator(0, 1), increasor(0, 1))
Location2 = motion_plan(increasor(0, -1), alternator(0, -1))
Location3 = motion_plan(alternator(0, -1), increasor(0, 1))
Location4 = motion_plan(increasor(0, -1), alternator(0, 1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With