【问题标题】:Simulating different temperatures on Cooja/Contiki在 Cooja/Contiki 上模拟不同的温度
【发布时间】:2015-01-18 19:32:01
【问题描述】:

我无法在 Cooja 上读取不同的温度读数。我做了以下事情:

  • 创建了一个初始化光和温度传感器的源文件。
  • 使用天空微尘创建了一个模拟。
  • 使用由 8 个节点组成的网络运行模拟。

每个微尘读取相同的温度,这对我想要的毫无用处。在过去的 8 个小时里,我一直在查看线程、文档(包括 Contiki wiki),但没有提出任何建议。

如果我误解了 Cooja/Contiki 的工作方式,我也希望能得到一些帮助,但归根结底,我如何模拟模拟环境中的微尘传感器可以读取的不同温度?

【问题讨论】:

    标签: c contiki


    【解决方案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));
    }
    

    【讨论】:

    • 感谢一百万,发挥了魅力。对于尝试代码的任何人,第一个链接解释了安装,但缺少一个步骤: - 在 SkyMoteType.java 的开头添加光传感器的导入作为额外内容,您能解释一下 MSP 在这种情况下是什么吗?
    【解决方案2】:

    这是 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) {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多