![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
opener-6
Dependencies: TextLCD_HelloWorld2 TextLCD mbed
Fork of TextLCD_HelloWorld2 by
main.cpp@5:93f48c84e645, 2018-04-16 (annotated)
- Committer:
- Martin1000
- Date:
- Mon Apr 16 12:09:03 2018 +0000
- Revision:
- 5:93f48c84e645
- Parent:
- 4:a3e4bb2053cb
door 6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Martin1000 | 5:93f48c84e645 | 1 | |
Martin1000 | 5:93f48c84e645 | 2 | // DOOR OPENER version 1.4 (Opener-6) |
Martin1000 | 5:93f48c84e645 | 3 | // |
Martin1000 | 5:93f48c84e645 | 4 | // |
Martin1000 | 5:93f48c84e645 | 5 | // program and related hardware serve to test the "door opener" |
Martin1000 | 5:93f48c84e645 | 6 | // general function: |
Martin1000 | 5:93f48c84e645 | 7 | // |
Martin1000 | 5:93f48c84e645 | 8 | // open and close via buttons or automatically |
Martin1000 | 5:93f48c84e645 | 9 | // motor stops when endposition is reached (or current to high) |
Martin1000 | 5:93f48c84e645 | 10 | // duty cycle, ramp- time and pause time for automatic |
Martin1000 | 5:93f48c84e645 | 11 | // are adjustable via potentiometers |
Martin1000 | 5:93f48c84e645 | 12 | // |
Martin1000 | 5:93f48c84e645 | 13 | // current, cycles, ramp- time, pause- time will be displayed |
Martin1000 | 5:93f48c84e645 | 14 | // |
Martin1000 | 5:93f48c84e645 | 15 | // |
Martin1000 | 5:93f48c84e645 | 16 | // |
Martin1000 | 5:93f48c84e645 | 17 | // include the following functions: |
Martin1000 | 5:93f48c84e645 | 18 | // read the digital inputs button open |
Martin1000 | 5:93f48c84e645 | 19 | // button close |
Martin1000 | 5:93f48c84e645 | 20 | // proximity switch open |
Martin1000 | 5:93f48c84e645 | 21 | // proximity switch close |
Martin1000 | 5:93f48c84e645 | 22 | // auto_on |
Martin1000 | 5:93f48c84e645 | 23 | // |
Martin1000 | 5:93f48c84e645 | 24 | // read the analog inputs motor_current |
Martin1000 | 5:93f48c84e645 | 25 | // duty_cycle 10..100% |
Martin1000 | 5:93f48c84e645 | 26 | // ramp_time 0.1...9.9 sec |
Martin1000 | 5:93f48c84e645 | 27 | // pause_time 1.0..99.9 sec |
Martin1000 | 5:93f48c84e645 | 28 | // |
Martin1000 | 5:93f48c84e645 | 29 | // set the outputs myled |
Martin1000 | 5:93f48c84e645 | 30 | // out_status_led |
Martin1000 | 5:93f48c84e645 | 31 | // out_led_open |
Martin1000 | 5:93f48c84e645 | 32 | // out_led_close |
Martin1000 | 5:93f48c84e645 | 33 | // pwm_close |
Martin1000 | 5:93f48c84e645 | 34 | // pwm_open |
Martin1000 | 5:93f48c84e645 | 35 | // motor enable |
Martin1000 | 5:93f48c84e645 | 36 | // --------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 37 | // first date : 26-07-2017 |
Martin1000 | 5:93f48c84e645 | 38 | // last change : 10-11-2017 |
Martin1000 | 5:93f48c84e645 | 39 | // name : Hillebrand, Martin (Iserlohn) |
Martin1000 | 5:93f48c84e645 | 40 | // target : FRDM K64F (from NXP) |
Martin1000 | 5:93f48c84e645 | 41 | // software : mbed (online) |
Martin1000 | 5:93f48c84e645 | 42 | // programming : USB direct to SDA- interface |
Martin1000 | 5:93f48c84e645 | 43 | // --------------------------------------------- |
wim | 0:a75049de1a82 | 44 | |
wim | 0:a75049de1a82 | 45 | #include "mbed.h" |
wim | 0:a75049de1a82 | 46 | #include "TextLCD.h" |
Martin1000 | 5:93f48c84e645 | 47 | |
Martin1000 | 5:93f48c84e645 | 48 | #define OPEN 1 |
Martin1000 | 5:93f48c84e645 | 49 | #define CLOSE 2 |
Martin1000 | 5:93f48c84e645 | 50 | |
Martin1000 | 5:93f48c84e645 | 51 | // variable declaration |
Martin1000 | 5:93f48c84e645 | 52 | // ==================== |
Martin1000 | 5:93f48c84e645 | 53 | |
Martin1000 | 5:93f48c84e645 | 54 | int old_value = 0; // for single shot putty- output |
Martin1000 | 5:93f48c84e645 | 55 | int new_value = 0; // dto. |
Martin1000 | 5:93f48c84e645 | 56 | |
Martin1000 | 5:93f48c84e645 | 57 | float duty_open = 0; // duty cycle in direction open |
Martin1000 | 5:93f48c84e645 | 58 | float duty_close = 0; // duty cycle in direction close |
Martin1000 | 5:93f48c84e645 | 59 | |
Martin1000 | 5:93f48c84e645 | 60 | double motor_current = 0; // current from motor controller CT |
Martin1000 | 5:93f48c84e645 | 61 | |
Martin1000 | 5:93f48c84e645 | 62 | int button_open = 0; // following variables contains the |
Martin1000 | 5:93f48c84e645 | 63 | int button_close = 0; // input values, but logical correct |
Martin1000 | 5:93f48c84e645 | 64 | int endpos_open = 0; // e. g. button_open = 1 activ |
Martin1000 | 5:93f48c84e645 | 65 | int endpos_close = 0; // switch_auto = 1, when auto is ON |
Martin1000 | 5:93f48c84e645 | 66 | int switch_auto = 0; // |
Martin1000 | 5:93f48c84e645 | 67 | int time_flag = 0; // flag for resetting timer |
Martin1000 | 5:93f48c84e645 | 68 | |
Martin1000 | 5:93f48c84e645 | 69 | int dir = 0; |
Martin1000 | 5:93f48c84e645 | 70 | int off_flag = 0; |
Martin1000 | 5:93f48c84e645 | 71 | int auto_status = 0; |
Martin1000 | 5:93f48c84e645 | 72 | int auto_cycle = 0; |
Martin1000 | 5:93f48c84e645 | 73 | int k = 0; |
Martin1000 | 5:93f48c84e645 | 74 | int start_move = 0; |
Martin1000 | 5:93f48c84e645 | 75 | int flag_9 = 0; |
Martin1000 | 5:93f48c84e645 | 76 | |
Martin1000 | 5:93f48c84e645 | 77 | |
Martin1000 | 5:93f48c84e645 | 78 | |
Martin1000 | 5:93f48c84e645 | 79 | float current_motor = 0; // |
Martin1000 | 5:93f48c84e645 | 80 | double duty_cycle_percent = 0; // |
Martin1000 | 5:93f48c84e645 | 81 | double duty_cycle_current = 0; // |
Martin1000 | 5:93f48c84e645 | 82 | |
Martin1000 | 5:93f48c84e645 | 83 | double pause_time = 0; // |
Martin1000 | 5:93f48c84e645 | 84 | double ramp_time = 0; |
Martin1000 | 5:93f48c84e645 | 85 | double voltage3v3 = 0; // analog values refer |
Martin1000 | 5:93f48c84e645 | 86 | double m_old_value = 0; |
Martin1000 | 5:93f48c84e645 | 87 | double m_new_value = 0; |
Martin1000 | 5:93f48c84e645 | 88 | double elapsed_time = 0; // elapsed time since t.start() |
Martin1000 | 5:93f48c84e645 | 89 | double timeout_x_time = 0; // elapsed time for timeout |
Martin1000 | 5:93f48c84e645 | 90 | double gradient = 0; // gradient for solving |
Martin1000 | 5:93f48c84e645 | 91 | double timeouttime = 60; // fix value for reaching the endposition |
Martin1000 | 5:93f48c84e645 | 92 | |
Martin1000 | 5:93f48c84e645 | 93 | |
Martin1000 | 5:93f48c84e645 | 94 | Timer t; |
Martin1000 | 5:93f48c84e645 | 95 | Timer x; |
Martin1000 | 5:93f48c84e645 | 96 | |
Martin1000 | 5:93f48c84e645 | 97 | // function declaration |
Martin1000 | 5:93f48c84e645 | 98 | // ==================== |
Martin1000 | 5:93f48c84e645 | 99 | |
Martin1000 | 5:93f48c84e645 | 100 | void initialize(void); |
Martin1000 | 5:93f48c84e645 | 101 | void state_to_usb(void); |
Martin1000 | 5:93f48c84e645 | 102 | void read_inputs(void); |
Martin1000 | 5:93f48c84e645 | 103 | void monitor_output(void); |
Martin1000 | 5:93f48c84e645 | 104 | void endpos_led(void); |
Martin1000 | 5:93f48c84e645 | 105 | void motor_manual(int); |
Martin1000 | 5:93f48c84e645 | 106 | void motor_auto(int); |
Martin1000 | 5:93f48c84e645 | 107 | void automatic(void); |
Martin1000 | 5:93f48c84e645 | 108 | void motor_off(void); |
Martin1000 | 5:93f48c84e645 | 109 | void motor_off_auto(void); |
Martin1000 | 5:93f48c84e645 | 110 | |
Martin1000 | 5:93f48c84e645 | 111 | void manual_mode(void); |
Martin1000 | 5:93f48c84e645 | 112 | void lcd_output_start(void); // display after reset |
Martin1000 | 5:93f48c84e645 | 113 | void lcd_output_setting(void); // setting mode |
Martin1000 | 5:93f48c84e645 | 114 | void lcd_output_manual(void); // auto, waiting for close door |
Martin1000 | 5:93f48c84e645 | 115 | void lcd_output_auto0(void); // auto, close, waiting for start |
Martin1000 | 5:93f48c84e645 | 116 | void lcd_output_auto1(void); // |
Martin1000 | 5:93f48c84e645 | 117 | void lcd_output_auto2(void); |
Martin1000 | 5:93f48c84e645 | 118 | void lcd_output_auto3(void); |
Martin1000 | 5:93f48c84e645 | 119 | |
Martin1000 | 5:93f48c84e645 | 120 | |
Martin1000 | 5:93f48c84e645 | 121 | // define inputs and outputs |
Martin1000 | 5:93f48c84e645 | 122 | // ========================= |
Martin1000 | 5:93f48c84e645 | 123 | |
Martin1000 | 5:93f48c84e645 | 124 | DigitalOut myled(LED1); // internal Test- LED |
Martin1000 | 5:93f48c84e645 | 125 | PwmOut pwm_open(PTD2); // PWM- signal for direction open |
Martin1000 | 5:93f48c84e645 | 126 | PwmOut pwm_close(PTD0); // PWM- signal for direction close |
Martin1000 | 5:93f48c84e645 | 127 | |
Martin1000 | 5:93f48c84e645 | 128 | DigitalOut out_enable(PTC4); // enable motor driver |
Martin1000 | 5:93f48c84e645 | 129 | DigitalOut out_led_open(PTC16); // indicator in button open |
Martin1000 | 5:93f48c84e645 | 130 | DigitalOut out_led_close(PTC17); // indicator in button close |
Martin1000 | 5:93f48c84e645 | 131 | DigitalOut out_led_status(PTB9); // led status |
wim | 4:a3e4bb2053cb | 132 | |
Martin1000 | 5:93f48c84e645 | 133 | DigitalIn in_btn_open(PTD1); // button open |
Martin1000 | 5:93f48c84e645 | 134 | DigitalIn in_btn_close(PTD3); // button close |
Martin1000 | 5:93f48c84e645 | 135 | DigitalIn in_end_pos_open(PTB23); // signal endposition open |
Martin1000 | 5:93f48c84e645 | 136 | DigitalIn in_end_pos_close(PTA1); // signal endposition close |
Martin1000 | 5:93f48c84e645 | 137 | DigitalIn in_sw_auto(PTC12); // switch to start auto sequence |
Martin1000 | 5:93f48c84e645 | 138 | |
Martin1000 | 5:93f48c84e645 | 139 | |
Martin1000 | 5:93f48c84e645 | 140 | AnalogIn a_in_motor(PTC10); // A5- motor current |
Martin1000 | 5:93f48c84e645 | 141 | AnalogIn a_in_duty(PTC11); // A4- duty cycle analog |
Martin1000 | 5:93f48c84e645 | 142 | AnalogIn a_in_ramp(PTB11); // A3- ramp analog value 0.1..9.9 seconds |
Martin1000 | 5:93f48c84e645 | 143 | AnalogIn a_in_pause(PTB10); // A2- pause analog value0.0..99.9 seconds |
Martin1000 | 5:93f48c84e645 | 144 | AnalogIn a_in_3v3(PTB3); // A1- reference voltage 3,3 volt |
Martin1000 | 5:93f48c84e645 | 145 | |
Martin1000 | 5:93f48c84e645 | 146 | |
wim | 0:a75049de1a82 | 147 | |
Martin1000 | 5:93f48c84e645 | 148 | /* |
Martin1000 | 5:93f48c84e645 | 149 | AnalogIn a_in_motor(PTB2); // A0- motor current |
Martin1000 | 5:93f48c84e645 | 150 | AnalogIn a_in_duty(PTB3); // A1- duty cycle analog |
Martin1000 | 5:93f48c84e645 | 151 | AnalogIn a_in_ramp(PTB10); // A2- ramp analog value 0.1..9.9 seconds |
Martin1000 | 5:93f48c84e645 | 152 | AnalogIn a_in_pause(PTB11); // A3- pause analog value0.0..99.9 seconds |
Martin1000 | 5:93f48c84e645 | 153 | AnalogIn a_in_3v3(PTC11); // A4- reference voltage 3,3 volt |
Martin1000 | 5:93f48c84e645 | 154 | |
Martin1000 | 5:93f48c84e645 | 155 | */ |
Martin1000 | 5:93f48c84e645 | 156 | Serial pc(USBTX, USBRX); // Host PC Communication channels tx, rx |
Martin1000 | 5:93f48c84e645 | 157 | I2C i2c_lcd(PTE25,PTE24); // SDA, SCL for K64 |
Martin1000 | 5:93f48c84e645 | 158 | |
Martin1000 | 5:93f48c84e645 | 159 | TextLCD_I2C lcd(&i2c_lcd, 0x27<<1, TextLCD::LCD20x4); |
wim | 0:a75049de1a82 | 160 | |
Martin1000 | 5:93f48c84e645 | 161 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 162 | void lcd_output_start(void) |
Martin1000 | 5:93f48c84e645 | 163 | { |
Martin1000 | 5:93f48c84e645 | 164 | wait(0.5); |
Martin1000 | 5:93f48c84e645 | 165 | lcd.setBacklight(TextLCD::LightOn); // LCD backlight on |
Martin1000 | 5:93f48c84e645 | 166 | lcd.cls(); |
Martin1000 | 5:93f48c84e645 | 167 | lcd.printf("- door opener v4.6 -"); // LCD Headline |
Martin1000 | 5:93f48c84e645 | 168 | } |
Martin1000 | 5:93f48c84e645 | 169 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 170 | void lcd_output_setting(void) |
Martin1000 | 5:93f48c84e645 | 171 | { |
Martin1000 | 5:93f48c84e645 | 172 | lcd.locate(0,0); |
Martin1000 | 5:93f48c84e645 | 173 | lcd.printf("--- setting mode ---"); |
Martin1000 | 5:93f48c84e645 | 174 | lcd.locate(0,1); |
Martin1000 | 5:93f48c84e645 | 175 | lcd.printf("duty cycle = %3.0f %% ",duty_cycle_percent); |
Martin1000 | 5:93f48c84e645 | 176 | lcd.locate(0,2); |
Martin1000 | 5:93f48c84e645 | 177 | lcd.printf("ramp time = %3.1f s ",ramp_time); |
Martin1000 | 5:93f48c84e645 | 178 | lcd.locate(0,3); |
Martin1000 | 5:93f48c84e645 | 179 | if (pause_time >=10.0) |
Martin1000 | 5:93f48c84e645 | 180 | { |
Martin1000 | 5:93f48c84e645 | 181 | lcd.printf("pause time = %3.1f s ",pause_time); |
Martin1000 | 5:93f48c84e645 | 182 | } |
Martin1000 | 5:93f48c84e645 | 183 | else |
Martin1000 | 5:93f48c84e645 | 184 | { |
Martin1000 | 5:93f48c84e645 | 185 | lcd.printf("pause time = %3.1f s ",pause_time); |
Martin1000 | 5:93f48c84e645 | 186 | } |
Martin1000 | 5:93f48c84e645 | 187 | } |
Martin1000 | 5:93f48c84e645 | 188 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 189 | void lcd_output_manual(void) |
Martin1000 | 5:93f48c84e645 | 190 | { |
Martin1000 | 5:93f48c84e645 | 191 | |
Martin1000 | 5:93f48c84e645 | 192 | if (button_open || button_close) |
Martin1000 | 5:93f48c84e645 | 193 | { |
Martin1000 | 5:93f48c84e645 | 194 | k++; |
Martin1000 | 5:93f48c84e645 | 195 | if (k >8) |
Martin1000 | 5:93f48c84e645 | 196 | { |
Martin1000 | 5:93f48c84e645 | 197 | lcd.cls(); |
Martin1000 | 5:93f48c84e645 | 198 | lcd.locate(0,0); |
Martin1000 | 5:93f48c84e645 | 199 | lcd.printf("--- manuel mode ---\n"); |
Martin1000 | 5:93f48c84e645 | 200 | motor_current = (double)a_in_motor*1000.0; |
Martin1000 | 5:93f48c84e645 | 201 | lcd.locate(0,2); |
Martin1000 | 5:93f48c84e645 | 202 | lcd.printf("Current = %6.1f mA\n",motor_current); |
Martin1000 | 5:93f48c84e645 | 203 | lcd.locate(0,3); |
Martin1000 | 5:93f48c84e645 | 204 | lcd.printf("duty = %5.0f %%\n",duty_cycle_current); |
Martin1000 | 5:93f48c84e645 | 205 | k = 0; |
Martin1000 | 5:93f48c84e645 | 206 | } |
Martin1000 | 5:93f48c84e645 | 207 | } |
Martin1000 | 5:93f48c84e645 | 208 | } |
Martin1000 | 5:93f48c84e645 | 209 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 210 | void lcd_output_auto0(void) |
Martin1000 | 5:93f48c84e645 | 211 | { |
Martin1000 | 5:93f48c84e645 | 212 | k++; |
Martin1000 | 5:93f48c84e645 | 213 | if (k >8) |
Martin1000 | 5:93f48c84e645 | 214 | { |
Martin1000 | 5:93f48c84e645 | 215 | |
Martin1000 | 5:93f48c84e645 | 216 | lcd.cls(); |
Martin1000 | 5:93f48c84e645 | 217 | lcd.locate(0,0); |
Martin1000 | 5:93f48c84e645 | 218 | lcd.printf("-- automatic mode --\n"); |
Martin1000 | 5:93f48c84e645 | 219 | lcd.locate(0,1); |
Martin1000 | 5:93f48c84e645 | 220 | lcd.printf("status = %d\n", auto_status); |
Martin1000 | 5:93f48c84e645 | 221 | motor_current = (double)a_in_motor*1000.0; |
Martin1000 | 5:93f48c84e645 | 222 | lcd.locate(0,2); |
Martin1000 | 5:93f48c84e645 | 223 | lcd.printf("door is open"); |
Martin1000 | 5:93f48c84e645 | 224 | lcd.locate(0,3); |
Martin1000 | 5:93f48c84e645 | 225 | lcd.printf("close door!\n"); |
Martin1000 | 5:93f48c84e645 | 226 | k = 0; |
Martin1000 | 5:93f48c84e645 | 227 | } |
Martin1000 | 5:93f48c84e645 | 228 | } |
Martin1000 | 5:93f48c84e645 | 229 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 230 | void lcd_output_auto1(void) |
Martin1000 | 5:93f48c84e645 | 231 | { |
Martin1000 | 5:93f48c84e645 | 232 | k++; |
Martin1000 | 5:93f48c84e645 | 233 | if (k >8) |
Martin1000 | 5:93f48c84e645 | 234 | { |
Martin1000 | 5:93f48c84e645 | 235 | |
Martin1000 | 5:93f48c84e645 | 236 | lcd.cls(); |
Martin1000 | 5:93f48c84e645 | 237 | lcd.locate(0,0); |
Martin1000 | 5:93f48c84e645 | 238 | lcd.printf("-- automatic mode --\n"); |
Martin1000 | 5:93f48c84e645 | 239 | lcd.locate(0,1); |
Martin1000 | 5:93f48c84e645 | 240 | lcd.printf("status = %d\n", auto_status); |
Martin1000 | 5:93f48c84e645 | 241 | motor_current = (double)a_in_motor*1000.0; |
Martin1000 | 5:93f48c84e645 | 242 | lcd.locate(0,2); |
Martin1000 | 5:93f48c84e645 | 243 | lcd.printf("door is closed"); |
Martin1000 | 5:93f48c84e645 | 244 | lcd.locate(0,3); |
Martin1000 | 5:93f48c84e645 | 245 | lcd.printf("waiting for start! "); |
Martin1000 | 5:93f48c84e645 | 246 | k = 0; |
Martin1000 | 5:93f48c84e645 | 247 | } |
Martin1000 | 5:93f48c84e645 | 248 | } |
Martin1000 | 5:93f48c84e645 | 249 | |
Martin1000 | 5:93f48c84e645 | 250 | |
Martin1000 | 5:93f48c84e645 | 251 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 252 | void lcd_output_auto2(void) |
Martin1000 | 5:93f48c84e645 | 253 | { |
wim | 4:a3e4bb2053cb | 254 | |
Martin1000 | 5:93f48c84e645 | 255 | k++; |
Martin1000 | 5:93f48c84e645 | 256 | if (k >8) |
Martin1000 | 5:93f48c84e645 | 257 | { |
Martin1000 | 5:93f48c84e645 | 258 | |
Martin1000 | 5:93f48c84e645 | 259 | lcd.cls(); |
Martin1000 | 5:93f48c84e645 | 260 | lcd.locate(0,0); |
Martin1000 | 5:93f48c84e645 | 261 | lcd.printf("-- automatic mode --\n"); |
Martin1000 | 5:93f48c84e645 | 262 | lcd.locate(0,1); |
Martin1000 | 5:93f48c84e645 | 263 | if (dir == OPEN) |
Martin1000 | 5:93f48c84e645 | 264 | { |
Martin1000 | 5:93f48c84e645 | 265 | lcd.printf("status = %d OPEN \n", auto_status); |
wim | 4:a3e4bb2053cb | 266 | } |
Martin1000 | 5:93f48c84e645 | 267 | if (dir == CLOSE) |
Martin1000 | 5:93f48c84e645 | 268 | { |
Martin1000 | 5:93f48c84e645 | 269 | lcd.printf("status = %d CLOSE\n", auto_status); |
Martin1000 | 5:93f48c84e645 | 270 | } |
Martin1000 | 5:93f48c84e645 | 271 | motor_current = (double)a_in_motor*1000.0; |
Martin1000 | 5:93f48c84e645 | 272 | lcd.locate(0,2); |
Martin1000 | 5:93f48c84e645 | 273 | lcd.printf("running, cycle %d ",auto_cycle); |
wim | 3:f238b4f7874f | 274 | |
Martin1000 | 5:93f48c84e645 | 275 | lcd.locate(0,3); |
Martin1000 | 5:93f48c84e645 | 276 | lcd.printf("Current = %6.1f mA\r\n",motor_current); |
Martin1000 | 5:93f48c84e645 | 277 | k = 0; |
wim | 4:a3e4bb2053cb | 278 | } |
wim | 4:a3e4bb2053cb | 279 | } |
wim | 3:f238b4f7874f | 280 | |
wim | 3:f238b4f7874f | 281 | |
Martin1000 | 5:93f48c84e645 | 282 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 283 | void lcd_output_auto3(void) |
Martin1000 | 5:93f48c84e645 | 284 | { |
Martin1000 | 5:93f48c84e645 | 285 | k++; |
Martin1000 | 5:93f48c84e645 | 286 | if (k >8) |
Martin1000 | 5:93f48c84e645 | 287 | { |
Martin1000 | 5:93f48c84e645 | 288 | |
Martin1000 | 5:93f48c84e645 | 289 | lcd.cls(); |
Martin1000 | 5:93f48c84e645 | 290 | lcd.locate(0,0); |
Martin1000 | 5:93f48c84e645 | 291 | lcd.printf("-- automatic mode --\n"); |
Martin1000 | 5:93f48c84e645 | 292 | lcd.locate(0,1); |
Martin1000 | 5:93f48c84e645 | 293 | lcd.printf("status = %d\n", auto_status); |
Martin1000 | 5:93f48c84e645 | 294 | motor_current = (double)a_in_motor*1000.0; |
Martin1000 | 5:93f48c84e645 | 295 | lcd.locate(0,2); |
Martin1000 | 5:93f48c84e645 | 296 | lcd.printf("running, cycle %d\r\n",auto_cycle); |
wim | 0:a75049de1a82 | 297 | |
Martin1000 | 5:93f48c84e645 | 298 | lcd.locate(0,3); |
Martin1000 | 5:93f48c84e645 | 299 | lcd.printf("Pause: %3.1f of %3.1f s\r\n",elapsed_time, pause_time); |
Martin1000 | 5:93f48c84e645 | 300 | k = 0; |
Martin1000 | 5:93f48c84e645 | 301 | } |
Martin1000 | 5:93f48c84e645 | 302 | } |
Martin1000 | 5:93f48c84e645 | 303 | |
Martin1000 | 5:93f48c84e645 | 304 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 305 | void monitor_output(void) |
Martin1000 | 5:93f48c84e645 | 306 | { |
Martin1000 | 5:93f48c84e645 | 307 | m_old_value = duty_cycle_percent + ramp_time + pause_time; |
Martin1000 | 5:93f48c84e645 | 308 | |
Martin1000 | 5:93f48c84e645 | 309 | if (abs(m_new_value - m_old_value) > 1.0) |
Martin1000 | 5:93f48c84e645 | 310 | { |
Martin1000 | 5:93f48c84e645 | 311 | pc.printf("\r\n"); |
Martin1000 | 5:93f48c84e645 | 312 | pc.printf("----------------------------\r\n"); |
Martin1000 | 5:93f48c84e645 | 313 | pc.printf("duty cycle = %3.1f Prozent \r\n", duty_cycle_percent); |
Martin1000 | 5:93f48c84e645 | 314 | pc.printf("ramp time = %3.1f Sekunden \r\n", ramp_time); |
Martin1000 | 5:93f48c84e645 | 315 | pc.printf("pause time = %4.1f Sekunden \r\n", pause_time); |
Martin1000 | 5:93f48c84e645 | 316 | wait(0.1); |
Martin1000 | 5:93f48c84e645 | 317 | } |
Martin1000 | 5:93f48c84e645 | 318 | m_new_value = m_old_value; |
Martin1000 | 5:93f48c84e645 | 319 | } |
Martin1000 | 5:93f48c84e645 | 320 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 321 | void read_inputs(void) // and calculate |
Martin1000 | 5:93f48c84e645 | 322 | { |
Martin1000 | 5:93f48c84e645 | 323 | button_open = !in_btn_open; |
Martin1000 | 5:93f48c84e645 | 324 | button_close = !in_btn_close; |
Martin1000 | 5:93f48c84e645 | 325 | |
Martin1000 | 5:93f48c84e645 | 326 | endpos_open = !in_end_pos_open; |
Martin1000 | 5:93f48c84e645 | 327 | endpos_close = !in_end_pos_close; |
Martin1000 | 5:93f48c84e645 | 328 | switch_auto = !in_sw_auto; |
Martin1000 | 5:93f48c84e645 | 329 | |
Martin1000 | 5:93f48c84e645 | 330 | |
Martin1000 | 5:93f48c84e645 | 331 | |
Martin1000 | 5:93f48c84e645 | 332 | voltage3v3 = (float)a_in_3v3; |
Martin1000 | 5:93f48c84e645 | 333 | |
Martin1000 | 5:93f48c84e645 | 334 | duty_cycle_percent = (double)(a_in_duty*100.0f/voltage3v3); |
Martin1000 | 5:93f48c84e645 | 335 | if (duty_cycle_percent > 100.0) duty_cycle_percent = 100.0; |
Martin1000 | 5:93f48c84e645 | 336 | if (duty_cycle_percent < 10.0) duty_cycle_percent = 10.0; |
Martin1000 | 5:93f48c84e645 | 337 | |
Martin1000 | 5:93f48c84e645 | 338 | ramp_time = (double)(a_in_ramp*9.9f/voltage3v3); |
Martin1000 | 5:93f48c84e645 | 339 | if (ramp_time <0.1) ramp_time = 0.1; |
Martin1000 | 5:93f48c84e645 | 340 | if (ramp_time >9.9) ramp_time = 9.9; |
Martin1000 | 5:93f48c84e645 | 341 | |
Martin1000 | 5:93f48c84e645 | 342 | pause_time = (double)(a_in_pause*99.9f/voltage3v3); |
Martin1000 | 5:93f48c84e645 | 343 | if (pause_time <1.0) pause_time = 1.0; |
Martin1000 | 5:93f48c84e645 | 344 | if (pause_time >99.9) pause_time = 99.9; |
Martin1000 | 5:93f48c84e645 | 345 | |
Martin1000 | 5:93f48c84e645 | 346 | if (!button_open && !button_close && !switch_auto) |
Martin1000 | 5:93f48c84e645 | 347 | { |
Martin1000 | 5:93f48c84e645 | 348 | lcd_output_setting(); |
Martin1000 | 5:93f48c84e645 | 349 | } |
Martin1000 | 5:93f48c84e645 | 350 | } |
Martin1000 | 5:93f48c84e645 | 351 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 352 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 353 | //----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 354 | void initialize(void) |
Martin1000 | 5:93f48c84e645 | 355 | { |
Martin1000 | 5:93f48c84e645 | 356 | out_led_status = 1; |
Martin1000 | 5:93f48c84e645 | 357 | out_enable = 1; |
Martin1000 | 5:93f48c84e645 | 358 | pwm_close.period(1.0/1000); |
Martin1000 | 5:93f48c84e645 | 359 | pwm_open.period(1.0/1000); |
Martin1000 | 5:93f48c84e645 | 360 | } |
Martin1000 | 5:93f48c84e645 | 361 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 362 | void endpos_led(void) |
Martin1000 | 5:93f48c84e645 | 363 | { |
Martin1000 | 5:93f48c84e645 | 364 | if (endpos_open) // switching ON of the "end pos"-led in the button OPEN |
Martin1000 | 5:93f48c84e645 | 365 | { |
Martin1000 | 5:93f48c84e645 | 366 | out_led_open = 1; |
Martin1000 | 5:93f48c84e645 | 367 | } else |
Martin1000 | 5:93f48c84e645 | 368 | { |
Martin1000 | 5:93f48c84e645 | 369 | out_led_open = 0; |
Martin1000 | 5:93f48c84e645 | 370 | } |
Martin1000 | 5:93f48c84e645 | 371 | //===================================================================== |
Martin1000 | 5:93f48c84e645 | 372 | if (endpos_close) // switching ON "end pos"-led in the button CLOSE |
Martin1000 | 5:93f48c84e645 | 373 | { |
Martin1000 | 5:93f48c84e645 | 374 | out_led_close = 1; |
Martin1000 | 5:93f48c84e645 | 375 | } else |
Martin1000 | 5:93f48c84e645 | 376 | { |
Martin1000 | 5:93f48c84e645 | 377 | out_led_close = 0; |
Martin1000 | 5:93f48c84e645 | 378 | } |
Martin1000 | 5:93f48c84e645 | 379 | } |
Martin1000 | 5:93f48c84e645 | 380 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 381 | void state_to_usb(void) |
Martin1000 | 5:93f48c84e645 | 382 | { |
Martin1000 | 5:93f48c84e645 | 383 | new_value = (int)(button_open * 1 + button_close * 2 + endpos_open * |
Martin1000 | 5:93f48c84e645 | 384 | 4 + endpos_close * 8 + switch_auto * 16 + auto_status *32); |
Martin1000 | 5:93f48c84e645 | 385 | |
Martin1000 | 5:93f48c84e645 | 386 | if (new_value!= old_value) |
Martin1000 | 5:93f48c84e645 | 387 | { // state of inputs if new values different from former values |
Martin1000 | 5:93f48c84e645 | 388 | |
Martin1000 | 5:93f48c84e645 | 389 | pc.printf("----------------------\r\n"); |
Martin1000 | 5:93f48c84e645 | 390 | pc.printf("Button Open %d \r\n", (int)button_open); |
Martin1000 | 5:93f48c84e645 | 391 | pc.printf("Button Close %d \r\n", (int)button_close); |
Martin1000 | 5:93f48c84e645 | 392 | pc.printf("End Position Open %d \r\n", (int)endpos_open); |
Martin1000 | 5:93f48c84e645 | 393 | pc.printf("End Position Close %d \r\n", (int)endpos_close); |
Martin1000 | 5:93f48c84e645 | 394 | pc.printf("Switch Auto %d \r\n", (int)switch_auto); |
Martin1000 | 5:93f48c84e645 | 395 | pc.printf("auto status %d \r\n", (int)auto_status); |
Martin1000 | 5:93f48c84e645 | 396 | |
Martin1000 | 5:93f48c84e645 | 397 | |
Martin1000 | 5:93f48c84e645 | 398 | old_value = new_value; |
Martin1000 | 5:93f48c84e645 | 399 | } |
Martin1000 | 5:93f48c84e645 | 400 | out_led_status = !out_led_status; |
Martin1000 | 5:93f48c84e645 | 401 | wait(0.05); |
Martin1000 | 5:93f48c84e645 | 402 | } |
Martin1000 | 5:93f48c84e645 | 403 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 404 | void automatic(void) |
Martin1000 | 5:93f48c84e645 | 405 | { |
Martin1000 | 5:93f48c84e645 | 406 | switch (auto_status) |
Martin1000 | 5:93f48c84e645 | 407 | { |
Martin1000 | 5:93f48c84e645 | 408 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 409 | case 0: |
Martin1000 | 5:93f48c84e645 | 410 | lcd_output_auto0(); |
Martin1000 | 5:93f48c84e645 | 411 | if (endpos_close) |
Martin1000 | 5:93f48c84e645 | 412 | { |
Martin1000 | 5:93f48c84e645 | 413 | auto_status = 1; |
Martin1000 | 5:93f48c84e645 | 414 | } |
Martin1000 | 5:93f48c84e645 | 415 | break; |
Martin1000 | 5:93f48c84e645 | 416 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 417 | case 1: |
Martin1000 | 5:93f48c84e645 | 418 | lcd_output_auto1(); |
Martin1000 | 5:93f48c84e645 | 419 | if (endpos_close && button_open) |
Martin1000 | 5:93f48c84e645 | 420 | { |
Martin1000 | 5:93f48c84e645 | 421 | auto_status = 2; // forward to status 2 |
Martin1000 | 5:93f48c84e645 | 422 | } |
Martin1000 | 5:93f48c84e645 | 423 | if (!endpos_close) // back to status 0 |
Martin1000 | 5:93f48c84e645 | 424 | { |
Martin1000 | 5:93f48c84e645 | 425 | auto_status = 0; |
Martin1000 | 5:93f48c84e645 | 426 | } |
Martin1000 | 5:93f48c84e645 | 427 | break; |
Martin1000 | 5:93f48c84e645 | 428 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 429 | case 2: |
Martin1000 | 5:93f48c84e645 | 430 | lcd_output_auto1(); |
Martin1000 | 5:93f48c84e645 | 431 | if (endpos_close && !button_open) // release start button |
Martin1000 | 5:93f48c84e645 | 432 | { |
Martin1000 | 5:93f48c84e645 | 433 | auto_status = 3; |
Martin1000 | 5:93f48c84e645 | 434 | } |
Martin1000 | 5:93f48c84e645 | 435 | if (!endpos_close) // back to status 0 |
Martin1000 | 5:93f48c84e645 | 436 | { |
Martin1000 | 5:93f48c84e645 | 437 | auto_status = 0; |
Martin1000 | 5:93f48c84e645 | 438 | } |
Martin1000 | 5:93f48c84e645 | 439 | break; |
Martin1000 | 5:93f48c84e645 | 440 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 441 | case 3: |
Martin1000 | 5:93f48c84e645 | 442 | lcd_output_auto2(); |
Martin1000 | 5:93f48c84e645 | 443 | if (endpos_close && !button_open) // release start button |
Martin1000 | 5:93f48c84e645 | 444 | { |
Martin1000 | 5:93f48c84e645 | 445 | wait(0.3); |
Martin1000 | 5:93f48c84e645 | 446 | auto_status = 4; |
Martin1000 | 5:93f48c84e645 | 447 | start_move = 1; //? |
Martin1000 | 5:93f48c84e645 | 448 | time_flag = 0; |
Martin1000 | 5:93f48c84e645 | 449 | } |
Martin1000 | 5:93f48c84e645 | 450 | break; |
Martin1000 | 5:93f48c84e645 | 451 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 452 | case 4: |
Martin1000 | 5:93f48c84e645 | 453 | lcd_output_auto2(); |
Martin1000 | 5:93f48c84e645 | 454 | motor_auto(OPEN); // motor runs until |
Martin1000 | 5:93f48c84e645 | 455 | // a.) position is open |
Martin1000 | 5:93f48c84e645 | 456 | // b.) auto switch turned off |
Martin1000 | 5:93f48c84e645 | 457 | // c.) timeout reached |
Martin1000 | 5:93f48c84e645 | 458 | // |
Martin1000 | 5:93f48c84e645 | 459 | start_move = 0; |
Martin1000 | 5:93f48c84e645 | 460 | if (!endpos_close) // motor leave endpos close |
Martin1000 | 5:93f48c84e645 | 461 | { |
Martin1000 | 5:93f48c84e645 | 462 | x.reset(); |
Martin1000 | 5:93f48c84e645 | 463 | x.start(); |
Martin1000 | 5:93f48c84e645 | 464 | timeout_x_time = 0; |
Martin1000 | 5:93f48c84e645 | 465 | |
Martin1000 | 5:93f48c84e645 | 466 | auto_status = 5; |
Martin1000 | 5:93f48c84e645 | 467 | |
Martin1000 | 5:93f48c84e645 | 468 | } |
Martin1000 | 5:93f48c84e645 | 469 | break; |
Martin1000 | 5:93f48c84e645 | 470 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 471 | case 5: |
Martin1000 | 5:93f48c84e645 | 472 | lcd_output_auto2(); |
Martin1000 | 5:93f48c84e645 | 473 | motor_auto(OPEN); // motor runs until |
Martin1000 | 5:93f48c84e645 | 474 | // a.) position is open |
Martin1000 | 5:93f48c84e645 | 475 | // b.) auto switch turned off |
Martin1000 | 5:93f48c84e645 | 476 | // c.) timeout reached |
Martin1000 | 5:93f48c84e645 | 477 | // |
Martin1000 | 5:93f48c84e645 | 478 | |
Martin1000 | 5:93f48c84e645 | 479 | timeout_x_time = x.read(); |
Martin1000 | 5:93f48c84e645 | 480 | if (timeout_x_time > timeouttime) |
Martin1000 | 5:93f48c84e645 | 481 | { |
Martin1000 | 5:93f48c84e645 | 482 | motor_off_auto(); |
Martin1000 | 5:93f48c84e645 | 483 | out_led_status = 1; |
Martin1000 | 5:93f48c84e645 | 484 | |
Martin1000 | 5:93f48c84e645 | 485 | } |
Martin1000 | 5:93f48c84e645 | 486 | |
Martin1000 | 5:93f48c84e645 | 487 | |
Martin1000 | 5:93f48c84e645 | 488 | |
Martin1000 | 5:93f48c84e645 | 489 | |
Martin1000 | 5:93f48c84e645 | 490 | |
Martin1000 | 5:93f48c84e645 | 491 | |
Martin1000 | 5:93f48c84e645 | 492 | if (endpos_open) // motor reach open position |
Martin1000 | 5:93f48c84e645 | 493 | { |
Martin1000 | 5:93f48c84e645 | 494 | motor_off_auto(); |
Martin1000 | 5:93f48c84e645 | 495 | auto_status = 6; |
Martin1000 | 5:93f48c84e645 | 496 | } |
Martin1000 | 5:93f48c84e645 | 497 | break; |
Martin1000 | 5:93f48c84e645 | 498 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 499 | case 6: |
Martin1000 | 5:93f48c84e645 | 500 | lcd_output_auto2(); |
Martin1000 | 5:93f48c84e645 | 501 | time_flag = 0; |
Martin1000 | 5:93f48c84e645 | 502 | if (endpos_open) // |
Martin1000 | 5:93f48c84e645 | 503 | { |
Martin1000 | 5:93f48c84e645 | 504 | auto_status = 7; |
Martin1000 | 5:93f48c84e645 | 505 | } |
Martin1000 | 5:93f48c84e645 | 506 | break; |
Martin1000 | 5:93f48c84e645 | 507 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 508 | case 7: |
Martin1000 | 5:93f48c84e645 | 509 | lcd_output_auto3(); |
Martin1000 | 5:93f48c84e645 | 510 | if (!time_flag) |
Martin1000 | 5:93f48c84e645 | 511 | { |
Martin1000 | 5:93f48c84e645 | 512 | t.reset(); |
Martin1000 | 5:93f48c84e645 | 513 | t.start(); |
Martin1000 | 5:93f48c84e645 | 514 | time_flag = 1; |
Martin1000 | 5:93f48c84e645 | 515 | elapsed_time = 0; |
Martin1000 | 5:93f48c84e645 | 516 | } |
Martin1000 | 5:93f48c84e645 | 517 | elapsed_time = t.read(); |
Martin1000 | 5:93f48c84e645 | 518 | if (elapsed_time > pause_time) |
Martin1000 | 5:93f48c84e645 | 519 | { |
Martin1000 | 5:93f48c84e645 | 520 | auto_status = 8; |
Martin1000 | 5:93f48c84e645 | 521 | time_flag = 0; |
Martin1000 | 5:93f48c84e645 | 522 | } |
Martin1000 | 5:93f48c84e645 | 523 | break; |
Martin1000 | 5:93f48c84e645 | 524 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 525 | case 8: |
Martin1000 | 5:93f48c84e645 | 526 | lcd_output_auto2(); |
Martin1000 | 5:93f48c84e645 | 527 | motor_auto(CLOSE); // motor runs until |
Martin1000 | 5:93f48c84e645 | 528 | // a.) position is open |
Martin1000 | 5:93f48c84e645 | 529 | // b.) auto switch turned off |
Martin1000 | 5:93f48c84e645 | 530 | // c.) timeout reached |
Martin1000 | 5:93f48c84e645 | 531 | if (!endpos_open) |
Martin1000 | 5:93f48c84e645 | 532 | { |
Martin1000 | 5:93f48c84e645 | 533 | auto_status = 9; |
Martin1000 | 5:93f48c84e645 | 534 | } |
Martin1000 | 5:93f48c84e645 | 535 | break; |
Martin1000 | 5:93f48c84e645 | 536 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 537 | case 9: |
Martin1000 | 5:93f48c84e645 | 538 | lcd_output_auto2(); |
Martin1000 | 5:93f48c84e645 | 539 | motor_auto(CLOSE); // motor runs until |
Martin1000 | 5:93f48c84e645 | 540 | // a.) position is open |
Martin1000 | 5:93f48c84e645 | 541 | // b.) auto switch turned off |
Martin1000 | 5:93f48c84e645 | 542 | // c.) timeout reached |
Martin1000 | 5:93f48c84e645 | 543 | if (endpos_close) |
Martin1000 | 5:93f48c84e645 | 544 | { |
Martin1000 | 5:93f48c84e645 | 545 | motor_off_auto(); |
Martin1000 | 5:93f48c84e645 | 546 | auto_status = 10; |
Martin1000 | 5:93f48c84e645 | 547 | time_flag = 0; |
Martin1000 | 5:93f48c84e645 | 548 | auto_cycle++; |
Martin1000 | 5:93f48c84e645 | 549 | if (auto_cycle > 100000) auto_cycle = 0; |
Martin1000 | 5:93f48c84e645 | 550 | } |
Martin1000 | 5:93f48c84e645 | 551 | break; |
Martin1000 | 5:93f48c84e645 | 552 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 553 | case 10: |
Martin1000 | 5:93f48c84e645 | 554 | lcd_output_auto3(); |
Martin1000 | 5:93f48c84e645 | 555 | if (!time_flag) |
Martin1000 | 5:93f48c84e645 | 556 | { |
Martin1000 | 5:93f48c84e645 | 557 | t.reset(); |
Martin1000 | 5:93f48c84e645 | 558 | t.start(); |
Martin1000 | 5:93f48c84e645 | 559 | time_flag = 1; |
Martin1000 | 5:93f48c84e645 | 560 | elapsed_time = 0; |
Martin1000 | 5:93f48c84e645 | 561 | } |
Martin1000 | 5:93f48c84e645 | 562 | elapsed_time = t.read(); |
Martin1000 | 5:93f48c84e645 | 563 | if (elapsed_time > pause_time) |
Martin1000 | 5:93f48c84e645 | 564 | { |
Martin1000 | 5:93f48c84e645 | 565 | auto_status = 11; |
Martin1000 | 5:93f48c84e645 | 566 | time_flag = 0; |
Martin1000 | 5:93f48c84e645 | 567 | } |
Martin1000 | 5:93f48c84e645 | 568 | break; |
Martin1000 | 5:93f48c84e645 | 569 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 570 | case 11: |
Martin1000 | 5:93f48c84e645 | 571 | lcd_output_auto2(); |
Martin1000 | 5:93f48c84e645 | 572 | if (endpos_close) |
Martin1000 | 5:93f48c84e645 | 573 | { |
Martin1000 | 5:93f48c84e645 | 574 | auto_status = 2; |
Martin1000 | 5:93f48c84e645 | 575 | } |
Martin1000 | 5:93f48c84e645 | 576 | break; |
Martin1000 | 5:93f48c84e645 | 577 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 578 | default: |
Martin1000 | 5:93f48c84e645 | 579 | break; |
Martin1000 | 5:93f48c84e645 | 580 | } |
Martin1000 | 5:93f48c84e645 | 581 | } |
Martin1000 | 5:93f48c84e645 | 582 | // ----------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 583 | void motor_manual(int direction) |
Martin1000 | 5:93f48c84e645 | 584 | { |
Martin1000 | 5:93f48c84e645 | 585 | if (!time_flag) |
Martin1000 | 5:93f48c84e645 | 586 | { |
Martin1000 | 5:93f48c84e645 | 587 | t.reset(); |
Martin1000 | 5:93f48c84e645 | 588 | t.start(); |
Martin1000 | 5:93f48c84e645 | 589 | time_flag = 1; |
Martin1000 | 5:93f48c84e645 | 590 | elapsed_time = 0; |
Martin1000 | 5:93f48c84e645 | 591 | } |
Martin1000 | 5:93f48c84e645 | 592 | elapsed_time = t.read(); |
Martin1000 | 5:93f48c84e645 | 593 | gradient = (duty_cycle_percent-10.0f)/ramp_time; |
Martin1000 | 5:93f48c84e645 | 594 | |
Martin1000 | 5:93f48c84e645 | 595 | if (elapsed_time < ramp_time) |
Martin1000 | 5:93f48c84e645 | 596 | { |
Martin1000 | 5:93f48c84e645 | 597 | duty_cycle_current = 10.0 + elapsed_time * gradient; |
Martin1000 | 5:93f48c84e645 | 598 | } |
Martin1000 | 5:93f48c84e645 | 599 | else |
Martin1000 | 5:93f48c84e645 | 600 | { |
Martin1000 | 5:93f48c84e645 | 601 | duty_cycle_current = duty_cycle_percent; |
Martin1000 | 5:93f48c84e645 | 602 | } |
Martin1000 | 5:93f48c84e645 | 603 | if (direction == OPEN) |
Martin1000 | 5:93f48c84e645 | 604 | { |
Martin1000 | 5:93f48c84e645 | 605 | pwm_open.write(duty_cycle_current/100.0f); |
Martin1000 | 5:93f48c84e645 | 606 | } |
Martin1000 | 5:93f48c84e645 | 607 | if (direction == CLOSE) |
Martin1000 | 5:93f48c84e645 | 608 | { |
Martin1000 | 5:93f48c84e645 | 609 | pwm_close.write(duty_cycle_current/100.0f); |
Martin1000 | 5:93f48c84e645 | 610 | } |
Martin1000 | 5:93f48c84e645 | 611 | lcd_output_manual(); |
Martin1000 | 5:93f48c84e645 | 612 | } |
Martin1000 | 5:93f48c84e645 | 613 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 614 | void motor_auto(int direction) |
Martin1000 | 5:93f48c84e645 | 615 | { |
Martin1000 | 5:93f48c84e645 | 616 | dir = direction; |
Martin1000 | 5:93f48c84e645 | 617 | |
Martin1000 | 5:93f48c84e645 | 618 | if (!time_flag) |
Martin1000 | 5:93f48c84e645 | 619 | { |
Martin1000 | 5:93f48c84e645 | 620 | t.reset(); |
Martin1000 | 5:93f48c84e645 | 621 | t.start(); |
Martin1000 | 5:93f48c84e645 | 622 | time_flag = 1; |
Martin1000 | 5:93f48c84e645 | 623 | elapsed_time = 0; |
Martin1000 | 5:93f48c84e645 | 624 | } |
Martin1000 | 5:93f48c84e645 | 625 | |
Martin1000 | 5:93f48c84e645 | 626 | elapsed_time = t.read(); |
Martin1000 | 5:93f48c84e645 | 627 | gradient = (duty_cycle_percent-10.0f)/ramp_time; |
Martin1000 | 5:93f48c84e645 | 628 | |
Martin1000 | 5:93f48c84e645 | 629 | if (elapsed_time < ramp_time) |
Martin1000 | 5:93f48c84e645 | 630 | { |
Martin1000 | 5:93f48c84e645 | 631 | duty_cycle_current = 10.0 + elapsed_time * gradient; |
Martin1000 | 5:93f48c84e645 | 632 | } |
Martin1000 | 5:93f48c84e645 | 633 | else |
Martin1000 | 5:93f48c84e645 | 634 | { |
Martin1000 | 5:93f48c84e645 | 635 | duty_cycle_current = duty_cycle_percent; |
Martin1000 | 5:93f48c84e645 | 636 | } |
Martin1000 | 5:93f48c84e645 | 637 | if (direction == OPEN) |
Martin1000 | 5:93f48c84e645 | 638 | { |
Martin1000 | 5:93f48c84e645 | 639 | pwm_open.write(duty_cycle_current/100.0f); |
Martin1000 | 5:93f48c84e645 | 640 | } |
Martin1000 | 5:93f48c84e645 | 641 | if (direction == CLOSE) |
Martin1000 | 5:93f48c84e645 | 642 | { |
Martin1000 | 5:93f48c84e645 | 643 | pwm_close.write(duty_cycle_current/100.0f); |
Martin1000 | 5:93f48c84e645 | 644 | } |
Martin1000 | 5:93f48c84e645 | 645 | } |
Martin1000 | 5:93f48c84e645 | 646 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 647 | void motor_off_auto(void) |
Martin1000 | 5:93f48c84e645 | 648 | { |
Martin1000 | 5:93f48c84e645 | 649 | duty_cycle_current = 0; |
Martin1000 | 5:93f48c84e645 | 650 | pwm_close.write(0); |
Martin1000 | 5:93f48c84e645 | 651 | pwm_open.write(0); |
Martin1000 | 5:93f48c84e645 | 652 | } |
Martin1000 | 5:93f48c84e645 | 653 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 654 | void motor_off(void) |
Martin1000 | 5:93f48c84e645 | 655 | { |
Martin1000 | 5:93f48c84e645 | 656 | duty_cycle_current = 0; |
Martin1000 | 5:93f48c84e645 | 657 | pwm_close.write(0); |
Martin1000 | 5:93f48c84e645 | 658 | pwm_open.write(0); |
Martin1000 | 5:93f48c84e645 | 659 | lcd_output_manual(); |
Martin1000 | 5:93f48c84e645 | 660 | } |
Martin1000 | 5:93f48c84e645 | 661 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 662 | void manual_mode(void) |
Martin1000 | 5:93f48c84e645 | 663 | { |
Martin1000 | 5:93f48c84e645 | 664 | auto_status = 0; |
Martin1000 | 5:93f48c84e645 | 665 | //----------------------------------- |
Martin1000 | 5:93f48c84e645 | 666 | if(button_open && !endpos_open) |
Martin1000 | 5:93f48c84e645 | 667 | { |
Martin1000 | 5:93f48c84e645 | 668 | motor_manual(OPEN); |
Martin1000 | 5:93f48c84e645 | 669 | off_flag = 0; |
Martin1000 | 5:93f48c84e645 | 670 | } |
Martin1000 | 5:93f48c84e645 | 671 | //----------------------------------- |
Martin1000 | 5:93f48c84e645 | 672 | if(button_close && !endpos_close) |
Martin1000 | 5:93f48c84e645 | 673 | { |
Martin1000 | 5:93f48c84e645 | 674 | motor_manual(CLOSE); |
Martin1000 | 5:93f48c84e645 | 675 | off_flag = 0; |
Martin1000 | 5:93f48c84e645 | 676 | } |
Martin1000 | 5:93f48c84e645 | 677 | //----------------------------------- |
Martin1000 | 5:93f48c84e645 | 678 | if (off_flag) |
Martin1000 | 5:93f48c84e645 | 679 | { |
Martin1000 | 5:93f48c84e645 | 680 | motor_off(); |
Martin1000 | 5:93f48c84e645 | 681 | off_flag = 0; |
Martin1000 | 5:93f48c84e645 | 682 | time_flag = 0; |
Martin1000 | 5:93f48c84e645 | 683 | } |
Martin1000 | 5:93f48c84e645 | 684 | } |
Martin1000 | 5:93f48c84e645 | 685 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 686 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 687 | // ---------------------------------------------------------------------------- |
Martin1000 | 5:93f48c84e645 | 688 | int main() |
Martin1000 | 5:93f48c84e645 | 689 | { |
Martin1000 | 5:93f48c84e645 | 690 | initialize(); |
Martin1000 | 5:93f48c84e645 | 691 | lcd_output_start(); |
Martin1000 | 5:93f48c84e645 | 692 | while (1) |
Martin1000 | 5:93f48c84e645 | 693 | { |
Martin1000 | 5:93f48c84e645 | 694 | read_inputs(); |
Martin1000 | 5:93f48c84e645 | 695 | state_to_usb(); |
Martin1000 | 5:93f48c84e645 | 696 | monitor_output(); |
Martin1000 | 5:93f48c84e645 | 697 | endpos_led(); |
Martin1000 | 5:93f48c84e645 | 698 | |
Martin1000 | 5:93f48c84e645 | 699 | off_flag = 1; // to prevent display flicker |
Martin1000 | 5:93f48c84e645 | 700 | |
Martin1000 | 5:93f48c84e645 | 701 | if (!switch_auto) // manual mode |
Martin1000 | 5:93f48c84e645 | 702 | { |
Martin1000 | 5:93f48c84e645 | 703 | manual_mode(); |
Martin1000 | 5:93f48c84e645 | 704 | } |
Martin1000 | 5:93f48c84e645 | 705 | if (switch_auto) // auto mode |
Martin1000 | 5:93f48c84e645 | 706 | { |
Martin1000 | 5:93f48c84e645 | 707 | automatic(); |
Martin1000 | 5:93f48c84e645 | 708 | } |
Martin1000 | 5:93f48c84e645 | 709 | } |
Martin1000 | 5:93f48c84e645 | 710 | } |