|
HI all,
I am trying to connect the sensor to UART pins
(UART RXD0,TXD0). The motes are programmed with TinyDBApp. Since the base
station sends the data over UART to TinyDB java application I was wondering can
that cause some problems on the mote that sits on the base station (as it is
programmed with the same program as remote motes, hence trying to access UART
pins)? I need only to read sensor data. When I try to run the application, it
seems that the program goes through rxByteReady, but I don't get any readings. I
also tried to send a fix value instead to the Attribute file I wrote but I see
only empty fields in the TinyDB GUI. Have I done something really wrong in the
sensor file or the problem is in Attribute file as well? What am I missing? Any
ideas? (At the moment powering on/off sensor is commented as I am using
fixed external voltage supply. The data is continuously sent to UART rx pin from
sensor board every 100ms and to take a reading (the whole packet) takes about
2ms.)
Here are the files:
UARToxygen.nc:
includes sensorboard;
configuration UARToxygen { provides { interface StdControl; interface ADC as Oxygen; } } implementation
{ components UARToxygenM, LedsC, UART; StdControl =
UARToxygenM;
Oxygen = UARToxygenM.Oxygen; UARToxygenM.ByteControl ->
UART;
UARToxygenM.ByteComm -> UART; UARToxygenM.Leds -> LedsC; } UARToxygenM.nc:
includes
sensorboard;
module UARToxygenM { provides {
interface StdControl; interface ADC as Oxygen; } uses { interface ByteComm; interface StdControl as ByteControl; interface Leds; } }
implementation { #define FLAG
0x7E // start/end of oxy UART
packet
#define ESCAPE 0x7D // ESCAPE character #define OxygenMsgLength 4 char state;
uint8_t buffer[4]; uint8_t rxCount; uint8_t *recPtr; int16_t oxy = 0;
bool previousEscaped = FALSE; //states
enum { IDLE, FLAGS, READ_BYTE, OXY_MEASUREMENT, POWER_OFF}; command result_t StdControl.init() { state = POWER_OFF; atomic { recPtr = (uint8_t *)&buffer; rxCount = 0; } call
Leds.init();
return call ByteControl.init(); } command result_t StdControl.start() { state = OXY_MEASUREMENT; // OXYGEN_LOGIC_POWER_ON(); // OXYGEN_POWER_ON(); return call ByteControl.start(); } command result_t StdControl.stop() { state = POWER_OFF; // OXYGEN_POWER_OFF(); // OXYGEN_LOGIC_POWER_OFF(); return call ByteControl.stop(); } default async event result_t
Oxygen.dataReady(uint16_t data) {
call StdControl.stop(); call Leds.yellowOff(); return SUCCESS; } // no such thing
async command result_t Oxygen.getContinuousData() { return FAIL; } async command result_t
Oxygen.getData(){
if (state == IDLE){ atomic{ state = OXY_MEASUREMENT; } call StdControl.start(); return SUCCESS; } state = IDLE; return FAIL; } /******************************************************************************
* Byte received from oxygen sensor * Oxygen messages starts and ends with '~' (0x7E) * * Since 0x7D is the escape byte, if you want to transmit the byte 0x7D it has * to be escaped (else how do you know that you're transmitting the byte 0x7D, * and not another escaped character?). 0x7D (along with other 'special' bytes * such as 0x7E - the flag byte) always get escaped. Thus if the protocol layer * encounters one of these bytes, it transmits the escape character (0x7D) * followed by the byte you want to transmit AND'd with 1101 1111. * * Thus 0x7D gets transmitted as 0x7D5D, and 0x7E gets transmitted as 0x7D5E, * etc. * * So when you read data from the UART, if you encounter a 0x7D, the actual * byte you want is the following byte OR'd with 0x20. * The packet structure:
FLAGS|NUM|DATA1|DATA2|CRC|FLAGS provided there are no
* escape
characters
*****************************************************************************/ async event result_t ByteComm.rxByteReady(uint8_t data, bool error, //ready to receive another byte uint16_t strength) { uint8_t i; uint16_t crc_calc; // call Leds.yellowOn(); if (error){ rxCount = 0; state = FLAGS; return FAIL; } if( data == FLAG ) if( state == FLAGS ) return SUCCESS; else { rxCount = 0; state = FLAGS; return FAIL; } state =
READ_BYTE;
if( previousEscaped ) { data ^= 0x20; previousEscaped = FALSE; } else if( data == ESCAPE ) { previousEscaped = TRUE; return SUCCESS; } // data == NUM || DATA1 ||
DATA2 || CHECKSUM
recPtr[rxCount++] =
data;
if( rxCount == OxygenMsgLength ) { crc_calc = 0; for (i = 0; i<OxygenMsgLength; i++) crc_calc += recPtr[i]; state
= FLAGS;
rxCount = 0; if (crc_calc != 0) { return FAIL; } else { oxy += buffer[1]; //read MSB oxy <<= 8; oxy += buffer[2]; //read LSB if(oxy > 100) oxy = 100; if(oxy < 0) oxy = 0; signal Oxygen.dataReady(oxy); // signal OxySensor.dataReady(50); } } return SUCCESS; } //not used async event result_t ByteComm.txByteReady(bool success){ return SUCCESS; } //not used
async event result_t ByteComm.txDone(){ return SUCCESS; } }
AttrOxygenM.nc:
module
AttrOxygenM
{ provides interface StdControl; uses { interface AttrRegister as OxygenAttr; interface ADC as Oxygen; interface StdControl as SensorControl; } } implementation { char *oxygen; command result_t StdControl.init() { call SensorControl.init(); if (call OxygenAttr.registerAttr("oxygen", UINT16, 2) != SUCCESS) return FAIL; return SUCCESS; } command result_t
StdControl.start()
{ call SensorControl.start(); return SUCCESS; } command result_t StdControl.stop() { call SensorControl.stop(); return SUCCESS; } event result_t
OxygenAttr.startAttr()
{ return call OxygenAttr.startAttrDone(); } event result_t
OxygenAttr.getAttr(char *name, char *resultBuf, SchemaErrorNo
*errorNo)
{ atomic { oxygen = resultBuf; } *(uint16_t*)oxygen = 0xffff; *errorNo = SCHEMA_ERROR; if (call Oxygen.getData() != SUCCESS) return FAIL; *errorNo = SCHEMA_RESULT_PENDING; return SUCCESS; } async event result_t
Oxygen.dataReady(uint16_t data)
{ *(uint16_t*)oxygen = data; call OxygenAttr.getAttrDone("oxygen", oxygen, SCHEMA_RESULT_READY); return SUCCESS; } event result_t OxygenAttr.setAttr(char *name, char *attrVal) { return FAIL; } } Thanks,
Amra
|
_______________________________________________ Tinyos-help mailing list [email protected] https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
