dd

Dependencies:   BufferedSerial FastAnalogIn FastPWM mbed SHT75

Committer:
sbh9428
Date:
Fri Mar 25 05:55:07 2016 +0000
Revision:
4:7ca449fca19b
Parent:
3:72644690e2e6
Child:
10:c751a0e8b7f9
r

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sbh9428 0:9bfc4aea91e2 1 /*
sbh9428 0:9bfc4aea91e2 2 * commandt.cpp
sbh9428 0:9bfc4aea91e2 3 *
sbh9428 0:9bfc4aea91e2 4 * Created on: 2016. 2. 19.
sbh9428 0:9bfc4aea91e2 5 * Author: sbh9428
sbh9428 0:9bfc4aea91e2 6 */
sbh9428 0:9bfc4aea91e2 7
sbh9428 0:9bfc4aea91e2 8 #include "commandt.h"
sbh9428 0:9bfc4aea91e2 9
sbh9428 0:9bfc4aea91e2 10 command_t::command_t() {
sbh9428 0:9bfc4aea91e2 11 // TODO Auto-generated constructor stub
sbh9428 0:9bfc4aea91e2 12
sbh9428 0:9bfc4aea91e2 13 }
sbh9428 0:9bfc4aea91e2 14
sbh9428 0:9bfc4aea91e2 15 command_t::command_t(BufferedSerial* _pc, control_t* _control)
sbh9428 0:9bfc4aea91e2 16 {
sbh9428 1:5c42ec7f1aeb 17 time=0;
sbh9428 0:9bfc4aea91e2 18 count=0;
sbh9428 0:9bfc4aea91e2 19 control=_control;
sbh9428 0:9bfc4aea91e2 20 pc=_pc;
sbh9428 0:9bfc4aea91e2 21 pc->baud(115200);
sbh9428 0:9bfc4aea91e2 22 }
sbh9428 0:9bfc4aea91e2 23
sbh9428 0:9bfc4aea91e2 24 command_t::~command_t() {
sbh9428 0:9bfc4aea91e2 25 // TODO Auto-generated destructor stub
sbh9428 0:9bfc4aea91e2 26 }
sbh9428 0:9bfc4aea91e2 27
sbh9428 0:9bfc4aea91e2 28 void command_t::clear_data()
sbh9428 0:9bfc4aea91e2 29 {
sbh9428 0:9bfc4aea91e2 30 for(int i=0;i<10;i++)
sbh9428 0:9bfc4aea91e2 31 {
sbh9428 0:9bfc4aea91e2 32 data[i]=0;
sbh9428 0:9bfc4aea91e2 33 }
sbh9428 0:9bfc4aea91e2 34 }
sbh9428 0:9bfc4aea91e2 35 void command_t::parse()
sbh9428 0:9bfc4aea91e2 36 {
sbh9428 0:9bfc4aea91e2 37 switch(data[0])
sbh9428 0:9bfc4aea91e2 38 {
sbh9428 0:9bfc4aea91e2 39 case 's':
sbh9428 0:9bfc4aea91e2 40 switch(data[1])
sbh9428 0:9bfc4aea91e2 41 {
sbh9428 0:9bfc4aea91e2 42 case 'm':
sbh9428 0:9bfc4aea91e2 43 control->set_mode(data[2]-'0');
sbh9428 1:5c42ec7f1aeb 44 printf("set mode %d\n", data[2]-'0');
sbh9428 0:9bfc4aea91e2 45 break;
sbh9428 1:5c42ec7f1aeb 46 case 'r':
sbh9428 0:9bfc4aea91e2 47 control->control_PWM(asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 48 control->set_PWM_value(asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 49 printf("set PWM duty ratio %f1.5\n", asci_to_bin(data+2));
sbh9428 0:9bfc4aea91e2 50 break;
sbh9428 0:9bfc4aea91e2 51 case 't':
sbh9428 1:5c42ec7f1aeb 52 control->set_target_temp(asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 53 printf("set target temp %f\n", asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 54 break;
sbh9428 1:5c42ec7f1aeb 55 case 'p':
sbh9428 1:5c42ec7f1aeb 56 control->set_P_value(asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 57 printf("set P value %f\n", asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 58 break;
sbh9428 1:5c42ec7f1aeb 59 case 'i':
sbh9428 1:5c42ec7f1aeb 60 control->set_I_value(asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 61 printf("set I value %f\n", asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 62 break;
sbh9428 1:5c42ec7f1aeb 63 case 'd':
sbh9428 1:5c42ec7f1aeb 64 control->set_D_value(asci_to_bin(data+2));
sbh9428 1:5c42ec7f1aeb 65 printf("set D value %f\n", asci_to_bin(data+2));
sbh9428 0:9bfc4aea91e2 66 break;
sbh9428 3:72644690e2e6 67 case 'e':
sbh9428 3:72644690e2e6 68 control->set_period((int)asci_to_bin(data+2));
sbh9428 3:72644690e2e6 69 printf("set period %d\n", (int)asci_to_bin(data+2));
sbh9428 3:72644690e2e6 70 break;
sbh9428 3:72644690e2e6 71 default:
sbh9428 3:72644690e2e6 72 pc->printf("command error");
sbh9428 0:9bfc4aea91e2 73 }
sbh9428 0:9bfc4aea91e2 74 break;
sbh9428 0:9bfc4aea91e2 75 case 'g':
sbh9428 1:5c42ec7f1aeb 76 switch(data[1])
sbh9428 1:5c42ec7f1aeb 77 {
sbh9428 1:5c42ec7f1aeb 78 case 'm':
sbh9428 1:5c42ec7f1aeb 79 printf("mode is %d\n", control->get_mode());
sbh9428 1:5c42ec7f1aeb 80 break;
sbh9428 1:5c42ec7f1aeb 81 case 'r':
sbh9428 1:5c42ec7f1aeb 82 printf("duty ratio is %f\n", control->get_PWM_value());
sbh9428 1:5c42ec7f1aeb 83 break;
sbh9428 1:5c42ec7f1aeb 84 case 't':
sbh9428 1:5c42ec7f1aeb 85 printf("target temp is %f\n", control->get_target_temp());
sbh9428 1:5c42ec7f1aeb 86 break;
sbh9428 1:5c42ec7f1aeb 87 case 'p':
sbh9428 1:5c42ec7f1aeb 88 printf("P value is %f\n", control->get_P_value());
sbh9428 1:5c42ec7f1aeb 89 break;
sbh9428 1:5c42ec7f1aeb 90 case 'i':
sbh9428 1:5c42ec7f1aeb 91 printf("I value is %f\n", control->get_I_value());
sbh9428 1:5c42ec7f1aeb 92 break;
sbh9428 1:5c42ec7f1aeb 93 case 'd':
sbh9428 1:5c42ec7f1aeb 94 printf("D value is %f\n", control->get_P_value());
sbh9428 1:5c42ec7f1aeb 95 break;
sbh9428 3:72644690e2e6 96 case 'e':
sbh9428 3:72644690e2e6 97 printf("Period is %d\n", control->get_period());
sbh9428 3:72644690e2e6 98 break;
sbh9428 3:72644690e2e6 99 case 'a':
sbh9428 3:72644690e2e6 100 control->print_table();
sbh9428 3:72644690e2e6 101 break;
sbh9428 3:72644690e2e6 102 default:
sbh9428 3:72644690e2e6 103 pc->printf("command error");
sbh9428 1:5c42ec7f1aeb 104 }
sbh9428 0:9bfc4aea91e2 105
sbh9428 0:9bfc4aea91e2 106 break;
sbh9428 0:9bfc4aea91e2 107 dafault:
sbh9428 0:9bfc4aea91e2 108 pc->printf("command error");
sbh9428 0:9bfc4aea91e2 109 }
sbh9428 0:9bfc4aea91e2 110 pc->printf("\n");
sbh9428 1:5c42ec7f1aeb 111 count=0;
sbh9428 0:9bfc4aea91e2 112 }
sbh9428 0:9bfc4aea91e2 113
sbh9428 0:9bfc4aea91e2 114 void command_t::get_data()
sbh9428 0:9bfc4aea91e2 115 {
sbh9428 0:9bfc4aea91e2 116 data [count]=pc->getc();
sbh9428 0:9bfc4aea91e2 117
sbh9428 0:9bfc4aea91e2 118 if(data [count]=='f')
sbh9428 0:9bfc4aea91e2 119 parse();
sbh9428 0:9bfc4aea91e2 120 else
sbh9428 0:9bfc4aea91e2 121 count++;
sbh9428 0:9bfc4aea91e2 122
sbh9428 0:9bfc4aea91e2 123 if(count>9)
sbh9428 0:9bfc4aea91e2 124 {
sbh9428 0:9bfc4aea91e2 125 count=0;
sbh9428 0:9bfc4aea91e2 126 pc->printf("command error\n");
sbh9428 0:9bfc4aea91e2 127 }
sbh9428 0:9bfc4aea91e2 128 }
sbh9428 0:9bfc4aea91e2 129
sbh9428 0:9bfc4aea91e2 130 void command_t::refreshPWM()
sbh9428 0:9bfc4aea91e2 131 {
sbh9428 4:7ca449fca19b 132 time=time+3;
sbh9428 1:5c42ec7f1aeb 133 control->refresh_PWM();
sbh9428 0:9bfc4aea91e2 134 }
sbh9428 0:9bfc4aea91e2 135
sbh9428 0:9bfc4aea91e2 136 double command_t::asci_to_bin(int *start)
sbh9428 0:9bfc4aea91e2 137 {
sbh9428 0:9bfc4aea91e2 138 double _data=0;
sbh9428 0:9bfc4aea91e2 139 int current=0;
sbh9428 0:9bfc4aea91e2 140
sbh9428 0:9bfc4aea91e2 141 double nth=1; //�ڸ���
sbh9428 0:9bfc4aea91e2 142 int mode=0;
sbh9428 0:9bfc4aea91e2 143 int sign=1;
sbh9428 0:9bfc4aea91e2 144
sbh9428 0:9bfc4aea91e2 145 if(*start=='-')
sbh9428 0:9bfc4aea91e2 146 {
sbh9428 0:9bfc4aea91e2 147 current++;
sbh9428 0:9bfc4aea91e2 148 sign=-1;
sbh9428 0:9bfc4aea91e2 149 }
sbh9428 0:9bfc4aea91e2 150 while(*(start+current)!='f'&&*(start+current)!='.')
sbh9428 0:9bfc4aea91e2 151 {
sbh9428 0:9bfc4aea91e2 152 _data*=10;
sbh9428 0:9bfc4aea91e2 153 _data+=*(start+current)-'0';
sbh9428 0:9bfc4aea91e2 154
sbh9428 0:9bfc4aea91e2 155 current++;
sbh9428 0:9bfc4aea91e2 156 }
sbh9428 0:9bfc4aea91e2 157 if(*(start+current)=='.')
sbh9428 0:9bfc4aea91e2 158 {
sbh9428 0:9bfc4aea91e2 159 current++;
sbh9428 0:9bfc4aea91e2 160 while(*(start+current)!='f')
sbh9428 0:9bfc4aea91e2 161 {
sbh9428 0:9bfc4aea91e2 162 nth/=10;
sbh9428 0:9bfc4aea91e2 163 _data+=nth*(*(start+current)-'0');
sbh9428 0:9bfc4aea91e2 164 current++;
sbh9428 0:9bfc4aea91e2 165 }
sbh9428 0:9bfc4aea91e2 166 }
sbh9428 0:9bfc4aea91e2 167 return sign*_data;
sbh9428 0:9bfc4aea91e2 168 }