Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:8419f38ed886, 2022-10-28 (annotated)
- Committer:
- michael_antonucci
- Date:
- Fri Oct 28 22:16:36 2022 +0000
- Revision:
- 0:8419f38ed886
Fully working and commented code. GRADE THIS
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| michael_antonucci | 0:8419f38ed886 | 1 | //Michael Antonucci |
| michael_antonucci | 0:8419f38ed886 | 2 | //Last modified Oct. 27, 2022 |
| michael_antonucci | 0:8419f38ed886 | 3 | //Assignment 4, Exercise 1: Create a working code to parse NMEA inputs into human-readable data |
| michael_antonucci | 0:8419f38ed886 | 4 | #include "mbed.h" |
| michael_antonucci | 0:8419f38ed886 | 5 | |
| michael_antonucci | 0:8419f38ed886 | 6 | Serial pc(USBTX, USBRX); |
| michael_antonucci | 0:8419f38ed886 | 7 | |
| michael_antonucci | 0:8419f38ed886 | 8 | char dataque[100]; //calls for an array of 100 characters long with the name "dataque" |
| michael_antonucci | 0:8419f38ed886 | 9 | char header[5]; //calls for an array of 5 characters long with name "header" |
| michael_antonucci | 0:8419f38ed886 | 10 | float lat; //creates float "lat" for latitude |
| michael_antonucci | 0:8419f38ed886 | 11 | float longi; //creates float "longi" for longitude |
| michael_antonucci | 0:8419f38ed886 | 12 | char ns[1]; //creates array of 1 character with name "ns" |
| michael_antonucci | 0:8419f38ed886 | 13 | char ew[1]; //creates array of 1 character with name "ew" |
| michael_antonucci | 0:8419f38ed886 | 14 | float elevation; //creates float "elevation" |
| michael_antonucci | 0:8419f38ed886 | 15 | char checksum[2]; //calls for an array of 2 characters long with name "checksum" |
| michael_antonucci | 0:8419f38ed886 | 16 | int seconds; //creates integer "seconds" for time |
| michael_antonucci | 0:8419f38ed886 | 17 | float xaccel; //creates float "xaccel" for x acceleration |
| michael_antonucci | 0:8419f38ed886 | 18 | float yaccel; //creates float "yaccel" for y acceleration |
| michael_antonucci | 0:8419f38ed886 | 19 | float zaccel; //creates float "zaccel" for z acceleration |
| michael_antonucci | 0:8419f38ed886 | 20 | float temp; //creates float "temp" for temperature |
| michael_antonucci | 0:8419f38ed886 | 21 | int main() { //starts main code |
| michael_antonucci | 0:8419f38ed886 | 22 | while(1) { |
| michael_antonucci | 0:8419f38ed886 | 23 | if (pc.readable()) { |
| michael_antonucci | 0:8419f38ed886 | 24 | pc.scanf("%s",dataque); |
| michael_antonucci | 0:8419f38ed886 | 25 | sscanf(dataque,"$%5s",header); |
| michael_antonucci | 0:8419f38ed886 | 26 | if (!strcmp(header,"GPRMC")) { //if the header is "GPRMC" |
| michael_antonucci | 0:8419f38ed886 | 27 | sscanf(dataque,"$%5s,%d,%f,%1s,%f,%1s,%f,*%2s", //reassigns variables to data |
| michael_antonucci | 0:8419f38ed886 | 28 | header,&seconds,&lat,ns,&longi,ew,&elevation,checksum); |
| michael_antonucci | 0:8419f38ed886 | 29 | pc.printf("GPS NMEA string recieved:\r\n"); //prints type of message |
| michael_antonucci | 0:8419f38ed886 | 30 | pc.printf("Time: %d seconds\r\n",seconds); //prints time |
| michael_antonucci | 0:8419f38ed886 | 31 | pc.printf("Latitude: %f degrees %s",lat,ns); //prints latitude |
| michael_antonucci | 0:8419f38ed886 | 32 | pc.printf("Longitude: %f degrees %s\r\n",longi,ew); //prints longitude |
| michael_antonucci | 0:8419f38ed886 | 33 | pc.printf("Elevation: %f m\r\n",elevation); //prints elevation |
| michael_antonucci | 0:8419f38ed886 | 34 | pc.printf("Checksum Value: %s",checksum); //prints checksum value |
| michael_antonucci | 0:8419f38ed886 | 35 | } |
| michael_antonucci | 0:8419f38ed886 | 36 | else if (!strcmp(header,"IMUPO")) { //if the header is "IMUPO", print a message |
| michael_antonucci | 0:8419f38ed886 | 37 | sscanf(dataque,"$%5s,%d,%f,%f,%f,*%2s", //reassigns variables to data |
| michael_antonucci | 0:8419f38ed886 | 38 | header, |
| michael_antonucci | 0:8419f38ed886 | 39 | &seconds, |
| michael_antonucci | 0:8419f38ed886 | 40 | &xaccel, |
| michael_antonucci | 0:8419f38ed886 | 41 | &yaccel, |
| michael_antonucci | 0:8419f38ed886 | 42 | &zaccel, |
| michael_antonucci | 0:8419f38ed886 | 43 | checksum); |
| michael_antonucci | 0:8419f38ed886 | 44 | pc.printf("Motion Message NMEA string recieved:\r\n"); //prints type of data |
| michael_antonucci | 0:8419f38ed886 | 45 | pc.printf("Time: %d seconds\r\n",seconds); //prints time |
| michael_antonucci | 0:8419f38ed886 | 46 | pc.printf("X-Acceleration: %f m/s^2 \r\n",xaccel); //prints x acceleration |
| michael_antonucci | 0:8419f38ed886 | 47 | pc.printf("Y-Acceleration: %f m/s^2 \r\n",yaccel); //prints y acceleration |
| michael_antonucci | 0:8419f38ed886 | 48 | pc.printf("Z-Acceleration: %f m/s^2 \r\n",zaccel); //prints z acceleration |
| michael_antonucci | 0:8419f38ed886 | 49 | pc.printf("Checksum Value: %2s \r\n",checksum); //prints checksum value |
| michael_antonucci | 0:8419f38ed886 | 50 | } |
| michael_antonucci | 0:8419f38ed886 | 51 | else if (!strcmp(header,"TEMPE")) { //if the header is "TEMPE", print a message |
| michael_antonucci | 0:8419f38ed886 | 52 | sscanf(dataque,"$%5s,%d,%f,*%2s", //reassigns variables to data |
| michael_antonucci | 0:8419f38ed886 | 53 | header, |
| michael_antonucci | 0:8419f38ed886 | 54 | &seconds, |
| michael_antonucci | 0:8419f38ed886 | 55 | &temp, |
| michael_antonucci | 0:8419f38ed886 | 56 | checksum); |
| michael_antonucci | 0:8419f38ed886 | 57 | pc.printf("Temperature NMEA string recieved:\r\n"); //prints type of data |
| michael_antonucci | 0:8419f38ed886 | 58 | pc.printf("Time: %d seconds\r\n",seconds); //prints time |
| michael_antonucci | 0:8419f38ed886 | 59 | pc.printf("Temperature: %f degrees Celcius\r\n",temp); //prints temperature |
| michael_antonucci | 0:8419f38ed886 | 60 | pc.printf("Checksum Value: %2s \r\n",checksum); //prints checksum value |
| michael_antonucci | 0:8419f38ed886 | 61 | } |
| michael_antonucci | 0:8419f38ed886 | 62 | else { //if the header is not one of those three, respond with an error message |
| michael_antonucci | 0:8419f38ed886 | 63 | pc.printf("NOT A VALID NMEA INDICATOR \r\n"); //shows that error is associated with header format |
| michael_antonucci | 0:8419f38ed886 | 64 | } |
| michael_antonucci | 0:8419f38ed886 | 65 | } |
| michael_antonucci | 0:8419f38ed886 | 66 | } |
| michael_antonucci | 0:8419f38ed886 | 67 | } |