标题:新手求助,关于代码里的yield的问题
只看楼主
icebreaker_l
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-7-2
结帖率:0
已结贴  问题点数:0 回复次数:3 
新手求助,关于代码里的yield的问题
程序总体结构如下,想请教下关于 def simulate(self):函数里yield i 的意义,注释里说是没个i值程序跑一次,我想问下,每次i取一个值的时候,之前所有的函数都重新调用吗? 我想在每次i取值的时候,改变def sensorSetup(self)函数里 actSensor.attachTo('egoCar4')和 actSensor.attachTo('egoCar6')的命令可以吗? 菜鸟一只,各位高手帮帮忙阿? Ps,这个编译不了的,各位看看结构就行,谢谢


import simplesim.configuration

from simplesim.Main.SimSetup import *

#we derive our simulation class from SimSetup.
class Pro_test(SimSetup):
    """
    Basic simulation example. Creates a road and a vehicle body moving on it.
    """
    def __init__(self):
        #This is a must:
        SimSetup.__init__(self)
#        self.aweterrainmatfile = 'data\\AWE\\TestData\\asphalt_scs.mtl'
        self.sensorActive = True #True
        
        self.terrainfile =       'data\\AEM\\autobahn\\concrete_flatland.obj' # Strassenobjekt
        self.aweterrainfile =    'data\\AEM\\autobahn\\concrete_flatland.obj'
        self.aweterrainmatfile = 'data\\AEM\\autobahn\\concrete_flatland.mtl' # Material fuer die Strasse
        
        self.egomesh = 'data\\AEM\\autobahn\\7070_MercedesBenz_Sprinter_ASCII.obj' # Darstellung von Auto
        self.awemesh = 'data\\AEM\\autobahn\\Van_rotated_scaled_20080806.obj'    # Autoobjekt fuer Raytracer
        self.matagentfile = 'data\\AEM\\autobahn\\body.mtl'
        

    def makeScene(self):
        """
        Creates a RoadNetwork object.
        See Example_Basic.makeScene(), we add guard rails in this example
        """
        #Create a Kruemmungsband object with one clothoid segment.
        k = Kruemmungsband()
        #set start position orientation and curvature, use helper function deg2rad()
        k.setStartValues( (0.0,0.0), 0.0, 0.0, 0.0)
        k.addSegmentDerivCurvature(270.0, 0.0)
        #Save the Kruemmungsband as object member variable
        #so it can be used in other functions.
        self.kruemmungsband = k

        #Create a road object along the Kruemmungsband
        oroad = OrnamentedRoad(k)

        #Define the stripes for the road and add them to the OrnamentedRoad object
        oroad.addStripe(RoadStripe(50.0), 'road')
        oroad.addStripe(MittelStreifen(2.0), 'rechter mittelstreifen')
        oroad.addStripe(MiddleDashStripe(-2.0), 'linker mittelstreifen')
        oroad.addStripe(BorderStripe(6.0), 'rechter standstreifen')
        oroad.addStripe(StandStreifen(-6.0), 'linker standstreifen')
        oroad.addStripe(GruenStreifen(50.0), 'gelaende')

        #define some helper variables:
        postDistance = 8.0 #Abstand zwischen den Pfosten
        lateralDistance = 8.0 #Abstand Fahrzeugmitte - Pfostenlinie
        startOffset = 3.0 #Wegpunkt des ersten Pfostens
        #create guard rail objects for left and right guardrails
        leftRail = RailOrnament(-lateralDistance, 'left', postDistance, startOffset)
        middelRail = RailOrnament(+lateralDistance, 'middel', postDistance, startOffset)
        rightRail = RailOrnament(+24, 'right', postDistance, startOffset)
        #configure guard rail objects:
        [i.setAweMatfile('data\\AWE\\TestData\\Post_Mcs.mtl') for i in [leftRail,middelRail,rightRail]]
        [i.setShowPlanes(True) for i in [leftRail,middelRail,rightRail]]  #Leitplanken abschalten!

        #place guardrail along road object
        oroad.addOrnament(leftRail, 'leftRail')
        oroad.addOrnament(middelRail, 'middelRail')
        oroad.addOrnament(rightRail, 'rightRail')   
        return (k,RoadNetwork([oroad]))

   
    def setup_ManyEgos(self):
        """
        Setup the ego object.
        """
        for i in range(1,6):
           #using static agents (un-movable)
           a = createAgent('Agents.StaticAgent','egoCar%d' % i)
   #        a.egoCarSetup = CarSetups.Mercedes_Sprinter(a)
           a.setMesh(self.egomesh)
           a.addWorld('AWEInterface',self.awemesh)
           a.setAweMaterial(self.matagentfile)
           a.addWorld('LogWorld','')
           a.setTrajectoryObject(NewPosition(random.randint(1, 30)+i*50,0,0,180,0,0))         
        
    def setup_Radar(self):
        #create a new radar simulation (using datetime as identifier)
        self.aweComponent().setParam_useSimulation('date')
        #wait for simulation results
        self.aweComponent().setParam_openLoopSimulation(False)
        #show interaction points of radar paths and environment
        self.aweComponent().setParam_showInteractionPoints(False)
        #set raytracer options (trace depth etc.)
        self.aweComponent().setParam_options(AweDefaultOptions())   
        
        
    def sensorSetup(self):
        if self.sensorActive:
            actSensor = self.createSensor('Agents.Sensors.AweSensor','awesens_tx')
            actSensor.setTrajectoryObject(NewPosition(-2.0,0.0,2.8))
            #actSensor.setXYZ(-2.0,0.0,2.8)
            actSensor.attachTo('egoCar4')
            actSensor.SetSampleTime(1)
            actSensor.deactivateReceiver()
            actSensor = self.createSensor('Agents.Sensors.AweSensor','awesens_rx')
            actSensor.setTrajectoryObject(NewPosition(-2.0,0.0,2.8))
            ##actSensor.setXYZ(-2.0,0.0,2.8)
            actSensor.attachTo('egoCar6')
            #actSensor.setTrajectoryObject(NewPosition(-489 , 43 , 1.5, 90,0.0,0.0))
            actSensor.SetSampleTime(1)
            actSensor.deactivateTransmitter()
            self.getApplication().setScreenShotMode(False)

            self.setup_Radar()

    def setup(self):
        """
        Setup the simulation. Add and configure agents, set simulation parameters, ...
        """
#        self.setup_Ego()
        self.setup_ManyEgos()
        self.setup_Terrain()
        self.setup_Camera()
#        self.sensorSetup()

    def simulate(self):
        """
        Simulation loop, defines simulation times in milliseconds.
        """
        #simulate 0 to 12 seconds in 100 ms steps
        for i in range(0,12000,20):
            yield i #calculate time step i
            print i #print something to stdout

#import all simplesim packages
from importAll import *

#the "main" entry point:
if __name__ == "__main__":
    doSim(Pro_test())

搜索更多相关主题的帖子: yield 代码 
2010-07-02 06:55
yangfanconan
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:10
帖 子:397
专家分:541
注 册:2009-9-22
得分:20 
功能将控制权转移给其它图形对象,包括非PowerBuilder对象。该函数检测消息队列,如果有消息,就把消息取出。利用该函数可以在执行耗时较长的 操作时把控制权转让给其它应用。
Yield()   
返回值Boolean。如果在消息队列中提取到了消息,那么函数返回TRUE,否则返回 FALSE。用法正常情况下,PowerBuilder应用程序在执行一段代码(比如函数或事件处理程序)的过程中不响应用户的操作。对耗时短暂的代码段 来说,这种处理方式没有什么不妥的地方,但是,如果某个代码段的执行耗时较长,应用程序又希望为用户提供更多的控制权,那么需要在这段代码中插入 Yield()函数,让用户能够进行其它操作,特别在循环执行的代码中更应该如此。应用程序执行Yield()函数后,如果发现消息队列中存在消息,它将 允许对象处理这些消息,处理之后,继续Yield()函数后面代码的执行。因此,代码中插入Yield()函数将降低应用程序的运行效率。
2010-07-02 09:13
yangfanconan
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:10
帖 子:397
专家分:541
注 册:2009-9-22
得分:0 
因该是可以的。话说你的代码我没看懂。

仅供参考
2010-07-02 09:25
icebreaker_l
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-7-2
得分:0 
回复 3楼 yangfanconan
多谢版主,不过你的解释我看得晕晕的阿,是这样的,这个知识整个程序的一部分,看起来可能是比较难懂。我想要做的是在def sensorSetup(self):函数中做些变动,actSensor.attachTo('egoCar4'),actSensor.attachTo('egoCar6')这两个函数是将传感器加到car4和car6上,在每次调用yield后改变car的数字,我的想法是将函数中加入变量def sensorSetup(self,m,n),m,n分别代表车辆的标示,然后在def simulate(self):里每次执行yield前调用sensorSetup函数,可是问题是,我这样做了,程序直接忽略了sensorSetup,不起作用。版主大人再指点下吧,谢了先
2010-07-02 12:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-311897-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.049975 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved