示例#1
0
def plant(T, initD):
    gain = sm.Gain(T)
    delay_x = sm.R(0)

    delay_y = sm.FeedbackAdd(sm.Gain(1), sm.R(initD))

    return sm.Cascade(sm.Cascade(gain, delay_x), delay_y)
示例#2
0
def wallFinderSystem(T, initD, k):
    controller_v = controller(k)
    plant_v = plant(T, initD)
    sensor_v = sensor(initD)
    return sm.Cascade(
        sm.Gain(-1), sm.FeedbackAdd(sm.Cascade(controller_v, plant_v),
                                    sensor_v))
def setup():
    mapper = mapMaker.MapMaker(xMin, xMax, yMin, yMax, gridSquareSize)
    replannerSM = replanner.ReplannerWithDynamicMap(goalPoint)
    robot.behavior = \
       sm.Cascade(sm.Parallel(sm.Cascade(sm.Parallel(mapper, sm.Wire()),
                                         replannerSM),
                              sm.Wire()),
                  move.MoveToDynamicPoint())
示例#4
0
 def __init__(self):
     self.sm1 = BA1
     self.sm1_inst = BA1()
     self.sm2 = BA2
     self.sm2_inst = BA2()
     self.ba1 = PureFunction(self.sm1_inst.getNextValues)
     self.ba2 = PureFunction(self.sm2_inst.getNextValues)
     self.double_bank = sm.Cascade(sm.Parallel2(self.ba1, self.ba2),
                                   Adder())
     self.startState = (0, 0)
def accumulatorDelay(init):
    """
    Args:
        init (int): the output at time 0

    Returns:
        y: a state machine whose output at time n is the sum of its inputs up to 
        and including time n-1
    """
    xDelay = sm.Delay(0)
    y = sm.Cascade(xDelay, accumulator(init))
    return y
示例#6
0
def wallFinderSystem(t, initD, k):
    """
    Returns a state machine whose input is the desired distance from the wall
    and whose output is the actual distance from the wall.

    Args:
        t - the duration of a time step
        initD - the starting distance from the wall
        k - Scalar for velocity of movement
    """
    return sm.FeedbackAdd(sm.Cascade(controller(k), plant(t, initD)),
                          sensor(initD))
def accumulatorDelayScaled(s, init):
    """
    Args:
        s (int): the scale factor
        init (int): the output at time 0

    Returns:
        y: a state machine whose output at time n is the sum of the scaled 
        inputs (each scaled by s) up to and including time n-1
    """
    xScale = sm.Gain(s)
    y = sm.Cascade(xScale, accumulatorDelay(init))
    return y
示例#8
0
def accumulatorDelay(init):
    """
    Returns a state machine such that y[n] = y[n-1] + x[n-1] witgh a starting state of init.
    """
    return sm.Cascade(accumulator(init), sm.R(init))
示例#9
0
        v = sonarDist.getDistanceRight(inp.sonars)
        print 'Dist from robot center to wall on right', v
        return (state, v)

# inp is the distance to the right
class WallFollower(sm.SM):
    startState = None
    def getNextValues(self, state, inp):

################
# Your code here
################

sensorMachine = Sensor()
sensorMachine.name = 'sensor'
mySM = sm.Cascade(sensorMachine, WallFollower())

######################################################################
#
#            Running the robot
#
######################################################################

def setup():
    robot.gfx = gfx.RobotGraphics(drawSlimeTrail=False)
    robot.gfx.addStaticPlotSMProbe(y=('rightDistance', 'sensor',
                                      'output', lambda x:x))
    robot.behavior = mySM
    robot.behavior.start(traceTasks = robot.gfx.tasks())

def step():
示例#10
0
def accumulatorDelayScaled(s, init):
    x_scale = sm.Gain(s)
    return sm.Cascade(x_scale, accumulatorDelay(init))
示例#11
0
def accumulatorDelayScaled(s, init):
    """
    Returns a state machine such that y[n] = y[n-1] + sx[n-1].
    """
    return sm.Cascade(accumulatorDelay(init), sm.Gain(s))
示例#12
0
import lib601.sm as sm

def neg(inp):
    return not(inp)

negate = sm.PureFunction(neg)

alternating = sm.Feedback(sm.Cascade(negate, sm.Delay(True)))

print alternating.transduce([1,2,3,True,False])
示例#13
0
def accumulatorDelay(init):
    x_delay = sm.R(0)
    return sm.Cascade(x_delay, accumulator(init))
示例#14
0
def makeCounter(init, step):
    return sm.Feedback(sm.Cascade(Increment(step), Delay(init)))
示例#15
0
################


# Input is SensorInput instance; output is a delayed front sonar reading
class Sensor(sm.SM):
    def __init__(self, initDist, numDelays):
        self.startState = [initDist] * numDelays

    def getNextValues(self, state, inp):
        print inp.sonars[3]
        output = state[-1]
        state = [inp.sonars[3]] + state[:-1]
        return (state, output)


mySM = sm.Cascade(Sensor(1.5, 1), Controller())
mySM.name = 'brainSM'

######################################################################
###
###          Brain methods
###
######################################################################


def plotSonar(sonarNum):
    robot.gfx.addStaticPlotFunction(
        y=('sonar' + str(sonarNum), lambda: io.SensorInput().sonars[sonarNum]))


def setup():
示例#16
0
reload(ffSkeleton)

from secretMessage import secret

# Set to True for verbose output on every step
verbose = False

# Rotated square points
squarePoints = [util.Point(0.5, 0.5), util.Point(0.0, 1.0),
                util.Point(-0.5, 0.5), util.Point(0.0, 0.0)]
goal_generator = ffSkeleton.FollowFigure(secret)
# goal_generator = sm.Constant(util.Point(1.0, 0.5))
dynamic_move_machine = dynamicMoveToPointSkeleton.DynamicMoveToPoint()

# Put your answer to step 1 here
mySM = sm.Cascade(sm.Parallel(goal_generator, sm.Wire()), dynamic_move_machine)

######################################################################
###
###          Brain methods
###
######################################################################

def setup():
    robot.gfx = gfx.RobotGraphics(drawSlimeTrail = True)
    robot.behavior = mySM

def brainStart():
    robot.behavior.start(traceTasks = robot.gfx.tasks(),
                         verbose = verbose)
示例#17
0
		self.f = f
	def getNextValues(self,state,inp):
		return (state,self.f(inp))
		
# Part 1: Maximize
# Make a state machine that computes the balances of both types of accounts
# and outputs the maximum of the two balances
# The input is a number
# Start by constructing a state machine whose input is a number and whose
# output is a tuple with two balances:
# Then combine this machine with sm.PureFunction


a1 = BA1()
a2 = BA2()
maxAccount = sm.Cascade(sm.Parallel(a1,a2),sm.PureFunction(max))

# maxAccount.transduce([1000,1500,2000,500],verbose = True)

# Part 2: Investment
# I put any deposit or withdrawal whose magnitude is > 3000 in the account1
# and all others in the account of type 2.  On every step both bank accounts
# should continue to earn relevant interest.  The output should be the sum
# of the balances in the two accounts.  Implement this by composing the two
# bank accoutns using sm.Parallel2 and cascading it with two simple machines
# you implement using sm.PureFunction

class Switcher(sm.SM):
	startState = None
	def getNextValues(self,state,inp):
		if abs(inp) > 3000:
示例#18
0
def wallFinderSystem(T, initD, k):
    return sm.FeedbackSubtract(sm.Cascade(controller(k), plant(T, initD)),
                               sensor(initD))
示例#19
0
        if inp != 0:
            newState += inp - 100
        return (newState, newState)


class BA2(sm.SM):
    startState = 0

    def getNextValues(self, state, inp):
        newState = state * 1.01 + inp
        return (newState, newState)


bank_one = BA1()
bank_two = BA2()
combined_bank = sm.Cascade(sm.Parallel(bank_one, bank_two), PureFunction(max))


def is_large_transaction(amount):
    return abs(amount) > 3000


def add(args):
    return args[0] + args[1]


class SwitchMachine(sm.SM):
    startState = None

    def getNextValues(self, state, inp):
        if is_large_transaction(inp):
class Increment(sm.SM):
    startState = 0

    def __init__(self, incr):
        self.incr = incr

    def getNextValues(self, state, inp):
        return (state, inp + self.incr)


# Fill in the behavior tables
# Note: list indexes correspond to times, e.g. list[1] -> list at t=1
# 1.
sm1 = Delay(1)
sm2 = Delay(2)
c = sm.Cascade(sm1, sm2)
c.transduce([3, 5, 7, 9])

# cascade means we take the output of the first sm as the input
# for the second
sm1input = [3, 5, 7, 9]
sm1state = [1, 3, 5, 7, 9]
sm1output = [1, 3, 5, 7]
sm2input = [1, 3, 5, 7]
sm2state = [2, 1, 3, 5, 7]
sm2output = [2, 1, 3, 5]

# 2.
sm1 = Delay(1)
sm2 = Increment(3)
c = sm.Cascade(sm1, sm2)
示例#21
0
def accumulatorDelayScaled(init, s):
    return sm.Cascade(sm.FeedbackAdd(sm.R(init), sm.Gain(1.0)), sm.Gain(s))
        if inp != 0:
            return state * 1.02 + inp - 100
        else:
            return state * 1.02


class BA2(sm.SM):
    startState = 0

    def getNextState(self, state, inp):
        return state * 1.01 + inp


ba1 = BA1()
ba2 = BA2()
maxAccount = sm.Cascade(sm.Parallel(ba1, ba2), sm.PureFunction(max))

print ba1.transduce([0, 100, 10000, -300])
print ba2.transduce([0, 100, 10000, -300])
print maxAccount.transduce([0, 100, 10000, -300])


def switchInput(inp):
    if inp > 3000 or inp < -3000:
        return (inp, 0)
    else:
        return (0, inp)


switchAccount = sm.Cascade(
    sm.Cascade(sm.PureFunction(switchInput), sm.Parallel2(ba1, ba2)),
示例#23
0
def plant(T, initD):
    return sm.Cascade(sm.Gain(-T), sm.FeedbackAdd(sm.R(initD), sm.Wire()))
forwardVelocity = 0.1

# No additional delay
class Sensor(sm.SM):
    def getNextValues(self, state, inp):
        return (state, sonarDist.getDistanceRight(inp.sonars))

# inp is the distance to the right
class WallFollower(sm.SM):
    def getNextValues(self, state, inp):

################
# Your code here
################

mySM = sm.Cascade(Sensor(), WallFollower())

######################################################################
#
#            Running the robot
#
######################################################################

def plotDist():
    func = lambda: sonarDist.getDistanceRight(io.SensorInput().sonars)
    robot.gfx.addStaticPlotFunction(y=('d_o', func))

def setup():
    robot.gfx = gfx.RobotGraphics(drawSlimeTrail=False)
    plotDist()
    robot.behavior = mySM
示例#25
0
class Increment(sm.SM):
    startState = 0
    def __init__(self, incr):
        self.incr = incr

    def getNextValues(self, state, inp):
        # print(type(inp))
        # print type(self.incr)
        if isinstance(inp, type(2)):
            return (state, inp + self.incr)  # need safe add
        else:
            return (state, self.incr)

sm1 = Delay(1)
sm2 = Delay(2)
c = sm.Cascade(sm1, sm2)
print c.transduce([3,5,7,9])

sm1 = Delay(1)
sm2 = Increment(3)
c = sm.Cascade(sm1, sm2)
print c.transduce([3,5,7,9])

'''3-1-2'''
class Cascade(sm.SM):
    def __init__(self, sm1, sm2):
        self.startState = (sm1.startState, sm2.startState)
        self.sm1 = sm1
        self.sm2 = sm2

    def getNextValues(self, state, inp):
示例#26
0
import lib601.sm as sm

# Define negate to be an instance of sm.PureFunction that takes a boolean
# and returns the negation of it
def negFunct(boolean):
	return not boolean

class PureFunction(sm.SM):
	def __init__(self, f):
		self.f = f
	def getNextValues(self,state,inp):
		return (state,self.f(inp))

negate = sm.PureFunction(negFunct)
# negate.transduce([True,False],verbose = True)

# Use sm.Feedback,sm.Cascade, and negate to construct an instance whose output alternates
# between True and False for any input sequence; starting with true.

alternating = sm.Feedback(sm.Cascade(sm.Delay(False),negate))
alternating.transduce([1,True,False,True,False,True,False],verbose = True)

# Not totally sure how I made this work, but hey it does.