Amit Twik / Mbed OS Amit

Dependencies:   gnss battery-charger-bq24295

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 u-blox
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "gnss.h"
00019 #include "battery_charger_bq24295.h"
00020 #include "onboard_modem_api.h"
00021 #include <SDBlockDevice.h>
00022 #include <FATFileSystem.h>
00023 
00024 #include <string>
00025 #include <iostream>
00026 
00027 using namespace std;
00028 using std::string;
00029 
00030 #define CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2]))
00031 
00032 // Set the minimum input voltage limit for the BQ24295 to 3.8 Volt
00033 #define MIN_INPUT_VOLTAGE_LIMIT_MV  3880
00034 
00035 SDBlockDevice sd(PE_6, PE_5, PE_2, PE_11);
00036 FATFileSystem fs("sd");
00037 
00038 // User LEDs
00039 DigitalOut ledRed(LED1, 1);
00040 DigitalOut ledGreen(LED2, 1);
00041 DigitalOut ledBlue(LED3, 1);
00042 
00043 //GNSS 1V8_MAX IO power
00044 DigitalOut GNSSOn(GNSSEN, 1);
00045 
00046 // Ethernet socket LED 
00047 DigitalOut ledYellow(LED4,1);
00048 
00049 // User Button
00050 #ifdef TARGET_UBLOX_C027
00051     // No user button on C027
00052     InterruptIn userButton(NC);
00053 #else
00054     InterruptIn userButton(SW0);
00055 #endif
00056 
00057 // GNSS
00058 GnssSerial gnss;
00059 
00060 // i2c3 Bus
00061 I2C i2c3(I2C_SDA_B, I2C_SCL_B);
00062     
00063 // Battery Charger BQ24295
00064 BatteryChargerBq24295 charger;
00065 
00066 // Delay between LED changes in second
00067 volatile float delay = 0.5;
00068 
00069 // To check if the user pressed the User Button or not
00070 void threadBodyUserButtonCheck(void const *args){
00071     while (1){
00072         if (userButton.read() == 1 ) {
00073         // User Button is pressed 
00074             delay = 0.1;
00075             //Indicate the button is pressed 
00076             ledYellow = 0;
00077         }
00078         else { 
00079         // User button is released
00080             delay = 0.5;
00081             //Turn off the Yellow LED on Ethernet socket
00082             ledYellow = 1;
00083         }
00084     }
00085 }
00086 
00087 void SDCardWriter(int time)///////////////////////////////////////////////////////////////////////////////
00088 {
00089     printf("\nWaiting for SD Card initialization\n\n");
00090     mkdir("/sd/Amit", 0777);
00091     FILE *fp = fopen("/sd/Amit/sdBlockDeviceTest.txt", "a");
00092     if(fp == NULL) {
00093         printf("\nCould not open file for write\n\n");
00094         return  ;
00095     }
00096     
00097     fprintf(fp, "\nGNSS Time: %d", time);
00098     printf("\nGNSS Time Recorded\n\n");
00099     fclose(fp);
00100 }////////////////////////////////////////////////////////////////////////////////////////////////////////////
00101 
00102 int main()
00103 {
00104     printf("\nu-blox C030\n");
00105     
00106     int count = 30;
00107     int i = 1;
00108     
00109     /////////////////////////////////////////////////////////////////////////////////////SD TEST
00110     string timestring = "test";
00111     sd.init();
00112     fs.mount(&sd);
00113     SDCardWriter(i);
00114     //fs.unmount();
00115     //sd.deinit();
00116     //////////////////////////////////////////////////////////////////////////////////////SD TEST
00117     
00118     GnssSerial gnss;
00119     int gnssReturnCode;
00120     int length;
00121     char buffer[256];
00122 
00123     // GNSS initialisation
00124     if(gnss.init()) {
00125         printf("GNSS initialised.\n\r");
00126     }
00127     else {
00128         printf("GNSS initialisation failure.\n\r");
00129     }
00130    
00131     // The battery charger initialisation
00132     charger.init(&i2c3);   
00133     charger.setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV); 
00134     // Disable the battery charger's watchdog, otherwise it resets the battry charger
00135     charger.setWatchdog(0);
00136    
00137     // Initialised the modem
00138     onboard_modem_init();
00139     
00140     // Power up the modem
00141     onboard_modem_power_up();
00142     
00143     // Create threadUserButtonCheck thread
00144     Thread user_button(osPriorityNormal);
00145     user_button.start(callback(threadBodyUserButtonCheck, (void *)"User Button Thread"));
00146 
00147     
00148     //Set GNSS IO On
00149     GNSSOn = 1;
00150     
00151     // Set the LED states
00152     ledRed = 0;
00153     ledGreen = 1;
00154     ledBlue = 1;
00155     
00156     printf("u-blox C030\n\r");
00157     
00158     //Main loop
00159     while(1) {
00160         wait(delay);
00161         //Shift the LED states
00162         int carry = ledBlue;
00163         ledBlue = ledRed;
00164         ledRed = ledGreen;
00165         ledGreen = carry;
00166         
00167         //Amittttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
00168         
00169         SDCardWriter(i);//////////////////////////////////////////////////////////////////////////
00170         
00171         if(i >= count)
00172         {
00173             fs.unmount();
00174             sd.deinit();
00175         }
00176         
00177         i++;
00178         
00179         gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer));
00180         if (gnssReturnCode > 0) {
00181                 ledGreen = 0;
00182                 ledBlue = 1;
00183                 ledRed = 1;
00184                 length = LENGTH(gnssReturnCode);
00185  
00186                 printf("NMEA: %.*s\n", length - 2, buffer);
00187  
00188                 if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
00189                     // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
00190                     if ((buffer[0] == '$') || buffer[1] == 'G') {
00191                       if (CHECK_TALKER("GLL")) {
00192                             double latitude = 0, longitude = 0;
00193                             char ch;
00194  
00195                             if (gnss.getNmeaAngle(1, buffer, length, latitude) && 
00196                                 gnss.getNmeaAngle(3, buffer, length, longitude) && 
00197                                 gnss.getNmeaItem(6, buffer, length, ch) && (ch == 'A')) {
00198                                 ledBlue = 0;
00199                                 ledRed = 0;
00200                                 ledGreen = 0;
00201  
00202                                 printf("\nGNSS: location is %.5f %.5f.\n\n", latitude, longitude);
00203                                 printf("I am here: https://maps.google.com/?q=%.5f,%.5f\n\n",
00204                                        latitude, longitude); 
00205                             }
00206                         } else if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) {
00207                             double altitude = 0; 
00208                             const char *timeString = NULL;
00209  
00210                             // Altitude
00211                             if (gnss.getNmeaItem(9, buffer, length, altitude)) {
00212                                 printf("\nGNSS: altitude is %.1f m.\n", altitude); 
00213                             }
00214  
00215                             // Time
00216                             timeString = gnss.findNmeaItemPos(1, buffer, buffer + length);
00217                             if (timeString != NULL) {
00218                                 ledBlue = 0;
00219                                 ledRed = 1;
00220                                 ledGreen = 1;
00221  
00222                                 printf("\nGNSS: time is %.6s.\n\n", timeString);
00223                                 
00224                             }
00225                         } else if (CHECK_TALKER("VTG")) {
00226                             double speed = 0; 
00227  
00228                             // Speed
00229                             if (gnss.getNmeaItem(7, buffer, length, speed)) {
00230                                 printf("\nGNSS: speed is %.1f km/h.\n\n", speed);
00231                             }
00232                         }
00233                     }
00234                 }
00235             }
00236         
00237         
00238     }
00239 }
00240 
00241 // End Of File