ver1.1
Dependencies: EthernetInterface LcdAcm1602ni QEI TextLCD mbed-rtos mbed
main.cpp@0:092f148e92a7, 2018-05-02 (annotated)
- Committer:
- acura55
- Date:
- Wed May 02 05:20:15 2018 +0000
- Revision:
- 0:092f148e92a7
ver1.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acura55 | 0:092f148e92a7 | 1 | /*--------------------------------ライブラリ------------------------------------*/ |
acura55 | 0:092f148e92a7 | 2 | // |
acura55 | 0:092f148e92a7 | 3 | // 以下の3つのライブラリを使用するので、それぞれのライブラリをImportする。 |
acura55 | 0:092f148e92a7 | 4 | // (1) EthernetInterface |
acura55 | 0:092f148e92a7 | 5 | // (2) mbed-rtos |
acura55 | 0:092f148e92a7 | 6 | // (3) TextLCD |
acura55 | 0:092f148e92a7 | 7 | |
acura55 | 0:092f148e92a7 | 8 | #include "mbed.h"//20170621 バージョン固定 |
acura55 | 0:092f148e92a7 | 9 | #include "LPC17xx.h"//LPCExpresso |
acura55 | 0:092f148e92a7 | 10 | #include "LcdAcm1602ni.h" |
acura55 | 0:092f148e92a7 | 11 | #include "string.h" |
acura55 | 0:092f148e92a7 | 12 | #include <limits.h> |
acura55 | 0:092f148e92a7 | 13 | //#include "TextLCD.h" |
acura55 | 0:092f148e92a7 | 14 | |
acura55 | 0:092f148e92a7 | 15 | /*-----------------------------------定義--------------------------------------*/ |
acura55 | 0:092f148e92a7 | 16 | |
acura55 | 0:092f148e92a7 | 17 | //MOTOR |
acura55 | 0:092f148e92a7 | 18 | #define CW 0 |
acura55 | 0:092f148e92a7 | 19 | #define CCW 1 |
acura55 | 0:092f148e92a7 | 20 | |
acura55 | 0:092f148e92a7 | 21 | |
acura55 | 0:092f148e92a7 | 22 | //MLED |
acura55 | 0:092f148e92a7 | 23 | #define OFF 0 |
acura55 | 0:092f148e92a7 | 24 | #define ON 1 |
acura55 | 0:092f148e92a7 | 25 | |
acura55 | 0:092f148e92a7 | 26 | |
acura55 | 0:092f148e92a7 | 27 | /*--------------------------------ピン定義-------------------------------------*/ |
acura55 | 0:092f148e92a7 | 28 | |
acura55 | 0:092f148e92a7 | 29 | //MLED mbed内LEDの設定 |
acura55 | 0:092f148e92a7 | 30 | DigitalOut mled0(LED1); |
acura55 | 0:092f148e92a7 | 31 | DigitalOut mled1(LED2); |
acura55 | 0:092f148e92a7 | 32 | DigitalOut mled2(LED3); |
acura55 | 0:092f148e92a7 | 33 | DigitalOut mled3(LED4); |
acura55 | 0:092f148e92a7 | 34 | |
acura55 | 0:092f148e92a7 | 35 | //TextLCD lcd(p24, p26, p27, p28, p29, p30); |
acura55 | 0:092f148e92a7 | 36 | LcdAcm1602ni lcd(p28,p27);// sda,scl |
acura55 | 0:092f148e92a7 | 37 | |
acura55 | 0:092f148e92a7 | 38 | //インターフェイス |
acura55 | 0:092f148e92a7 | 39 | //LED |
acura55 | 0:092f148e92a7 | 40 | //DigitalOut open_led(p9); |
acura55 | 0:092f148e92a7 | 41 | //DigitalOut close_led(p10); |
acura55 | 0:092f148e92a7 | 42 | DigitalOut open_signal(p9); |
acura55 | 0:092f148e92a7 | 43 | DigitalOut close_signal(p10); |
acura55 | 0:092f148e92a7 | 44 | //スイッチ |
acura55 | 0:092f148e92a7 | 45 | DigitalIn close_sw(p19); |
acura55 | 0:092f148e92a7 | 46 | DigitalIn open_sw(p20); |
acura55 | 0:092f148e92a7 | 47 | |
acura55 | 0:092f148e92a7 | 48 | //モーター① 入出力ピン |
acura55 | 0:092f148e92a7 | 49 | DigitalOut ccw_pwm(p21);//CW方向へ回転 |
acura55 | 0:092f148e92a7 | 50 | DigitalOut cw_pwm(p22);//CCW方向へ回転 |
acura55 | 0:092f148e92a7 | 51 | DigitalIn close_limit(p15); |
acura55 | 0:092f148e92a7 | 52 | DigitalIn open_limit(p16); |
acura55 | 0:092f148e92a7 | 53 | DigitalIn tim(p17); |
acura55 | 0:092f148e92a7 | 54 | |
acura55 | 0:092f148e92a7 | 55 | |
acura55 | 0:092f148e92a7 | 56 | //モーター② |
acura55 | 0:092f148e92a7 | 57 | DigitalOut ccw_pwm_1(p11);//CW方向へ回転 |
acura55 | 0:092f148e92a7 | 58 | DigitalOut cw_pwm_1(p12);//CCW方向へ回転 |
acura55 | 0:092f148e92a7 | 59 | DigitalIn close_limit_1(p7); |
acura55 | 0:092f148e92a7 | 60 | DigitalIn open_limit_1(p8); |
acura55 | 0:092f148e92a7 | 61 | DigitalIn tim1(p18); |
acura55 | 0:092f148e92a7 | 62 | |
acura55 | 0:092f148e92a7 | 63 | //マスター判別ピン |
acura55 | 0:092f148e92a7 | 64 | DigitalIn master_pin(p23); |
acura55 | 0:092f148e92a7 | 65 | |
acura55 | 0:092f148e92a7 | 66 | Ticker flipper; |
acura55 | 0:092f148e92a7 | 67 | Serial pc(USBTX,USBRX); |
acura55 | 0:092f148e92a7 | 68 | /*-------------------------------グローバル変数---------------------------------*/ |
acura55 | 0:092f148e92a7 | 69 | |
acura55 | 0:092f148e92a7 | 70 | long wt = 800;//モーターパルス用変数 wt = 周期T/2 max500 |
acura55 | 0:092f148e92a7 | 71 | int dir = 1; |
acura55 | 0:092f148e92a7 | 72 | int master_flg = 0; |
acura55 | 0:092f148e92a7 | 73 | float tim_pulse = 0; |
acura55 | 0:092f148e92a7 | 74 | float tim1_pulse = 0; |
acura55 | 0:092f148e92a7 | 75 | float last_tim = 0; |
acura55 | 0:092f148e92a7 | 76 | float last_tim1 = 0; |
acura55 | 0:092f148e92a7 | 77 | float tim_d = 0; |
acura55 | 0:092f148e92a7 | 78 | float tim1_d = 0;; |
acura55 | 0:092f148e92a7 | 79 | int tim_arry[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
acura55 | 0:092f148e92a7 | 80 | int tim1_arry[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
acura55 | 0:092f148e92a7 | 81 | |
acura55 | 0:092f148e92a7 | 82 | /*-----------------------------------step-------------------------------------*/ |
acura55 | 0:092f148e92a7 | 83 | //モーターに1パルスを与える |
acura55 | 0:092f148e92a7 | 84 | void step(){ |
acura55 | 0:092f148e92a7 | 85 | mled3 = ON; |
acura55 | 0:092f148e92a7 | 86 | //open |
acura55 | 0:092f148e92a7 | 87 | if(dir == CW){ |
acura55 | 0:092f148e92a7 | 88 | mled2 = ON; |
acura55 | 0:092f148e92a7 | 89 | if(master_flg == 1 && open_limit == OFF){ |
acura55 | 0:092f148e92a7 | 90 | //if(master_flg == 1 && close_limit == ON){ |
acura55 | 0:092f148e92a7 | 91 | open_signal = OFF; |
acura55 | 0:092f148e92a7 | 92 | }else{ |
acura55 | 0:092f148e92a7 | 93 | cw_pwm = ON; |
acura55 | 0:092f148e92a7 | 94 | cw_pwm_1 = ON; |
acura55 | 0:092f148e92a7 | 95 | } |
acura55 | 0:092f148e92a7 | 96 | wait_us(wt); |
acura55 | 0:092f148e92a7 | 97 | cw_pwm = OFF; |
acura55 | 0:092f148e92a7 | 98 | cw_pwm_1 = OFF; |
acura55 | 0:092f148e92a7 | 99 | wait_us(wt); |
acura55 | 0:092f148e92a7 | 100 | } |
acura55 | 0:092f148e92a7 | 101 | //close |
acura55 | 0:092f148e92a7 | 102 | if(dir == CCW){ |
acura55 | 0:092f148e92a7 | 103 | mled1 = ON; |
acura55 | 0:092f148e92a7 | 104 | if(master_flg == 1 && close_limit == OFF){ |
acura55 | 0:092f148e92a7 | 105 | //if(master_flg == 1 && open_limit == ON){ |
acura55 | 0:092f148e92a7 | 106 | close_signal = OFF; |
acura55 | 0:092f148e92a7 | 107 | }else{ |
acura55 | 0:092f148e92a7 | 108 | ccw_pwm = ON; |
acura55 | 0:092f148e92a7 | 109 | ccw_pwm_1 = ON; |
acura55 | 0:092f148e92a7 | 110 | } |
acura55 | 0:092f148e92a7 | 111 | wait_us(wt); |
acura55 | 0:092f148e92a7 | 112 | ccw_pwm = OFF; |
acura55 | 0:092f148e92a7 | 113 | ccw_pwm_1 = OFF; |
acura55 | 0:092f148e92a7 | 114 | wait_us(wt); |
acura55 | 0:092f148e92a7 | 115 | } |
acura55 | 0:092f148e92a7 | 116 | mled1 = OFF; |
acura55 | 0:092f148e92a7 | 117 | mled3 = OFF; |
acura55 | 0:092f148e92a7 | 118 | } |
acura55 | 0:092f148e92a7 | 119 | |
acura55 | 0:092f148e92a7 | 120 | /*-----------------------------flip 割り込み----------------------------------------*/ |
acura55 | 0:092f148e92a7 | 121 | void flip(){ |
acura55 | 0:092f148e92a7 | 122 | tim_pulse = tim.read(); |
acura55 | 0:092f148e92a7 | 123 | tim1_pulse = tim1.read(); |
acura55 | 0:092f148e92a7 | 124 | tim_d = last_tim - tim_pulse; |
acura55 | 0:092f148e92a7 | 125 | tim1_d = last_tim1 - tim1_pulse; |
acura55 | 0:092f148e92a7 | 126 | last_tim = tim_pulse; |
acura55 | 0:092f148e92a7 | 127 | last_tim1 = tim1_pulse; |
acura55 | 0:092f148e92a7 | 128 | } |
acura55 | 0:092f148e92a7 | 129 | |
acura55 | 0:092f148e92a7 | 130 | /*-----------------------------MotorContoroll---------------------------------*/ |
acura55 | 0:092f148e92a7 | 131 | /*20180130 |
acura55 | 0:092f148e92a7 | 132 | void motorControll(){ |
acura55 | 0:092f148e92a7 | 133 | mled3 = ON; |
acura55 | 0:092f148e92a7 | 134 | lcd.initialize(); |
acura55 | 0:092f148e92a7 | 135 | lcd.locate(0,0); |
acura55 | 0:092f148e92a7 | 136 | lcd.printf(" ***STATE***"); |
acura55 | 0:092f148e92a7 | 137 | while(open_sw == ON||close_sw == ON){ |
acura55 | 0:092f148e92a7 | 138 | if(open_limit == ON && close_limit == ON){ |
acura55 | 0:092f148e92a7 | 139 | open_led = OFF; |
acura55 | 0:092f148e92a7 | 140 | close_led = OFF; |
acura55 | 0:092f148e92a7 | 141 | } |
acura55 | 0:092f148e92a7 | 142 | if(open_limit == OFF && open_limit_1 == OFF && open_sw == ON){ |
acura55 | 0:092f148e92a7 | 143 | lcd.locate(1,0); |
acura55 | 0:092f148e92a7 | 144 | lcd.printf("open"); |
acura55 | 0:092f148e92a7 | 145 | open_led = ON; |
acura55 | 0:092f148e92a7 | 146 | } |
acura55 | 0:092f148e92a7 | 147 | if(close_limit == OFF && close_limit_1 == OFF && close_sw == ON){ |
acura55 | 0:092f148e92a7 | 148 | lcd.locate(1,0); |
acura55 | 0:092f148e92a7 | 149 | lcd.printf("close"); |
acura55 | 0:092f148e92a7 | 150 | open_led = ON; |
acura55 | 0:092f148e92a7 | 151 | close_led = ON; |
acura55 | 0:092f148e92a7 | 152 | } |
acura55 | 0:092f148e92a7 | 153 | step(); |
acura55 | 0:092f148e92a7 | 154 | } |
acura55 | 0:092f148e92a7 | 155 | mled3 = OFF; |
acura55 | 0:092f148e92a7 | 156 | */ |
acura55 | 0:092f148e92a7 | 157 | //timパルス実装検討中 |
acura55 | 0:092f148e92a7 | 158 | void motorControll(){ |
acura55 | 0:092f148e92a7 | 159 | mled2 = ON; |
acura55 | 0:092f148e92a7 | 160 | while(open_sw == ON||close_sw == ON){ |
acura55 | 0:092f148e92a7 | 161 | step(); |
acura55 | 0:092f148e92a7 | 162 | } |
acura55 | 0:092f148e92a7 | 163 | mled2 = OFF; |
acura55 | 0:092f148e92a7 | 164 | } |
acura55 | 0:092f148e92a7 | 165 | |
acura55 | 0:092f148e92a7 | 166 | |
acura55 | 0:092f148e92a7 | 167 | /*---------------------------------main---------------------------------------*/ |
acura55 | 0:092f148e92a7 | 168 | |
acura55 | 0:092f148e92a7 | 169 | int main(){ |
acura55 | 0:092f148e92a7 | 170 | //LCD初期化 |
acura55 | 0:092f148e92a7 | 171 | /* |
acura55 | 0:092f148e92a7 | 172 | lcd.initialize(); |
acura55 | 0:092f148e92a7 | 173 | lcd.locate(0,0); |
acura55 | 0:092f148e92a7 | 174 | lcd.printf("*AutoSlideCover*"); |
acura55 | 0:092f148e92a7 | 175 | pc.printf("*AutoSlideCover*"); |
acura55 | 0:092f148e92a7 | 176 | lcd.locate(1,0); |
acura55 | 0:092f148e92a7 | 177 | lcd.printf("setup...."); |
acura55 | 0:092f148e92a7 | 178 | pc.printf("setup...."); |
acura55 | 0:092f148e92a7 | 179 | */ |
acura55 | 0:092f148e92a7 | 180 | //ピンモード初期化 |
acura55 | 0:092f148e92a7 | 181 | open_sw.mode(PullDown); |
acura55 | 0:092f148e92a7 | 182 | close_sw.mode(PullDown); |
acura55 | 0:092f148e92a7 | 183 | open_limit.mode(PullDown); |
acura55 | 0:092f148e92a7 | 184 | open_limit_1.mode(PullDown); |
acura55 | 0:092f148e92a7 | 185 | close_limit.mode(PullDown); |
acura55 | 0:092f148e92a7 | 186 | close_limit_1.mode(PullDown); |
acura55 | 0:092f148e92a7 | 187 | tim.mode(PullDown); |
acura55 | 0:092f148e92a7 | 188 | tim1.mode(PullDown); |
acura55 | 0:092f148e92a7 | 189 | master_pin.mode(PullDown); |
acura55 | 0:092f148e92a7 | 190 | |
acura55 | 0:092f148e92a7 | 191 | // tim.mode(PullNone); |
acura55 | 0:092f148e92a7 | 192 | // tim1.mode(PullNone); |
acura55 | 0:092f148e92a7 | 193 | // wait(2.0); |
acura55 | 0:092f148e92a7 | 194 | |
acura55 | 0:092f148e92a7 | 195 | // lcd.locate(1,0); |
acura55 | 0:092f148e92a7 | 196 | // lcd.printf("complete!"); |
acura55 | 0:092f148e92a7 | 197 | // pc.printf("complete!\n"); |
acura55 | 0:092f148e92a7 | 198 | // wait_ms(100); |
acura55 | 0:092f148e92a7 | 199 | |
acura55 | 0:092f148e92a7 | 200 | if(master_pin == ON){ |
acura55 | 0:092f148e92a7 | 201 | master_flg = 1; |
acura55 | 0:092f148e92a7 | 202 | mled0 = ON; |
acura55 | 0:092f148e92a7 | 203 | } |
acura55 | 0:092f148e92a7 | 204 | int open_buf[10]={0,0,0,0,0,0,0,0,0,0}; |
acura55 | 0:092f148e92a7 | 205 | int close_buf[10]={0,0,0,0,0,0,0,0,0,0}; |
acura55 | 0:092f148e92a7 | 206 | |
acura55 | 0:092f148e92a7 | 207 | /* |
acura55 | 0:092f148e92a7 | 208 | if(close_limit == ON && close_limit_1 == ON){ |
acura55 | 0:092f148e92a7 | 209 | lcd.initialize(); |
acura55 | 0:092f148e92a7 | 210 | lcd.locate(0,0); |
acura55 | 0:092f148e92a7 | 211 | lcd.printf(" ***STATE***"); |
acura55 | 0:092f148e92a7 | 212 | } |
acura55 | 0:092f148e92a7 | 213 | if(open_limit == ON && open_limit_1 == ON){ |
acura55 | 0:092f148e92a7 | 214 | lcd.initialize(); |
acura55 | 0:092f148e92a7 | 215 | lcd.locate(0,0); |
acura55 | 0:092f148e92a7 | 216 | lcd.printf(" ***STATE***"); |
acura55 | 0:092f148e92a7 | 217 | } |
acura55 | 0:092f148e92a7 | 218 | if(close_limit == OFF && close_limit_1 == OFF){ |
acura55 | 0:092f148e92a7 | 219 | lcd.initialize(); |
acura55 | 0:092f148e92a7 | 220 | lcd.locate(0,0); |
acura55 | 0:092f148e92a7 | 221 | lcd.printf(" ***STATE***"); |
acura55 | 0:092f148e92a7 | 222 | lcd.locate(1,0); |
acura55 | 0:092f148e92a7 | 223 | lcd.printf("close"); |
acura55 | 0:092f148e92a7 | 224 | } |
acura55 | 0:092f148e92a7 | 225 | if(open_limit == OFF && open_limit_1 == OFF){ |
acura55 | 0:092f148e92a7 | 226 | lcd.initialize(); |
acura55 | 0:092f148e92a7 | 227 | lcd.locate(0,0); |
acura55 | 0:092f148e92a7 | 228 | lcd.printf(" ***STATE***"); |
acura55 | 0:092f148e92a7 | 229 | lcd.locate(1,0); |
acura55 | 0:092f148e92a7 | 230 | lcd.printf("open"); |
acura55 | 0:092f148e92a7 | 231 | } |
acura55 | 0:092f148e92a7 | 232 | */ |
acura55 | 0:092f148e92a7 | 233 | while(1){ |
acura55 | 0:092f148e92a7 | 234 | // if(open_limit == 1)pc.printf("open_limit\n"); |
acura55 | 0:092f148e92a7 | 235 | // if(close_limit == 1)pc.printf("close_limit\n"); |
acura55 | 0:092f148e92a7 | 236 | // if(open_limit_1 == 1)pc.printf("open_limit_1\n"); |
acura55 | 0:092f148e92a7 | 237 | // if(close_limit_1 == 1)pc.printf("close_limit_1\n"); |
acura55 | 0:092f148e92a7 | 238 | if(master_flg == 0){ |
acura55 | 0:092f148e92a7 | 239 | if(open_sw == ON){ |
acura55 | 0:092f148e92a7 | 240 | mled0 = ON; |
acura55 | 0:092f148e92a7 | 241 | dir = CW; |
acura55 | 0:092f148e92a7 | 242 | motorControll(); |
acura55 | 0:092f148e92a7 | 243 | mled0 = OFF; |
acura55 | 0:092f148e92a7 | 244 | } |
acura55 | 0:092f148e92a7 | 245 | if(close_sw == ON){ |
acura55 | 0:092f148e92a7 | 246 | mled1 = ON; |
acura55 | 0:092f148e92a7 | 247 | dir = CCW; |
acura55 | 0:092f148e92a7 | 248 | motorControll(); |
acura55 | 0:092f148e92a7 | 249 | mled1 = OFF; |
acura55 | 0:092f148e92a7 | 250 | } |
acura55 | 0:092f148e92a7 | 251 | }else if(master_flg == 1){ |
acura55 | 0:092f148e92a7 | 252 | mled1 = OFF; |
acura55 | 0:092f148e92a7 | 253 | mled2 = OFF; |
acura55 | 0:092f148e92a7 | 254 | mled3 = OFF; |
acura55 | 0:092f148e92a7 | 255 | int open_sum = 0; |
acura55 | 0:092f148e92a7 | 256 | int close_sum = 0; |
acura55 | 0:092f148e92a7 | 257 | for(int i=0;i<10;i++){ |
acura55 | 0:092f148e92a7 | 258 | open_buf[i] = open_sw; |
acura55 | 0:092f148e92a7 | 259 | close_buf[i] = close_sw; |
acura55 | 0:092f148e92a7 | 260 | wait_ms(50); |
acura55 | 0:092f148e92a7 | 261 | } |
acura55 | 0:092f148e92a7 | 262 | for(int j=0;j<10;j++){ |
acura55 | 0:092f148e92a7 | 263 | open_sum = open_sum + open_buf[j]; |
acura55 | 0:092f148e92a7 | 264 | close_sum = close_sum + close_buf[j]; |
acura55 | 0:092f148e92a7 | 265 | } |
acura55 | 0:092f148e92a7 | 266 | if(open_sum == 10){ |
acura55 | 0:092f148e92a7 | 267 | dir = CW; |
acura55 | 0:092f148e92a7 | 268 | open_signal = ON; |
acura55 | 0:092f148e92a7 | 269 | motorControll(); |
acura55 | 0:092f148e92a7 | 270 | open_signal = OFF; |
acura55 | 0:092f148e92a7 | 271 | } |
acura55 | 0:092f148e92a7 | 272 | if(close_sum == 10){ |
acura55 | 0:092f148e92a7 | 273 | dir = CCW; |
acura55 | 0:092f148e92a7 | 274 | close_signal = ON; |
acura55 | 0:092f148e92a7 | 275 | motorControll(); |
acura55 | 0:092f148e92a7 | 276 | close_signal = OFF; |
acura55 | 0:092f148e92a7 | 277 | } |
acura55 | 0:092f148e92a7 | 278 | } |
acura55 | 0:092f148e92a7 | 279 | } |
acura55 | 0:092f148e92a7 | 280 | } |