GPS Data Parsing - raspberry-pi
I got my hands on a USB DeLORME Earthmate GPA LT-20, I want to use it as part of a mobile GPS ratification unit, Raspberry Pi based. I have been able to access the raw serial data but am at odds with an effective means of parsing the data into a usable format. the current plan is just to have it printed on screen in a meaningful way. just looking at ideas. Bellow is a sampling of the data, i have altered the GPS location data to remove the particular location of testing. Perfer to code in C
I have read the following refrence sites:
http://www.gpsinformation.org/dale/nmea.htm
https://en.wikipedia.org/wiki/List_of_GPS_satellites
$GPRMC,050229.000,A,3XX8.647,N,11XX1.282,W,0.1,0.0,140518,11.7,E*4B
$GPGGA,050229.000,3XX8.64662,N,11XX1.28205,W,1,06,1.5,725.48,M,-28.4,M,,*5D
$GPVTG,0.0,T,11.7,M,0.1,N,0.1,K*79
$GPGSV,3,1,09,10,34,240,34,13,24,054,00,15,47,086,26,16,25,292,30*77
$GPGSV,3,2,09,20,79,310,31,21,65,345,37,26,25,260,00,27,11,320,00*78
$GPGSV,3,3,09,29,46,147,34,,,,,,,,,,,,*4C
$PSTMECH,21,7,20,7,15,7,29,7,10,7,00,0,16,7,00,0,00,0,00,0,00,0,00,0*5C
Looking at this information:
`"$GPRMC,050229.000,A,3008.647,N,11001.282,W,0.1,0.0,140518,11.7,E*4B"`
Use strtok for parsing:
int main(void)
{
FILE *fp = fopen("test.txt", "r");
char buf[256];
char *array[20];
while(fgets(buf, sizeof(buf), fp))
{
if(strstr(buf, "$GPRMC"))
{
int count = 0;
char *token;
token = strtok(buf, ",");
while(token != NULL)
{
array[count++] = token;
token = strtok(NULL, ",");
if(count == 20)
break;
}
printf("Latitude : %s %s\n", array[3], array[4]);
printf("Longitude : %s %s\n", array[5], array[6]);
}
}
return 0;
}
Result:
Latitude : 3008.647 N
Longitude : 11001.282 W
Related
Xilinx Echo Server Data Variable
I want to have my Zedboard return a numeric value using the Xilinx lwIP example as a base but no matter what I do I can't figure out what stores the data received or transmitted. I have found the void type payload but I don't know what to do with it. Snapshot of one instance of payload and a list of lwIP files Below is the closest function to my goal: err_t recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err){ /* do not read the packet if we are not in ESTABLISHED state */ if (!p) { tcp_close(tpcb); tcp_recv(tpcb, NULL); return ERR_OK; } /* indicate that the packet has been received */ tcp_recved(tpcb, p->len); /* echo back the payload */ /* in this case, we assume that the payload is < TCP_SND_BUF */ if (tcp_sndbuf(tpcb) > p->len) { err = tcp_write(tpcb, p->payload, p->len, 1); //I need to change p->paylod but IDK where it is given a value. } else xil_printf("no space in tcp_sndbuf\n\r"); /* free the received pbuf */ pbuf_free(p); return ERR_OK; } Any guidance is appreciated. Thanks, Turtlemii
-I cheated and just made sure that the function has access to Global_tpcb from echo.c -tcp_write() reads in an address and displays each char it seems. void Print_Code() { /* Prepare for TRANSMISSION */ char header[] = "\rSwitch: 1 2 3 4 5 6 7 8\n\r"; //header text char data_t[] = " \n\r\r"; //area for storing the data unsigned char mask = 10000000; //mask to decode switches swc_value = XGpio_DiscreteRead(&SWCInst, 1); //Save switch values /* Write switch values to the LEDs for visual. */ XGpio_DiscreteWrite(&LEDInst, LED_CHANNEL, swc_value); for (int i =0; i<=7; i++) //load data_t with switch values (0/1) { data_t[8+2*i] = '0' + ((swc_value & mask)/mask); //convert one bit to 0/1 mask = mask >> 1;//move to next bit } int len_header = *(&header + 1) - header; //find the length of the header string int len_data = *(&data_t + 1) - data_t; //find the length of the data string tcp_write(Global_tpcb, &header, len_header, 1); //print the header tcp_write(Global_tpcb, &data_t, len_data, 1); //print the data }
Losing values with iterative realloc in C
I am working in C with Netbeans8.0 I have to read files in an iterative approach to get list of words. That is, in single iteration a file is read into an array of strings and then merge this array into a single array. void merge_array(char** a,int* M, char** b,int N) { //............. Add extra memory to a ..............*/ void *tmp = realloc(a, (*M+N) * sizeof(*a)); if (tmp == NULL) { perror("Merging -> Could not reallocate"); exit(EXIT_FAILURE); } a = tmp; memset(a+(*M), 0, N*sizeof(*a)); //............. copy strings in b to a ..............*/ int i,j=0; for(i=*M; i<((*M)+N); i++) { size_t wlen = strlen(b[j]); a[i] = malloc((wlen+1) * sizeof(char)); if (a[i] == NULL) { perror("Failed to replicate string"); exit(EXIT_FAILURE); } memcpy(a[i], b[j], wlen+1); j++; } (*M) = (*M)+N; // resetting the count printf("Confirm - %s, %d\n",a[0],*M); } Above function reads the contents of a file. In main above function is called iteratively and merged into a single array named 'termlist'. Main code is given below char** termlist; int termCount=0; while(files[i]){ char **word_array; int wdCnt,a; char* tmp = (char*) malloc(strlen(path)*sizeof(char)); strcpy(tmp,path); strcat(tmp,files[i]); strcpy(files[i],tmp); printf("\n\n******* Reading file %s...\n",files[i]); word_array = getTerms_fscanf(files[i],&a); //reading contents of file wdCnt = a; if(i==0) // before reading the first file initializing the termlist { termlist = (char**) malloc(wdCnt*sizeof(char*)); } merge_array(termlist,&termCount,word_array,wdCnt); printf("CHECK - %s, %d\n",termlist[0],termCount); free(word_array); ++i; } Now the problem is that, After 1st two iterations, Inside function everything works fine but in main values of termlist[0], termlist[1] turns out to be junk.. That is first 2 words read from first file is lost. The 3rd iteration returns with failure at merge_array function call. Output is ******* Reading F:/Netbeans C/Test Docs/doc1.txt... Confirm - tour, 52 CHECK - tour, 52 ******* Reading F:/Netbeans C/Test Docs/doc2.txt... Confirm - tour, 71 CHECK - Ôk'aÔk'a`œ€`œ€äk'aäk'aìk'aìk'aôk'aôk'aük'aük'ah“€, 71 I am not able to identify problem with this.. Please help with this..
GPS output being incorrectly written to file on SD card- Arduino
I have a sketch to take information (Lat, Long) from an EM-406a GPS receiver and write the information to an SD card on an Arduino shield. The program is as follows: #include <TinyGPS++.h> #include <SoftwareSerial.h> #include <SD.h> TinyGPSPlus gps; SoftwareSerial ss(4, 3); //pins for the GPS Sd2Card card; SdVolume volume; SdFile root; SdFile file; void setup() { Serial.begin(115200); //for the serial output ss.begin(4800); //start ss at 4800 baud Serial.println("gpsLogger by Aaron McRuer"); Serial.println("based on code by Mikal Hart"); Serial.println(); //initialize the SD card if(!card.init(SPI_FULL_SPEED, 9)) { Serial.println("card.init failed"); } //initialize a FAT volume if(!volume.init(&card)){ Serial.println("volume.init failed"); } //open the root directory if(!root.openRoot(&volume)){ Serial.println("openRoot failed"); } //create new file char name[] = "WRITE00.TXT"; for (uint8_t i = 0; i < 100; i++){ name[5] = i/10 + '0'; name[6] = i%10 + '0'; if(file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)){ break; } } if(!file.isOpen()) { Serial.println("file.create"); } file.print("Ready...\n"); } void loop() { bool newData = false; //For one second we parse GPS data and report some key values for (unsigned long start = millis(); millis() - start < 1000;) { while (ss.available()) { char c = ss.read(); //Serial.write(c); //uncomment this line if you want to see the GPS data flowing if(gps.encode(c)) //did a new valid sentence come in? newData = true; } } if(newData) { file.write(gps.location.lat()); file.write("\n"); file.write(gps.location.lng()); file.write("\n"); } file.close(); } When I open up the file on the SD card when the program is finished executing, I get a message that it has an encoding error. I'm currently inside (and unable to get a GPS signal, thus the 0), but the encoding problem needs to be tackled, and there should be as many lines as there are seconds that the device has been on. There's only that one. What do I need to do to make things work correctly here?
Closing the file in the loop, and never reopening it, is the reason there's only one set of data in your file. Are you sure gps.location.lat() and gps.location.lng() return strings, not an integer or float? That would explain the binary data and the "encoding error" you see.
Trying to determine h.264 profile & level pragmatically
Ideally the solution would be in python and cross platform, but that's probably not too likely, so all I require is it work in linux, and I can use a c extension to interface w/python if necessary. I see there is a python binding for ffmpeg which I was thinking about using, however I can't figure out how to determine the profile and level as it is, with fmmpeg or anything else, much less do it pragmatically. Google is not much help on the matter either. I've been able to determine what features I'd be looking for if I needed to determine the profile and levels manually then I can do that, but then that leads to the question, can ffmpeg then determine if the video was encoded with that feature set? I guess what I'm wondering to that effect is, is it perhaps not possible to fully determine the level and specific profile after encoding? I would think you'd have to know in order to decode it, but maybe not; that would explain why I can't find any information on it. I've been toying with this on and off for awhile, but recently decided to consider a project I'd been thinking about, but this is one of this big things holding me back.
Here is a small program I wrote. It prints the profile and level of MP4 files that use h264 as the video codec. You can compile it with the following command line: gcc -std=c99 printProfileAndLevel.c -o printProfileAndLevel Here is the C source : #include <stdio.h> #include <stdlib.h> void printProfile(int profile_idc, int profile_iop, int level_idc) { switch(profile_idc) { case 0x42: printf("Baseline Profile"); break; case 0x4D: printf("Main Profile"); break; case 0x58: printf("Extended Profile"); break; case 0x64: printf("High Profile"); break; default: printf("Unknown profile (%x)", profile_idc); } switch(level_idc) { case 0x15: printf(" # Level 2.1\n"); break; case 0x1F: printf(" # Level 3.1\n"); break; case 0x29: printf(" # Level 4.1\n"); break; case 0x33: printf(" # Level 5.1\n"); break; default: printf(" # unknown level (%x)", level_idc); } } int main(int argc, char* argv[]) { if(argc < 2) { printf("syntax: %s <files>\n", argv[0]); exit(-1); } int buffsize = 1024; char *buffer = malloc(buffsize + 1); for(int nArg = 1; nArg < argc; nArg++) { printf("File %s:\n", argv[nArg]); FILE *file = fopen(argv[nArg], "r+"); if(file == NULL) { printf("Cannot open input file %s\n", argv[nArg]); continue; } int nRead = 0; nRead = fread(buffer, 1, buffsize, file); for(int i = 0; i < nRead - 7; i++) { if(buffer[i] == 0x61 && buffer[i+1] == 0x76 && buffer[i+2] == 0x63 && buffer[i+3] == 0x43) { printProfile(buffer[i+5], buffer[i+6], buffer[i+7]); } } fclose(file); } free(buffer); return 0; }
Basically you need to identify SPS (Sequence Parameter Set) in the bitstream and decode a couple of its leading bytes. See H.264 stream header and links there.
AudioFileWriteBytes fails with error code -40
I'm trying to write raw audio bytes to a file using AudioFileWriteBytes(). Here's what I'm doing: void writeSingleChannelRingBufferDataToFileAsSInt16(AudioFileID audioFileID, AudioConverterRef audioConverter, ringBuffer *rb, SInt16 *holdingBuffer) { // First, figure out which bits of audio we'll be // writing to file from the ring buffer UInt32 lastFreshSample = rb->lastWrittenIndex; OSStatus status; int numSamplesToWrite; UInt32 numBytesToWrite; if (lastFreshSample < rb->lastReadIndex) { numSamplesToWrite = kNumPointsInWave + lastFreshSample - rb->lastReadIndex - 1; } else { numSamplesToWrite = lastFreshSample - rb->lastReadIndex; } numBytesToWrite = numSamplesToWrite*sizeof(SInt16); Then we copy the audio data (stored as floats) to a holding buffer (SInt16) that will be written directly to the file. The copying looks funky because it's from a ring buffer. UInt32 buffLen = rb->sizeOfBuffer - 1; for (int i=0; i < numSamplesToWrite; ++i) { holdingBuffer[i] = rb->data[(i + rb->lastReadIndex) & buffLen]; } Okay, now we actually try to write the audio from the SInt16 buffer "holdingBuffer" to the audio file. The NSLog will spit out an error -40, but also claims that it's writing bytes. No data is written to file. status = AudioFileWriteBytes(audioFileID, NO, 0, &numBytesToWrite, &holdingBuffer); rb->lastReadIndex = lastFreshSample; NSLog(#"Error = %d, wrote %d bytes", status, numBytesToWrite); return; What is this error -40? By the way, everything works fine if I write straight from the ringBuffer to the file. Of course it sounds like junk, because I'm writing floats, not SInt16s, but AudioFileWriteBytes doesn't complain.
The key is to explicitly change the endianness of the incoming data to big endian. All I had to do was wrap CFSwapInt16HostToBig around my data to get: float audioVal = rb->data[(i + rb->lastReadIndex) & buffLen]; holdingBuffer[i] = CFSwapInt16HostToBig((SInt16) audioVal );