x

Dependencies:   gnss sd-driver battery-charger-bq24295

Committer:
atwik
Date:
Mon Apr 13 05:27:49 2020 +0000
Revision:
7:8ba0130944d6
Parent:
6:4d61a0f32573
\

Who changed what in which revision?

UserRevisionLine numberNew contents of line
euygun 0:25fcf12b0ba2 1 /* mbed Microcontroller Library
euygun 0:25fcf12b0ba2 2 * Copyright (c) 2017 u-blox
euygun 0:25fcf12b0ba2 3 *
euygun 0:25fcf12b0ba2 4 * Licensed under the Apache License, Version 2.0 (the "License");
euygun 0:25fcf12b0ba2 5 * you may not use this file except in compliance with the License.
euygun 0:25fcf12b0ba2 6 * You may obtain a copy of the License at
euygun 0:25fcf12b0ba2 7 *
euygun 0:25fcf12b0ba2 8 * http://www.apache.org/licenses/LICENSE-2.0
euygun 0:25fcf12b0ba2 9 *
euygun 0:25fcf12b0ba2 10 * Unless required by applicable law or agreed to in writing, software
euygun 0:25fcf12b0ba2 11 * distributed under the License is distributed on an "AS IS" BASIS,
euygun 0:25fcf12b0ba2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
euygun 0:25fcf12b0ba2 13 * See the License for the specific language governing permissions and
euygun 0:25fcf12b0ba2 14 * limitations under the License.
euygun 0:25fcf12b0ba2 15 */
euygun 0:25fcf12b0ba2 16
euygun 0:25fcf12b0ba2 17 #include "mbed.h"
euygun 3:b9051f3f2fcd 18 #include "gnss.h"
euygun 2:c95852ac6953 19 #include "battery_charger_bq24295.h"
euygun 0:25fcf12b0ba2 20 #include "onboard_modem_api.h"
atwik 7:8ba0130944d6 21 #include <SDBlockDevice.h>
atwik 7:8ba0130944d6 22 #include <FATFileSystem.h>
atwik 7:8ba0130944d6 23
atwik 7:8ba0130944d6 24 #include <string>
atwik 7:8ba0130944d6 25 #include <iostream>
atwik 7:8ba0130944d6 26
atwik 7:8ba0130944d6 27 using namespace std;
atwik 7:8ba0130944d6 28 using std::string;
atwik 7:8ba0130944d6 29
atwik 7:8ba0130944d6 30 #define CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2]))
euygun 0:25fcf12b0ba2 31
euygun 2:c95852ac6953 32 // Set the minimum input voltage limit for the BQ24295 to 3.8 Volt
euygun 2:c95852ac6953 33 #define MIN_INPUT_VOLTAGE_LIMIT_MV 3880
euygun 2:c95852ac6953 34
atwik 7:8ba0130944d6 35 SDBlockDevice sd(PE_6, PE_5, PE_2, PE_11);
atwik 7:8ba0130944d6 36 FATFileSystem fs("sd");
atwik 7:8ba0130944d6 37
euygun 0:25fcf12b0ba2 38 // User LEDs
euygun 0:25fcf12b0ba2 39 DigitalOut ledRed(LED1, 1);
euygun 0:25fcf12b0ba2 40 DigitalOut ledGreen(LED2, 1);
euygun 0:25fcf12b0ba2 41 DigitalOut ledBlue(LED3, 1);
euygun 0:25fcf12b0ba2 42
euygun 3:b9051f3f2fcd 43 //GNSS 1V8_MAX IO power
euygun 3:b9051f3f2fcd 44 DigitalOut GNSSOn(GNSSEN, 1);
euygun 3:b9051f3f2fcd 45
euygun 0:25fcf12b0ba2 46 // Ethernet socket LED
euygun 0:25fcf12b0ba2 47 DigitalOut ledYellow(LED4,1);
euygun 0:25fcf12b0ba2 48
euygun 0:25fcf12b0ba2 49 // User Button
euygun 0:25fcf12b0ba2 50 #ifdef TARGET_UBLOX_C027
euygun 0:25fcf12b0ba2 51 // No user button on C027
euygun 0:25fcf12b0ba2 52 InterruptIn userButton(NC);
euygun 0:25fcf12b0ba2 53 #else
euygun 0:25fcf12b0ba2 54 InterruptIn userButton(SW0);
euygun 0:25fcf12b0ba2 55 #endif
euygun 0:25fcf12b0ba2 56
euygun 3:b9051f3f2fcd 57 // GNSS
euygun 3:b9051f3f2fcd 58 GnssSerial gnss;
euygun 3:b9051f3f2fcd 59
euygun 2:c95852ac6953 60 // i2c3 Bus
euygun 2:c95852ac6953 61 I2C i2c3(I2C_SDA_B, I2C_SCL_B);
euygun 2:c95852ac6953 62
euygun 2:c95852ac6953 63 // Battery Charger BQ24295
euygun 2:c95852ac6953 64 BatteryChargerBq24295 charger;
euygun 2:c95852ac6953 65
euygun 0:25fcf12b0ba2 66 // Delay between LED changes in second
euygun 0:25fcf12b0ba2 67 volatile float delay = 0.5;
euygun 0:25fcf12b0ba2 68
euygun 1:e11c75d931b5 69 // To check if the user pressed the User Button or not
euygun 0:25fcf12b0ba2 70 void threadBodyUserButtonCheck(void const *args){
euygun 0:25fcf12b0ba2 71 while (1){
euygun 0:25fcf12b0ba2 72 if (userButton.read() == 1 ) {
euygun 1:e11c75d931b5 73 // User Button is pressed
euygun 0:25fcf12b0ba2 74 delay = 0.1;
euygun 0:25fcf12b0ba2 75 //Indicate the button is pressed
euygun 0:25fcf12b0ba2 76 ledYellow = 0;
euygun 0:25fcf12b0ba2 77 }
euygun 0:25fcf12b0ba2 78 else {
euygun 1:e11c75d931b5 79 // User button is released
euygun 0:25fcf12b0ba2 80 delay = 0.5;
euygun 0:25fcf12b0ba2 81 //Turn off the Yellow LED on Ethernet socket
euygun 0:25fcf12b0ba2 82 ledYellow = 1;
euygun 0:25fcf12b0ba2 83 }
euygun 0:25fcf12b0ba2 84 }
euygun 0:25fcf12b0ba2 85 }
euygun 0:25fcf12b0ba2 86
atwik 7:8ba0130944d6 87 void SDCardWriter(int time)///////////////////////////////////////////////////////////////////////////////
atwik 7:8ba0130944d6 88 {
atwik 7:8ba0130944d6 89 printf("\nWaiting for SD Card initialization\n\n");
atwik 7:8ba0130944d6 90 mkdir("/sd/Amit", 0777);
atwik 7:8ba0130944d6 91 FILE *fp = fopen("/sd/Amit/sdBlockDeviceTest.txt", "a");
atwik 7:8ba0130944d6 92 if(fp == NULL) {
atwik 7:8ba0130944d6 93 printf("\nCould not open file for write\n\n");
atwik 7:8ba0130944d6 94 return ;
atwik 7:8ba0130944d6 95 }
atwik 7:8ba0130944d6 96
atwik 7:8ba0130944d6 97 fprintf(fp, "\nGNSS Time: %d", time);
atwik 7:8ba0130944d6 98 printf("\nGNSS Time Recorded\n\n");
atwik 7:8ba0130944d6 99 fclose(fp);
atwik 7:8ba0130944d6 100 }////////////////////////////////////////////////////////////////////////////////////////////////////////////
euygun 0:25fcf12b0ba2 101
euygun 0:25fcf12b0ba2 102 int main()
euygun 0:25fcf12b0ba2 103 {
atwik 7:8ba0130944d6 104 printf("\nu-blox C030\n");
atwik 7:8ba0130944d6 105
atwik 7:8ba0130944d6 106 int count = 30;
atwik 7:8ba0130944d6 107 int i = 1;
atwik 7:8ba0130944d6 108
atwik 7:8ba0130944d6 109 /////////////////////////////////////////////////////////////////////////////////////SD TEST
atwik 7:8ba0130944d6 110 string timestring = "test";
atwik 7:8ba0130944d6 111 sd.init();
atwik 7:8ba0130944d6 112 fs.mount(&sd);
atwik 7:8ba0130944d6 113 SDCardWriter(i);
atwik 7:8ba0130944d6 114 //fs.unmount();
atwik 7:8ba0130944d6 115 //sd.deinit();
atwik 7:8ba0130944d6 116 //////////////////////////////////////////////////////////////////////////////////////SD TEST
atwik 7:8ba0130944d6 117
atwik 7:8ba0130944d6 118 GnssSerial gnss;
atwik 7:8ba0130944d6 119 int gnssReturnCode;
atwik 7:8ba0130944d6 120 int length;
atwik 7:8ba0130944d6 121 char buffer[256];
euygun 3:b9051f3f2fcd 122
euygun 3:b9051f3f2fcd 123 // GNSS initialisation
euygun 3:b9051f3f2fcd 124 if(gnss.init()) {
euygun 3:b9051f3f2fcd 125 printf("GNSS initialised.\n\r");
euygun 3:b9051f3f2fcd 126 }
euygun 3:b9051f3f2fcd 127 else {
euygun 3:b9051f3f2fcd 128 printf("GNSS initialisation failure.\n\r");
euygun 3:b9051f3f2fcd 129 }
euygun 3:b9051f3f2fcd 130
euygun 2:c95852ac6953 131 // The battery charger initialisation
euygun 2:c95852ac6953 132 charger.init(&i2c3);
euygun 2:c95852ac6953 133 charger.setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV);
euygun 2:c95852ac6953 134 // Disable the battery charger's watchdog, otherwise it resets the battry charger
euygun 2:c95852ac6953 135 charger.setWatchdog(0);
euygun 2:c95852ac6953 136
euygun 1:e11c75d931b5 137 // Initialised the modem
euygun 0:25fcf12b0ba2 138 onboard_modem_init();
euygun 0:25fcf12b0ba2 139
euygun 1:e11c75d931b5 140 // Power up the modem
euygun 0:25fcf12b0ba2 141 onboard_modem_power_up();
euygun 0:25fcf12b0ba2 142
euygun 1:e11c75d931b5 143 // Create threadUserButtonCheck thread
Mudassar Hussain 6:4d61a0f32573 144 Thread user_button(osPriorityNormal);
Mudassar Hussain 6:4d61a0f32573 145 user_button.start(callback(threadBodyUserButtonCheck, (void *)"User Button Thread"));
euygun 0:25fcf12b0ba2 146
euygun 3:b9051f3f2fcd 147
euygun 3:b9051f3f2fcd 148 //Set GNSS IO On
euygun 3:b9051f3f2fcd 149 GNSSOn = 1;
euygun 3:b9051f3f2fcd 150
euygun 1:e11c75d931b5 151 // Set the LED states
euygun 0:25fcf12b0ba2 152 ledRed = 0;
euygun 0:25fcf12b0ba2 153 ledGreen = 1;
euygun 0:25fcf12b0ba2 154 ledBlue = 1;
euygun 0:25fcf12b0ba2 155
atwik 7:8ba0130944d6 156 printf("u-blox C030\n\r");
euygun 0:25fcf12b0ba2 157
euygun 0:25fcf12b0ba2 158 //Main loop
euygun 0:25fcf12b0ba2 159 while(1) {
euygun 0:25fcf12b0ba2 160 wait(delay);
euygun 0:25fcf12b0ba2 161 //Shift the LED states
euygun 0:25fcf12b0ba2 162 int carry = ledBlue;
euygun 0:25fcf12b0ba2 163 ledBlue = ledRed;
euygun 0:25fcf12b0ba2 164 ledRed = ledGreen;
euygun 0:25fcf12b0ba2 165 ledGreen = carry;
atwik 7:8ba0130944d6 166
atwik 7:8ba0130944d6 167 //Amittttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
atwik 7:8ba0130944d6 168
atwik 7:8ba0130944d6 169 SDCardWriter(i);//////////////////////////////////////////////////////////////////////////
atwik 7:8ba0130944d6 170
atwik 7:8ba0130944d6 171 if(i >= count)
atwik 7:8ba0130944d6 172 {
atwik 7:8ba0130944d6 173 fs.unmount();
atwik 7:8ba0130944d6 174 sd.deinit();
atwik 7:8ba0130944d6 175 }
atwik 7:8ba0130944d6 176
atwik 7:8ba0130944d6 177 i++;
atwik 7:8ba0130944d6 178
atwik 7:8ba0130944d6 179 gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer));
atwik 7:8ba0130944d6 180 if (gnssReturnCode > 0) {
atwik 7:8ba0130944d6 181 ledGreen = 0;
atwik 7:8ba0130944d6 182 ledBlue = 1;
atwik 7:8ba0130944d6 183 ledRed = 1;
atwik 7:8ba0130944d6 184 length = LENGTH(gnssReturnCode);
atwik 7:8ba0130944d6 185
atwik 7:8ba0130944d6 186 printf("NMEA: %.*s\n", length - 2, buffer);
atwik 7:8ba0130944d6 187
atwik 7:8ba0130944d6 188 if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
atwik 7:8ba0130944d6 189 // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
atwik 7:8ba0130944d6 190 if ((buffer[0] == '$') || buffer[1] == 'G') {
atwik 7:8ba0130944d6 191 if (CHECK_TALKER("GLL")) {
atwik 7:8ba0130944d6 192 double latitude = 0, longitude = 0;
atwik 7:8ba0130944d6 193 char ch;
atwik 7:8ba0130944d6 194
atwik 7:8ba0130944d6 195 if (gnss.getNmeaAngle(1, buffer, length, latitude) &&
atwik 7:8ba0130944d6 196 gnss.getNmeaAngle(3, buffer, length, longitude) &&
atwik 7:8ba0130944d6 197 gnss.getNmeaItem(6, buffer, length, ch) && (ch == 'A')) {
atwik 7:8ba0130944d6 198 ledBlue = 0;
atwik 7:8ba0130944d6 199 ledRed = 0;
atwik 7:8ba0130944d6 200 ledGreen = 0;
atwik 7:8ba0130944d6 201
atwik 7:8ba0130944d6 202 printf("\nGNSS: location is %.5f %.5f.\n\n", latitude, longitude);
atwik 7:8ba0130944d6 203 printf("I am here: https://maps.google.com/?q=%.5f,%.5f\n\n",
atwik 7:8ba0130944d6 204 latitude, longitude);
atwik 7:8ba0130944d6 205 }
atwik 7:8ba0130944d6 206 } else if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) {
atwik 7:8ba0130944d6 207 double altitude = 0;
atwik 7:8ba0130944d6 208 const char *timeString = NULL;
atwik 7:8ba0130944d6 209
atwik 7:8ba0130944d6 210 // Altitude
atwik 7:8ba0130944d6 211 if (gnss.getNmeaItem(9, buffer, length, altitude)) {
atwik 7:8ba0130944d6 212 printf("\nGNSS: altitude is %.1f m.\n", altitude);
atwik 7:8ba0130944d6 213 }
atwik 7:8ba0130944d6 214
atwik 7:8ba0130944d6 215 // Time
atwik 7:8ba0130944d6 216 timeString = gnss.findNmeaItemPos(1, buffer, buffer + length);
atwik 7:8ba0130944d6 217 if (timeString != NULL) {
atwik 7:8ba0130944d6 218 ledBlue = 0;
atwik 7:8ba0130944d6 219 ledRed = 1;
atwik 7:8ba0130944d6 220 ledGreen = 1;
atwik 7:8ba0130944d6 221
atwik 7:8ba0130944d6 222 printf("\nGNSS: time is %.6s.\n\n", timeString);
atwik 7:8ba0130944d6 223
atwik 7:8ba0130944d6 224 }
atwik 7:8ba0130944d6 225 } else if (CHECK_TALKER("VTG")) {
atwik 7:8ba0130944d6 226 double speed = 0;
atwik 7:8ba0130944d6 227
atwik 7:8ba0130944d6 228 // Speed
atwik 7:8ba0130944d6 229 if (gnss.getNmeaItem(7, buffer, length, speed)) {
atwik 7:8ba0130944d6 230 printf("\nGNSS: speed is %.1f km/h.\n\n", speed);
atwik 7:8ba0130944d6 231 }
atwik 7:8ba0130944d6 232 }
atwik 7:8ba0130944d6 233 }
atwik 7:8ba0130944d6 234 }
atwik 7:8ba0130944d6 235 }
atwik 7:8ba0130944d6 236
atwik 7:8ba0130944d6 237
euygun 0:25fcf12b0ba2 238 }
euygun 0:25fcf12b0ba2 239 }
euygun 0:25fcf12b0ba2 240
euygun 0:25fcf12b0ba2 241 // End Of File