111

Dependencies:   BufferedSerial FastPWM mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers commandt.cpp Source File

commandt.cpp

00001 /*
00002  * commandt.cpp
00003  *
00004  *  Created on: 2016. 2. 19.
00005  *      Author: sbh9428
00006  */
00007 
00008 #include "commandt.h"
00009 
00010 command_t::command_t() {
00011     // TODO Auto-generated constructor stub
00012 
00013 }
00014 
00015 command_t::command_t(BufferedSerial* _pc, control_t* _control)
00016 {
00017     time=0;
00018     count=0;
00019     control=_control;
00020     pc=_pc; 
00021 }
00022 
00023 command_t::~command_t() {
00024     // TODO Auto-generated destructor stub
00025 }
00026 
00027 void command_t::clear_data()
00028 {
00029     for(int i=0;i<10;i++)
00030     {
00031         data[i]=0;  
00032     }   
00033 }
00034 void command_t::parse()
00035 {
00036     switch(data[0])
00037     {
00038         case 's':
00039             switch(data[1])
00040             {
00041                 case 'm':
00042                     control->setMode(data[2]-'0');
00043                     printf("set mode %d\n", data[2]-'0');
00044                 break;
00045                 case 'p':
00046                     control->setP(asci_to_bin(data+2));
00047                     printf("set P %f1.5\n", asci_to_bin(data+2));
00048                 break;
00049                 case 'i':
00050                     control->setI(asci_to_bin(data+2));
00051                     printf("set I %f\n", asci_to_bin(data+2));
00052                 break;  
00053                 case 'd':
00054                     control->setD(asci_to_bin(data+2));
00055                     printf("set D %f\n", asci_to_bin(data+2));
00056                 break;  
00057                 case 'o':
00058                     control->setPower(asci_to_bin(data+2));
00059                     printf("set I value %f\n", asci_to_bin(data+2));
00060                 break;  
00061                 case 'a':
00062                     control->setRatio(asci_to_bin(data+2));
00063                     printf("set Ratio %f\n", asci_to_bin(data+2));
00064                 break;  
00065             }
00066         break;
00067         case 'g':
00068             switch(data[1])
00069             {
00070                 case 'm':
00071                     printf("mode is %d\n", control->getMode());
00072                 break;
00073                 case 'p':
00074                     printf("P is %f\n", control->getP());
00075                 break;
00076                 case 'i':
00077                     printf("I is %f\n", control->getI());
00078                 break;
00079                 case 'd':   
00080                     printf("D is %f\n", control->getD());
00081                 break;
00082                 case 'o':
00083                     printf("power is %f\n", control->getPower());
00084                 break;
00085                 case 'a':
00086                     printf("ratio is %f\n", control->getRatio());
00087                 break;
00088             }
00089         
00090         break;  
00091         dafault:
00092         pc->printf("command error");
00093     }
00094     pc->printf("\n");
00095     count=0;
00096 }
00097 
00098 void command_t::get_data()
00099 {
00100     data [count]=pc->getc();
00101 
00102     if(data [count]=='f')
00103     parse();
00104     else
00105     count++;
00106 
00107     if(count>9)
00108     {
00109         count=0;
00110          pc->printf("command error\n");
00111      }
00112 }
00113 
00114 double command_t::asci_to_bin(int *start)
00115 {
00116     double _data=0;
00117     int current=0;
00118 
00119     double nth=1; //�ڸ���
00120     int mode=0;
00121     int sign=1;
00122     
00123     if(*start=='-')
00124     {
00125         current++;  
00126         sign=-1;
00127     }
00128     while(*(start+current)!='f'&&*(start+current)!='.')
00129     {
00130         _data*=10;
00131         _data+=*(start+current)-'0';
00132 
00133         current++;
00134     }
00135     if(*(start+current)=='.')
00136     {
00137         current++;
00138         while(*(start+current)!='f')
00139         {
00140             nth/=10;
00141             _data+=nth*(*(start+current)-'0');
00142             current++;
00143         }   
00144     }
00145     return sign*_data;
00146 }