模拟Cooja / Contiki的不同温度

时间:2015-01-18 19:32:01

标签: c contiki

我在阅读Cooja的不同温度读数时遇到了麻烦。我做了以下事情:

  • 创建了一个初始化光和温度传感器的源文件。
  • 使用Sky mote创建模拟。
  • 使用8个节点的网络进行模拟。

每个微尘都读取相同的温度,这对我想要的东西毫无用处。我花了最近8个小时来查看线程,文档(包括Contiki wiki)并且无法想出任何东西。

如果我误解了Cooja / Contiki的工作方式,我也会对此有所帮助,但是,如何,我如何模拟模拟环境中的微尘传感器可以读取的不同温度?

2 个答案:

答案 0 :(得分:1)

默认情况下,Cooja / MSPsim不会尝试模拟真实的传感器读数。为此,您需要扩展MSPsim的Java代码。

sky上读取温度传感器只是意味着读取ADC端口。 这意味着要模拟该端口上的自定义读数,您需要在该端口上设置自定义ADCInput

Here是一个简单示例,here是一个高级示例(也显示了如何模拟DAC)。

从第一个链接改编的代码:

private SkyMote skyMote;

private final int MY_VALUE = 123;

protected class MyADC implements ADCInput {
   private int fixedValue;

   public MyADC(int value) {
       fixedValue = value;
   }
   public int nextData() {
       return fixedValue;
   }
}

// temperature sensor on sky is on ADC port 10
IOUnit temperatureADC = skyMote.getCPU().getIOUnit("ADC10");
if (temperatureADC instanceof ADC12) {
   ((ADC12) temperatureADC).setADCInput(4, new MyADC(MY_VALUE));
}

答案 1 :(得分:0)

以下是z1 mote的一个示例(经过kfx答案中的链接进行了大量修改):

/**
 * @{
 * \file
 *         Method for simulating environmental temperatures for z1 sensors in COOJA
 *         Three temperature simulation methods are included:
 *              - fixed value
 *              - a slider UI that can be set in the Cooja simulation interface
 *              - a mathematical formula that links temperature to position; this
 *                  has the effect of simulating a cold spot in a very hot environment.
 * \author
 *         Dave Hirsch
 */

package org.contikios.cooja.mspmote;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


import java.util.logging.*;
import org.jdom.Element;

import org.contikios.cooja.ClassDescription;
import org.contikios.cooja.Mote;
import org.contikios.cooja.MoteInterface;
import org.contikios.cooja.MoteInterfaceHandler;
import org.contikios.cooja.interfaces.Position;
import org.contikios.cooja.Simulation;
import org.contikios.cooja.interfaces.Button;
import org.contikios.cooja.mspmote.MspMoteTimeEvent;
import org.contikios.cooja.mspmote.Z1Mote;
import se.sics.mspsim.core.ADC12;
import se.sics.mspsim.core.ADCInput;
import se.sics.mspsim.core.IOUnit;

@ClassDescription("Z1 Temp sensor")
public class Z1TempInt extends MoteInterface {
    private static Logger logger = Logger.getLogger("org.contikios.cooja.mspmote.interfaces.Z1Temp");
    private static int lastVal = -1;
    private IOUnit adc = null;

    private Z1Mote z1Mote = null;

    protected class ADCfixed implements ADCInput {
        private int fixedVal;
        public ADCfixed(int inp) {
            logger.log(Level.FINE, "In ADCfixed. ");
            fixedVal = inp;
        }

        public int nextData(){
            return fixedVal;
        }

    }

    protected class ADCsliderVal implements ADCInput {
        private JSlider myslider; 

        public ADCsliderVal(JSlider slider){
            logger.log(Level.FINE, "In ADCsliderVal(). ");
            myslider = slider;
        }

        public int nextData(){
            if (myslider == null) {
                return 88;
            } else {
                if (myslider.getValue() != lastVal) {
                    lastVal = myslider.getValue();
                    logger.log(Level.FINE, "DMH-new value: " + myslider.getValue() );
                }
                return myslider.getValue();
            }  
        }
    }

    protected class ADCTempPos implements ADCInput {

        public ADCTempPos(){
            logger.log(Level.FINE, "In ADCTempPos(). ");
        }

        public int nextData(){
            if (getZ1Mote() == null) {
                return 87;
            } else {
                Position pos = getZ1Mote().getInterfaces().getPosition();
                double x = pos.getXCoordinate();
                double y = pos.getYCoordinate();
                return (int) Math.floor(Math.sqrt(x*x + y*y));
            }  
        }
    }

    public Z1TempInt(Mote mote) {
        z1Mote = (Z1Mote) mote;

        try {
            Handler fh = new FileHandler("/home/user/contiki/Z1Temp.log");
            logger.addHandler(fh);
            logger.setLevel(Level.FINEST);
        } catch (Exception ex) {
            return;
        }
        logger.log(Level.FINE, "Creating Z1Temp object.");
        adc = z1Mote.getCPU().getIOUnit("ADC12");
        String adcClass = adc.getClass().getSimpleName();
        logger.log(Level.FINE, "ADC is:" + adc);
        if (adc instanceof ADC12) {
            logger.log(Level.FINE, "ADC is an ADC12.");
            ((ADC12) adc).setADCInput(10, new ADCTempPos());
        }

    }

    public Z1Mote getZ1Mote() {
        return z1Mote;
    }

    public JPanel getInterfaceVisualizer() {
        JPanel panel = new JPanel(new GridLayout(2,0));
        final JSlider sADC1 = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 25);
        panel.add(new JLabel("Temperature:"));
        panel.add(sADC1);
        logger.log(Level.FINE, "Setting up panel");
        if (adc != null) {
            logger.log(Level.FINE, "Connecting ADC to slider");
            ((ADC12) adc).setADCInput(10, new ADCsliderVal(sADC1));
        }
        return panel;
    }

    public void releaseInterfaceVisualizer(JPanel panel) {
    }

    public Collection<Element> getConfigXML() {
        return null;
    }

    public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
    }
}