From 05335740fc73904880b3bcf82aea058e2f1b3446 Mon Sep 17 00:00:00 2001 From: pipeng Date: Thu, 15 Feb 2007 10:23:30 +0000 Subject: [PATCH] Add new app to test xmts310 sensor driver. --- apps/tests/mica2/mts300/Makefile | 2 +- apps/tests/mica2/mts300/TestMts300C.nc | 141 ++++--- apps/tests/mica2/mts300/TestMts300P.nc | 509 +++++++++++++++++++++---- apps/tests/mica2/mts300/XMTS300.h | 22 ++ 4 files changed, 539 insertions(+), 135 deletions(-) create mode 100644 apps/tests/mica2/mts300/XMTS300.h diff --git a/apps/tests/mica2/mts300/Makefile b/apps/tests/mica2/mts300/Makefile index c96ba58d..3a7dc907 100644 --- a/apps/tests/mica2/mts300/Makefile +++ b/apps/tests/mica2/mts300/Makefile @@ -1,4 +1,4 @@ COMPONENT=TestMts300C SENSORBOARD=mts300 -#CFLAGS += -I%T/sensorboards/mts300 include $(MAKERULES) + diff --git a/apps/tests/mica2/mts300/TestMts300C.nc b/apps/tests/mica2/mts300/TestMts300C.nc index 42c78527..c08f06f9 100644 --- a/apps/tests/mica2/mts300/TestMts300C.nc +++ b/apps/tests/mica2/mts300/TestMts300C.nc @@ -1,55 +1,86 @@ -/** - * Copyright (c) 2004-2005 Crossbow Technology, Inc. - * All rights reserved. - * - * Permission to use, copy, modify, and distribute this software and its - * documentation for any purpose, without fee, and without written - * agreement is hereby granted, provided that the above copyright - * notice, the (updated) modification history and the author appear in - * all copies of this source code. - * - * Permission is also granted to distribute this software under the - * standard BSD license as contained in the TinyOS distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, - * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - * - * @author Martin Turon - * - * $Id$ - */ - -/** - * This application tests the mts300 sensorboard. - * Specifically, this handles the thermistor and light sensors. - * - * @author Martin Turon - * @date October 19, 2005 - */ -configuration TestMts300C { -} -implementation -{ - components MainC, TestMts300P, LedsC, new OskiTimerMilliC(), - SensorMts300C; - - - MainC.SoftwareInit -> SensorMts300C; - - TestMts300P -> MainC.Boot; - TestMts300P.Leds -> LedsC; - TestMts300P.AppTimer -> OskiTimerMilliC; - - TestMts300P.SensorControl -> SensorMts300C; - TestMts300P.Temp -> SensorMts300C.Temp; - TestMts300P.Light -> SensorMts300C.Light; -} - +// $Id$ + +/* + * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. + * downloading, copying, installing or using the software you agree to + * this license. If you do not agree to this license, do not download, + * install, copy or use the software. + * + * Copyright (c) 2004-2006 Crossbow Technology, Inc. + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without written + * agreement is hereby granted, provided that the above copyright + * notice, the (updated) modification history and the author appear in + * all copies of this source code. + * + * Permission is also granted to distribute this software under the + * standard BSD license as contained in the TinyOS distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * TinyOS 1.x to TinyOS 2.x translation layer. + * + * @author Alif + */ + +#include "XMTS300.h" +#include "mts300.h" + +configuration TestMts300C +{ +} +implementation +{ + components MainC, TestMts300P, LedsC, NoLedsC; + components new TimerMilliC() as MTS300Timer; + + components ActiveMessageC as Radio; + components SerialActiveMessageC as Serial; + +// sensorboard devices + components new SensorMts300C(); + components SounderC; + + TestMts300P -> MainC.Boot; + + TestMts300P.MTS300Timer -> MTS300Timer; + TestMts300P.Leds -> NoLedsC; + + // communication + TestMts300P.RadioControl -> Radio; + TestMts300P.RadioSend -> Radio.AMSend[AM_MTS300MSG]; + TestMts300P.RadioPacket -> Radio; + + TestMts300P.UartControl -> Serial; + TestMts300P.UartSend -> Serial.AMSend[AM_MTS300MSG]; + TestMts300P.UartPacket -> Serial; + + // sensor components + MainC.SoftwareInit -> SensorMts300C; + TestMts300P.MTS300Control -> SensorMts300C.StdControl; + TestMts300P.Vref -> SensorMts300C.Vref; + TestMts300P.Light -> SensorMts300C.Light; + TestMts300P.Temp -> SensorMts300C.Temp; + TestMts300P.Microphone -> SensorMts300C.Microphone; + TestMts300P.AccelX -> SensorMts300C.AccelX; + TestMts300P.AccelY -> SensorMts300C.AccelY; + TestMts300P.MagX -> SensorMts300C.MagX; + TestMts300P.MagY -> SensorMts300C.MagY; + + MainC.SoftwareInit -> SounderC; + TestMts300P.Sounder -> SounderC; +} diff --git a/apps/tests/mica2/mts300/TestMts300P.nc b/apps/tests/mica2/mts300/TestMts300P.nc index ac3ece11..2f46d90e 100644 --- a/apps/tests/mica2/mts300/TestMts300P.nc +++ b/apps/tests/mica2/mts300/TestMts300P.nc @@ -1,79 +1,430 @@ -/** - * Copyright (c) 2004-2005 Crossbow Technology, Inc. - * All rights reserved. - * - * Permission to use, copy, modify, and distribute this software and its - * documentation for any purpose, without fee, and without written - * agreement is hereby granted, provided that the above copyright - * notice, the (updated) modification history and the author appear in - * all copies of this source code. - * - * Permission is also granted to distribute this software under the - * standard BSD license as contained in the TinyOS distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, - * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - * - * @author Martin Turon - * - * $Id$ - */ - -#include "Timer.h" - -/** - * This application tests the mts300 sensorboard. - * Specifically, this handles the thermistor and light sensors. - * - * @author Martin Turon - * @date October 19, 2005 - */ -module TestMts300P -{ - uses { - interface Boot; - interface Leds; - interface Timer as AppTimer; - - interface StdControl as SensorControl; - interface AcquireData as Temp; - interface AcquireData as Light; - } -} -implementation -{ - event void Boot.booted() { - call Leds.led0On(); - call Leds.led1On(); // power led - call SensorControl.start(); - } - - event void AppTimer.fired() { - call Leds.led0Toggle(); // heartbeat indicator - call Light.getData(); - call Temp.getData(); - } - - event void Light.dataReady(uint16_t data) { - call Leds.led1Toggle(); - } - - event void Temp.dataReady(uint16_t data) { - call Leds.led2Toggle(); - } - - event void Light.error(uint16_t info) { - } - - event void Temp.error(uint16_t info) { - } -} - +// $Id$ + +/* + * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. + * downloading, copying, installing or using the software you agree to + * this license. If you do not agree to this license, do not download, + * install, copy or use the software. + * + * Copyright (c) 2004-2006 Crossbow Technology, Inc. + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without written + * agreement is hereby granted, provided that the above copyright + * notice, the (updated) modification history and the author appear in + * all copies of this source code. + * + * Permission is also granted to distribute this software under the + * standard BSD license as contained in the TinyOS distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * TinyOS 1.x to TinyOS 2.x translation layer. + * + * @author Alif + */ + +#include "Timer.h" +#include "XMTS300.h" +#include "mts300.h" + +module TestMts300P +{ + uses + { + interface Leds; + interface Boot; + interface Timer as MTS300Timer; + // communication + interface SplitControl as RadioControl; + interface Packet as RadioPacket; + interface AMSend as RadioSend; + + interface SplitControl as UartControl; + interface Packet as UartPacket; + interface AMSend as UartSend; + // sensor components + interface StdControl as MTS300Control; + interface StdControl as Sounder; + interface Read as Vref; //!< voltage + interface Read as Light; + interface Read as Temp; + interface Read as Microphone; //!< Mic sensor + interface Read as AccelX; //!< Accelerometer sensor + interface Read as AccelY; //!< Accelerometer sensor + interface Read as MagX; //!< magnetometer sensor + interface Read as MagY; //!< magnetometer sensor + + } +} +implementation +{ + + enum + { + STATE_IDLE = 0, + STATE_VREF_START, + STATE_VREF_READY, //!< smaple complete + STATE_LIGHT_START, + STATE_LIGHT_READY, //!< smaple complete + STATE_TEMP_START, + STATE_TEMP_READY, //!< smaple complete + STATE_MIC_START, + STATE_MIC_READY, //!< smaple complete + STATE_ACCELX_START, + STATE_ACCELX_READY, //!< smaple complete + STATE_ACCELY_START, + STATE_ACCELy_READY, //!< smaple complete + STATE_MAGX_START, + STATE_MAGX_READY, //!< smaple complete + STATE_MAGY_START, + STATE_MAGY_READY, //!< smaple complete + }; + + bool sending_packet; + bool packet_ready; + uint8_t state; + uint16_t counter = 0; + message_t packet; + Mts300Msg* pMsg; + + // Zero out the accelerometer, chrl@20070213 + norace uint16_t accel_ave_x, accel_ave_y; + norace uint8_t accel_ave_points; + +////////////////////////////////////////////////////////////////////////////// +// +// basic control routines +// +////////////////////////////////////////////////////////////////////////////// + event void Boot.booted() + { + sending_packet = FALSE; + packet_ready = FALSE; + state = STATE_IDLE; + pMsg = (Mts300Msg*)call RadioPacket.getPayload(&packet, NULL); + + // Zero out the accelerometer, chrl@20070213 + accel_ave_x = 0; + accel_ave_y = 0; + accel_ave_points = ACCEL_AVERAGE_POINTS; + + call RadioControl.start(); + call UartControl.start(); + } + + event void RadioControl.startDone(error_t err) + { + if (err != SUCCESS) + { + call RadioControl.start(); + } + } + + event void RadioControl.stopDone(error_t err) + { + // do nothing + } + + event void UartControl.startDone(error_t err) + { + if (err == SUCCESS) + { + call MTS300Control.start(); + call Sounder.start(); + call MTS300Timer.startPeriodic( 300 ); + } + else + { + call UartControl.start(); + } + } + + event void UartControl.stopDone(error_t err) + { + // do nothing + } + +////////////////////////////////////////////////////////////////////////////// +// +// timer control routines +// +////////////////////////////////////////////////////////////////////////////// + event void MTS300Timer.fired() + { + uint8_t l_state; + atomic l_state = state; + + // Zero out the accelerometer, chrl@20070213 + if (accel_ave_points >0) + { + if (accel_ave_points == 1) + { + call MTS300Timer.stop(); + call MTS300Timer.startPeriodic(1000); + } + atomic state = STATE_ACCELX_START; + call AccelX.read(); + return ; + } + + call Leds.led1Toggle(); + counter++; + + if(counter==1) + { + call Sounder.stop(); + } + + if (sending_packet) return ; + + if (l_state == STATE_IDLE) + { + atomic state = STATE_VREF_START; + call Vref.read(); + return ; + } + + if (packet_ready) + { + atomic packet_ready = FALSE; + // check length of the allocated buffer to see if it is enough for our packet + if (call RadioPacket.maxPayloadLength() < sizeof(Mts300Msg)) + { + return ; + } + // OK, the buffer is large enough + //pMsg->vref = counter; + if (call UartSend.send(AM_BROADCAST_ADDR, &packet, sizeof(Mts300Msg)) == SUCCESS) + { + sending_packet = TRUE; + call Leds.led2On(); + } + } + } + + /** + * reference voltage data read + * + */ + event void Vref.readDone(error_t result, uint16_t data) + { + if (result == SUCCESS) + { + pMsg->vref = data; + } + else + { + pMsg->vref = 0; + } +// atomic packet_ready = TRUE; + atomic state = STATE_LIGHT_START; + call Light.read(); + } + + /** + * Light data read + * + */ + event void Light.readDone(error_t result, uint16_t data) + { + if (result == SUCCESS) + { + pMsg->light = data; + } + else + { + pMsg->light = 0; + } + atomic state = STATE_TEMP_START; + call Temp.read(); + } + + + /** + * Temperature data read + * + */ + event void Temp.readDone(error_t result, uint16_t data) + { + if (result == SUCCESS) + { + pMsg->thermistor = data; + } + else + { + pMsg->thermistor = 0; + } + atomic state = STATE_MIC_START; + call Microphone.read(); + } + + /** + * Microphone data read + * + */ + event void Microphone.readDone(error_t result, uint16_t data) + { + if (result == SUCCESS) + { + pMsg->mic = data; + } + else + { + pMsg->mic = 0; + } +// atomic packet_ready = TRUE; + atomic state = STATE_ACCELX_START; + call AccelX.read(); + } + + /** + * AccelX data read + * + */ + event void AccelX.readDone(error_t result, uint16_t data) + { + // Zero out the accelerometer, chrl@20061207 + if (accel_ave_points>0) + { + accel_ave_x = accel_ave_x + data; + call AccelY.read(); + return ; + } + + if (result == SUCCESS) + { + pMsg->accelX = data - accel_ave_x; + } + else + { + pMsg->accelX = 0; + } + atomic state = STATE_ACCELY_START; + call AccelY.read(); + } + + /** + * AccelY data read + * + */ + event void AccelY.readDone(error_t result, uint16_t data) + { + // Zero out the accelerometer, chrl@20061207 + if (accel_ave_points>0) + { + accel_ave_y = accel_ave_y + data; + accel_ave_points--; + if(accel_ave_points == 0) + { + accel_ave_x = accel_ave_x / ACCEL_AVERAGE_POINTS - 450; + accel_ave_y = accel_ave_y / ACCEL_AVERAGE_POINTS - 450; + } + atomic state = STATE_IDLE; + return ; + } + + if (result == SUCCESS) + { + pMsg->accelY = data - accel_ave_y; + } + else + { + pMsg->accelY = 0; + } +// atomic packet_ready = TRUE; + atomic state = STATE_MAGX_START; + call MagX.read(); + } + + /** + * MagX data read + * + */ + event void MagX.readDone(error_t result, uint16_t data) + { + if (result == SUCCESS) + { + pMsg->magX = data; + } + else + { + pMsg->magX = 0; + } + atomic state = STATE_MAGY_START; + call MagY.read(); + } + + /** + * MagY data read + * + */ + event void MagY.readDone(error_t result, uint16_t data) + { + if (result == SUCCESS) + { + pMsg->magY = data; + } + else + { + pMsg->magY = 0; + } + atomic packet_ready = TRUE; + } + + /** + * Data packet sent to RADIO + * + */ + event void RadioSend.sendDone(message_t* bufPtr, error_t error) + { + if (&packet == bufPtr) + { + call Leds.led2Off(); + } + else + { + call Leds.led0On(); + } + sending_packet = FALSE; + atomic state = STATE_IDLE; + } + + /** + * Data packet sent to UART + * + */ + event void UartSend.sendDone(message_t* bufPtr, error_t error) + { + if (&packet == bufPtr) + { + if (call RadioSend.send(AM_BROADCAST_ADDR, &packet, sizeof(Mts300Msg)) != SUCCESS) + { + call Leds.led0On(); + sending_packet = FALSE; + atomic state = STATE_IDLE; + } + } + else + { + call Leds.led0On(); + sending_packet = FALSE; + atomic state = STATE_IDLE; + } + } + +// end of the implementation +} diff --git a/apps/tests/mica2/mts300/XMTS300.h b/apps/tests/mica2/mts300/XMTS300.h new file mode 100644 index 00000000..a4259dd1 --- /dev/null +++ b/apps/tests/mica2/mts300/XMTS300.h @@ -0,0 +1,22 @@ +#ifndef MTS_300_H +#define MTS_300_H + +// data pacet struct +typedef struct Mts300Msg { + uint16_t vref; + uint16_t thermistor; + uint16_t light; + uint16_t mic; + uint16_t accelX; + uint16_t accelY; + uint16_t magX; + uint16_t magY; +} Mts300Msg; + +enum { + AM_MTS300MSG = 6, +}; + +#define ACCEL_AVERAGE_POINTS 3 + +#endif -- 2.39.2