WathchDog

Dependencies:   mbed WDT

Committer:
orain3
Date:
Thu Sep 12 05:42:04 2019 +0000
Revision:
8:0c5a091410cf
Parent:
7:f201e2d0ed4f
Child:
9:6c267eecb235
WDT Ver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
orain3 0:63c74ee8bffc 1 #include "mbed.h"
orain3 8:0c5a091410cf 2 #include "WDT.h"
orain3 6:1948d19f854f 3
orain3 4:cb6587253326 4 //const int _ID = 4;
orain3 3:b082ed02d08c 5
orain3 3:b082ed02d08c 6 void irqrecv();
orain3 4:cb6587253326 7 int rs485_recv(float ,float ,float ,float ,float);
orain3 3:b082ed02d08c 8 char rsgetc();
orain3 3:b082ed02d08c 9 int rslen();
orain3 3:b082ed02d08c 10
orain3 0:63c74ee8bffc 11 Serial pc(SERIAL_TX,SERIAL_RX);
orain3 1:c896654c679b 12 Serial modem(D1,D0);
orain3 1:c896654c679b 13 AnalogIn adc_1(A0);
orain3 1:c896654c679b 14 AnalogIn adc_2(A1);
orain3 1:c896654c679b 15 AnalogIn adc_3(A2);
orain3 1:c896654c679b 16 AnalogIn adc_4(A3);
orain3 0:63c74ee8bffc 17
orain3 0:63c74ee8bffc 18 DigitalOut led(LED1);
orain3 1:c896654c679b 19 DigitalOut sokutei(D3);
orain3 4:cb6587253326 20 DigitalOut pos1(D4);
orain3 4:cb6587253326 21 DigitalOut pos2(D5);
orain3 1:c896654c679b 22 DigitalIn in1(D9,PullNone);
orain3 1:c896654c679b 23 DigitalIn in2(D10,PullNone);
orain3 1:c896654c679b 24 DigitalIn kansyutu(D6,PullNone);
orain3 1:c896654c679b 25 DigitalIn commandin(D7,PullNone);
orain3 1:c896654c679b 26 DigitalIn jmp(D2,PullUp);
orain3 4:cb6587253326 27 DigitalIn pulse1(D9,PullNone);
orain3 4:cb6587253326 28 DigitalIn pulse2(D10,PullNone);
orain3 4:cb6587253326 29
orain3 4:cb6587253326 30 Timer pulse_watcher;
orain3 0:63c74ee8bffc 31
orain3 0:63c74ee8bffc 32
orain3 7:f201e2d0ed4f 33 #define bufsize 2048
orain3 3:b082ed02d08c 34 char RxBuff[bufsize+1];
orain3 3:b082ed02d08c 35 int RxPtr;
orain3 3:b082ed02d08c 36 int RdPtr;
orain3 3:b082ed02d08c 37
orain3 4:cb6587253326 38 int data[40];
orain3 4:cb6587253326 39
orain3 4:cb6587253326 40
orain3 3:b082ed02d08c 41 char rsgetc() {
orain3 3:b082ed02d08c 42
orain3 3:b082ed02d08c 43 RdPtr++;
orain3 3:b082ed02d08c 44 if(RdPtr >= bufsize-1) RdPtr = 0;
orain3 3:b082ed02d08c 45 return(RxBuff[RdPtr]);
orain3 3:b082ed02d08c 46 }
orain3 3:b082ed02d08c 47
orain3 3:b082ed02d08c 48 int rslen() {
orain3 3:b082ed02d08c 49 // if (RxPtr < RdPtr) {
orain3 3:b082ed02d08c 50 // return(bufsize + RxPtr - RdPtr);
orain3 3:b082ed02d08c 51 // } else {
orain3 3:b082ed02d08c 52 // return(RxPtr - RdPtr);
orain3 3:b082ed02d08c 53 // }
orain3 3:b082ed02d08c 54 return(RxPtr - RdPtr);
orain3 3:b082ed02d08c 55 }
orain3 3:b082ed02d08c 56
orain3 3:b082ed02d08c 57 void irqrecv() {
orain3 3:b082ed02d08c 58 if (modem.readable()) {
orain3 3:b082ed02d08c 59 if (RxPtr <= bufsize - 1) {
orain3 3:b082ed02d08c 60 RxBuff[++RxPtr] = modem.getc();
orain3 3:b082ed02d08c 61 } else {
orain3 3:b082ed02d08c 62 RxPtr = 0;
orain3 3:b082ed02d08c 63 RxBuff[RxPtr] = modem.getc();
orain3 3:b082ed02d08c 64 }
orain3 3:b082ed02d08c 65 }
orain3 3:b082ed02d08c 66 }
orain3 3:b082ed02d08c 67
orain3 7:f201e2d0ed4f 68 //unsigned char buf485[512];
orain3 7:f201e2d0ed4f 69 unsigned char buf485[2048];
orain3 3:b082ed02d08c 70 int cnt485;
orain3 3:b082ed02d08c 71
orain3 0:63c74ee8bffc 72 int main()
orain3 0:63c74ee8bffc 73 {
orain3 1:c896654c679b 74 float sum1,sum2,sum3,sum4;
orain3 1:c896654c679b 75 float ad1,ad2,ad3,ad4;
orain3 3:b082ed02d08c 76 static float temp;//,turb,s;
orain3 0:63c74ee8bffc 77 int i,cnt;
orain3 4:cb6587253326 78 int pulse;
orain3 0:63c74ee8bffc 79
orain3 8:0c5a091410cf 80 pc.printf("\n--Initial Start--\r\n");
orain3 8:0c5a091410cf 81
orain3 8:0c5a091410cf 82 WDT wdgTimer;
orain3 6:1948d19f854f 83
orain3 8:0c5a091410cf 84 wdgTimer.Configure(10);
orain3 8:0c5a091410cf 85
orain3 1:c896654c679b 86 modem.baud(38400);
orain3 1:c896654c679b 87 modem.format(8,Serial::None,1);
orain3 3:b082ed02d08c 88 RxPtr = 0;
orain3 3:b082ed02d08c 89 RdPtr = 0;
orain3 3:b082ed02d08c 90 modem.attach(irqrecv,RawSerial::RxIrq);
orain3 3:b082ed02d08c 91 cnt485 = 0;
orain3 3:b082ed02d08c 92 buf485[0] = '\0';
orain3 0:63c74ee8bffc 93
orain3 8:0c5a091410cf 94 wdgTimer.Service();
orain3 7:f201e2d0ed4f 95
orain3 7:f201e2d0ed4f 96
orain3 4:cb6587253326 97 pos1 = 1;
orain3 4:cb6587253326 98 pos2 = 1;
orain3 4:cb6587253326 99
orain3 0:63c74ee8bffc 100 temp = 0;
orain3 3:b082ed02d08c 101 // turb = 0;
orain3 3:b082ed02d08c 102 // s = 0;
orain3 1:c896654c679b 103 sokutei = 1;
orain3 4:cb6587253326 104 pulse_watcher.start();
orain3 4:cb6587253326 105 pulse = pulse1 || pulse2;
orain3 4:cb6587253326 106
orain3 8:0c5a091410cf 107
orain3 8:0c5a091410cf 108
orain3 7:f201e2d0ed4f 109 for (i = 0;i<40;i++) {
orain3 7:f201e2d0ed4f 110 data[i] = 0;
orain3 8:0c5a091410cf 111 wdgTimer.Service();
orain3 7:f201e2d0ed4f 112 }
orain3 8:0c5a091410cf 113
orain3 8:0c5a091410cf 114 pc.printf("\n--Initial End--\r\n");
orain3 8:0c5a091410cf 115
orain3 0:63c74ee8bffc 116 while(1) {
orain3 4:cb6587253326 117 if (pulse_watcher.read() > 100) pulse_watcher.reset();
orain3 1:c896654c679b 118
orain3 4:cb6587253326 119 // if (jmp) sokutei = 1;
orain3 4:cb6587253326 120 // else sokutei = 0;
orain3 4:cb6587253326 121
orain3 4:cb6587253326 122 // if (jmp) {
orain3 4:cb6587253326 123 // pos1 = 1;
orain3 4:cb6587253326 124 // pos2 = 1;
orain3 4:cb6587253326 125 // } else {
orain3 4:cb6587253326 126 // pos1 = 0;
orain3 4:cb6587253326 127 // pos2 = 0;
orain3 4:cb6587253326 128 // }
orain3 1:c896654c679b 129
orain3 0:63c74ee8bffc 130 sum1 = 0;
orain3 0:63c74ee8bffc 131 sum2 = 0;
orain3 1:c896654c679b 132 sum3 = 0;
orain3 1:c896654c679b 133 sum4 = 0;
orain3 0:63c74ee8bffc 134 cnt = 0;
orain3 3:b082ed02d08c 135 for (i=0;i<1000;i++) {
orain3 4:cb6587253326 136 rs485_recv(temp,ad1,ad2,ad3,ad4);
orain3 3:b082ed02d08c 137
orain3 0:63c74ee8bffc 138 sum1 += adc_1.read();
orain3 0:63c74ee8bffc 139 sum2 += adc_2.read();
orain3 1:c896654c679b 140 sum3 += adc_3.read();
orain3 1:c896654c679b 141 sum4 += adc_4.read();
orain3 0:63c74ee8bffc 142 cnt += 1;
orain3 0:63c74ee8bffc 143 wait(0.001);
orain3 0:63c74ee8bffc 144
orain3 2:d69d10b670c5 145 // if (modem.readable()) {
orain3 2:d69d10b670c5 146 // pc.printf("%c\r",modem.getc());
orain3 2:d69d10b670c5 147 // wait(0.01);
orain3 2:d69d10b670c5 148 // modem.printf("=04,PVAL,%01.04f,0,\r",temp);
orain3 2:d69d10b670c5 149 // }
orain3 4:cb6587253326 150 // pulse watch
orain3 4:cb6587253326 151 if ( (pulse1 || pulse2) != pulse ) {
orain3 4:cb6587253326 152 pulse_watcher.reset();
orain3 4:cb6587253326 153 pos1 = 0;
orain3 4:cb6587253326 154 pos2 = 0;
orain3 4:cb6587253326 155 }
orain3 4:cb6587253326 156 pulse = pulse1 || pulse2;
orain3 4:cb6587253326 157 if ( pulse_watcher.read() > 10) {
orain3 4:cb6587253326 158 pos1 = 1;
orain3 4:cb6587253326 159 pos2 = 1;
orain3 4:cb6587253326 160 }
orain3 4:cb6587253326 161
orain3 8:0c5a091410cf 162 // if (jmp != 0) wdgTimer.Service();
orain3 8:0c5a091410cf 163 wdgTimer.Service();
orain3 7:f201e2d0ed4f 164
orain3 4:cb6587253326 165 // pulsewatch
orain3 0:63c74ee8bffc 166 }
orain3 0:63c74ee8bffc 167 if(cnt != 0) {
orain3 0:63c74ee8bffc 168 ad1 = sum1/cnt;
orain3 0:63c74ee8bffc 169 ad2 = sum2/cnt;
orain3 1:c896654c679b 170 ad3 = sum3/cnt;
orain3 1:c896654c679b 171 ad4 = sum4/cnt;
orain3 0:63c74ee8bffc 172 } else {
orain3 0:63c74ee8bffc 173 ad1 = adc_1.read();
orain3 0:63c74ee8bffc 174 ad2 = adc_2.read();
orain3 1:c896654c679b 175 ad3 = adc_3.read();
orain3 1:c896654c679b 176 ad4 = adc_4.read();
orain3 0:63c74ee8bffc 177 }
orain3 0:63c74ee8bffc 178
orain3 3:b082ed02d08c 179 if (ad1 < 0.2435L) ad1 = 0.2435L;
orain3 3:b082ed02d08c 180 if (ad1 > 0.8085L) ad1 = 0.8085L;
orain3 3:b082ed02d08c 181
orain3 3:b082ed02d08c 182 temp = (double)(ad1 - 0.2435L) * 60.0L / (0.8085L - 0.2425L) - 10.0L;
orain3 3:b082ed02d08c 183 // pc.printf("ADC = %f %f %f %f %f \r\n", ad1,ad2,ad3,ad4,temp);
orain3 3:b082ed02d08c 184 // s = 0;
orain3 0:63c74ee8bffc 185
orain3 4:cb6587253326 186 // data set
orain3 4:cb6587253326 187 data[8] = ad1 * 0xffff;
orain3 4:cb6587253326 188 data[9] = ad2 * 0xffff;
orain3 4:cb6587253326 189 data[10] = ad3 * 0xffff;
orain3 4:cb6587253326 190 data[12] = ad4 * 0xffff;
orain3 6:1948d19f854f 191
orain3 7:f201e2d0ed4f 192 //WDT test
orain3 7:f201e2d0ed4f 193 // if (jmp == 0) while(1) ;
orain3 7:f201e2d0ed4f 194
orain3 7:f201e2d0ed4f 195 // wdg.kick(); // IWDGをリセット
orain3 8:0c5a091410cf 196 // if (jmp != 0) wdgTimer.Service();
orain3 8:0c5a091410cf 197 wdgTimer.Service();
orain3 0:63c74ee8bffc 198 }
orain3 0:63c74ee8bffc 199 }
orain3 3:b082ed02d08c 200
orain3 3:b082ed02d08c 201
orain3 4:cb6587253326 202 int rs485_recv(float temp,float ad1,float ad2,float ad3,float ad4) {
orain3 3:b082ed02d08c 203 char chr;
orain3 4:cb6587253326 204 // char idbuf[8];
orain3 4:cb6587253326 205 char sendbuf[512];
orain3 4:cb6587253326 206 // int i;
orain3 4:cb6587253326 207
orain3 3:b082ed02d08c 208 if (rslen()) {
orain3 3:b082ed02d08c 209
orain3 3:b082ed02d08c 210 chr = rsgetc();
orain3 3:b082ed02d08c 211 buf485[cnt485++] = chr;
orain3 3:b082ed02d08c 212 buf485[cnt485] = '\0';
orain3 3:b082ed02d08c 213 if (chr == '?') cnt485 = 0;
orain3 3:b082ed02d08c 214
orain3 4:cb6587253326 215 if (chr == 0x0a) {
orain3 5:efffadf6d0e0 216 // pc.printf("CR\r\n");
orain3 4:cb6587253326 217
orain3 4:cb6587253326 218 if( ('C' == buf485[0]) ) {
orain3 4:cb6587253326 219 if (kansyutu) data[0] = 1;
orain3 4:cb6587253326 220 else data[0] = 0;
orain3 3:b082ed02d08c 221
orain3 4:cb6587253326 222 // sokutei on/off
orain3 5:efffadf6d0e0 223 if( buf485[2] & 0x05 ) {
orain3 4:cb6587253326 224 sokutei = 1;
orain3 4:cb6587253326 225 data[0] = data[0] | 64 | 128;
orain3 4:cb6587253326 226 } else {
orain3 4:cb6587253326 227 sokutei = 0;
orain3 4:cb6587253326 228 }
orain3 5:efffadf6d0e0 229 if( buf485[2] & 0x0a ) {
orain3 4:cb6587253326 230 wait(0.1);
orain3 5:efffadf6d0e0 231 modem.printf("?00,WIPE,1,\r");
orain3 4:cb6587253326 232 } else {
orain3 4:cb6587253326 233 // answer
orain3 4:cb6587253326 234 sprintf(sendbuf,"=%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x\r\n",
orain3 4:cb6587253326 235 data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],
orain3 4:cb6587253326 236 data[10],data[11],data[12],data[13],data[14],data[15],data[16],data[17],data[18],data[19],
orain3 4:cb6587253326 237 data[20],data[21],data[22],data[23],data[24],data[25],data[26],data[27],data[28],data[29],
orain3 4:cb6587253326 238 data[30],data[31],data[32],data[33],data[34],data[35],data[36],data[37],data[38],data[39]
orain3 4:cb6587253326 239 );
orain3 4:cb6587253326 240 //modem.printf("=%c%c,PVAL,%01.04f,%01.04f,%01.04f,%01.04f,%01.04f,%01d\r", 0x30 | idbuf[0],0x30 | idbuf[1],temp,ad1,ad2,ad3,ad4,kan);
orain3 4:cb6587253326 241
orain3 4:cb6587253326 242 wait(0.1);
orain3 4:cb6587253326 243 modem.printf("%s",sendbuf);
orain3 4:cb6587253326 244 pc.printf("%s",sendbuf);
orain3 4:cb6587253326 245 }
orain3 3:b082ed02d08c 246 }
orain3 4:cb6587253326 247 RxPtr = 0;
orain3 4:cb6587253326 248 RdPtr = 0;
orain3 4:cb6587253326 249 RxBuff[0] = '\0';
orain3 4:cb6587253326 250 buf485[0] = '\0'; // clear
orain3 4:cb6587253326 251 cnt485 = 0;
orain3 3:b082ed02d08c 252 }
orain3 3:b082ed02d08c 253 }
orain3 3:b082ed02d08c 254 return(0);
orain3 3:b082ed02d08c 255 }
orain3 3:b082ed02d08c 256
orain3 3:b082ed02d08c 257