stewartplatform

Dependencies:   mbed

Fork of PwmOut_HelloWorld by Mbed

src/usart.cpp

Committer:
wheels
Date:
2016-05-07
Revision:
2:50062ac8646d
Child:
3:c5ea5b6a7460

File content as of revision 2:50062ac8646d:

#include "usart.h"

Serial usart(USBTX, USBRX);

char positionTemp[7];
char position[6];
bool instructionReceived = false;

//serial interupt service function
//ͨÐÅЭÒé£bpositionTemp = {0xff, ID, angle, 0xEE}
// angle·¶Î§£º[0,180]£¬¶ÔÓ¦[-90, 90]
typedef enum
{
    CheckS,CheckW,Checka,Checkb,Checkc,Checkd,Checke,Checkf,CheckSum
}STATE;

void usart_ISR() {
        //if (usart.readable()) 
        {
            char data = usart.getc();
            //usart.printf("%d \n", data);
            static STATE State = CheckS;
            switch(State)
            {
               case CheckS:
                    if(data==0XFF) {
                        State=CheckW;
                        //usart.printf("receive S\n");
                        //instructionReceived = false;   
                    }   
                    else
                        State=CheckS;
                    break;
               case CheckW:
                    if(data==0XFE){
                        State=Checka;
                        //usart.printf("receive W\n");
                        }
                    else if(data==0XFF)
                        State=CheckW;
                    else 
                        State=CheckS;
                    break;
                case Checka:
                    positionTemp[0]=data;
                    State=Checkb;
                    //usart.printf("receive pos 0 \n");
                    break;
                case Checkb:
                    positionTemp[1]=data;
                    State=Checkc;
                    //usart.printf("receive pos 1 \n");                    
                    break;
                case Checkc:
                    positionTemp[2]=data;
                    //usart.printf("receive pos 2 \n");
                    State=Checkd;
                    break;
                case Checkd:
                    positionTemp[3]=data;
                     //usart.printf("receive pos 3 \n");
                   State=Checke;
                    break;
                case Checke:
                    positionTemp[4]=data;
                    //usart.printf("receive pos 4 \n");
                    State=Checkf;
                    break;
                case Checkf:
                    positionTemp[5]=data;
                    //usart.printf("receive pos 5 \n");
                    State=CheckSum;
                    break;
                case CheckSum:
                    positionTemp[6]=data;
                    //usart.printf("receive sum \n");
                   //if(positionTemp[6]==(char)(positionTemp[0]+positionTemp[1]+positionTemp[2]+positionTemp[3]+positionTemp[4]+positionTemp[5]))
                        {
                            position[0]    = positionTemp[0];
                            position[1]    = positionTemp[1];
                            position[2]    = positionTemp[2];
                            position[3]    = positionTemp[3];
                            position[4]    = positionTemp[4];
                            position[5]    = positionTemp[5];
                            instructionReceived = true;
                        }
                    State=CheckS;
                    break;
                default:
                    State=CheckS;
                    //instructionReceived = false;
                    break;
            }

        }
}


void usart_init(int baud) {
      usart.baud(baud);
    usart.attach(&usart_ISR);                                                           // attach serial interupt service function
    wait(1);
    //usart.printf("this is a testing code \n");
}