jajn HA
/
C027_SupportTest
final
Fork of C027_SupportTest by
Revision 34:2d06e42fac2c, committed 2017-07-20
- Comitter:
- david8251
- Date:
- Thu Jul 20 09:02:49 2017 +0000
- Parent:
- 33:e27f40fada64
- Commit message:
- final
Changed in this revision
--- a/C027_Support.lib Thu Aug 11 07:12:02 2016 +0000 +++ b/C027_Support.lib Thu Jul 20 09:02:49 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/ublox/code/C027_Support/#7b747676de86 \ No newline at end of file +https://developer.mbed.org/users/david8251/code/C027_Support/#b2d68d47a2fb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7455.cpp Thu Jul 20 09:02:49 2017 +0000 @@ -0,0 +1,295 @@ +/****************************************************************************** + * Includes + *****************************************************************************/ + +#include "mbed.h" +#include "mbed_debug.h" + +#include "MMA7455.h" + +/****************************************************************************** + * Defines and typedefs + *****************************************************************************/ + +#define MMA7455_I2C_ADDR (0x1D << 1) + +#define MMA7455_ADDR_XOUTL 0x00 +#define MMA7455_ADDR_XOUTH 0x01 +#define MMA7455_ADDR_YOUTL 0x02 +#define MMA7455_ADDR_YOUTH 0x03 +#define MMA7455_ADDR_ZOUTL 0x04 +#define MMA7455_ADDR_ZOUTH 0x05 +#define MMA7455_ADDR_XOUT8 0x06 +#define MMA7455_ADDR_YOUT8 0x07 +#define MMA7455_ADDR_ZOUT8 0x08 +#define MMA7455_ADDR_STATUS 0x09 +#define MMA7455_ADDR_DETSRC 0x0A +#define MMA7455_ADDR_TOUT 0x0B +#define MMA7455_ADDR_I2CAD 0x0D +#define MMA7455_ADDR_USRINF 0x0E +#define MMA7455_ADDR_WHOAMI 0x0F +#define MMA7455_ADDR_XOFFL 0x10 +#define MMA7455_ADDR_XOFFH 0x11 +#define MMA7455_ADDR_YOFFL 0x12 +#define MMA7455_ADDR_YOFFH 0x13 +#define MMA7455_ADDR_ZOFFL 0x14 +#define MMA7455_ADDR_ZOFFH 0x15 +#define MMA7455_ADDR_MCTL 0x16 +#define MMA7455_ADDR_INTRST 0x17 +#define MMA7455_ADDR_CTL1 0x18 +#define MMA7455_ADDR_CTL2 0x19 +#define MMA7455_ADDR_LDTH 0x1A +#define MMA7455_ADDR_PDTH 0x1B +#define MMA7455_ADDR_PW 0x1C +#define MMA7455_ADDR_LT 0x1D +#define MMA7455_ADDR_TW 0x1E + +#define MMA7455_MCTL_MODE(m) ((m) << 0) +#define MMA7455_MCTL_GLVL(g) ((g) << 2) + +#define MMA7455_STATUS_DRDY (1 << 0) +#define MMA7455_STATUS_DOVR (1 << 1) +#define MMA7455_STATUS_PERR (1 << 2) + + +MMA7455::MMA7455(PinName sda, PinName scl) : _i2c(sda, scl) +{ + _mode = ModeStandby; + _range = Range_8g; + + _xOff = 0; + _yOff = 0; + _zOff = 0; +} + +bool MMA7455::setMode(Mode mode) { + bool result = false; + int mCtrl = 0; + + do { + mCtrl = getModeControl(); + if (mCtrl < 0) break; + + mCtrl &= ~(0x03 << 0); + mCtrl |= MMA7455_MCTL_MODE(mode); + + if (setModeControl((uint8_t)mCtrl) < 0) { + break; + } + + _mode = mode; + result = true; + } while(0); + + + + return result; +} + +bool MMA7455::setRange(Range range) { + bool result = false; + int mCtrl = 0; + + do { + mCtrl = getModeControl(); + if (mCtrl < 0) break; + + mCtrl &= ~(0x03 << 2); + mCtrl |= MMA7455_MCTL_GLVL(range); + + if (setModeControl((uint8_t)mCtrl) < 0) { + break; + } + + _range = range; + result = true; + } while(0); + + + + return result; + +} + +bool MMA7455::read(int32_t& x, int32_t& y, int32_t& z) { + bool result = false; + + + // nothing to read in standby mode + if (_mode == ModeStandby) return false; + + // wait for ready flag + int status = 0; + do { + status = getStatus(); + } while (status >= 0 && (status & MMA7455_STATUS_DRDY) == 0); + + + do { + if (status < 0) break; + + + char buf[6]; + buf[0] = MMA7455_ADDR_XOUTL; + if (_i2c.write(MMA7455_I2C_ADDR, buf, 1) != 0) break; + if (_i2c.read(MMA7455_I2C_ADDR, buf, 6) != 0) break; + + // check if second bit is set in high byte -> negative value + // expand negative value to full byte + if (buf[1] & 0x02) buf[1] |= 0xFC; + if (buf[3] & 0x02) buf[3] |= 0xFC; + if (buf[5] & 0x02) buf[5] |= 0xFC; + + x = (int16_t)((buf[1] << 8) | buf[0]) + _xOff; + y = (int16_t)((buf[3] << 8) | buf[2]) + _yOff; + z = (int16_t)((buf[5] << 8) | buf[4]) + _zOff; + + + result = true; + + } while(0); + + + return result; +} + +bool MMA7455::calibrate() { + bool result = false; + bool failed = false; + + int32_t x = 0; + int32_t y = 0; + int32_t z = 0; + + int32_t xr = 0; + int32_t yr = 0; + int32_t zr = 0; + + int xOff = 0; + int yOff = 0; + int zOff = 16; + if (_range == Range_2g) { + zOff = 64; + } + if (_range == Range_4g) { + zOff = 32; + } + + do { + + // get an average of 6 values + for (int i = 0; i < 6; i++) { + if (!read(xr, yr, zr)) { + failed = true; + break; + } + x += xr; + y += yr; + z += zr; + + wait_ms(100); + } + + if (failed) break; + x /= 6; + y /= 6; + z /= 6; + + xOff -= x; + yOff -= y; + zOff -= z; + + /* + * For some reason we have not got correct/reliable calibration + * by using the offset drift registers. Instead we are + * calculating the offsets and store them in member variables. + * + * These member variables are then used in the read() method + */ + + _xOff = xOff; + _yOff = yOff; + _zOff = zOff; + + + result = true; + + } while (0); + + + + return result; +} + +bool MMA7455::setCalibrationOffsets(int32_t xOff, int32_t yOff, int32_t zOff) { + _xOff = xOff; + _yOff = yOff; + _zOff = zOff; + + return true; +} + +bool MMA7455::getCalibrationOffsets(int32_t& xOff, int32_t& yOff, int32_t& zOff) { + xOff = _xOff; + yOff = _yOff; + zOff = _zOff; + + return true; +} + +int MMA7455::getStatus() { + int result = -1; + char data[1]; + + do { + data[0] = MMA7455_ADDR_STATUS; + if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break; + + if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break; + + result = data[0]; + + } while (0); + + + + return result; +} + +int MMA7455::getModeControl() { + + int result = -1; + char data[1]; + + do { + data[0] = MMA7455_ADDR_MCTL; + if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break; + + if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break; + + result = data[0]; + + } while (0); + + + + return result; +} + +int MMA7455::setModeControl(uint8_t mctl) { + int result = -1; + char data[2]; + + do { + data[0] = MMA7455_ADDR_MCTL; + data[1] = (char)mctl; + if (_i2c.write(MMA7455_I2C_ADDR, data, 2) != 0) break; + + result = 0; + + } while (0); + + + + return result; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7455.h Thu Jul 20 09:02:49 2017 +0000 @@ -0,0 +1,93 @@ +#ifndef MMA7455_H +#define MMA7455_H + + +/** + * Freescale Accelerometer MMA7455. + */ +class MMA7455 { +public: + + enum Mode { + ModeStandby = 0, + ModeMeasurement = 1, + }; + + /** Acceleration range */ + enum Range { + Range_8g = 0, + Range_2g = 1, + Range_4g = 2 + }; + + /** + * Create an interface to the MMA7455 accelerometer + * + * @param sda I2C data line pin + * @param scl I2C clock line pin + */ + MMA7455(PinName sda, PinName scl); + + bool setMode(Mode mode); + bool setRange(Range range); + + bool read(int32_t& x, int32_t& y, int32_t& z); + + /** + * Calibrate for 0g, that is, calculate offset to achieve + * 0g values when accelerometer is placed on flat surface. + * + * Please make sure the accelerometer is placed on a flat surface before + * calling this function. + * + * @return true if request was successful; otherwise false + */ + bool calibrate(); + + /** + * Get calculated offset values. Offsets will be calculated by the + * calibrate() method. + * + * Use these values and put them in persistent storage to avoid + * having to calibrate the accelerometer after a reset/power cycle. + * + * @param xOff x offset is written to this argument + * @param yOff y offset is written to this argument + * @param zOff z offset is written to this argument + * + * @return true if request was successful; otherwise false + */ + bool getCalibrationOffsets(int32_t& xOff, int32_t& yOff, int32_t& zOff); + + /** + * Set calibration offset values. These values should normally + * at one point in time have been retrieved by calling the + * getCalibrationOffsets method. + * + * + * @param xOff x offset + * @param yOff y offset + * @param zOff z offset + * + * @return true if request was successful; otherwise false + */ + bool setCalibrationOffsets(int32_t xOff, int32_t yOff, int32_t zOff); + + + +private: + + I2C _i2c; + Mode _mode; + Range _range; + int32_t _xOff; + int32_t _yOff; + int32_t _zOff; + + int getStatus(); + int getModeControl(); + int setModeControl(uint8_t mctl); + +}; + +#endif
--- a/main.cpp Thu Aug 11 07:12:02 2016 +0000 +++ b/main.cpp Thu Jul 20 09:02:49 2017 +0000 @@ -1,61 +1,51 @@ #include "mbed.h" +#include "MMA7455.h" +#include "MDM.h" -//------------------------------------------------------------------------------------ -/* This example was tested on C027-U20 and C027-G35 with the on board modem. - - Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON - is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this - configuration the following platforms were tested (it is likely that others - will work as well) - - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD) - - NXP: LPC1549v2, LPC4088qsb - - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F - - STM: NUCLEO-F401RE, NUCLEO-F030R8 - mount resistors SB13/14 1k, SB62/63 0R -*/ -#include "GPS.h" -#include "MDM.h" -//------------------------------------------------------------------------------------ -// You need to configure these cellular modem / SIM parameters. -// These parameters are ignored for LISA-C200 variants and can be left NULL. -//------------------------------------------------------------------------------------ -//! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. +#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253 + #define SIMPIN "1922" -/*! The APN of your network operator SIM, sometimes it is "internet" check your - contract with the network operator. You can also try to look-up your settings in - google: https://www.google.de/search?q=APN+list */ + #define APN NULL //! Set the user name for your APN, or NULL if not needed #define USERNAME NULL //! Set the password for your APN, or NULL if not needed #define PASSWORD NULL -//------------------------------------------------------------------------------------ + +MMA7455 acc(p32,p31); + +void task_MMA(); -//#define CELLOCATE +double a,b,c; +char str[90]; +char str1[90]; +char str2[90]; int main(void) { + acc.setMode(MMA7455::ModeMeasurement); + acc.setRange(MMA7455::Range_2g); + acc.calibrate(); + int ret; #ifdef LARGE_DATA char buf[2048] = ""; #else char buf[512] = ""; #endif - - // Create the GPS object -#if 1 // use GPSI2C class - GPSI2C gps; -#else // or GPSSerial class - GPSSerial gps; -#endif - // Create the modem object - MDMSerial mdm; // use mdm(D1,D0) if you connect the cellular shield to a C027 - //mdm.setDebug(4); // enable this for debugging issues - // initialize the modem + + MDMSerial mdm; MDMParser::DevStatus devStatus = {}; MDMParser::NetStatus netStatus = {}; bool mdmOk = mdm.init(SIMPIN, &devStatus); mdm.dumpDevStatus(&devStatus); + + + + task_MMA(); + + + if (mdmOk) { #if 0 // file system API @@ -71,8 +61,6 @@ mdm.delFile(filename); } #endif - - // wait until we are connected mdmOk = mdm.registerNet(&netStatus); mdm.dumpNetStatus(&netStatus); } @@ -90,10 +78,9 @@ if (socket >= 0) { mdm.socketSetBlocking(socket, 10000); - if (mdm.socketConnect(socket, "mbed.org", 80)) + if (mdm.socketConnect(socket, "140.118.206.213", 555)) { - const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; - mdm.socketSend(socket, http, sizeof(http)-1); + mdm.socketSend(socket, str, sizeof(str)-1); ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); if (ret > 0) @@ -175,20 +162,12 @@ mdm.disconnect(); } - // http://www.geckobeach.com/cellular/secrets/gsmcodes.php - // http://de.wikipedia.org/wiki/USSD-Codes const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION" printf("Ussd Send Command %s\r\n", ussd); ret = mdm.ussdCommand(ussd, buf); if (ret > 0) printf("Ussd Got Answer: \"%s\"\r\n", buf); } - - printf("SMS and GPS Loop\r\n"); - char link[128] = ""; - unsigned int i = 0xFFFFFFFF; - const int wait = 100; - bool abort = false; #ifdef CELLOCATE const int sensorMask = 3; // Hybrid: GNSS + CellLocate const int timeoutMargin = 5; // seconds @@ -198,106 +177,52 @@ bool cellLocWait = false; MDMParser::CellLocData loc; - //Token can be released from u-blox site, when you got one replace "TOKEN" below if (!mdm.cellLocSrvHttp("TOKEN")) mdm.cellLocSrvUdp(); mdm.cellLocConfigSensor(1); // Deep scan mode - //mdm.cellUnsolIndication(1); + #endif - //DigitalOut led(LED1); - while (!abort) { - // led = !led; -#ifndef CELLOCATE - while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) - { - int len = LENGTH(ret); - //printf("NMEA: %.*s\r\n", len-2, msg); - if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) - { - // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS - if ((buf[0] == '$') || buf[1] == 'G') { - #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) - if (_CHECK_TALKER("GLL")) { - double la = 0, lo = 0; - char ch; - if (gps.getNmeaAngle(1,buf,len,la) && - gps.getNmeaAngle(3,buf,len,lo) && - gps.getNmeaItem(6,buf,len,ch) && ch == 'A') - { - printf("GPS Location: %.5f %.5f\r\n", la, lo); - sprintf(link, "I am here!\n" - "https://maps.google.com/?q=%.5f,%.5f", la, lo); - } - } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) { - double a = 0; - if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] - printf("GPS Altitude: %.1f\r\n", a); - } else if (_CHECK_TALKER("VTG")) { - double s = 0; - if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] - printf("GPS Speed: %.1f\r\n", s); - } - } - } - } -#endif -#ifdef CELLOCATE - if (mdmOk && (j++ == submitPeriod * 1000/wait)) { - j=0; - printf("CellLocate Request\r\n"); - mdm.cellLocRequest(sensorMask, submitPeriod-timeoutMargin, targetAccuracy); - cellLocWait = true; - } - if (cellLocWait && mdm.cellLocGet(&loc)){ - cellLocWait = false; - printf("CellLocate position received, sensor_used: %d, \r\n", loc.sensorUsed ); - printf(" latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", loc.latitue, loc.longitude, loc.altitutude); - if (loc.sensorUsed == 1) - printf(" uncertainty: %d, speed: %d, direction: %d, vertical_acc: %d, satellite used: %d \r\n", loc.uncertainty,loc.speed,loc.direction,loc.verticalAcc,loc.svUsed); - if (loc.sensorUsed == 1 || loc.sensorUsed == 2) - sprintf(link, "I am here!\n" - "https://maps.google.com/?q=%.5f,%.5f", loc.latitue, loc.longitude); - } - if (cellLocWait && (j%100 == 0 )) - printf("Waiting for CellLocate...\r\n"); -#endif - if (mdmOk && (i++ == 5000/wait)) { - i = 0; - // check the network status - if (mdm.checkNetStatus(&netStatus)) { - mdm.dumpNetStatus(&netStatus, fprintf, stdout); - } - - // checking unread sms - int ix[8]; - int n = mdm.smsList("REC UNREAD", ix, 8); - if (8 < n) n = 8; - while (0 < n--) - { - char num[32]; - printf("Unread SMS at index %d\r\n", ix[n]); - if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) { - printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf); - printf("Delete SMS at index %d\r\n", ix[n]); - mdm.smsDelete(ix[n]); - // provide a reply - const char* reply = "Hello my friend"; - if (strstr(buf, /*w*/"here are you")) - reply = *link ? link : "I don't know"; // reply wil location link - else if (strstr(buf, /*s*/"hutdown")) - abort = true, reply = "bye bye"; - printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num); - mdm.smsSend(num, reply); - } - } - } -#ifdef RTOS_H - Thread::wait(wait); -#else - ::wait_ms(wait); -#endif - } - gps.powerOff(); mdm.powerOff(); return 0; } +void task_MMA(){ + + + char filename[64]; + int32_t x,y,z; + double x1,y1,z1; + double xg,yg,zg; + double xd,yd,zd; + acc.read(x,y,z); + + xg=x*0.016; //實際g值 + yg=y*0.016; //實際g值 + zg=z*0.016; //實際g值 + + x1=xg/(sqrt(pow(yg,2.0)+pow(zg,2.0))); + y1=yg/(sqrt(pow(xg,2.0)+pow(zg,2.0))); + z1=(sqrt(pow(yg,2.0)+pow(xg,2.0)))/zg; + + xd=atan(x1); //徑度 + yd=atan(y1); //徑度 + zd=atan(z1); //徑度 + + a=xd*180.0/PI; + b=yd*180.0/PI; + c=zd*180.0/PI; + + sprintf(str,"%.2f\t",a); + sprintf(str1,"%.2f\t",b); + sprintf(str2,"%.2f",c); + + strcat(str,str1); + strcat(str,str2); + + puts(str); + + + printf("xd: %.2f ",xd*180.0/PI); + printf("yd: %.2f ",yd*180.0/PI); + printf("zd: %.2f ",zd*180.0/PI); + printf("\r\n\n"); +} \ No newline at end of file