Simulating different temperatures on Cooja/Contiki

ⅰ亾dé卋堺 提交于 2019-12-18 09:45:04

问题


I'm having trouble reading different temperature readings on Cooja. I've done the following:

  • Created a source file that initializes a light- and temperature-sensor.
  • Created a simulation using a Sky mote.
  • Ran the simulation with a network of 8 motes.

Each mote reads the same temperature which is useless for what I want. I've spent the last 8 hours looking through threads, documentation (including the Contiki wiki) and haven't been able to come up with anything.

If I'm misunderstanding the way Cooja/Contiki works I'd also appreciate some help with that but, bottom line, how do I simulate different temperatures that can be read by the mote sensors within the simulation environment?


回答1:


Cooja/MSPsim does not attempt to simulate realistic sensor readings by default. To do that, you'll need to extend the Java code of MSPsim.

Reading the temperature sensor on sky simply means reading an ADC port. This means that to simulate custom readings on that port, you need to set up a custom ADCInput on that port.

Here is a simple example, and here is an advanced example (that also shows how to simulate DAC).

Code adapted from the first link:

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));
}



回答2:


Here's an example for the z1 mote (heavily modified from the links in kfx's answer):

/**
 * @{
 * \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) {
    }
}


来源:https://stackoverflow.com/questions/28013883/simulating-different-temperatures-on-cooja-contiki

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!