Help: Finite State Machine for NMEA parsing

Microcontroller Topics
Post Reply
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Help: Finite State Machine for NMEA parsing

Post by Herath » Sun Jun 17, 2012 11:48 pm

I am on a ATMega32 writing a little GPS reciever. The system is almost completed. However, I am thinking of doing some modifications in the way I parse the NMEA sentences. I am thinking of using a state machine so that it could be easier to spot problems and debug.

However, I am thinking that I am going to have to add the other code like printing on the LCD to the machine. And I think that it is logical since I am going to have to wait for the data before printing them to the LCD. I started writing some code and please have a look. Or I just want to know if this is possible on a microcontroller. :)
Some of the function implementations are not included in the code. Just a skelton code.

Code: Select all

typedef enum state{
	Idle,
	RetrieveMsgID,
	RetrieveSentenceData,
	RetrieveChecksum,
	RetrieveEndOfSentence,
	WaitForNextMessage, //Multipart messages, ex: GPGSV
	DisplayData,
	Timeout
} State;

State _state;

void init_NMEA_FSM(){
	_state=Idle;
	
	makeNullTerminating(msgID,6);
	while(1){
		switch(_state){
			case Idle:
			idle();
			break;
			case RetrieveMsgID:
			retrieveMsgId();
			break;
			case RetrieveSentenceData:
			retrieveSentenceData();
			break;
			case RetrieveChecksum:
			break;
			case RetrieveEndOfSentence:
			break;
			case WaitForNextMessage:
			break;
			case DisplayData:
			break;
			case Timeout:
			handleTimeOut();
			break;
		}
	}	
	
}

void idle(){
	while(readFromBuffer()!='$'); //Wait for the $ to come along
	_state=RetrieveMsgID;//Change the machine state
}

void retrieveMsgId(){
	readNextParameter(msgID);
	_state=RetrieveSentenceData;
}

void retrieveSentenceData(){
	if(strcmp(msgID,"GPGLL")==0){//processGLL
		
	}
}
void handleTimeOut(){
	_state=Idle;
}
}
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Re: Help: Finite State Machine for NMEA parsing

Post by SevenZero » Mon Jun 18, 2012 4:07 pm

Yes. It is possible to handle it in this way. May be it would be better on an Atmega such as 32, 64 or 128. On smaller ones with lower hardware stack, it would be slower to handle in this way.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Help: Finite State Machine for NMEA parsing

Post by Herath » Mon Jun 18, 2012 5:57 pm

Thanks. I will try to implement this in FSM. I have another parser working. But I am not happy with it. And the whole thing is also working fine with occasional resetting when the display toggle push button (that I added) is pressed rapidly. I am thinking of adding hardware debouncing circuit to it and see what happens.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Help: Finite State Machine for NMEA parsing

Post by Herath » Wed Jun 20, 2012 4:03 pm

I wrote the code and debugged. It is working quite ok now. The FSM is not fully implemented with timeouts etc.I am not sure if I really want to implement timeouts.
I added the code to my github. Did not check if I could make the repository at unfuddle public.

https://github.com/herath/ATMega-GPS/bl ... nmea_fsm.c

This is my first project on a micro controller. :) . My coding could be awful. And I am open for suggestions and guidance.
Thanks!
Post Reply

Return to “Microcontrollers”