Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TextLCD_HelloWorld2 TextLCD mbed
Fork of TextLCD_HelloWorld2 by
main.cpp
00001 00002 // DOOR OPENER version 1.4 (Opener-6) 00003 // 00004 // 00005 // program and related hardware serve to test the "door opener" 00006 // general function: 00007 // 00008 // open and close via buttons or automatically 00009 // motor stops when endposition is reached (or current to high) 00010 // duty cycle, ramp- time and pause time for automatic 00011 // are adjustable via potentiometers 00012 // 00013 // current, cycles, ramp- time, pause- time will be displayed 00014 // 00015 // 00016 // 00017 // include the following functions: 00018 // read the digital inputs button open 00019 // button close 00020 // proximity switch open 00021 // proximity switch close 00022 // auto_on 00023 // 00024 // read the analog inputs motor_current 00025 // duty_cycle 10..100% 00026 // ramp_time 0.1...9.9 sec 00027 // pause_time 1.0..99.9 sec 00028 // 00029 // set the outputs myled 00030 // out_status_led 00031 // out_led_open 00032 // out_led_close 00033 // pwm_close 00034 // pwm_open 00035 // motor enable 00036 // --------------------------------------------- 00037 // first date : 26-07-2017 00038 // last change : 10-11-2017 00039 // name : Hillebrand, Martin (Iserlohn) 00040 // target : FRDM K64F (from NXP) 00041 // software : mbed (online) 00042 // programming : USB direct to SDA- interface 00043 // --------------------------------------------- 00044 00045 #include "mbed.h" 00046 #include "TextLCD.h" 00047 00048 #define OPEN 1 00049 #define CLOSE 2 00050 00051 // variable declaration 00052 // ==================== 00053 00054 int old_value = 0; // for single shot putty- output 00055 int new_value = 0; // dto. 00056 00057 float duty_open = 0; // duty cycle in direction open 00058 float duty_close = 0; // duty cycle in direction close 00059 00060 double motor_current = 0; // current from motor controller CT 00061 00062 int button_open = 0; // following variables contains the 00063 int button_close = 0; // input values, but logical correct 00064 int endpos_open = 0; // e. g. button_open = 1 activ 00065 int endpos_close = 0; // switch_auto = 1, when auto is ON 00066 int switch_auto = 0; // 00067 int time_flag = 0; // flag for resetting timer 00068 00069 int dir = 0; 00070 int off_flag = 0; 00071 int auto_status = 0; 00072 int auto_cycle = 0; 00073 int k = 0; 00074 int start_move = 0; 00075 int flag_9 = 0; 00076 00077 00078 00079 float current_motor = 0; // 00080 double duty_cycle_percent = 0; // 00081 double duty_cycle_current = 0; // 00082 00083 double pause_time = 0; // 00084 double ramp_time = 0; 00085 double voltage3v3 = 0; // analog values refer 00086 double m_old_value = 0; 00087 double m_new_value = 0; 00088 double elapsed_time = 0; // elapsed time since t.start() 00089 double timeout_x_time = 0; // elapsed time for timeout 00090 double gradient = 0; // gradient for solving 00091 double timeouttime = 60; // fix value for reaching the endposition 00092 00093 00094 Timer t; 00095 Timer x; 00096 00097 // function declaration 00098 // ==================== 00099 00100 void initialize(void); 00101 void state_to_usb(void); 00102 void read_inputs(void); 00103 void monitor_output(void); 00104 void endpos_led(void); 00105 void motor_manual(int); 00106 void motor_auto(int); 00107 void automatic(void); 00108 void motor_off(void); 00109 void motor_off_auto(void); 00110 00111 void manual_mode(void); 00112 void lcd_output_start(void); // display after reset 00113 void lcd_output_setting(void); // setting mode 00114 void lcd_output_manual(void); // auto, waiting for close door 00115 void lcd_output_auto0(void); // auto, close, waiting for start 00116 void lcd_output_auto1(void); // 00117 void lcd_output_auto2(void); 00118 void lcd_output_auto3(void); 00119 00120 00121 // define inputs and outputs 00122 // ========================= 00123 00124 DigitalOut myled(LED1); // internal Test- LED 00125 PwmOut pwm_open(PTD2); // PWM- signal for direction open 00126 PwmOut pwm_close(PTD0); // PWM- signal for direction close 00127 00128 DigitalOut out_enable(PTC4); // enable motor driver 00129 DigitalOut out_led_open(PTC16); // indicator in button open 00130 DigitalOut out_led_close(PTC17); // indicator in button close 00131 DigitalOut out_led_status(PTB9); // led status 00132 00133 DigitalIn in_btn_open(PTD1); // button open 00134 DigitalIn in_btn_close(PTD3); // button close 00135 DigitalIn in_end_pos_open(PTB23); // signal endposition open 00136 DigitalIn in_end_pos_close(PTA1); // signal endposition close 00137 DigitalIn in_sw_auto(PTC12); // switch to start auto sequence 00138 00139 00140 AnalogIn a_in_motor(PTC10); // A5- motor current 00141 AnalogIn a_in_duty(PTC11); // A4- duty cycle analog 00142 AnalogIn a_in_ramp(PTB11); // A3- ramp analog value 0.1..9.9 seconds 00143 AnalogIn a_in_pause(PTB10); // A2- pause analog value0.0..99.9 seconds 00144 AnalogIn a_in_3v3(PTB3); // A1- reference voltage 3,3 volt 00145 00146 00147 00148 /* 00149 AnalogIn a_in_motor(PTB2); // A0- motor current 00150 AnalogIn a_in_duty(PTB3); // A1- duty cycle analog 00151 AnalogIn a_in_ramp(PTB10); // A2- ramp analog value 0.1..9.9 seconds 00152 AnalogIn a_in_pause(PTB11); // A3- pause analog value0.0..99.9 seconds 00153 AnalogIn a_in_3v3(PTC11); // A4- reference voltage 3,3 volt 00154 00155 */ 00156 Serial pc(USBTX, USBRX); // Host PC Communication channels tx, rx 00157 I2C i2c_lcd(PTE25,PTE24); // SDA, SCL for K64 00158 00159 TextLCD_I2C lcd(&i2c_lcd, 0x27<<1, TextLCD::LCD20x4); 00160 00161 //----------------------------------------------------------------------------- 00162 void lcd_output_start(void) 00163 { 00164 wait(0.5); 00165 lcd.setBacklight(TextLCD::LightOn); // LCD backlight on 00166 lcd.cls(); 00167 lcd.printf("- door opener v4.6 -"); // LCD Headline 00168 } 00169 //----------------------------------------------------------------------------- 00170 void lcd_output_setting(void) 00171 { 00172 lcd.locate(0,0); 00173 lcd.printf("--- setting mode ---"); 00174 lcd.locate(0,1); 00175 lcd.printf("duty cycle = %3.0f %% ",duty_cycle_percent); 00176 lcd.locate(0,2); 00177 lcd.printf("ramp time = %3.1f s ",ramp_time); 00178 lcd.locate(0,3); 00179 if (pause_time >=10.0) 00180 { 00181 lcd.printf("pause time = %3.1f s ",pause_time); 00182 } 00183 else 00184 { 00185 lcd.printf("pause time = %3.1f s ",pause_time); 00186 } 00187 } 00188 //----------------------------------------------------------------------------- 00189 void lcd_output_manual(void) 00190 { 00191 00192 if (button_open || button_close) 00193 { 00194 k++; 00195 if (k >8) 00196 { 00197 lcd.cls(); 00198 lcd.locate(0,0); 00199 lcd.printf("--- manuel mode ---\n"); 00200 motor_current = (double)a_in_motor*1000.0; 00201 lcd.locate(0,2); 00202 lcd.printf("Current = %6.1f mA\n",motor_current); 00203 lcd.locate(0,3); 00204 lcd.printf("duty = %5.0f %%\n",duty_cycle_current); 00205 k = 0; 00206 } 00207 } 00208 } 00209 //----------------------------------------------------------------------------- 00210 void lcd_output_auto0(void) 00211 { 00212 k++; 00213 if (k >8) 00214 { 00215 00216 lcd.cls(); 00217 lcd.locate(0,0); 00218 lcd.printf("-- automatic mode --\n"); 00219 lcd.locate(0,1); 00220 lcd.printf("status = %d\n", auto_status); 00221 motor_current = (double)a_in_motor*1000.0; 00222 lcd.locate(0,2); 00223 lcd.printf("door is open"); 00224 lcd.locate(0,3); 00225 lcd.printf("close door!\n"); 00226 k = 0; 00227 } 00228 } 00229 //----------------------------------------------------------------------------- 00230 void lcd_output_auto1(void) 00231 { 00232 k++; 00233 if (k >8) 00234 { 00235 00236 lcd.cls(); 00237 lcd.locate(0,0); 00238 lcd.printf("-- automatic mode --\n"); 00239 lcd.locate(0,1); 00240 lcd.printf("status = %d\n", auto_status); 00241 motor_current = (double)a_in_motor*1000.0; 00242 lcd.locate(0,2); 00243 lcd.printf("door is closed"); 00244 lcd.locate(0,3); 00245 lcd.printf("waiting for start! "); 00246 k = 0; 00247 } 00248 } 00249 00250 00251 //----------------------------------------------------------------------------- 00252 void lcd_output_auto2(void) 00253 { 00254 00255 k++; 00256 if (k >8) 00257 { 00258 00259 lcd.cls(); 00260 lcd.locate(0,0); 00261 lcd.printf("-- automatic mode --\n"); 00262 lcd.locate(0,1); 00263 if (dir == OPEN) 00264 { 00265 lcd.printf("status = %d OPEN \n", auto_status); 00266 } 00267 if (dir == CLOSE) 00268 { 00269 lcd.printf("status = %d CLOSE\n", auto_status); 00270 } 00271 motor_current = (double)a_in_motor*1000.0; 00272 lcd.locate(0,2); 00273 lcd.printf("running, cycle %d ",auto_cycle); 00274 00275 lcd.locate(0,3); 00276 lcd.printf("Current = %6.1f mA\r\n",motor_current); 00277 k = 0; 00278 } 00279 } 00280 00281 00282 //----------------------------------------------------------------------------- 00283 void lcd_output_auto3(void) 00284 { 00285 k++; 00286 if (k >8) 00287 { 00288 00289 lcd.cls(); 00290 lcd.locate(0,0); 00291 lcd.printf("-- automatic mode --\n"); 00292 lcd.locate(0,1); 00293 lcd.printf("status = %d\n", auto_status); 00294 motor_current = (double)a_in_motor*1000.0; 00295 lcd.locate(0,2); 00296 lcd.printf("running, cycle %d\r\n",auto_cycle); 00297 00298 lcd.locate(0,3); 00299 lcd.printf("Pause: %3.1f of %3.1f s\r\n",elapsed_time, pause_time); 00300 k = 0; 00301 } 00302 } 00303 00304 //----------------------------------------------------------------------------- 00305 void monitor_output(void) 00306 { 00307 m_old_value = duty_cycle_percent + ramp_time + pause_time; 00308 00309 if (abs(m_new_value - m_old_value) > 1.0) 00310 { 00311 pc.printf("\r\n"); 00312 pc.printf("----------------------------\r\n"); 00313 pc.printf("duty cycle = %3.1f Prozent \r\n", duty_cycle_percent); 00314 pc.printf("ramp time = %3.1f Sekunden \r\n", ramp_time); 00315 pc.printf("pause time = %4.1f Sekunden \r\n", pause_time); 00316 wait(0.1); 00317 } 00318 m_new_value = m_old_value; 00319 } 00320 //----------------------------------------------------------------------------- 00321 void read_inputs(void) // and calculate 00322 { 00323 button_open = !in_btn_open; 00324 button_close = !in_btn_close; 00325 00326 endpos_open = !in_end_pos_open; 00327 endpos_close = !in_end_pos_close; 00328 switch_auto = !in_sw_auto; 00329 00330 00331 00332 voltage3v3 = (float)a_in_3v3; 00333 00334 duty_cycle_percent = (double)(a_in_duty*100.0f/voltage3v3); 00335 if (duty_cycle_percent > 100.0) duty_cycle_percent = 100.0; 00336 if (duty_cycle_percent < 10.0) duty_cycle_percent = 10.0; 00337 00338 ramp_time = (double)(a_in_ramp*9.9f/voltage3v3); 00339 if (ramp_time <0.1) ramp_time = 0.1; 00340 if (ramp_time >9.9) ramp_time = 9.9; 00341 00342 pause_time = (double)(a_in_pause*99.9f/voltage3v3); 00343 if (pause_time <1.0) pause_time = 1.0; 00344 if (pause_time >99.9) pause_time = 99.9; 00345 00346 if (!button_open && !button_close && !switch_auto) 00347 { 00348 lcd_output_setting(); 00349 } 00350 } 00351 //----------------------------------------------------------------------------- 00352 //----------------------------------------------------------------------------- 00353 //----------------------------------------------------------------------------- 00354 void initialize(void) 00355 { 00356 out_led_status = 1; 00357 out_enable = 1; 00358 pwm_close.period(1.0/1000); 00359 pwm_open.period(1.0/1000); 00360 } 00361 // ----------------------------------------------------------------------------- 00362 void endpos_led(void) 00363 { 00364 if (endpos_open) // switching ON of the "end pos"-led in the button OPEN 00365 { 00366 out_led_open = 1; 00367 } else 00368 { 00369 out_led_open = 0; 00370 } 00371 //===================================================================== 00372 if (endpos_close) // switching ON "end pos"-led in the button CLOSE 00373 { 00374 out_led_close = 1; 00375 } else 00376 { 00377 out_led_close = 0; 00378 } 00379 } 00380 // ---------------------------------------------------------------------------- 00381 void state_to_usb(void) 00382 { 00383 new_value = (int)(button_open * 1 + button_close * 2 + endpos_open * 00384 4 + endpos_close * 8 + switch_auto * 16 + auto_status *32); 00385 00386 if (new_value!= old_value) 00387 { // state of inputs if new values different from former values 00388 00389 pc.printf("----------------------\r\n"); 00390 pc.printf("Button Open %d \r\n", (int)button_open); 00391 pc.printf("Button Close %d \r\n", (int)button_close); 00392 pc.printf("End Position Open %d \r\n", (int)endpos_open); 00393 pc.printf("End Position Close %d \r\n", (int)endpos_close); 00394 pc.printf("Switch Auto %d \r\n", (int)switch_auto); 00395 pc.printf("auto status %d \r\n", (int)auto_status); 00396 00397 00398 old_value = new_value; 00399 } 00400 out_led_status = !out_led_status; 00401 wait(0.05); 00402 } 00403 // ----------------------------------------------------------------------------- 00404 void automatic(void) 00405 { 00406 switch (auto_status) 00407 { 00408 // ----------------------------------------------------------------------------- 00409 case 0: 00410 lcd_output_auto0(); 00411 if (endpos_close) 00412 { 00413 auto_status = 1; 00414 } 00415 break; 00416 // ----------------------------------------------------------------------------- 00417 case 1: 00418 lcd_output_auto1(); 00419 if (endpos_close && button_open) 00420 { 00421 auto_status = 2; // forward to status 2 00422 } 00423 if (!endpos_close) // back to status 0 00424 { 00425 auto_status = 0; 00426 } 00427 break; 00428 // ----------------------------------------------------------------------------- 00429 case 2: 00430 lcd_output_auto1(); 00431 if (endpos_close && !button_open) // release start button 00432 { 00433 auto_status = 3; 00434 } 00435 if (!endpos_close) // back to status 0 00436 { 00437 auto_status = 0; 00438 } 00439 break; 00440 // ----------------------------------------------------------------------------- 00441 case 3: 00442 lcd_output_auto2(); 00443 if (endpos_close && !button_open) // release start button 00444 { 00445 wait(0.3); 00446 auto_status = 4; 00447 start_move = 1; //? 00448 time_flag = 0; 00449 } 00450 break; 00451 // ----------------------------------------------------------------------------- 00452 case 4: 00453 lcd_output_auto2(); 00454 motor_auto(OPEN); // motor runs until 00455 // a.) position is open 00456 // b.) auto switch turned off 00457 // c.) timeout reached 00458 // 00459 start_move = 0; 00460 if (!endpos_close) // motor leave endpos close 00461 { 00462 x.reset(); 00463 x.start(); 00464 timeout_x_time = 0; 00465 00466 auto_status = 5; 00467 00468 } 00469 break; 00470 // ----------------------------------------------------------------------------- 00471 case 5: 00472 lcd_output_auto2(); 00473 motor_auto(OPEN); // motor runs until 00474 // a.) position is open 00475 // b.) auto switch turned off 00476 // c.) timeout reached 00477 // 00478 00479 timeout_x_time = x.read(); 00480 if (timeout_x_time > timeouttime) 00481 { 00482 motor_off_auto(); 00483 out_led_status = 1; 00484 00485 } 00486 00487 00488 00489 00490 00491 00492 if (endpos_open) // motor reach open position 00493 { 00494 motor_off_auto(); 00495 auto_status = 6; 00496 } 00497 break; 00498 // ----------------------------------------------------------------------------- 00499 case 6: 00500 lcd_output_auto2(); 00501 time_flag = 0; 00502 if (endpos_open) // 00503 { 00504 auto_status = 7; 00505 } 00506 break; 00507 // ----------------------------------------------------------------------------- 00508 case 7: 00509 lcd_output_auto3(); 00510 if (!time_flag) 00511 { 00512 t.reset(); 00513 t.start(); 00514 time_flag = 1; 00515 elapsed_time = 0; 00516 } 00517 elapsed_time = t.read(); 00518 if (elapsed_time > pause_time) 00519 { 00520 auto_status = 8; 00521 time_flag = 0; 00522 } 00523 break; 00524 // ----------------------------------------------------------------------------- 00525 case 8: 00526 lcd_output_auto2(); 00527 motor_auto(CLOSE); // motor runs until 00528 // a.) position is open 00529 // b.) auto switch turned off 00530 // c.) timeout reached 00531 if (!endpos_open) 00532 { 00533 auto_status = 9; 00534 } 00535 break; 00536 // ----------------------------------------------------------------------------- 00537 case 9: 00538 lcd_output_auto2(); 00539 motor_auto(CLOSE); // motor runs until 00540 // a.) position is open 00541 // b.) auto switch turned off 00542 // c.) timeout reached 00543 if (endpos_close) 00544 { 00545 motor_off_auto(); 00546 auto_status = 10; 00547 time_flag = 0; 00548 auto_cycle++; 00549 if (auto_cycle > 100000) auto_cycle = 0; 00550 } 00551 break; 00552 // ----------------------------------------------------------------------------- 00553 case 10: 00554 lcd_output_auto3(); 00555 if (!time_flag) 00556 { 00557 t.reset(); 00558 t.start(); 00559 time_flag = 1; 00560 elapsed_time = 0; 00561 } 00562 elapsed_time = t.read(); 00563 if (elapsed_time > pause_time) 00564 { 00565 auto_status = 11; 00566 time_flag = 0; 00567 } 00568 break; 00569 // ----------------------------------------------------------------------------- 00570 case 11: 00571 lcd_output_auto2(); 00572 if (endpos_close) 00573 { 00574 auto_status = 2; 00575 } 00576 break; 00577 // ----------------------------------------------------------------------------- 00578 default: 00579 break; 00580 } 00581 } 00582 // ----------------------------------------------------------------------------- 00583 void motor_manual(int direction) 00584 { 00585 if (!time_flag) 00586 { 00587 t.reset(); 00588 t.start(); 00589 time_flag = 1; 00590 elapsed_time = 0; 00591 } 00592 elapsed_time = t.read(); 00593 gradient = (duty_cycle_percent-10.0f)/ramp_time; 00594 00595 if (elapsed_time < ramp_time) 00596 { 00597 duty_cycle_current = 10.0 + elapsed_time * gradient; 00598 } 00599 else 00600 { 00601 duty_cycle_current = duty_cycle_percent; 00602 } 00603 if (direction == OPEN) 00604 { 00605 pwm_open.write(duty_cycle_current/100.0f); 00606 } 00607 if (direction == CLOSE) 00608 { 00609 pwm_close.write(duty_cycle_current/100.0f); 00610 } 00611 lcd_output_manual(); 00612 } 00613 // ---------------------------------------------------------------------------- 00614 void motor_auto(int direction) 00615 { 00616 dir = direction; 00617 00618 if (!time_flag) 00619 { 00620 t.reset(); 00621 t.start(); 00622 time_flag = 1; 00623 elapsed_time = 0; 00624 } 00625 00626 elapsed_time = t.read(); 00627 gradient = (duty_cycle_percent-10.0f)/ramp_time; 00628 00629 if (elapsed_time < ramp_time) 00630 { 00631 duty_cycle_current = 10.0 + elapsed_time * gradient; 00632 } 00633 else 00634 { 00635 duty_cycle_current = duty_cycle_percent; 00636 } 00637 if (direction == OPEN) 00638 { 00639 pwm_open.write(duty_cycle_current/100.0f); 00640 } 00641 if (direction == CLOSE) 00642 { 00643 pwm_close.write(duty_cycle_current/100.0f); 00644 } 00645 } 00646 // ---------------------------------------------------------------------------- 00647 void motor_off_auto(void) 00648 { 00649 duty_cycle_current = 0; 00650 pwm_close.write(0); 00651 pwm_open.write(0); 00652 } 00653 // ---------------------------------------------------------------------------- 00654 void motor_off(void) 00655 { 00656 duty_cycle_current = 0; 00657 pwm_close.write(0); 00658 pwm_open.write(0); 00659 lcd_output_manual(); 00660 } 00661 // ---------------------------------------------------------------------------- 00662 void manual_mode(void) 00663 { 00664 auto_status = 0; 00665 //----------------------------------- 00666 if(button_open && !endpos_open) 00667 { 00668 motor_manual(OPEN); 00669 off_flag = 0; 00670 } 00671 //----------------------------------- 00672 if(button_close && !endpos_close) 00673 { 00674 motor_manual(CLOSE); 00675 off_flag = 0; 00676 } 00677 //----------------------------------- 00678 if (off_flag) 00679 { 00680 motor_off(); 00681 off_flag = 0; 00682 time_flag = 0; 00683 } 00684 } 00685 // ---------------------------------------------------------------------------- 00686 // ---------------------------------------------------------------------------- 00687 // ---------------------------------------------------------------------------- 00688 int main() 00689 { 00690 initialize(); 00691 lcd_output_start(); 00692 while (1) 00693 { 00694 read_inputs(); 00695 state_to_usb(); 00696 monitor_output(); 00697 endpos_led(); 00698 00699 off_flag = 1; // to prevent display flicker 00700 00701 if (!switch_auto) // manual mode 00702 { 00703 manual_mode(); 00704 } 00705 if (switch_auto) // auto mode 00706 { 00707 automatic(); 00708 } 00709 } 00710 }
Generated on Fri Jul 22 2022 03:05:09 by
1.7.2
