⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postprocess.py

📁 tinyos最新版
💻 PY
📖 第 1 页 / 共 2 页
字号:
    #                                       "ADC_NOISE_REDUCTION", \    #                                       "POWER_DOWN", \    #                                       "POWER_SAVE", \    #                                       "RESERVED", \    #                                       "RESERVED", \    #                                       "STANDBY", \    #                                       "EXTENDED_STANDBY"}    # The energy model should have keys for each of the form CPU_`state`    global state    state[mote]["cpu"] = newstate[1]def adc_handler(mote, time, newstate):    global state    #FIXME: The ADC has to be on for any ADC event to work-check this    action = newstate[1]    if action == 'SAMPLE':        state[mote]["adc"] = 1    elif action == 'DATA_READY':        state[mote]["adc"] = 0    elif action == 'ON':        state[mote]["adc_on"] = 1    elif action == 'OFF':        state[mote]["adc_on"] = 0    else:        quit(0,"Line %d: Syntax error: adc action %s unknown" % (lineno,action))def radio_state_handler(mote, time, newstate):    """    The radio is one of the more complicated pieces:    The possible values for newstate:    ON  - turn radio on.  As far as I can tell, goes back    to it's previous state    OFF - turn radio off.    TX - go into transmit mode.  The transmit power is the same         as it was before (either the default, or the latest SetRFPower)    RX - go into receive mode    SetRFPower XX  for some hex value of XX-there should be an    energy model entry for RADIO_TX_XX        Thus, the state for the radio is:    {'on':ON/OFF,'tx': TX/RX,'txpower':PowerLevel}    """    global state    oldstate = state[mote]['radio']    op = newstate[1]    if op == "ON":        # Parameters are set to defaults when turning on        oldstate['on'] = 1        oldstate['tx'] = 0 # Defaults to RX mode        oldstate['txpower'] = em['RADIO_DEFAULT_POWER']    elif op == "OFF":         oldstate['on'] = 0    elif op == "SetRFPower":        oldstate['txpower'] = int(newstate[2],16)  # must be a hex number    elif op == "TX":        # The mica(1) stack, doesn't explicitly turn radio on, so        # TX/RX transitions also turn it on.  Should be valid for mica2        # as well, unless it tries to send while the radio is off, which        # probably qualifies as a bug        oldstate['on'] = 1         oldstate['tx'] = 1    elif op == "RX":        oldstate['on'] = 1        oldstate['tx'] = 0    else:        quit(0,"Line %d: Syntax error: radio state %s unknown" % (lineno,op))    def led_state_handler(mote, time, newstate):    """ The state for the LEDs is pretty simple:        They start out off, and here we just keep track of which are on        in a dictionary.  So the state[mote]['led'] looks like        {'RED':onoff, 'GREEN':onoff, 'YELLOW':onoff}    """    global state    msg = newstate[1]    if msg.endswith("_OFF"):        state[mote]['led'][msg[:-4]]=0    else:        assert msg.endswith("_ON")        state[mote]['led'][msg[:-3]]=1def sensor_state_handler(mote, time, newstate):    global state    # If we're doing sensor stuff, there must be a sensor board:    type = newstate[1]    action = newstate[2]    if action == 'ON':        state[mote]['sensor'][type] = 1    elif action == 'OFF':        state[mote]['sensor'][type] = 0    else:        quit(0, "Line %d: Syntax error: sensor state %s unknown"             % (lineno, action))def eeprom_state_handler(mote, time, newstate):    global state    type = newstate[1]    action = newstate[2]    if type == 'READ':        if action == 'START':            state[mote]['eeprom']['read'] = 1        elif action == 'STOP':            state[mote]['eeprom']['read'] = 0        else:            quit(0, "Line %d: Syntax error: EEPROM READ action %s unknown"             % (lineno, action))    elif type == 'WRITE':        if action == 'START':            state[mote]['eeprom']['write'] = 1        elif action == 'STOP':            state[mote]['eeprom']['write'] = 0        else:            quit(0, "Line %d: Syntax error: EEPROM WRITE action %s unknown"             % (lineno, action))    else:        quit(0, "Line %d: Syntax error: EEPROM TYPE %s unknown"             % (lineno, type))# A table of event type to the appropriate handlerevent_handler = {'CPU_CYCLES'  :    cpu_cycle_handler,                 'CPU_STATE'   :    cpu_state_handler,                 'ADC'  :          adc_handler,                 'RADIO_STATE' :  radio_state_handler,                 'LED_STATE'   :    led_state_handler,                 'SENSOR_STATE': sensor_state_handler,                 'EEPROM'      : eeprom_state_handler}def time_diff(t_from, t_to):    """Returns the difference, in seconds from 't_from' to 't_to', where both    are expressed in cycles.  Uses the CPU_FREQ energy model parameter"""    return (float(t_to) - float(t_from))/em['CPU_FREQ']# Updates every total for every timestep.  This is inefficient,# because if the radio is on for 100 events, there's no need to do 100# small adds But it's simpler this way.  Can fix it (by making# prev_time parametrized by total type) if it's a problemdef update_totals(time):    global total    for m in range(maxseen+1):        for t in totals:            td = time_diff(prev_time[m], time)            total[m][t] += td * prev_current[m][t] * voltagedef update_currents(time):    global prev_time, prev_current    for m in range(maxseen+1):        prev_time[m]=time        for t in totals:            prev_current[m][t] = current_fn_map[t](m)def dump_currents(mote,time):    global data_file, debug    m=mote    if not data_file[m]:        # Open the file        data_file[m] = open(basename + str(m)+".dat", "w")        # Write the header        data_file[m].write("#%11s" % "time");        for x in ['total'] + totals:            data_file[m].write("%12s" % x)        data_file[m].write("\n")    if debug: print prev_current[m]['total'], get_current(m)    if prev_current[m]['total'] != get_current(m):        # To make a square wave, print the previous currents up to "just        # before now", then print the new currents        tm = float(time) / em['CPU_FREQ'] - 0.000001        data_file[m].write("%12f" % tm)        for t in ['total'] + totals:            c = float(prev_current[m][t])            data_file[m].write("%12f" % c)        data_file[m].write("\n");                tm = float(time)/em['CPU_FREQ']        c = get_current(m)        prev_current[m]['total'] = c        data_file[m].write("%12f%12f" % (tm,c))        for t in totals:            c = current_fn_map[t](m)            data_file[m].write("%12f" % c);        data_file[m].write("\n");dbg_unknown_event_types = {}# Takes a line, parses it, and performs the appropriate changes to the# mote state and totals.# The line format we expect consists of whitespace separated fields:# DATA can consist of more than 1 field, but the rest must not# junk POWER: Mote # STATE_TYPE {DATA...} at TIME(in cycles)def handle_event(l):    global maxseen, detail    if debug: print lineno, l    event = l.split()    # Check if this is a power event    if event[1] != "POWER:":        return        mote = int(event[3])    if(mote > maxseen): maxseen = mote    time = event[-1]    #    print "handling event: '%s'" % l    #    print event    if event[4] in event_handler:        # Update the totals up to just before this event        update_totals(time)        # Update the state due to this event        event_handler[event[4]](mote,time,event[4:-2])        if detail:            # At this point, the state is updated, but still have the old            # current values            dump_currents(mote,time)        # Update the prev_current values        update_currents(time)            else:        global dbg_unknown_event_types        if not event[4] in dbg_unknown_event_types:            print "Don't know how to handle "+event[4]+" events"            dbg_unknown_event_types[event[4]] = 1########################  "Main" code ###################def print_summary():    global total    global maxseen    print "maxseen %d" % maxseen    for mote in range(maxseen+1):        sum = 0        if not prettyprint:            s = str(mote)+"   "        for t in totals:            if prettyprint:                print "Mote %d, %s total: %f" % (mote, t, total[mote][t])            else:                s += "%.4f" % total[mote][t]                s += "   "            sum += total[mote][t]        cpu_active_e = state[mote]['cpu_cycles'] * voltage * em['CPU_ACTIVE']/em['CPU_FREQ']        if prettyprint:             print "Mote %d, cpu_cycle total: %f" % (mote, cpu_active_e)        else:            s += "%.4f" % cpu_active_e            s += "   "        sum += cpu_active_e        if prettyprint:            print "Mote %d, Total energy: %f\n" %(mote, sum)        else:            s += "%.4f" % sum            print sif __name__=='__main__':    parse_args()    initstate()    lineno = 1    l=trace.readline()    while l:        handle_event(l)        lineno += 1        l = trace.readline()    if summary:        print_summary()    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -