在 Cooja/Contiki 上模拟不同的温度
Simulating different temperatures on Cooja/Contiki
我在 Cooja 上读取不同的温度读数时遇到问题。我已完成以下操作:
- 创建了初始化光和温度传感器的源文件。
- 使用天空微尘创建了模拟。
- 运行 8 个微尘网络的模拟。
每个微尘读取相同的温度,这对我想要的是无用的。在过去的 8 小时里,我一直在查看线程、文档(包括 Contiki wiki),但没有找到任何东西。
如果我误解了 Cooja/Contiki 的工作方式,我也希望能得到一些帮助,但最重要的是,我如何模拟不同的温度,这些温度可以由模拟环境中的微尘传感器读取?
默认情况下,Cooja/MSPsim 不会尝试模拟真实的传感器读数。为此,您需要扩展 MSPsim 的 Java 代码。
读取 sky
上的温度传感器仅意味着读取 ADC 端口。
这意味着要模拟该端口上的自定义读数,您需要在该端口上设置自定义 ADCInput
。
Here is a simple example, and here 是一个高级示例(还展示了如何模拟 DAC)。
代码改编自第一个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));
}
这是 z1 节点的示例(根据 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) {
}
}
我在 Cooja 上读取不同的温度读数时遇到问题。我已完成以下操作:
- 创建了初始化光和温度传感器的源文件。
- 使用天空微尘创建了模拟。
- 运行 8 个微尘网络的模拟。
每个微尘读取相同的温度,这对我想要的是无用的。在过去的 8 小时里,我一直在查看线程、文档(包括 Contiki wiki),但没有找到任何东西。
如果我误解了 Cooja/Contiki 的工作方式,我也希望能得到一些帮助,但最重要的是,我如何模拟不同的温度,这些温度可以由模拟环境中的微尘传感器读取?
Cooja/MSPsim 不会尝试模拟真实的传感器读数。为此,您需要扩展 MSPsim 的 Java 代码。
读取 sky
上的温度传感器仅意味着读取 ADC 端口。
这意味着要模拟该端口上的自定义读数,您需要在该端口上设置自定义 ADCInput
。
Here is a simple example, and here 是一个高级示例(还展示了如何模拟 DAC)。
代码改编自第一个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));
}
这是 z1 节点的示例(根据 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) {
}
}