This is used for sending Data to receiving mDot
Dependencies: libmDot-dev-mbed5-deprecated sd-driver ISL29011
Fork of mdot-examples by
Diff: send_main.cpp
- Revision:
- 30:218c795a8081
- Parent:
- 29:0f0a059f023e
--- a/send_main.cpp Fri Apr 13 00:08:44 2018 +0000 +++ b/send_main.cpp Sat Apr 14 18:09:54 2018 +0000 @@ -1,7 +1,6 @@ /* - 3/27/2018 mdot version = 3.0.2, mbed version = 5.5.7 WORKS - 3/27/2018 mdot version = 3.1.0, mbed version = 5.7.4 WORKS - 4/12/2018 mdot version = 3.1.0, mbed version = 5.7.4 Eh + 3/27/2018 mdot version 3.1.0, mbed version 5.7.4 + 4/14/2018 mdot version 3.1.0, mbed version 5.7.7 */ #include <stdlib.h> @@ -11,51 +10,20 @@ #include "dot_util.h" #include "RadioEvent.h" #include "itoa.h" -#include <sstream> - -// For time Keeping -#include <ctime> - -#include <errno.h> // For SD card error checking -#include "platform/FilePath.h" // For SD card struct stat - -// Headers for the SD card -#include "SDBlockDevice.h" -#include "FATFileSystem.h" #define BUFFER_SIZE 10 -//------------------------------SD CARD------------------------------------// - -SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs -FATFileSystem fs("sd"); - -FILE *fp; - -// Error checking SD stuff -int err; - //note: added GPS functions , variables below (will organize better later ski //and gps code in main is noted - ///-----------------FOR GPS------BELOW---------------------------------- // #include "MBed_Adafruit_GPS.h" -Serial * gps_Serial = new Serial(PA_2,PA_3); -//Initalize using pa2 and pa3 (D0 and D1 on the mdot) (D0->TX on gps) (D1->RX on gps) -//gps_Serial = new Serial(PA_2,PA_3); //serial object for use w/ GPS USING PA_2 and PA_3 -Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class -char c; //when read via Adafruit_GPS::read(), the class returns single character stored here -Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? -const int refresh_Time = 2000; //refresh time in ms - - // how long are max NMEA lines to parse? #define MAXLINELENGTH 120 @@ -402,6 +370,18 @@ /////FOR GPS-----------------ABOVE------------------------------------- + + + + + + + + + + + + ///////////////////////////////////////////////////////////////////////////// // -------------------- DOT LIBRARY REQUIRED ------------------------------// // * Because these example programs can be used for both mDot and xDot // @@ -428,8 +408,8 @@ lora::ChannelPlan* plan = NULL; //--------------End of MDOT variables-------------------// -// PC serial communication Serial pc(USBTX, USBRX); +Ticker Periodic; // ADXL372 Slave I2C I2C ADXL372(I2C_SDA, I2C_SCL); // (D14,D15) (MISO, CS) @@ -452,8 +432,11 @@ const int ADXL372_Address_7bit = 0x1D; // Address for the I2C if MISO pulled low, 0x53 if pulled high const int ADXL372_Address_8bit = ADXL372_Address_7bit << 1; // Same -// Used by all sensors to pass register address of device -int regAddress; +const int DS7505s_Address_7bit = 0x48; // A0 set LOR, A1 set LOW, A2 set LOW +const int DS7505s_Address_8bit = DS7505s_Address_7bit << 1; // Same + + +int regAddress; // Used by all sensors /* * Variables used for ADT7410 Temperature @@ -467,6 +450,8 @@ /* * Variables used for mDot */ + + uint32_t tx_frequency; uint8_t tx_datarate; uint8_t tx_power; @@ -488,7 +473,6 @@ uint16_t YData; uint16_t ZData; - uint16_t XDataInterrupt[BUFFER_SIZE]; uint16_t YDataInterrupt[BUFFER_SIZE]; uint16_t ZDataInterrupt[BUFFER_SIZE]; @@ -506,13 +490,12 @@ * Prototype functions */ -std::string convertInt(int number); char twosComplementConversion(char value); void ADXL372Initialize(void); void ADXL372Reset(void); void I2CSelfTest(void); -void accelerometerI2CWrite(uint8_t hexAddress, uint8_t hexData); +void accelerometerI2CWrite(int hexAddress, int hexData); char * accelerometerI2CRead(int hexAddress); void ADT7410Initialize(void); @@ -531,15 +514,13 @@ takeAccelerometer = true; // Take accelerometer because something happened } -// This is where all the reading and sending takes place void takePeriodicReading(std::vector<uint8_t> tx_data){ pc.printf("Regular periodic reading \n\r"); /* * Taking accelerometer data */ regAddress = 0x08; // This is the register address for XData - accelValues = accelerometerI2CRead(regAddress); // Get the dadta - // Raw Data + accelValues = accelerometerI2CRead(regAddress); Xmsb = *(accelValues + 0); Xlsb = *(accelValues + 1); Ymsb = *(accelValues + 2); @@ -547,27 +528,13 @@ Zmsb = *(accelValues + 4); Zlsb = *(accelValues + 5); - // Combine two bytes into short int, remove last 4 bits because 12bit resolution - XData = (Xmsb << 8 | Xlsb) >> 4; + XData = (Xmsb << 8 | Xlsb) >> 4; // Combine two bytes into short int, remove last 4 flag bits YData = (Ymsb << 8 | Ylsb) >> 4; ZData = (Zmsb << 8 | Zlsb) >> 4; - // Check if twos complement is needed - //XData = twosComplementConversion(XData); - //YData = twosComplementConversion(YData); - //ZData = twosComplementConversion(ZData); - - // Split Complemented Data - Xlsb = XData & 0xff; // Split the lower 8 bits here - Xmsb = XData >> 8; // Split the higher 8 bits here - - Ylsb = YData & 0xff; // Split the lower 8 bits here - Ymsb = YData >> 8; // Split the higher 8 bits here - - Zlsb = ZData & 0xff; // Split the lower 8bits here - Zmsb = ZData >> 8; // Split the higher 8bits here - - + XData = twosComplementConversion(XData); + YData = twosComplementConversion(YData); + ZData = twosComplementConversion(ZData); /* * Taking temperature data @@ -576,96 +543,18 @@ rawTempValues = ADT7410Read(regAddress); convertedTempValue = ((*(rawTempValues + 0) << 8) | *(rawTempValues + 1)) >> 3; // Combine the two bytes into // a short int variable(16 bits), remove last 3 bits - - - c = myGPS.read(); //queries the GPS - - // if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused - //check if we recieved a new message from GPS, if so, attempt to parse it, - if ( myGPS.newNMEAreceived() ) { - if ( !myGPS.parse(myGPS.lastNMEA()) ) { - - } - } - pc.printf("Refresh timer: %d\n\r", refresh_Timer.read_ms()); - //check if enough time has passed to warrant printing GPS info to screen - //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing - if (refresh_Timer.read_ms() >= refresh_Time) { - refresh_Timer.reset(); - - /* pc.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); - pc.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year); - pc.printf("Fix: %d\n", (int) myGPS.fix); - pc.printf("Quality: %d\n", (int) myGPS.fixquality);*/ - if (myGPS.fix) { - pc.printf("\r"); - pc.printf("GPS log:\n"); - pc.printf("\r"); - pc.printf("Time(GMT): %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); - pc.printf("\r"); - pc.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year); - pc.printf("\r"); - pc.printf("Fix: %d\n", (int) myGPS.fix); - pc.printf("\r"); - pc.printf("Quality: %d\n", (int) myGPS.fixquality); - pc.printf("\r"); - pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); - pc.printf("\r"); - pc.printf("Speed: %5.2f knots\n", myGPS.speed); - pc.printf("\r"); - pc.printf("Angle: %5.2f\n", myGPS.angle); - pc.printf("\r"); - pc.printf("Altitude: %5.2f\n", myGPS.altitude); - pc.printf("\r"); - pc.printf("Satellites: %d\n", myGPS.satellites); - pc.printf("\r"); - } - } - pc.printf("Accelerometer::: "); pc.printf("X: 0x%x | Y: 0x%x | Z: 0x%x\n\r", XData, YData, ZData); - //pc.printf("X: 0x%x %x | Y: 0x%x %x | Z: 0x%x %x\n\r", Xmsb, Xlsb, Ymsb, Ylsb,Zmsb, Zlsb); pc.printf("Temperature::: "); pc.printf("Celsius: 0x%x\n\r", convertedTempValue); - // Push 1 for temperature - tx_data.push_back(1); + tx_data.push_back((convertedTempValue >> 8) & 0xFF); tx_data.push_back(convertedTempValue & 0xFF); - - // Push 2 for AccelerometerX - tx_data.push_back(2); - tx_data.push_back((XData >> 8) & 0xFF); - tx_data.push_back(XData & 0xFF); - // Push 3 for AccelerometerY - tx_data.push_back(3); - tx_data.push_back((YData >> 8) & 0xFF); - tx_data.push_back(YData & 0xFF); - // Push 4 for AccelerometerZ - tx_data.push_back(4); - tx_data.push_back((ZData >> 8) & 0xFF); - tx_data.push_back(ZData & 0xFF); logInfo("Temperautre: %lu [0x%04X]", convertedTempValue, convertedTempValue); send_data(tx_data); - - // Time - //tx_data.push_back(7); - //tx_data.push_back(); - // GPS - - - - - // Save data -// fprintf(fp,"%d,%d,%d,%d,%f,%f,%d/%d/%d,%d:%d:%d:%d\n", -// convertedTempValue, -// XData, YData, ZData, -// myGPS.longitude, myGPS.latitude, -// myGPS.month, myGPS.day, myGPS.year, -// myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); -// periodicReadingTrigger = false; // Flip back to no trigger - + periodicReadingTrigger = false; // Flip back to no trigger } void takePeriodicReadingTicker(void){ @@ -682,47 +571,14 @@ *////////////////////////////////////////////////////////////////////////////// int main(void) { - // Custom event handler for automatically displaying RX data //interruptEverything.attach(&interruptReadTemperature, 7.0); RadioEvent events; - // Change baud rate in serial terminal to this value pc.baud(115200); ADXL372.frequency(300000); // I2C devices are connected to the same clock ADT7410.frequency(300000); // Redundant but whatever ADT7410_Int.rise(&CriticalTemperatureInterrupt); - myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *) - //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf - - myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation - myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); - myGPS.sendCommand(PGCMD_ANTENNA); - - refresh_Timer.start(); //starts the clock on the timer - - // current date/time based on current system - std::time_t now = time(0); - - // convert now to string format - char* dt = ctime(&now); - // To access individual times - tm *ltm = localtime(&now); - - std::string file_Date = convertInt(ltm->tm_mon) + "-" + - convertInt(ltm->tm_mday) + "-" + - convertInt(ltm->tm_year + 1900) + ".txt"; - std::string directory = "sd/mytest/"; - std::string fileName = directory + file_Date; - - // Show the file name - pc.printf("%s\n", fileName.c_str()); - - // Initialize sensors - ADT7410Initialize(); - ADXL372Initialize(); - - mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); @@ -833,85 +689,129 @@ // Display configuration // It's gonna output a lot of information onto the Serial Terminal display_config(); +// ******************************************GPS + Serial * gps_Serial; + //Initalize using pa2 and pa3 (D0 and D1 on the mdot) (D0->TX on gps) (D1->RX on gps) + gps_Serial = new Serial(PA_2,PA_3); //serial object for use w/ GPS USING PA_2 and PA_3 + Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class + char c; //when read via Adafruit_GPS::read(), the class returns single character stored here + Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? + const int refresh_Time = 2000; //refresh time in ms + + myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *) + //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf + + myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation + myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); + myGPS.sendCommand(PGCMD_ANTENNA); + + pc.printf("Connection established at 115200 baud...\n\r"); + + wait(1); + + refresh_Timer.start(); //starts the clock on the timer - + while(true){ + c = myGPS.read(); //queries the GPS + + if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused + //check if we recieved a new message from GPS, if so, attempt to parse it, + if ( myGPS.newNMEAreceived() ) { + if ( !myGPS.parse(myGPS.lastNMEA()) ) { + continue; + } + } + + //check if enough time has passed to warrant printing GPS info to screen + //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing + if (refresh_Timer.read_ms() >= refresh_Time) { + pc.printf("Refresh Timer: %d\n\r", refresh_Timer.read_ms()); + refresh_Timer.reset(); + + pc.printf("Time: %d:%d:%d.%u\n\r", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); + pc.printf("Date: %d/%d/20%d\n\r", myGPS.day, myGPS.month, myGPS.year); + pc.printf("Fix: %d\n\r", (int) myGPS.fix); + pc.printf("Quality: %d\n\r", (int) myGPS.fixquality); + if (myGPS.fix) { + pc.printf("Location: %5.2f%c, %5.2f%c\n\r", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); + pc.printf("Speed: %5.2f knots\n\r", myGPS.speed); + pc.printf("Angle: %5.2f\n\r", myGPS.angle); + pc.printf("Altitude: %5.2f\n\r", myGPS.altitude); + pc.printf("Satellites: %d\n\r", myGPS.satellites); + } + } + }//end while - // //below is acc,temp sensors - // ADT7410Initialize(); - // ADXL372Initialize(); - + + +// ******************************************end GPS + + + + + +//below is acc,temp sensors + ADT7410Initialize(); + ADXL372Initialize(); -// // Initialize the SD card -// if(sd.init() == 0) -// { -// pc.printf("Init success \n\r"); -// } -// else pc.printf("Init failed \n\r"); -// err = fs.mount(&sd); -// pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); -// if (err) -// return err; -// pc.printf("Error for mounting was %d\n\r", err); -// -// pc.printf("Opening file %s... ", fileName); -// -// // Open your file -// fp = fopen(fileName.c_str(), "w+"); -// pc.printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n")); -// -// if (!fp) -// { -// // Check whether directory '/sd/mytest' exists. -// pc.printf("\r\nChecking directory '%s'...\r\n", fileName); -// struct stat info; -// err = stat(directory.c_str(), &info); -// if (err) -// { -// pc.printf("Directory '%s' does not exist.\r\n", directory); -// pc.printf("Trying to create it..."); -// err = mkdir(directory.c_str(), 0777); -// pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); -// if (err) -// return err; -// } -// -// // Create a new 'sdtest.txt' file. -// pc.printf("File not found, creating a new one...\r\n"); -// fp = fopen(fileName.c_str(), "w+"); -// pc.printf("%s\r\n", (!fp ? "Failed :(" : "OK")); -// if (!fp) -// { -// error("error: %s (%d)\r\n", strerror(errno), -errno); -// return errno; -// } -// } -// -// fprintf(fp,"Temperature,X Acceleration,Y Acceleration,Z Acceleration,\ -// Longitude,Latitude,Date,Time\n"); + Periodic.attach(&takePeriodicReadingTicker,5); - - // Continous loop while(1){ // Create a vector of uint8_t elements to be sent later std::vector<uint8_t> tx_data; - - takePeriodicReading(tx_data); - + if(periodicReadingTrigger) + { + takePeriodicReading(tx_data); + } if(takeAccelerometer || takeTemperature){ pc.printf("INTERRUPTEDDDDDDDD: "); if(takeTemperature) pc.printf("Temperature triggered!!!!!!!!!!!!\n\r"); else if(takeAccelerometer) pc.printf("AccelerometerTriggered!!!!!!!!!!!!!\n\r"); + for(int i = 0; i < BUFFER_SIZE; ++i){ + /* + * Taking accelerometer data + */ + regAddress = 0x08; // This is the register address for XData + accelValues = accelerometerI2CRead(regAddress); + Xmsb = *(accelValues + 0); + Xlsb = *(accelValues + 1); + Ymsb = *(accelValues + 2); + Ylsb = *(accelValues + 3); + Zmsb = *(accelValues + 4); + Zlsb = *(accelValues + 5); + + XDataInterrupt[i] = (Xmsb << 8 | Xlsb) >> 4; // Combine two bytes into short int, remove last 4 flag bits + YDataInterrupt[i] = (Ymsb << 8 | Ylsb) >> 4; + ZDataInterrupt[i] = (Zmsb << 8 | Zlsb) >> 4; + + XDataInterrupt[i] = twosComplementConversion(XDataInterrupt[i]); + YDataInterrupt[i] = twosComplementConversion(YDataInterrupt[i]); + ZDataInterrupt[i] = twosComplementConversion(ZDataInterrupt[i]); + + /* + * Taking temperature data + */ + regAddress = 0x00; + rawTempValues = ADT7410Read(regAddress); + temperatureBuffer[i] = ((*(rawTempValues + 0) << 8) | *(rawTempValues + 1)) >> 3; // Combine the two bytes into + // a short int variable(16 bits), remove last 3 bits + } - takePeriodicReading(tx_data); - + for(int i = 0; i < BUFFER_SIZE; ++i){ + pc.printf("Accelerometer::: "); + pc.printf("X: 0x%x | Y: 0x%x | Z: 0x%x\n\r", XDataInterrupt[i], YDataInterrupt[i], ZDataInterrupt[i]); + pc.printf("Temperature::: "); + pc.printf("Celsius: 0x%x\n\r", temperatureBuffer[i]); + } + //wait(0.2); takeAccelerometer = false; // Flip back to no trigger takeTemperature = false; // Flip back to no trigger @@ -919,27 +819,13 @@ - // Go to sleep for 5 seconds and wake up after 5 seconds or wakeup from interrupt - if(dot->sleep(5,mDot::RTC_ALARM_OR_INTERRUPT, false) == mDot::MDOT_OK) - { - pc.printf("Sleep well\n\r"); - } - + //wait(1); } return 0; } - -std::string convertInt(int number) -{ - std::stringstream ss; - ss << number; - return ss.str(); - -} - /******************************************************************************* * Not really used at the moment * Not really needed. But keep just in case because I don't want to rewrite it @@ -967,23 +853,9 @@ ******************************************************************************/ //////////////////////////////////////////////////////////////////////////////// void ADXL372Initialize(void){ - ADXL372Reset(); pc.printf("Initializing ADXL372 accelerometer\n\r"); - accelerometerI2CWrite(0x20, 0x0F); // X offset - accelerometerI2CWrite(0x21, 0x02); // Y offset - accelerometerI2CWrite(0x22, 0x05); // Z offset - // accelerometerI2CWrite(0x33, 0x01); // X axis used - // accelerometerI2CWrite(0x35, 0x01); // Y axis used - // accelerometerI2CWrite(0x37, 0x01); // Z axis used - - //accelerometerI2CWrite(0x3A, 0x00); // FIFO takes X,Y,Z - //accelerometerI2CWrite(0x3D, 0x03); // ODR 3200 Hz - accelerometerI2CWrite(0x3E, 0x03); // Bandwidth 1600 -// accelerometerI2CWrite(0x3F, 0x2F); // High instant on threshold, LPF&HPF disabled, Instant on Mode - accelerometerI2CWrite(0x3F, 0x3E); // Enable I2C highspeed,Low Pass, High pass and full bandwidth measurement mode + accelerometerI2CWrite(0x3F, 0x0F); // Enable I2C highspeed,Low Pass, High pass and full bandwidth measurement mode accelerometerI2CWrite(0x38, 0x01); // Enable the High pass filter corner 1 at register 0x38 - //accelerometerI2CWrite(0x3E, 0x44); // AutoSleep is enabled with LINKLOOP at DEFAULTMODE , Bandwidth at default, - /* accelerometerI2CWrite(0x24, 0x01); // X used for activity threshold accelerometerI2CWrite(0x26, 0x01); // Y used for activity threshold accelerometerI2CWrite(0x28, 0x01); // Z used for activity threshold */ @@ -1000,16 +872,12 @@ //////////////////////////////////////////////////////////////////////////////// void ADT7410Initialize(void){ pc.printf("Initializing ADT7410 Temperature\n\r"); - // Make INT/CRIT pins comp mode and both pins active HIGH - ADT7410Write(0x03, 0x1A); // // Make critical temperature be 24 celsius ADT7410Write(0x08, 0x01); // MSB of Temperature Crit value ADT7410Write(0x09, 0x80); // LSB of Temperature Crit value - // - // Make CRIT pin active high - //ADT7410Write(0x03, 0x08); // Turn INT HIGH, works for the interrupt pin + ADT7410Write(0x03, 0x08); // Turn INT HIGH, works for the interrupt pin pc.printf("\n\n\r"); } //////////////////////////////////////////////////////////////////////////////// @@ -1086,7 +954,7 @@ * 2: Timeout ******************************************************************************/ //////////////////////////////////////////////////////////////////////////////// -void accelerometerI2CWrite(uint8_t hexAddress, uint8_t hexData){ +void accelerometerI2CWrite(int hexAddress, int hexData){ int flag; int registerAddress = hexAddress; @@ -1217,4 +1085,3 @@ } //////////////////////////////////////////////////////////////////////////////// -