![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: BufferedSerial FastAnalogIn FastPWM mbed SHT75
commandt.cpp@1:5c42ec7f1aeb, 2016-02-22 (annotated)
- Committer:
- sbh9428
- Date:
- Mon Feb 22 06:03:11 2016 +0000
- Revision:
- 1:5c42ec7f1aeb
- Parent:
- 0:9bfc4aea91e2
- Child:
- 2:4c51394fb35b
PID?;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:9bfc4aea91e2 | 67 | } |
sbh9428 | 0:9bfc4aea91e2 | 68 | break; |
sbh9428 | 0:9bfc4aea91e2 | 69 | case 'g': |
sbh9428 | 1:5c42ec7f1aeb | 70 | switch(data[1]) |
sbh9428 | 1:5c42ec7f1aeb | 71 | { |
sbh9428 | 1:5c42ec7f1aeb | 72 | case 'm': |
sbh9428 | 1:5c42ec7f1aeb | 73 | printf("mode is %d\n", control->get_mode()); |
sbh9428 | 1:5c42ec7f1aeb | 74 | break; |
sbh9428 | 1:5c42ec7f1aeb | 75 | case 'r': |
sbh9428 | 1:5c42ec7f1aeb | 76 | printf("duty ratio is %f\n", control->get_PWM_value()); |
sbh9428 | 1:5c42ec7f1aeb | 77 | break; |
sbh9428 | 1:5c42ec7f1aeb | 78 | case 't': |
sbh9428 | 1:5c42ec7f1aeb | 79 | printf("target temp is %f\n", control->get_target_temp()); |
sbh9428 | 1:5c42ec7f1aeb | 80 | break; |
sbh9428 | 1:5c42ec7f1aeb | 81 | case 'p': |
sbh9428 | 1:5c42ec7f1aeb | 82 | printf("P value is %f\n", control->get_P_value()); |
sbh9428 | 1:5c42ec7f1aeb | 83 | break; |
sbh9428 | 1:5c42ec7f1aeb | 84 | case 'i': |
sbh9428 | 1:5c42ec7f1aeb | 85 | printf("I value is %f\n", control->get_I_value()); |
sbh9428 | 1:5c42ec7f1aeb | 86 | break; |
sbh9428 | 1:5c42ec7f1aeb | 87 | case 'd': |
sbh9428 | 1:5c42ec7f1aeb | 88 | printf("D value is %f\n", control->get_P_value()); |
sbh9428 | 1:5c42ec7f1aeb | 89 | break; |
sbh9428 | 1:5c42ec7f1aeb | 90 | } |
sbh9428 | 0:9bfc4aea91e2 | 91 | |
sbh9428 | 0:9bfc4aea91e2 | 92 | break; |
sbh9428 | 0:9bfc4aea91e2 | 93 | dafault: |
sbh9428 | 0:9bfc4aea91e2 | 94 | pc->printf("command error"); |
sbh9428 | 0:9bfc4aea91e2 | 95 | } |
sbh9428 | 0:9bfc4aea91e2 | 96 | pc->printf("\n"); |
sbh9428 | 1:5c42ec7f1aeb | 97 | count=0; |
sbh9428 | 0:9bfc4aea91e2 | 98 | } |
sbh9428 | 0:9bfc4aea91e2 | 99 | |
sbh9428 | 0:9bfc4aea91e2 | 100 | void command_t::get_data() |
sbh9428 | 0:9bfc4aea91e2 | 101 | { |
sbh9428 | 0:9bfc4aea91e2 | 102 | data [count]=pc->getc(); |
sbh9428 | 0:9bfc4aea91e2 | 103 | |
sbh9428 | 0:9bfc4aea91e2 | 104 | if(data [count]=='f') |
sbh9428 | 0:9bfc4aea91e2 | 105 | parse(); |
sbh9428 | 0:9bfc4aea91e2 | 106 | else |
sbh9428 | 0:9bfc4aea91e2 | 107 | count++; |
sbh9428 | 0:9bfc4aea91e2 | 108 | |
sbh9428 | 0:9bfc4aea91e2 | 109 | if(count>9) |
sbh9428 | 0:9bfc4aea91e2 | 110 | { |
sbh9428 | 0:9bfc4aea91e2 | 111 | count=0; |
sbh9428 | 0:9bfc4aea91e2 | 112 | pc->printf("command error\n"); |
sbh9428 | 0:9bfc4aea91e2 | 113 | } |
sbh9428 | 0:9bfc4aea91e2 | 114 | } |
sbh9428 | 0:9bfc4aea91e2 | 115 | |
sbh9428 | 0:9bfc4aea91e2 | 116 | void command_t::refreshPWM() |
sbh9428 | 0:9bfc4aea91e2 | 117 | { |
sbh9428 | 1:5c42ec7f1aeb | 118 | printf(",%2.2f\n", control->get_temp()); |
sbh9428 | 1:5c42ec7f1aeb | 119 | time=time+3; |
sbh9428 | 1:5c42ec7f1aeb | 120 | control->refresh_PWM(); |
sbh9428 | 0:9bfc4aea91e2 | 121 | } |
sbh9428 | 0:9bfc4aea91e2 | 122 | |
sbh9428 | 0:9bfc4aea91e2 | 123 | double command_t::asci_to_bin(int *start) |
sbh9428 | 0:9bfc4aea91e2 | 124 | { |
sbh9428 | 0:9bfc4aea91e2 | 125 | double _data=0; |
sbh9428 | 0:9bfc4aea91e2 | 126 | int current=0; |
sbh9428 | 0:9bfc4aea91e2 | 127 | |
sbh9428 | 0:9bfc4aea91e2 | 128 | double nth=1; //�ڸ��� |
sbh9428 | 0:9bfc4aea91e2 | 129 | int mode=0; |
sbh9428 | 0:9bfc4aea91e2 | 130 | int sign=1; |
sbh9428 | 0:9bfc4aea91e2 | 131 | |
sbh9428 | 0:9bfc4aea91e2 | 132 | if(*start=='-') |
sbh9428 | 0:9bfc4aea91e2 | 133 | { |
sbh9428 | 0:9bfc4aea91e2 | 134 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 135 | sign=-1; |
sbh9428 | 0:9bfc4aea91e2 | 136 | } |
sbh9428 | 0:9bfc4aea91e2 | 137 | while(*(start+current)!='f'&&*(start+current)!='.') |
sbh9428 | 0:9bfc4aea91e2 | 138 | { |
sbh9428 | 0:9bfc4aea91e2 | 139 | _data*=10; |
sbh9428 | 0:9bfc4aea91e2 | 140 | _data+=*(start+current)-'0'; |
sbh9428 | 0:9bfc4aea91e2 | 141 | |
sbh9428 | 0:9bfc4aea91e2 | 142 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 143 | } |
sbh9428 | 0:9bfc4aea91e2 | 144 | if(*(start+current)=='.') |
sbh9428 | 0:9bfc4aea91e2 | 145 | { |
sbh9428 | 0:9bfc4aea91e2 | 146 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 147 | while(*(start+current)!='f') |
sbh9428 | 0:9bfc4aea91e2 | 148 | { |
sbh9428 | 0:9bfc4aea91e2 | 149 | nth/=10; |
sbh9428 | 0:9bfc4aea91e2 | 150 | _data+=nth*(*(start+current)-'0'); |
sbh9428 | 0:9bfc4aea91e2 | 151 | current++; |
sbh9428 | 0:9bfc4aea91e2 | 152 | } |
sbh9428 | 0:9bfc4aea91e2 | 153 | } |
sbh9428 | 0:9bfc4aea91e2 | 154 | return sign*_data; |
sbh9428 | 0:9bfc4aea91e2 | 155 | } |