C语言 模拟Cooja/Contiki上的不同温度



我在Cooja上读取不同的温度读数时遇到问题。我做了以下工作:

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

每个微尘读取相同的温度,这对我想要的东西毫无用处。在过去的 8 个小时里,我浏览了线程、文档(包括 Contiki wiki),但什么也没能想出来。

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

默认情况下,

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

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

下面是一个简单的示例,下面是一个高级示例(还演示如何模拟 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));
}

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

最新更新