Revision 1:0370ea94b64a, committed 2012-04-26
- Comitter:
- narshu
- Date:
- Thu Apr 26 17:50:00 2012 +0000
- Parent:
- 0:7583de124698
- Commit message:
Changed in this revision
--- a/RF12B/RF12B.cpp Fri Mar 30 19:44:25 2012 +0000
+++ b/RF12B/RF12B.cpp Thu Apr 26 17:50:00 2012 +0000
@@ -2,41 +2,38 @@
#include "RF_defs.h"
#include <algorithm>
-
-//#include "globals.h"
+#include "system.h"
+#include "globals.h"
-//DigitalOut DBG2(LED2);
-//DigitalOut DBG3(LED3);
-//DigitalOut DBG4(LED4);
RF12B::RF12B(PinName _SDI,
PinName _SDO,
PinName _SCK,
PinName _NCS,
PinName _NIRQ):spi(_SDI, _SDO, _SCK),
- NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ){// rfled(LED3) {
+ NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ) {// rfled(LED3) {
- /* SPI frequency, word lenght, polarity and phase */
+ // SPI frequency, word lenght, polarity and phase */
spi.format(16,0);
spi.frequency(2000000);
- /* Set ~CS high */
+ // Set ~CS high
NCS = 1;
- /* Initialise RF Module */
+ // Initialise RF Module
init();
- /* Setup interrupt to happen on falling edge of NIRQ */
+ // Setup interrupt to happen on falling edge of NIRQ
NIRQ.fall(this, &RF12B::rxISR);
}
-/* Returns the packet length if data is available in the receive buffer, 0 otherwise*/
-unsigned int RF12B::available() {
- return fifo.size();
-}
+// Returns the packet length if data is available in the receive buffer, 0 otherwise
+//unsigned int RF12B::available() {
+// return fifo.size();
+//}
-/* Reads a packet of data, with length "size" Returns false if read failed. TODO: make a metafifo to isolate packets*/
-bool RF12B::read(unsigned char* data, unsigned int size) {
+// Reads a packet of data, with length "size" Returns false if read failed. TODO: make a metafifo to isolate packets
+/*bool RF12B::read(unsigned char* data, unsigned int size) {
if (fifo.size() == 0) {
return false;
} else {
@@ -48,8 +45,10 @@
return true;
}
}
+*/
-/* Reads a byte of data from the receive buffer */
+// Reads a byte of data from the receive buffer
+/*
unsigned char RF12B::read() {
if (available()) {
unsigned char data = fifo.front();
@@ -59,12 +58,13 @@
return 0xFF; // Error val although could also be data...
}
}
+*/
-/* Sends a packet of data to the RF module for transmission TODO: Make asych*/
+// Sends a packet of data to the RF module for transmission TODO: Make asych
void RF12B::write(unsigned char *data, unsigned char length) {
unsigned char crc = 0;
-
- /* Transmitter mode */
+
+ // Transmitter mode
changeMode(TX);
writeCmd(0x0000);
@@ -73,12 +73,12 @@
send(0xAA);
send(0x2D); // SYNC
send(0xD4);
- /* Packet Length */
+ // Packet Length
send(length);
crc = crc8(crc, length);
send(crc);
crc = crc8(crc, crc);
- /* Packet Data */
+ // Packet Data
for (unsigned char i=0; i<length; i++) {
send(data[i]);
crc = crc8(crc, data[i]);
@@ -88,31 +88,31 @@
send(0xAA);
send(0xAA);
- /* Back to receiver mode */
+ // Back to receiver mode
changeMode(RX);
status();
-
+
}
-/* Transmit a 1-byte data packet */
+// Transmit a 1-byte data packet
void RF12B::write(unsigned char data) {
write(&data, 1);
}
-
+/*
void RF12B::write(queue<char> &data, int length) {
char crc = 0;
char length_byte = 0;
-
- /* -1 means try to transmit everything in the queue */
- if(length == -1) {
+
+ // -1 means try to transmit everything in the queue
+ if (length == -1) {
length = data.size();
}
-
- /* max length of packet is 255 */
+
+ // max length of packet is 255
length_byte = min(length, 255);
-
- /* Transmitter mode */
+
+ // Transmitter mode
changeMode(TX);
writeCmd(0x0000);
@@ -121,12 +121,12 @@
send(0xAA);
send(0x2D); // SYNC
send(0xD4);
- /* Packet Length */
+ // Packet Length
send(length_byte);
crc = crc8(crc, length_byte);
send(crc);
crc = crc8(crc, crc);
- /* Packet Data */
+ // Packet Data
for (char i=0; i<length_byte; i++) {
send(data.front());
crc = crc8(crc, data.front());
@@ -137,18 +137,18 @@
send(0xAA);
send(0xAA);
- /* Back to receiver mode */
+ // Back to receiver mode
changeMode(RX);
status();
}
-
+*/
/**********************************************************************
* PRIVATE FUNCTIONS
*********************************************************************/
-/* Initialises the RF12B module */
+// Initialises the RF12B module
void RF12B::init() {
- /* writeCmd(0x80E7); //EL,EF,868band,12.0pF
+ // writeCmd(0x80E7); //EL,EF,868band,12.0pF
changeMode(RX);
writeCmd(0xA640); //frequency select
writeCmd(0xC647); //4.8kbps
@@ -161,7 +161,7 @@
writeCmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDIT�CBW0
writeCmd(0xE000); //NOT USED
writeCmd(0xC800); //NOT USED
- writeCmd(0xC040); //1.66MHz,2.2V */
+ writeCmd(0xC040); //1.66MHz,2.2V
writeCmd(
RFM_CONFIG_EL |
@@ -273,25 +273,18 @@
}
}
-/* Interrupt routine for data reception */
+// Interrupt routine for data reception */
void RF12B::rxISR() {
-
- //static int cnt = 0;
- //printf("%d hits\r\n", cnt);
- //cnt++;
-
- //DBG2 = !(cnt%3);
- //DBG3 = !((cnt+1)%3);
- //DBG4 = !((cnt+2)%3);
unsigned int data = 0;
static int i = -2;
static unsigned char packet_length = 0;
static unsigned char crc = 0;
+ static unsigned char temp;
//Loop while interrupt is asserted
while (!NIRQ_in && mode == RX) {
-
+
// Grab the packet's length byte
if (i == -2) {
data = writeCmd(0x0000);
@@ -331,11 +324,12 @@
if (NIRQ_in)
break;
- // Grab the packet's data
+ // Grab the packet's data
if (i >= 0 && i < packet_length) {
data = writeCmd(0x0000);
if ( (data&0x8000) ) {
data = writeCmd(0xB000);
+ temp = data&0x00FF;
//temp.push(data&0x00FF);
crc = crc8(crc, (unsigned char)(data&0x00FF));
i++;
@@ -353,9 +347,16 @@
if ((unsigned char)(data & 0x00FF) == crc) {
//If the checksum is correct, add our data to the end of the output buffer
//while (!temp.empty()) {
- // fifo.push(temp.front());
- // temp.pop();
- //}
+ //fifo.push(temp);
+ // temp.pop();
+#ifdef ROBOT_SECONDARY
+ if (callbackfunc)
+ (*callbackfunc)(temp);
+
+ if (callbackobj && mcallbackfunc)
+ (callbackobj->*mcallbackfunc)(temp);
+#endif
+ // }
}
// Tell RF Module we are finished, and clean up
@@ -367,19 +368,20 @@
}
}
}
+
}
unsigned int RF12B::status() {
return writeCmd(0x0000);
}
-/* Tell the RF Module this packet is received and wait for the next */
+// Tell the RF Module this packet is received and wait for the next */
void RF12B::resetRX() {
writeCmd(0xCA81);
writeCmd(0xCA83);
};
-/* Calculate CRC8 */
+// Calculate CRC8 */
unsigned char RF12B::crc8(unsigned char crc, unsigned char data) {
crc = crc ^ data;
for (int i = 0; i < 8; i++) {
--- a/RF12B/RF12B.h Fri Mar 30 19:44:25 2012 +0000
+++ b/RF12B/RF12B.h Thu Apr 26 17:50:00 2012 +0000
@@ -2,10 +2,12 @@
#define _RF12B_H
#include "mbed.h"
-#include <queue>
+//#include <queue>
enum rfmode_t{RX, TX};
+class DummyCT;
+
class RF12B {
public:
/* Constructor */
@@ -14,6 +16,7 @@
PinName SCK,
PinName NCS,
PinName NIRQ);
+
/* Reads a packet of data. Returns false if read failed. Use available() to check how much space to allocate for buffer */
@@ -26,15 +29,20 @@
/* Transmits a packet of data */
void write(unsigned char* data, unsigned char length);
void write(unsigned char data); /* 1-byte packet */
- void write(std::queue<char> &data, int length = -1); /* sends a whole queue */
+// void write(std::queue<char> &data, int length = -1); /* sends a whole queue */
/* Returns the packet length if data is available in the receive buffer, 0 otherwise*/
unsigned int available();
+
+ /** A assigns a callback function when a new reading is available **/
+ void (*callbackfunc)(unsigned char rx_code);
+ DummyCT* callbackobj;
+ void (DummyCT::*mcallbackfunc)(unsigned char rx_code);
protected:
/* Receive FIFO buffer */
- std::queue<unsigned char> fifo;
- std::queue<unsigned char> temp; //for storing stuff mid-packet
+// std::queue<unsigned char> fifo;
+// std::queue<unsigned char> temp; //for storing stuff mid-packet
/* SPI module */
SPI spi;
--- a/RF12B/RFSerial.cpp Fri Mar 30 19:44:25 2012 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-
-/* Constructor */
-#include "RFSerial.h"
-
-RFSerial::RFSerial(PinName _SDI,
- PinName _SDO,
- PinName _SCK,
- PinName _NCS,
- PinName _NIRQ)
-:RF12B(_SDI, _SDO, _SCK, _NCS, _NIRQ) {
-
-}
-
-// Stream implementation functions
-int RFSerial::_putc(int value) {
- RF12B::write((unsigned char) value);
- return value;
-}
-int RFSerial::_getc() {
- if(available()) {
- return RF12B::read();
- } else {
- return EOF;
- }
-}
\ No newline at end of file
--- a/RF12B/RFSerial.h Fri Mar 30 19:44:25 2012 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#include "mbed.h"
-#include "RF12B.h"
-
-class RFSerial : public Stream, public RF12B {
-public:
- /* Constructor */
- RFSerial(PinName _SDI,
- PinName _SDO,
- PinName _SCK,
- PinName _NCS,
- PinName _NIRQ);
-
-protected:
- // Stream implementation functions
- virtual int _putc(int value);
- virtual int _getc();
-};
\ No newline at end of file
--- a/RFSRF05.cpp Fri Mar 30 19:44:25 2012 +0000
+++ b/RFSRF05.cpp Thu Apr 26 17:50:00 2012 +0000
@@ -1,7 +1,8 @@
#include "RFSRF05.h"
#include "mbed.h"
-
+#include "globals.h"
+#include "system.h"
RFSRF05::RFSRF05(PinName trigger,
@@ -24,22 +25,34 @@
_echo3(echo3),
_echo4(echo4),
_echo5(echo5) {
+
// initialises codes
- _code[0] = CODE0;
- _code[1] = CODE1;
- _code[2] = CODE2;
+ codes[0] = CODE0;
+ codes[1] = CODE1;
+ codes[2] = CODE2;
//set callback execute to true
ValidPulse = false;
// Attach interrupts
- _echo0.rise(this, &RFSRF05::_rising);
+#ifdef SONAR_ECHO_INV
+ // inverted sonar inputs
+ _echo5.fall(this, &RFSRF05::_rising);
+ _echo0.rise(this, &RFSRF05::_falling);
+ _echo1.rise(this, &RFSRF05::_falling);
+ _echo2.rise(this, &RFSRF05::_falling);
+ _echo3.rise(this, &RFSRF05::_falling);
+ _echo4.rise(this, &RFSRF05::_falling);
+ _echo5.rise(this, &RFSRF05::_falling);
+#else
+ _echo5.rise(this, &RFSRF05::_rising);
_echo0.fall(this, &RFSRF05::_falling);
_echo1.fall(this, &RFSRF05::_falling);
_echo2.fall(this, &RFSRF05::_falling);
_echo3.fall(this, &RFSRF05::_falling);
_echo4.fall(this, &RFSRF05::_falling);
_echo5.fall(this, &RFSRF05::_falling);
+#endif
//init callabck function
@@ -50,31 +63,61 @@
// innitialises beacon counter
_beacon_counter = 0;
- //Interrupts every 50ms
- //_ticker.attach(this, &RFSRF05::_startRange, 0.05);
+#ifdef PRIMARY_ROBOT
+ //Interrupts every 50ms for primary robot
+ _ticker.attach(this, &RFSRF05::_startRange, 0.05);
+#else
+ //attach callback
+ _rf.callbackobj = (DummyCT*)this;
+ _rf.mcallbackfunc = (void (DummyCT::*)(unsigned char rx_data)) &RFSRF05::startRange;
+#endif
+
}
-
-void RFSRF05::startRange() {
+#ifdef PRIMARY_ROBOT
+void RFSRF05::_startRange() {
//printf("Srange\r\r");
// increments counter
_beacon_counter = (_beacon_counter + 1) % 3;
- // writes code to RF port
- _rf.write(_code[_beacon_counter]);
- // send a trigger pulse, 10uS long
+ // set flags
ValidPulse = false;
expValidPulse = true;
+ // writes code to RF port
+ _rf.write(codes[_beacon_counter]);
+
+ // send a trigger pulse, 10uS long
_trigger = 1;
wait_us (10);
_trigger = 0;
- wait_us(50);
+
}
+#else
+void RFSRF05::startRange(unsigned char rx_code) {
+ for (int i = 0; i < 3; i++) {
+ if (rx_code == codes[i]) {
+
+ // assign beacon_counter
+ _beacon_counter = i;
+
+ // set flags
+ ValidPulse = false;
+ expValidPulse = true;
+
+ // send a trigger pulse, 10uS long
+ _trigger = 1;
+ wait_us (10);
+ _trigger = 0;
+ break;
+ }
+ }
+}
+#endif
// Clear and start the timer at the begining of the echo pulse
void RFSRF05::_rising(void) {
@@ -104,7 +147,7 @@
(*callbackfunc)(_beacon_counter, _dist[_beacon_counter]);
if (callbackobj && mcallbackfunc)
- (callbackobj->*mcallbackfunc)(_beacon_counter, _dist[_beacon_counter]);
+ (callbackobj->*mcallbackfunc)(_beacon_counter, _dist[_beacon_counter], sonarvariance);
}
@@ -130,6 +173,10 @@
return (_dist[beaconnum]);
}
+void RFSRF05::setCode(int code_index, unsigned char code) {
+ codes[code_index] = code;
+}
+
//SRF05::operator float() {
// return read();
//}
--- a/RFSRF05.h Fri Mar 30 19:44:25 2012 +0000
+++ b/RFSRF05.h Thu Apr 26 17:50:00 2012 +0000
@@ -2,8 +2,12 @@
#ifndef MBED_RFSRF05_H
#define MBED_RFSRF05_H
+
+
#include "mbed.h"
#include "RF12B.h"
+#include "globals.h"
+
#define CODE0 0x22
#define CODE1 0x44
@@ -47,23 +51,30 @@
/** A non-blocking function that will return the last measurement
*
- * @returns floating point representation of distance in cm
+ * @returns floating point representation of distance in mm
*/
float read0();
float read1();
float read2();
float read(unsigned int beaconnum);
+
/** A assigns a callback function when a new reading is available **/
void (*callbackfunc)(int beaconnum, float distance);
DummyCT* callbackobj;
- void (DummyCT::*mcallbackfunc)(int beaconnum, float distance);
+ void (DummyCT::*mcallbackfunc)(int beaconnum, float distance, float variance);
//triggers a read
- void startRange();
+ #ifndef PRIMARY_ROBOT
+ void startRange(unsigned char rx_code);
+ #endif
+
+ //set codes
+ void setCode(int code_index, unsigned char code);
+ unsigned char codes[3];
/** A short hand way of using the read function */
- operator float();
+ //operator float();
private :
RF12B _rf;
@@ -76,10 +87,12 @@
InterruptIn _echo5;
Timer _timer;
Ticker _ticker;
+ #ifdef PRIMARY_ROBOT
+ void _startRange(void);
+ #endif
void _rising (void);
void _falling (void);
float _dist[3];
- char _code[3];
int _beacon_counter;
bool ValidPulse;
bool expValidPulse;