simon king
/
medica_demo_V10
Medica demo control system.
main.cpp@0:00a9f58f32d0, 2010-10-28 (annotated)
- Committer:
- sjk127
- Date:
- Thu Oct 28 13:27:46 2010 +0000
- Revision:
- 0:00a9f58f32d0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjk127 | 0:00a9f58f32d0 | 1 | #include "mbed.h" |
sjk127 | 0:00a9f58f32d0 | 2 | #include "TextLCD.h" |
sjk127 | 0:00a9f58f32d0 | 3 | |
sjk127 | 0:00a9f58f32d0 | 4 | Serial pc(USBTX, USBRX); |
sjk127 | 0:00a9f58f32d0 | 5 | TextLCD lcd(p18, p19, p20, p21, p22, p23, p24); |
sjk127 | 0:00a9f58f32d0 | 6 | InterruptIn M2counter(p9); // only active when actuators running (hall effect input) |
sjk127 | 0:00a9f58f32d0 | 7 | InterruptIn M1counter(p8); // only active when actuators running (hall effect input) |
sjk127 | 0:00a9f58f32d0 | 8 | InterruptIn cpr(p14); // push button that interrupts any process |
sjk127 | 0:00a9f58f32d0 | 9 | InterruptIn sense(p7); // opto sensor that interrupts any process after activation |
sjk127 | 0:00a9f58f32d0 | 10 | BusIn switches (p10,p11,p12,p13); // 12 push button in's through a diode matrix giving a 4 bit binary code for each button |
sjk127 | 0:00a9f58f32d0 | 11 | AnalogIn h_pot (p16); // softpot input for actuator 2 |
sjk127 | 0:00a9f58f32d0 | 12 | AnalogIn l_pot (p15); // softpot input for actuator 1 |
sjk127 | 0:00a9f58f32d0 | 13 | BusOut act2 (p29,p28); // two relays that control actuator 2 direction |
sjk127 | 0:00a9f58f32d0 | 14 | BusOut act1 (p25,p26); // two relays that control actuator 1 direction |
sjk127 | 0:00a9f58f32d0 | 15 | DigitalOut fet2 (p30); // fet for actuator 2 switched on after relay direction set |
sjk127 | 0:00a9f58f32d0 | 16 | DigitalOut fet1 (p27); // fet for actuator 1 switched on after relay direction set |
sjk127 | 0:00a9f58f32d0 | 17 | DigitalOut alarm (p17); // alarm o/p actvated when cpr pushed or sensor in tripped |
sjk127 | 0:00a9f58f32d0 | 18 | |
sjk127 | 0:00a9f58f32d0 | 19 | Timer timer1; |
sjk127 | 0:00a9f58f32d0 | 20 | Timer timer2; |
sjk127 | 0:00a9f58f32d0 | 21 | Timer timer3; |
sjk127 | 0:00a9f58f32d0 | 22 | |
sjk127 | 0:00a9f58f32d0 | 23 | int M2direction; // actuator 2 direction flag |
sjk127 | 0:00a9f58f32d0 | 24 | int M1direction; // actuator 2 direction flag |
sjk127 | 0:00a9f58f32d0 | 25 | int bus_state; // switches BusIn is read then stored in this reg |
sjk127 | 0:00a9f58f32d0 | 26 | int M2count; // actuator 2 counter reg |
sjk127 | 0:00a9f58f32d0 | 27 | int M1count; // actuator 1 counter reg |
sjk127 | 0:00a9f58f32d0 | 28 | int M2_old; // used to store first sample of counter reg for act' 2 |
sjk127 | 0:00a9f58f32d0 | 29 | int M2_new; // used to store second sample of counter reg for act' 2 |
sjk127 | 0:00a9f58f32d0 | 30 | int M1_old; // used to store second sample of counter reg for act' 1 |
sjk127 | 0:00a9f58f32d0 | 31 | int M1_new; // used to store second sample of counter reg for act' 1 |
sjk127 | 0:00a9f58f32d0 | 32 | int result; // old and new values (above) are compared by subtraction and when result = 0 act' limit switches have been met |
sjk127 | 0:00a9f58f32d0 | 33 | int lock; // flag used to lock keypad |
sjk127 | 0:00a9f58f32d0 | 34 | int senseflag; // flag to say when = 0 sensor off and when = 1 sensor on |
sjk127 | 0:00a9f58f32d0 | 35 | int abs_M2count; // absolute values of counter reg's |
sjk127 | 0:00a9f58f32d0 | 36 | int abs_M1count; // absolute values of counter reg's |
sjk127 | 0:00a9f58f32d0 | 37 | |
sjk127 | 0:00a9f58f32d0 | 38 | // the following reg's are used to evaluate the position of the act's relative to the target position |
sjk127 | 0:00a9f58f32d0 | 39 | |
sjk127 | 0:00a9f58f32d0 | 40 | int h_target; // temp reg used in most routines |
sjk127 | 0:00a9f58f32d0 | 41 | int fh_target; // |
sjk127 | 0:00a9f58f32d0 | 42 | int h_high; |
sjk127 | 0:00a9f58f32d0 | 43 | int fh_high; |
sjk127 | 0:00a9f58f32d0 | 44 | int h_low; |
sjk127 | 0:00a9f58f32d0 | 45 | int fh_low; |
sjk127 | 0:00a9f58f32d0 | 46 | int l_target; |
sjk127 | 0:00a9f58f32d0 | 47 | int fl_target; |
sjk127 | 0:00a9f58f32d0 | 48 | int l_high; |
sjk127 | 0:00a9f58f32d0 | 49 | int fl_high; |
sjk127 | 0:00a9f58f32d0 | 50 | int l_low; |
sjk127 | 0:00a9f58f32d0 | 51 | int fl_low; |
sjk127 | 0:00a9f58f32d0 | 52 | // reg's to store pot readings in as reading |
sjk127 | 0:00a9f58f32d0 | 53 | float hpot; |
sjk127 | 0:00a9f58f32d0 | 54 | float lpot; |
sjk127 | 0:00a9f58f32d0 | 55 | int result_l; |
sjk127 | 0:00a9f58f32d0 | 56 | int result_h; |
sjk127 | 0:00a9f58f32d0 | 57 | int alarmflag; |
sjk127 | 0:00a9f58f32d0 | 58 | int F2_flag; |
sjk127 | 0:00a9f58f32d0 | 59 | int M1flag; |
sjk127 | 0:00a9f58f32d0 | 60 | int M2flag; |
sjk127 | 0:00a9f58f32d0 | 61 | |
sjk127 | 0:00a9f58f32d0 | 62 | //_______________Interrupts___________________________________ |
sjk127 | 0:00a9f58f32d0 | 63 | |
sjk127 | 0:00a9f58f32d0 | 64 | |
sjk127 | 0:00a9f58f32d0 | 65 | void herga_splash (void) { // main menu screen |
sjk127 | 0:00a9f58f32d0 | 66 | lcd.cls(); |
sjk127 | 0:00a9f58f32d0 | 67 | lcd.locate(0,0); |
sjk127 | 0:00a9f58f32d0 | 68 | lcd.printf("LEGS| HERGA |HEAD"); |
sjk127 | 0:00a9f58f32d0 | 69 | lcd.locate(4,1); |
sjk127 | 0:00a9f58f32d0 | 70 | lcd.printf("| CONTROL |"); |
sjk127 | 0:00a9f58f32d0 | 71 | lcd.locate(4,2); |
sjk127 | 0:00a9f58f32d0 | 72 | lcd.printf("| SYSTEMS |"); |
sjk127 | 0:00a9f58f32d0 | 73 | lcd.locate(1,3); |
sjk127 | 0:00a9f58f32d0 | 74 | lcd.printf("PATIENT DETECT OFF"); |
sjk127 | 0:00a9f58f32d0 | 75 | } |
sjk127 | 0:00a9f58f32d0 | 76 | |
sjk127 | 0:00a9f58f32d0 | 77 | //--------------------------------- |
sjk127 | 0:00a9f58f32d0 | 78 | |
sjk127 | 0:00a9f58f32d0 | 79 | void sense_int () { // patient detect sensor tripped |
sjk127 | 0:00a9f58f32d0 | 80 | alarmflag = 1; // set flag to exit RTZ corectly if sensor interrupts it |
sjk127 | 0:00a9f58f32d0 | 81 | fet1 = 0; // switch motors off |
sjk127 | 0:00a9f58f32d0 | 82 | fet2 = 0; |
sjk127 | 0:00a9f58f32d0 | 83 | wait(0.05); // wait for induced motor current to dissipate |
sjk127 | 0:00a9f58f32d0 | 84 | act2 = 0x00; // switch relays off (break motor) |
sjk127 | 0:00a9f58f32d0 | 85 | act1 = 0x00; |
sjk127 | 0:00a9f58f32d0 | 86 | |
sjk127 | 0:00a9f58f32d0 | 87 | lcd.cls(); // set up alarm screen |
sjk127 | 0:00a9f58f32d0 | 88 | lcd.locate(2,1); |
sjk127 | 0:00a9f58f32d0 | 89 | lcd.printf("MOVEMENT DETECTED"); |
sjk127 | 0:00a9f58f32d0 | 90 | lcd.locate(2,2); |
sjk127 | 0:00a9f58f32d0 | 91 | lcd.printf("PRESS ALARM RESET"); |
sjk127 | 0:00a9f58f32d0 | 92 | |
sjk127 | 0:00a9f58f32d0 | 93 | while (1) { // sit in alarm loop |
sjk127 | 0:00a9f58f32d0 | 94 | alarm = 1; |
sjk127 | 0:00a9f58f32d0 | 95 | wait (0.05); |
sjk127 | 0:00a9f58f32d0 | 96 | alarm = 0; |
sjk127 | 0:00a9f58f32d0 | 97 | wait (0.1); |
sjk127 | 0:00a9f58f32d0 | 98 | alarm = 1; |
sjk127 | 0:00a9f58f32d0 | 99 | wait (0.1); |
sjk127 | 0:00a9f58f32d0 | 100 | alarm = 0; |
sjk127 | 0:00a9f58f32d0 | 101 | wait (0.5); |
sjk127 | 0:00a9f58f32d0 | 102 | if (switches == 0x08) { // has the alarm reset button been pressed |
sjk127 | 0:00a9f58f32d0 | 103 | break; |
sjk127 | 0:00a9f58f32d0 | 104 | } |
sjk127 | 0:00a9f58f32d0 | 105 | } |
sjk127 | 0:00a9f58f32d0 | 106 | sense.rise(NULL); // turn interrupt off (turned on when button pressed) |
sjk127 | 0:00a9f58f32d0 | 107 | herga_splash(); // refresh main menu screen |
sjk127 | 0:00a9f58f32d0 | 108 | lock = 0; // unlock keypad |
sjk127 | 0:00a9f58f32d0 | 109 | } |
sjk127 | 0:00a9f58f32d0 | 110 | |
sjk127 | 0:00a9f58f32d0 | 111 | //------------------------------------ |
sjk127 | 0:00a9f58f32d0 | 112 | |
sjk127 | 0:00a9f58f32d0 | 113 | void cpr_int () { // CPR button pressed |
sjk127 | 0:00a9f58f32d0 | 114 | wait(0.1); // wait incase spike |
sjk127 | 0:00a9f58f32d0 | 115 | if (cpr == 1) { // if button still pressed (ie not a spike) then continue |
sjk127 | 0:00a9f58f32d0 | 116 | alarmflag = 1; // set flag to exit RTZ corectly if CPR interrupts it |
sjk127 | 0:00a9f58f32d0 | 117 | fet1 = 0; // switch off motors |
sjk127 | 0:00a9f58f32d0 | 118 | fet2 = 0; |
sjk127 | 0:00a9f58f32d0 | 119 | wait(0.05); // wait for the induced motor current |
sjk127 | 0:00a9f58f32d0 | 120 | act2 = 0x00; // switch relays off (break motor) |
sjk127 | 0:00a9f58f32d0 | 121 | act1 = 0x00; |
sjk127 | 0:00a9f58f32d0 | 122 | lcd.cls(); // set up cpr alarm screen |
sjk127 | 0:00a9f58f32d0 | 123 | lcd.locate(3,1); |
sjk127 | 0:00a9f58f32d0 | 124 | lcd.printf("CPR EMERGENCY"); |
sjk127 | 0:00a9f58f32d0 | 125 | lcd.locate(1,2); |
sjk127 | 0:00a9f58f32d0 | 126 | lcd.printf("PRESS ALARM RESET"); |
sjk127 | 0:00a9f58f32d0 | 127 | while (1) { // alarm loop |
sjk127 | 0:00a9f58f32d0 | 128 | alarm = 1; |
sjk127 | 0:00a9f58f32d0 | 129 | wait (0.5); |
sjk127 | 0:00a9f58f32d0 | 130 | alarm = 0; |
sjk127 | 0:00a9f58f32d0 | 131 | wait (0.5); |
sjk127 | 0:00a9f58f32d0 | 132 | if (switches == 0x08) { // if alarm reset button pressed... |
sjk127 | 0:00a9f58f32d0 | 133 | break; // .. exit while loop |
sjk127 | 0:00a9f58f32d0 | 134 | } |
sjk127 | 0:00a9f58f32d0 | 135 | } |
sjk127 | 0:00a9f58f32d0 | 136 | if (lock ==1) { // if keypad locked ... |
sjk127 | 0:00a9f58f32d0 | 137 | lock = 0; // ....unlock keypad |
sjk127 | 0:00a9f58f32d0 | 138 | lcd.locate(0,0); |
sjk127 | 0:00a9f58f32d0 | 139 | lcd.printf(" "); |
sjk127 | 0:00a9f58f32d0 | 140 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 141 | lcd.printf(" KEYPAD UNLOCKED "); |
sjk127 | 0:00a9f58f32d0 | 142 | lcd.locate(0,2); |
sjk127 | 0:00a9f58f32d0 | 143 | lcd.printf(" "); |
sjk127 | 0:00a9f58f32d0 | 144 | wait(1); |
sjk127 | 0:00a9f58f32d0 | 145 | } |
sjk127 | 0:00a9f58f32d0 | 146 | herga_splash(); // refresh main screen |
sjk127 | 0:00a9f58f32d0 | 147 | } |
sjk127 | 0:00a9f58f32d0 | 148 | } |
sjk127 | 0:00a9f58f32d0 | 149 | |
sjk127 | 0:00a9f58f32d0 | 150 | //----------------------------------- |
sjk127 | 0:00a9f58f32d0 | 151 | |
sjk127 | 0:00a9f58f32d0 | 152 | void M2countup () { // counter for actuator 2 hall effect sensor |
sjk127 | 0:00a9f58f32d0 | 153 | if (M2direction == 1) { // check direction flag, set before motor started |
sjk127 | 0:00a9f58f32d0 | 154 | M2count++; // count up |
sjk127 | 0:00a9f58f32d0 | 155 | abs_M2count = abs(M2count); // counter register absoluted |
sjk127 | 0:00a9f58f32d0 | 156 | } |
sjk127 | 0:00a9f58f32d0 | 157 | if (M2direction == 0) { |
sjk127 | 0:00a9f58f32d0 | 158 | M2count--; |
sjk127 | 0:00a9f58f32d0 | 159 | abs_M2count = abs(M2count); |
sjk127 | 0:00a9f58f32d0 | 160 | } |
sjk127 | 0:00a9f58f32d0 | 161 | } |
sjk127 | 0:00a9f58f32d0 | 162 | |
sjk127 | 0:00a9f58f32d0 | 163 | void M1countup () { // counter for actuator 2 |
sjk127 | 0:00a9f58f32d0 | 164 | if (M1direction == 1) { // same as above |
sjk127 | 0:00a9f58f32d0 | 165 | M1count++; |
sjk127 | 0:00a9f58f32d0 | 166 | abs_M1count = abs(M1count); |
sjk127 | 0:00a9f58f32d0 | 167 | } |
sjk127 | 0:00a9f58f32d0 | 168 | if (M1direction == 0) { |
sjk127 | 0:00a9f58f32d0 | 169 | M1count--; |
sjk127 | 0:00a9f58f32d0 | 170 | abs_M1count = abs(M1count); |
sjk127 | 0:00a9f58f32d0 | 171 | } |
sjk127 | 0:00a9f58f32d0 | 172 | } |
sjk127 | 0:00a9f58f32d0 | 173 | |
sjk127 | 0:00a9f58f32d0 | 174 | //---------------------------------------- |
sjk127 | 0:00a9f58f32d0 | 175 | |
sjk127 | 0:00a9f58f32d0 | 176 | void rtz (void) { // returns each actuator to limits and zero's counters |
sjk127 | 0:00a9f58f32d0 | 177 | M1direction = 1; // set actuator direction |
sjk127 | 0:00a9f58f32d0 | 178 | M2direction = 1; // set actuator direction |
sjk127 | 0:00a9f58f32d0 | 179 | lcd.locate (7,0); // rtz screen |
sjk127 | 0:00a9f58f32d0 | 180 | lcd.printf (" "); |
sjk127 | 0:00a9f58f32d0 | 181 | lcd.locate (0,1); |
sjk127 | 0:00a9f58f32d0 | 182 | lcd.printf ("DOWN| RTZ | UP "); |
sjk127 | 0:00a9f58f32d0 | 183 | lcd.locate (6,2); |
sjk127 | 0:00a9f58f32d0 | 184 | lcd.printf (" "); // " " |
sjk127 | 0:00a9f58f32d0 | 185 | act2 = 0x02; // set M2 relays for up |
sjk127 | 0:00a9f58f32d0 | 186 | act1 = 0x01; // set M1 relays for down |
sjk127 | 0:00a9f58f32d0 | 187 | wait(0.015); // wait for relay contacts to close |
sjk127 | 0:00a9f58f32d0 | 188 | fet2 = 1; // switch motors on |
sjk127 | 0:00a9f58f32d0 | 189 | fet1 = 1; // " " |
sjk127 | 0:00a9f58f32d0 | 190 | wait (0.1); // wait for a couple of counts |
sjk127 | 0:00a9f58f32d0 | 191 | |
sjk127 | 0:00a9f58f32d0 | 192 | // sit in the following while loop until no more pulses are received from the hall effect sensor |
sjk127 | 0:00a9f58f32d0 | 193 | |
sjk127 | 0:00a9f58f32d0 | 194 | while (1) { |
sjk127 | 0:00a9f58f32d0 | 195 | pc.printf("abs_M2count = %d\n",abs_M2count); |
sjk127 | 0:00a9f58f32d0 | 196 | M2_old = abs_M2count; // take first count samples |
sjk127 | 0:00a9f58f32d0 | 197 | M1_old = abs_M1count; // " " |
sjk127 | 0:00a9f58f32d0 | 198 | wait (0.1); // wait for a few more |
sjk127 | 0:00a9f58f32d0 | 199 | M2_new = abs_M2count; // take second samples |
sjk127 | 0:00a9f58f32d0 | 200 | M1_new = abs_M1count; |
sjk127 | 0:00a9f58f32d0 | 201 | if ((M2_old - M2_new == 0)&&(M1_old - M1_new == 0)) { // when both actuators have reached the limits |
sjk127 | 0:00a9f58f32d0 | 202 | break; // exit while loop |
sjk127 | 0:00a9f58f32d0 | 203 | } |
sjk127 | 0:00a9f58f32d0 | 204 | } |
sjk127 | 0:00a9f58f32d0 | 205 | fet1 = 0; // turn off motors |
sjk127 | 0:00a9f58f32d0 | 206 | fet2 = 0; |
sjk127 | 0:00a9f58f32d0 | 207 | wait (0.05); // wait for induced current to dissipate |
sjk127 | 0:00a9f58f32d0 | 208 | act1 = 0; // turn relays off |
sjk127 | 0:00a9f58f32d0 | 209 | act2 = 0; |
sjk127 | 0:00a9f58f32d0 | 210 | wait(0.05); |
sjk127 | 0:00a9f58f32d0 | 211 | lcd.locate(7,0); // refresh main menu screen |
sjk127 | 0:00a9f58f32d0 | 212 | lcd.printf("HERGA"); |
sjk127 | 0:00a9f58f32d0 | 213 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 214 | lcd.printf(" | CONTROL | "); |
sjk127 | 0:00a9f58f32d0 | 215 | lcd.locate(6,2); |
sjk127 | 0:00a9f58f32d0 | 216 | lcd.printf("SYSTEMS"); |
sjk127 | 0:00a9f58f32d0 | 217 | if (alarmflag == 1) { // if RTZ has been interrupted by sensor or cpr alarm button |
sjk127 | 0:00a9f58f32d0 | 218 | alarmflag = 0; |
sjk127 | 0:00a9f58f32d0 | 219 | return; |
sjk127 | 0:00a9f58f32d0 | 220 | } |
sjk127 | 0:00a9f58f32d0 | 221 | M2count = 0; // if RTZ not interrupted exit RTZ.... |
sjk127 | 0:00a9f58f32d0 | 222 | M1count = 0; // ....having zero'd count registers |
sjk127 | 0:00a9f58f32d0 | 223 | abs_M2count = abs(M2count); |
sjk127 | 0:00a9f58f32d0 | 224 | abs_M1count = abs(M1count); |
sjk127 | 0:00a9f58f32d0 | 225 | } |
sjk127 | 0:00a9f58f32d0 | 226 | |
sjk127 | 0:00a9f58f32d0 | 227 | //---------------------------------------- |
sjk127 | 0:00a9f58f32d0 | 228 | void head_up () { |
sjk127 | 0:00a9f58f32d0 | 229 | M2direction = 1; // count direction flag |
sjk127 | 0:00a9f58f32d0 | 230 | lcd.locate(15,1); // pos cursor |
sjk127 | 0:00a9f58f32d0 | 231 | lcd.printf(" UP "); // disp UP |
sjk127 | 0:00a9f58f32d0 | 232 | act2 = 0x02; // set relay direction |
sjk127 | 0:00a9f58f32d0 | 233 | wait(0.015); // wait for relay contacts to close |
sjk127 | 0:00a9f58f32d0 | 234 | fet2 = 1; // switch FET on |
sjk127 | 0:00a9f58f32d0 | 235 | } |
sjk127 | 0:00a9f58f32d0 | 236 | //----------------------------------------- |
sjk127 | 0:00a9f58f32d0 | 237 | void head_down () { |
sjk127 | 0:00a9f58f32d0 | 238 | M2direction = 0; // count direction flag |
sjk127 | 0:00a9f58f32d0 | 239 | lcd.locate(15,1); // pos cursor |
sjk127 | 0:00a9f58f32d0 | 240 | lcd.printf("DOWN"); // disp DOWN |
sjk127 | 0:00a9f58f32d0 | 241 | act2 = 0x01; // set relay direction |
sjk127 | 0:00a9f58f32d0 | 242 | wait(0.015); // wait for relay contacts to close |
sjk127 | 0:00a9f58f32d0 | 243 | fet2 = 1; // switch FET on |
sjk127 | 0:00a9f58f32d0 | 244 | } |
sjk127 | 0:00a9f58f32d0 | 245 | //----------------------------------------- |
sjk127 | 0:00a9f58f32d0 | 246 | void head_off () { |
sjk127 | 0:00a9f58f32d0 | 247 | fet2 = 0; // turn FET off |
sjk127 | 0:00a9f58f32d0 | 248 | wait(0.05); // wait for induced motor current to dissipate |
sjk127 | 0:00a9f58f32d0 | 249 | act2 = 0x00; // break motor |
sjk127 | 0:00a9f58f32d0 | 250 | lcd.locate(15,1); // pos cursor |
sjk127 | 0:00a9f58f32d0 | 251 | lcd.printf(" "); // clear UP from screen |
sjk127 | 0:00a9f58f32d0 | 252 | } |
sjk127 | 0:00a9f58f32d0 | 253 | //------------------------------------------ |
sjk127 | 0:00a9f58f32d0 | 254 | void legs_up () { |
sjk127 | 0:00a9f58f32d0 | 255 | M1direction = 0; // count direction flag |
sjk127 | 0:00a9f58f32d0 | 256 | lcd.locate(0,1); // pos cursor |
sjk127 | 0:00a9f58f32d0 | 257 | lcd.printf(" UP "); // disp UP |
sjk127 | 0:00a9f58f32d0 | 258 | act1 = 0x02; // set relay direction |
sjk127 | 0:00a9f58f32d0 | 259 | wait(0.015); // wait for relay contacts to close |
sjk127 | 0:00a9f58f32d0 | 260 | fet1 = 1; // turn FET on |
sjk127 | 0:00a9f58f32d0 | 261 | } |
sjk127 | 0:00a9f58f32d0 | 262 | //------------------------------------------ |
sjk127 | 0:00a9f58f32d0 | 263 | void legs_down () { |
sjk127 | 0:00a9f58f32d0 | 264 | M1direction = 1; // count direction flag |
sjk127 | 0:00a9f58f32d0 | 265 | lcd.locate(0,1); // pos cursor |
sjk127 | 0:00a9f58f32d0 | 266 | lcd.printf("DOWN"); // disp DOWN |
sjk127 | 0:00a9f58f32d0 | 267 | act1 = 0x01; // set relay direction |
sjk127 | 0:00a9f58f32d0 | 268 | wait(0.015); // wait for relay contacts to close |
sjk127 | 0:00a9f58f32d0 | 269 | fet1 = 1; // turn FET on |
sjk127 | 0:00a9f58f32d0 | 270 | } |
sjk127 | 0:00a9f58f32d0 | 271 | //------------------------------------------ |
sjk127 | 0:00a9f58f32d0 | 272 | void legs_off () { |
sjk127 | 0:00a9f58f32d0 | 273 | fet1 = 0; // turn FET off |
sjk127 | 0:00a9f58f32d0 | 274 | wait(0.05); // wait for induced motor current to disipate |
sjk127 | 0:00a9f58f32d0 | 275 | act1 = 0x00; // break motor |
sjk127 | 0:00a9f58f32d0 | 276 | lcd.locate(0,1); // clear "UP" from screen |
sjk127 | 0:00a9f58f32d0 | 277 | lcd.printf(" "); |
sjk127 | 0:00a9f58f32d0 | 278 | } |
sjk127 | 0:00a9f58f32d0 | 279 | //------------------------------------------ |
sjk127 | 0:00a9f58f32d0 | 280 | void manual_head_run () { |
sjk127 | 0:00a9f58f32d0 | 281 | while (1) { |
sjk127 | 0:00a9f58f32d0 | 282 | pc.printf("abs_M2count = %d\n",abs_M2count); |
sjk127 | 0:00a9f58f32d0 | 283 | M2_old = abs_M2count; // take first sample |
sjk127 | 0:00a9f58f32d0 | 284 | wait (0.1); // allow some counts |
sjk127 | 0:00a9f58f32d0 | 285 | M2_new = abs_M2count; // take second sample |
sjk127 | 0:00a9f58f32d0 | 286 | if (M2_old - M2_new == 0) { // sub 2nd sample from 1st, if ans = 0 then limit switch is met... |
sjk127 | 0:00a9f58f32d0 | 287 | if (M2direction == 1) { |
sjk127 | 0:00a9f58f32d0 | 288 | M2count = 0; // .... therefore zero the count reg.... |
sjk127 | 0:00a9f58f32d0 | 289 | } |
sjk127 | 0:00a9f58f32d0 | 290 | abs_M2count = abs(M2count); // ... and the abs_M2count reg |
sjk127 | 0:00a9f58f32d0 | 291 | break; // exit |
sjk127 | 0:00a9f58f32d0 | 292 | } |
sjk127 | 0:00a9f58f32d0 | 293 | if (M2direction == 1 && switches != 0x02) { // if button released.... |
sjk127 | 0:00a9f58f32d0 | 294 | break; // exit |
sjk127 | 0:00a9f58f32d0 | 295 | } |
sjk127 | 0:00a9f58f32d0 | 296 | if (M2direction == 0 && switches != 0x01) { // if button released.... |
sjk127 | 0:00a9f58f32d0 | 297 | break; // exit |
sjk127 | 0:00a9f58f32d0 | 298 | } |
sjk127 | 0:00a9f58f32d0 | 299 | } |
sjk127 | 0:00a9f58f32d0 | 300 | } |
sjk127 | 0:00a9f58f32d0 | 301 | //----------------------------------------- |
sjk127 | 0:00a9f58f32d0 | 302 | void manual_legs_run () { |
sjk127 | 0:00a9f58f32d0 | 303 | while (1) { |
sjk127 | 0:00a9f58f32d0 | 304 | M1_old = abs_M1count; // take first sample |
sjk127 | 0:00a9f58f32d0 | 305 | wait (0.1); // allow some counts |
sjk127 | 0:00a9f58f32d0 | 306 | M1_new = abs_M1count; // take second sample |
sjk127 | 0:00a9f58f32d0 | 307 | if (M1_old - M1_new == 0) { // sub 2nd sample from 1st, if ans = 0 then limit switch is met... |
sjk127 | 0:00a9f58f32d0 | 308 | if (M1direction == 1) { |
sjk127 | 0:00a9f58f32d0 | 309 | M1count = 0; // .... therefore zero the count reg.... |
sjk127 | 0:00a9f58f32d0 | 310 | } |
sjk127 | 0:00a9f58f32d0 | 311 | abs_M1count = abs(M1count); // ... and the abs_M1count reg |
sjk127 | 0:00a9f58f32d0 | 312 | break; // .... therefore exit |
sjk127 | 0:00a9f58f32d0 | 313 | } |
sjk127 | 0:00a9f58f32d0 | 314 | if (M1direction == 1 && switches != 0x0A) { // if button released.... |
sjk127 | 0:00a9f58f32d0 | 315 | break; // exit |
sjk127 | 0:00a9f58f32d0 | 316 | } |
sjk127 | 0:00a9f58f32d0 | 317 | if (M1direction == 0 && switches != 0x03) { // if button released.... |
sjk127 | 0:00a9f58f32d0 | 318 | break; // exit |
sjk127 | 0:00a9f58f32d0 | 319 | } |
sjk127 | 0:00a9f58f32d0 | 320 | } |
sjk127 | 0:00a9f58f32d0 | 321 | } |
sjk127 | 0:00a9f58f32d0 | 322 | |
sjk127 | 0:00a9f58f32d0 | 323 | //----------------------------------------- |
sjk127 | 0:00a9f58f32d0 | 324 | |
sjk127 | 0:00a9f58f32d0 | 325 | void head_autorun () { |
sjk127 | 0:00a9f58f32d0 | 326 | while (1) { |
sjk127 | 0:00a9f58f32d0 | 327 | M2_old = abs_M2count; // take first sample |
sjk127 | 0:00a9f58f32d0 | 328 | wait (0.1); // allow some counts |
sjk127 | 0:00a9f58f32d0 | 329 | M2_new = abs_M2count; // take second sample |
sjk127 | 0:00a9f58f32d0 | 330 | result = M2_old - M2_new; |
sjk127 | 0:00a9f58f32d0 | 331 | pc.printf("abs_M2count = %d\n",abs_M2count); |
sjk127 | 0:00a9f58f32d0 | 332 | |
sjk127 | 0:00a9f58f32d0 | 333 | if ((abs_M2count >= h_low )&&(abs_M2count <= h_high)) { |
sjk127 | 0:00a9f58f32d0 | 334 | break; |
sjk127 | 0:00a9f58f32d0 | 335 | } else if (alarmflag ==1) { |
sjk127 | 0:00a9f58f32d0 | 336 | alarmflag = 0; |
sjk127 | 0:00a9f58f32d0 | 337 | break; |
sjk127 | 0:00a9f58f32d0 | 338 | } else if (result == 0) { // sub 2nd sample from 1st, if ans = 0 then limit switch is met... |
sjk127 | 0:00a9f58f32d0 | 339 | if (M2direction == 1) { |
sjk127 | 0:00a9f58f32d0 | 340 | M2count = 0; // .... therefore zero the count reg.... |
sjk127 | 0:00a9f58f32d0 | 341 | } |
sjk127 | 0:00a9f58f32d0 | 342 | abs_M2count = abs(M2count); // ... and the abs_M1count reg |
sjk127 | 0:00a9f58f32d0 | 343 | break; |
sjk127 | 0:00a9f58f32d0 | 344 | } else if (switches == 0x01 || switches ==0x02) { |
sjk127 | 0:00a9f58f32d0 | 345 | break; |
sjk127 | 0:00a9f58f32d0 | 346 | } |
sjk127 | 0:00a9f58f32d0 | 347 | } |
sjk127 | 0:00a9f58f32d0 | 348 | } |
sjk127 | 0:00a9f58f32d0 | 349 | |
sjk127 | 0:00a9f58f32d0 | 350 | //----------------------------------------- |
sjk127 | 0:00a9f58f32d0 | 351 | |
sjk127 | 0:00a9f58f32d0 | 352 | void legs_autorun () { |
sjk127 | 0:00a9f58f32d0 | 353 | while (1) { |
sjk127 | 0:00a9f58f32d0 | 354 | M1_old = abs_M1count; // take first sample |
sjk127 | 0:00a9f58f32d0 | 355 | wait (0.1); // allow some counts |
sjk127 | 0:00a9f58f32d0 | 356 | M1_new = abs_M1count; // take second sample |
sjk127 | 0:00a9f58f32d0 | 357 | result = M1_old - M1_new; |
sjk127 | 0:00a9f58f32d0 | 358 | if ((abs_M1count >= l_low )&&(abs_M1count <= l_high)) { |
sjk127 | 0:00a9f58f32d0 | 359 | break; |
sjk127 | 0:00a9f58f32d0 | 360 | } else if (alarmflag ==1) { |
sjk127 | 0:00a9f58f32d0 | 361 | alarmflag = 0; |
sjk127 | 0:00a9f58f32d0 | 362 | break; |
sjk127 | 0:00a9f58f32d0 | 363 | } else if (result == 0) { |
sjk127 | 0:00a9f58f32d0 | 364 | if (M1direction == 1) { |
sjk127 | 0:00a9f58f32d0 | 365 | M1count = 0; // .... therefore zero the count reg.... |
sjk127 | 0:00a9f58f32d0 | 366 | } |
sjk127 | 0:00a9f58f32d0 | 367 | abs_M1count = M1count; // ... and the abs_M1count reg |
sjk127 | 0:00a9f58f32d0 | 368 | break; // .... therefore exit |
sjk127 | 0:00a9f58f32d0 | 369 | } else if (switches == 0x03 || switches == 0x0A) { |
sjk127 | 0:00a9f58f32d0 | 370 | break; |
sjk127 | 0:00a9f58f32d0 | 371 | } |
sjk127 | 0:00a9f58f32d0 | 372 | } |
sjk127 | 0:00a9f58f32d0 | 373 | } |
sjk127 | 0:00a9f58f32d0 | 374 | |
sjk127 | 0:00a9f58f32d0 | 375 | //-----------------------MAIN------------------------------- |
sjk127 | 0:00a9f58f32d0 | 376 | |
sjk127 | 0:00a9f58f32d0 | 377 | int main() { |
sjk127 | 0:00a9f58f32d0 | 378 | |
sjk127 | 0:00a9f58f32d0 | 379 | herga_splash(); // display main screen |
sjk127 | 0:00a9f58f32d0 | 380 | M2counter.rise(&M2countup); // set up actuator 2 counter interrupt |
sjk127 | 0:00a9f58f32d0 | 381 | M1counter.rise(&M1countup); // set up actuator 1 counter interrupt |
sjk127 | 0:00a9f58f32d0 | 382 | rtz(); // zero both actuators |
sjk127 | 0:00a9f58f32d0 | 383 | timer1.start(); // start timer for switch data bus read |
sjk127 | 0:00a9f58f32d0 | 384 | wait_ms(10); // delay between timer starts, probably not necessary !! |
sjk127 | 0:00a9f58f32d0 | 385 | timer2.start(); // start timer for potentiometer 1 read |
sjk127 | 0:00a9f58f32d0 | 386 | wait_ms(10); // again prob not necessary !! |
sjk127 | 0:00a9f58f32d0 | 387 | timer3.start(); // start timer for potentiometer 2 read |
sjk127 | 0:00a9f58f32d0 | 388 | cpr.rise(&cpr_int); // set up cpr button interrupt |
sjk127 | 0:00a9f58f32d0 | 389 | M2count = 0; // zero counter registers |
sjk127 | 0:00a9f58f32d0 | 390 | M1count = 0; |
sjk127 | 0:00a9f58f32d0 | 391 | lock = 0; // unlock the keypad functions |
sjk127 | 0:00a9f58f32d0 | 392 | sense.mode(PullNone); // opto sensor has external pull up |
sjk127 | 0:00a9f58f32d0 | 393 | |
sjk127 | 0:00a9f58f32d0 | 394 | while (1) { |
sjk127 | 0:00a9f58f32d0 | 395 | |
sjk127 | 0:00a9f58f32d0 | 396 | if (timer1.read_ms()>=5) { // every 5 ms the state of the switch input bus is read |
sjk127 | 0:00a9f58f32d0 | 397 | if (lock == 0) { // when locked do not enter to check switch bus |
sjk127 | 0:00a9f58f32d0 | 398 | |
sjk127 | 0:00a9f58f32d0 | 399 | if (switches == 0x09) { // if F2 (switch bus code 0x09) is pressed.... |
sjk127 | 0:00a9f58f32d0 | 400 | fh_target = abs_M2count; // .... store current actuator positions |
sjk127 | 0:00a9f58f32d0 | 401 | fh_low = fh_target - 2; // create lower count limit |
sjk127 | 0:00a9f58f32d0 | 402 | fh_high = fh_target + 2; // create upper count limit |
sjk127 | 0:00a9f58f32d0 | 403 | |
sjk127 | 0:00a9f58f32d0 | 404 | fl_target = abs_M1count; |
sjk127 | 0:00a9f58f32d0 | 405 | fl_low = fl_target - 2; |
sjk127 | 0:00a9f58f32d0 | 406 | fl_high = fl_target + 2; |
sjk127 | 0:00a9f58f32d0 | 407 | |
sjk127 | 0:00a9f58f32d0 | 408 | //pc.printf("fh_target = %d\n",fh_target); //for debugging |
sjk127 | 0:00a9f58f32d0 | 409 | //pc.printf("fh_low = %d\n",fh_low); |
sjk127 | 0:00a9f58f32d0 | 410 | //pc.printf("fh_high = %d\n",fh_high); |
sjk127 | 0:00a9f58f32d0 | 411 | |
sjk127 | 0:00a9f58f32d0 | 412 | lcd.locate(0,0); // refresh main menu screen |
sjk127 | 0:00a9f58f32d0 | 413 | lcd.printf(" SAVING "); |
sjk127 | 0:00a9f58f32d0 | 414 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 415 | lcd.printf(" USER "); |
sjk127 | 0:00a9f58f32d0 | 416 | lcd.locate(0,2); |
sjk127 | 0:00a9f58f32d0 | 417 | lcd.printf(" PRESET "); |
sjk127 | 0:00a9f58f32d0 | 418 | |
sjk127 | 0:00a9f58f32d0 | 419 | while (switches != 0x00) {} // wait till F2 button released |
sjk127 | 0:00a9f58f32d0 | 420 | wait(1); |
sjk127 | 0:00a9f58f32d0 | 421 | lcd.locate(0,0); |
sjk127 | 0:00a9f58f32d0 | 422 | lcd.printf("LEGS| HERGA |HEAD"); // refresh main screen |
sjk127 | 0:00a9f58f32d0 | 423 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 424 | lcd.printf(" | CONTROL | "); |
sjk127 | 0:00a9f58f32d0 | 425 | lcd.locate(0,2); |
sjk127 | 0:00a9f58f32d0 | 426 | lcd.printf(" | SYSTEMS | "); |
sjk127 | 0:00a9f58f32d0 | 427 | |
sjk127 | 0:00a9f58f32d0 | 428 | F2_flag = 1; // If this 'if' has been entered do not check the switch bus again |
sjk127 | 0:00a9f58f32d0 | 429 | } |
sjk127 | 0:00a9f58f32d0 | 430 | } |
sjk127 | 0:00a9f58f32d0 | 431 | if (F2_flag == 0) { |
sjk127 | 0:00a9f58f32d0 | 432 | bus_state = switches; |
sjk127 | 0:00a9f58f32d0 | 433 | } |
sjk127 | 0:00a9f58f32d0 | 434 | F2_flag = 0; // clear flag for next time around |
sjk127 | 0:00a9f58f32d0 | 435 | timer1.reset(); |
sjk127 | 0:00a9f58f32d0 | 436 | } |
sjk127 | 0:00a9f58f32d0 | 437 | |
sjk127 | 0:00a9f58f32d0 | 438 | // the pots used in this code are softpots embedded into a label and therefore act in a momentary way, |
sjk127 | 0:00a9f58f32d0 | 439 | // ie when you press the pot you get a voltage level out of the wiper relevant to the position |
sjk127 | 0:00a9f58f32d0 | 440 | // along the pots length and when you release it the output returns to 0V! |
sjk127 | 0:00a9f58f32d0 | 441 | // search for spectrasymbol in google for more info |
sjk127 | 0:00a9f58f32d0 | 442 | |
sjk127 | 0:00a9f58f32d0 | 443 | if (timer2.read_ms()>=50) { // every 50ms the head pot (act 2) is checked |
sjk127 | 0:00a9f58f32d0 | 444 | hpot = h_pot; // grab the pot value |
sjk127 | 0:00a9f58f32d0 | 445 | if (hpot >0.001) { // has it been pressed? |
sjk127 | 0:00a9f58f32d0 | 446 | wait (0.05); // if yes wait incase it was noise |
sjk127 | 0:00a9f58f32d0 | 447 | hpot = h_pot; // grab the pot value again |
sjk127 | 0:00a9f58f32d0 | 448 | if (hpot<0) { // if below 0 do nothing (i know.. not needed!) |
sjk127 | 0:00a9f58f32d0 | 449 | bus_state = 0; |
sjk127 | 0:00a9f58f32d0 | 450 | } else if (hpot>0.02 && hpot<0.1) { // if between these values |
sjk127 | 0:00a9f58f32d0 | 451 | bus_state = 0x0F; // set bus_state for case statement later |
sjk127 | 0:00a9f58f32d0 | 452 | h_target = 1200; // set counter target |
sjk127 | 0:00a9f58f32d0 | 453 | h_high = 1202; // set upper limit |
sjk127 | 0:00a9f58f32d0 | 454 | h_low = 1198; // set lower limit |
sjk127 | 0:00a9f58f32d0 | 455 | // limits help in case counter encounters some noise and |
sjk127 | 0:00a9f58f32d0 | 456 | // picks up a spurious rising edge |
sjk127 | 0:00a9f58f32d0 | 457 | } else if (hpot>0.15 && hpot<0.2) { |
sjk127 | 0:00a9f58f32d0 | 458 | bus_state = 0x0F; |
sjk127 | 0:00a9f58f32d0 | 459 | h_target = 900; |
sjk127 | 0:00a9f58f32d0 | 460 | h_high = 902; |
sjk127 | 0:00a9f58f32d0 | 461 | h_low = 898; |
sjk127 | 0:00a9f58f32d0 | 462 | } else if (hpot>0.27 && hpot<0.37) { |
sjk127 | 0:00a9f58f32d0 | 463 | bus_state = 0x0F; |
sjk127 | 0:00a9f58f32d0 | 464 | h_target = 600; |
sjk127 | 0:00a9f58f32d0 | 465 | h_high = 602; |
sjk127 | 0:00a9f58f32d0 | 466 | h_low = 598; |
sjk127 | 0:00a9f58f32d0 | 467 | } else if (hpot>0.45 && hpot<0.55) { |
sjk127 | 0:00a9f58f32d0 | 468 | bus_state = 0x0F; |
sjk127 | 0:00a9f58f32d0 | 469 | h_target = 300; |
sjk127 | 0:00a9f58f32d0 | 470 | h_high = 302; |
sjk127 | 0:00a9f58f32d0 | 471 | h_low = 298; |
sjk127 | 0:00a9f58f32d0 | 472 | } else if (hpot>0.65) { |
sjk127 | 0:00a9f58f32d0 | 473 | bus_state = 0x0F; |
sjk127 | 0:00a9f58f32d0 | 474 | h_target = 0; |
sjk127 | 0:00a9f58f32d0 | 475 | h_high = -2; |
sjk127 | 0:00a9f58f32d0 | 476 | h_low = 2; |
sjk127 | 0:00a9f58f32d0 | 477 | } |
sjk127 | 0:00a9f58f32d0 | 478 | } |
sjk127 | 0:00a9f58f32d0 | 479 | timer2.reset(); |
sjk127 | 0:00a9f58f32d0 | 480 | } |
sjk127 | 0:00a9f58f32d0 | 481 | |
sjk127 | 0:00a9f58f32d0 | 482 | if (timer3.read_ms()>=50) { // as above but for legs pot (act 1) |
sjk127 | 0:00a9f58f32d0 | 483 | lpot = l_pot; |
sjk127 | 0:00a9f58f32d0 | 484 | if (lpot >0.001) { |
sjk127 | 0:00a9f58f32d0 | 485 | wait (0.05); |
sjk127 | 0:00a9f58f32d0 | 486 | lpot = l_pot; |
sjk127 | 0:00a9f58f32d0 | 487 | if (lpot<0) { |
sjk127 | 0:00a9f58f32d0 | 488 | bus_state = 0; |
sjk127 | 0:00a9f58f32d0 | 489 | } else if (lpot>0.02 && lpot<0.1) { |
sjk127 | 0:00a9f58f32d0 | 490 | bus_state = 0x0E; |
sjk127 | 0:00a9f58f32d0 | 491 | l_target = 0; |
sjk127 | 0:00a9f58f32d0 | 492 | l_high = -2; |
sjk127 | 0:00a9f58f32d0 | 493 | l_low = 2; |
sjk127 | 0:00a9f58f32d0 | 494 | } else if (lpot>0.15 && lpot<0.2) { |
sjk127 | 0:00a9f58f32d0 | 495 | bus_state = 0x0E; |
sjk127 | 0:00a9f58f32d0 | 496 | l_target = 300; |
sjk127 | 0:00a9f58f32d0 | 497 | l_high = 302; |
sjk127 | 0:00a9f58f32d0 | 498 | l_low = 298; |
sjk127 | 0:00a9f58f32d0 | 499 | } else if (lpot>0.27 && lpot<0.37) { |
sjk127 | 0:00a9f58f32d0 | 500 | bus_state = 0x0E; |
sjk127 | 0:00a9f58f32d0 | 501 | l_target = 600; |
sjk127 | 0:00a9f58f32d0 | 502 | l_high = 602; |
sjk127 | 0:00a9f58f32d0 | 503 | l_low = 598; |
sjk127 | 0:00a9f58f32d0 | 504 | } else if (lpot>0.45 && lpot<0.55) { |
sjk127 | 0:00a9f58f32d0 | 505 | bus_state = 0x0E; |
sjk127 | 0:00a9f58f32d0 | 506 | l_target = 900; |
sjk127 | 0:00a9f58f32d0 | 507 | l_high = 902; |
sjk127 | 0:00a9f58f32d0 | 508 | l_low = 898; |
sjk127 | 0:00a9f58f32d0 | 509 | } else if (lpot>0.65) { |
sjk127 | 0:00a9f58f32d0 | 510 | bus_state = 0x0E; |
sjk127 | 0:00a9f58f32d0 | 511 | l_target = 1200; |
sjk127 | 0:00a9f58f32d0 | 512 | l_high = 1202; |
sjk127 | 0:00a9f58f32d0 | 513 | l_low = 1198; |
sjk127 | 0:00a9f58f32d0 | 514 | } |
sjk127 | 0:00a9f58f32d0 | 515 | } |
sjk127 | 0:00a9f58f32d0 | 516 | timer3.reset(); |
sjk127 | 0:00a9f58f32d0 | 517 | } |
sjk127 | 0:00a9f58f32d0 | 518 | |
sjk127 | 0:00a9f58f32d0 | 519 | if (bus_state == 0x0C) { // toggle lock to block button inputs |
sjk127 | 0:00a9f58f32d0 | 520 | lock=!lock; |
sjk127 | 0:00a9f58f32d0 | 521 | if (lock == 1) { // locked so inform user |
sjk127 | 0:00a9f58f32d0 | 522 | lcd.locate(0,0); |
sjk127 | 0:00a9f58f32d0 | 523 | lcd.printf(" "); |
sjk127 | 0:00a9f58f32d0 | 524 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 525 | lcd.printf(" KEYPAD LOCKED "); |
sjk127 | 0:00a9f58f32d0 | 526 | lcd.locate(0,2); |
sjk127 | 0:00a9f58f32d0 | 527 | lcd.printf(" "); |
sjk127 | 0:00a9f58f32d0 | 528 | } else if (lock == 0) { // unlocked so return to main screen |
sjk127 | 0:00a9f58f32d0 | 529 | herga_splash(); |
sjk127 | 0:00a9f58f32d0 | 530 | } |
sjk127 | 0:00a9f58f32d0 | 531 | while (switches != 0x00) {} |
sjk127 | 0:00a9f58f32d0 | 532 | } |
sjk127 | 0:00a9f58f32d0 | 533 | |
sjk127 | 0:00a9f58f32d0 | 534 | |
sjk127 | 0:00a9f58f32d0 | 535 | if (lock == 0) { // when unlocked check all other buttons |
sjk127 | 0:00a9f58f32d0 | 536 | switch (bus_state) { |
sjk127 | 0:00a9f58f32d0 | 537 | case 0x02: // head up button pressed |
sjk127 | 0:00a9f58f32d0 | 538 | head_up(); // set direction of actuator |
sjk127 | 0:00a9f58f32d0 | 539 | wait(0.1); // wait to accumulate some pulses (needed if at 0) |
sjk127 | 0:00a9f58f32d0 | 540 | manual_head_run(); // move actuator |
sjk127 | 0:00a9f58f32d0 | 541 | head_off(); // stop actuator |
sjk127 | 0:00a9f58f32d0 | 542 | while (switches != 0x00) {} // stop any other button having an effect |
sjk127 | 0:00a9f58f32d0 | 543 | break; |
sjk127 | 0:00a9f58f32d0 | 544 | case 0x01: // head down button pressed |
sjk127 | 0:00a9f58f32d0 | 545 | head_down(); // set direction of actuator |
sjk127 | 0:00a9f58f32d0 | 546 | wait(0.1); // wait to accumulate some pulses (needed if at 0) |
sjk127 | 0:00a9f58f32d0 | 547 | manual_head_run(); // move actuator |
sjk127 | 0:00a9f58f32d0 | 548 | head_off(); // stop actuator |
sjk127 | 0:00a9f58f32d0 | 549 | while (switches != 0x00) {} // stop any other button having an effect |
sjk127 | 0:00a9f58f32d0 | 550 | break; |
sjk127 | 0:00a9f58f32d0 | 551 | case 0x03: // legs up button pressed |
sjk127 | 0:00a9f58f32d0 | 552 | legs_up(); // set direction of actuator |
sjk127 | 0:00a9f58f32d0 | 553 | wait(0.1); // wait to accumulate some pulses (needed if at 0) // |
sjk127 | 0:00a9f58f32d0 | 554 | manual_legs_run(); // move actuator |
sjk127 | 0:00a9f58f32d0 | 555 | legs_off(); // stop actuator |
sjk127 | 0:00a9f58f32d0 | 556 | while (switches != 0x00) {} // stop any other button having an effect |
sjk127 | 0:00a9f58f32d0 | 557 | break; |
sjk127 | 0:00a9f58f32d0 | 558 | case 0x0A: // legs down button pressed |
sjk127 | 0:00a9f58f32d0 | 559 | legs_down(); // set direction of actuator |
sjk127 | 0:00a9f58f32d0 | 560 | wait(0.1); // wait to accumulate some pulses (needed if at 0) |
sjk127 | 0:00a9f58f32d0 | 561 | manual_legs_run(); // move actuator |
sjk127 | 0:00a9f58f32d0 | 562 | legs_off(); // stop actuator |
sjk127 | 0:00a9f58f32d0 | 563 | while (switches != 0x00) {} // stop any other button having an effect |
sjk127 | 0:00a9f58f32d0 | 564 | break; |
sjk127 | 0:00a9f58f32d0 | 565 | case 0x06: // return to zero position |
sjk127 | 0:00a9f58f32d0 | 566 | rtz(); |
sjk127 | 0:00a9f58f32d0 | 567 | break; |
sjk127 | 0:00a9f58f32d0 | 568 | //***** HEAD AUTO |
sjk127 | 0:00a9f58f32d0 | 569 | case 0x0F: // had an input on head pot |
sjk127 | 0:00a9f58f32d0 | 570 | result = abs_M2count - h_target; // decide on motor direction |
sjk127 | 0:00a9f58f32d0 | 571 | if (result > 0) { // result is positive so set up move up |
sjk127 | 0:00a9f58f32d0 | 572 | head_up(); |
sjk127 | 0:00a9f58f32d0 | 573 | } else if (result < 0 ) { |
sjk127 | 0:00a9f58f32d0 | 574 | head_down(); // result is negative so set up move down |
sjk127 | 0:00a9f58f32d0 | 575 | } |
sjk127 | 0:00a9f58f32d0 | 576 | head_autorun(); // move act 2 to target position |
sjk127 | 0:00a9f58f32d0 | 577 | head_off(); |
sjk127 | 0:00a9f58f32d0 | 578 | while (switches != 0x00) {} // sit here until button released |
sjk127 | 0:00a9f58f32d0 | 579 | break; |
sjk127 | 0:00a9f58f32d0 | 580 | //****** LEGS AUTO |
sjk127 | 0:00a9f58f32d0 | 581 | case 0x0E: |
sjk127 | 0:00a9f58f32d0 | 582 | result = abs_M1count - l_target; // do calculation to determine direction |
sjk127 | 0:00a9f58f32d0 | 583 | if (result < 0) { // if result is negative set up to move up |
sjk127 | 0:00a9f58f32d0 | 584 | legs_up(); |
sjk127 | 0:00a9f58f32d0 | 585 | } else if (result > 0 ) { // if positive result set up to move down |
sjk127 | 0:00a9f58f32d0 | 586 | legs_down(); |
sjk127 | 0:00a9f58f32d0 | 587 | } |
sjk127 | 0:00a9f58f32d0 | 588 | legs_autorun(); // move act 1 to target position |
sjk127 | 0:00a9f58f32d0 | 589 | legs_off(); |
sjk127 | 0:00a9f58f32d0 | 590 | while (switches != 0x00) {} |
sjk127 | 0:00a9f58f32d0 | 591 | break; |
sjk127 | 0:00a9f58f32d0 | 592 | //****** switch sensor on/off |
sjk127 | 0:00a9f58f32d0 | 593 | case 0x04 : // sensor button has been pressed so toggle sensor arm/disarm |
sjk127 | 0:00a9f58f32d0 | 594 | senseflag = !senseflag; // invert senseflag |
sjk127 | 0:00a9f58f32d0 | 595 | if (senseflag == 0) { // if senseflag = 0 |
sjk127 | 0:00a9f58f32d0 | 596 | lcd.locate(17,3); // display OFF (PATIENT DETECT "OFF") |
sjk127 | 0:00a9f58f32d0 | 597 | lcd.printf("FF "); |
sjk127 | 0:00a9f58f32d0 | 598 | sense.rise(NULL); // ignore patient detect sensor |
sjk127 | 0:00a9f58f32d0 | 599 | } else if (senseflag == 1) { // if senseflag = 1 |
sjk127 | 0:00a9f58f32d0 | 600 | lcd.locate(17,3); // display OFF (PATIENT DETECT "ON") |
sjk127 | 0:00a9f58f32d0 | 601 | lcd.printf("N "); |
sjk127 | 0:00a9f58f32d0 | 602 | sense.rise(&sense_int); // arm patient detect sensor |
sjk127 | 0:00a9f58f32d0 | 603 | } |
sjk127 | 0:00a9f58f32d0 | 604 | while (switches != 0x00) {} |
sjk127 | 0:00a9f58f32d0 | 605 | break; |
sjk127 | 0:00a9f58f32d0 | 606 | //******* F1 |
sjk127 | 0:00a9f58f32d0 | 607 | case 0x05: // F1 pressed so move both actuators to stored targets |
sjk127 | 0:00a9f58f32d0 | 608 | result_h = abs_M2count - fh_target; |
sjk127 | 0:00a9f58f32d0 | 609 | if (result_h > 0) { |
sjk127 | 0:00a9f58f32d0 | 610 | M2direction = 1; |
sjk127 | 0:00a9f58f32d0 | 611 | lcd.locate(15,1); |
sjk127 | 0:00a9f58f32d0 | 612 | lcd.printf(" UP "); |
sjk127 | 0:00a9f58f32d0 | 613 | act2 = 0x02; |
sjk127 | 0:00a9f58f32d0 | 614 | } else if (result_h < 0 ) { |
sjk127 | 0:00a9f58f32d0 | 615 | M2direction = 0; |
sjk127 | 0:00a9f58f32d0 | 616 | lcd.locate(15,1); |
sjk127 | 0:00a9f58f32d0 | 617 | lcd.printf("DOWN"); |
sjk127 | 0:00a9f58f32d0 | 618 | act2 = 0x01; |
sjk127 | 0:00a9f58f32d0 | 619 | } |
sjk127 | 0:00a9f58f32d0 | 620 | result_l = abs_M1count - fl_target; |
sjk127 | 0:00a9f58f32d0 | 621 | if (result_l < 0) { // if result is negative actuator must move up |
sjk127 | 0:00a9f58f32d0 | 622 | M1direction = 0; // decrement counter flag |
sjk127 | 0:00a9f58f32d0 | 623 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 624 | lcd.printf(" UP "); |
sjk127 | 0:00a9f58f32d0 | 625 | act1 = 0x02; |
sjk127 | 0:00a9f58f32d0 | 626 | } else if (result_l > 0 ) { // if the result of the first calc was positive |
sjk127 | 0:00a9f58f32d0 | 627 | pc.printf("M1 down\n"); |
sjk127 | 0:00a9f58f32d0 | 628 | M1direction = 1; // the acutator must move down |
sjk127 | 0:00a9f58f32d0 | 629 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 630 | act1 = 0x01; |
sjk127 | 0:00a9f58f32d0 | 631 | } |
sjk127 | 0:00a9f58f32d0 | 632 | fet1 = 1; |
sjk127 | 0:00a9f58f32d0 | 633 | fet2 = 1; |
sjk127 | 0:00a9f58f32d0 | 634 | while (1) { |
sjk127 | 0:00a9f58f32d0 | 635 | if (abs_M1count >= fl_low && abs_M1count <= fl_high) { |
sjk127 | 0:00a9f58f32d0 | 636 | fet1 = 0; |
sjk127 | 0:00a9f58f32d0 | 637 | wait(0.05); |
sjk127 | 0:00a9f58f32d0 | 638 | act1 = 0x00; |
sjk127 | 0:00a9f58f32d0 | 639 | M1flag =1; |
sjk127 | 0:00a9f58f32d0 | 640 | } |
sjk127 | 0:00a9f58f32d0 | 641 | if (abs_M2count >= fh_low &&abs_M2count <= fh_high) { |
sjk127 | 0:00a9f58f32d0 | 642 | fet2 = 0; |
sjk127 | 0:00a9f58f32d0 | 643 | wait(0.05); |
sjk127 | 0:00a9f58f32d0 | 644 | act2 = 0x00; |
sjk127 | 0:00a9f58f32d0 | 645 | M2flag =1; |
sjk127 | 0:00a9f58f32d0 | 646 | } |
sjk127 | 0:00a9f58f32d0 | 647 | M1_old = abs_M1count; // take first sample |
sjk127 | 0:00a9f58f32d0 | 648 | M2_old = abs_M2count; |
sjk127 | 0:00a9f58f32d0 | 649 | wait (0.1); // allow some counts |
sjk127 | 0:00a9f58f32d0 | 650 | M1_new = abs_M1count; // take second sample |
sjk127 | 0:00a9f58f32d0 | 651 | M2_new = abs_M2count; // take second sample |
sjk127 | 0:00a9f58f32d0 | 652 | result_h = M2_old - M2_new; |
sjk127 | 0:00a9f58f32d0 | 653 | result_l = M1_old - M1_new; |
sjk127 | 0:00a9f58f32d0 | 654 | |
sjk127 | 0:00a9f58f32d0 | 655 | if (M1flag == 1 && M2flag == 1) { |
sjk127 | 0:00a9f58f32d0 | 656 | break; |
sjk127 | 0:00a9f58f32d0 | 657 | } else if (alarmflag ==1) { |
sjk127 | 0:00a9f58f32d0 | 658 | alarmflag = 0; |
sjk127 | 0:00a9f58f32d0 | 659 | break; |
sjk127 | 0:00a9f58f32d0 | 660 | } else if ((result_h == 0) && (result_l == 0)) { |
sjk127 | 0:00a9f58f32d0 | 661 | if (M1count < 10) { |
sjk127 | 0:00a9f58f32d0 | 662 | M1count = 0; // .... therefore zero the count reg.... |
sjk127 | 0:00a9f58f32d0 | 663 | } |
sjk127 | 0:00a9f58f32d0 | 664 | abs_M1count = M1count; // ... and the abs_M1count reg |
sjk127 | 0:00a9f58f32d0 | 665 | if (M2count <10) { |
sjk127 | 0:00a9f58f32d0 | 666 | M2count = 0; // .... therefore zero the count reg.... |
sjk127 | 0:00a9f58f32d0 | 667 | } |
sjk127 | 0:00a9f58f32d0 | 668 | abs_M2count = M2count; // ... and the abs_M1count reg |
sjk127 | 0:00a9f58f32d0 | 669 | break; // .... therefore exit*/ |
sjk127 | 0:00a9f58f32d0 | 670 | } else if (switches == 0x01 || switches == 0x02 || switches == 0x03 || switches == 0x0A ) { |
sjk127 | 0:00a9f58f32d0 | 671 | break; |
sjk127 | 0:00a9f58f32d0 | 672 | } |
sjk127 | 0:00a9f58f32d0 | 673 | } |
sjk127 | 0:00a9f58f32d0 | 674 | M1flag = 0; |
sjk127 | 0:00a9f58f32d0 | 675 | M2flag = 0; |
sjk127 | 0:00a9f58f32d0 | 676 | fet1 = 0; |
sjk127 | 0:00a9f58f32d0 | 677 | fet2 = 0; |
sjk127 | 0:00a9f58f32d0 | 678 | wait(0.05); |
sjk127 | 0:00a9f58f32d0 | 679 | act1 = 0x00; |
sjk127 | 0:00a9f58f32d0 | 680 | act2 = 0x00; |
sjk127 | 0:00a9f58f32d0 | 681 | lcd.locate(0,1); |
sjk127 | 0:00a9f58f32d0 | 682 | lcd.printf(" "); |
sjk127 | 0:00a9f58f32d0 | 683 | lcd.locate(15,1); |
sjk127 | 0:00a9f58f32d0 | 684 | lcd.printf(" "); |
sjk127 | 0:00a9f58f32d0 | 685 | while (switches !=0) {} |
sjk127 | 0:00a9f58f32d0 | 686 | break; |
sjk127 | 0:00a9f58f32d0 | 687 | } |
sjk127 | 0:00a9f58f32d0 | 688 | |
sjk127 | 0:00a9f58f32d0 | 689 | } |
sjk127 | 0:00a9f58f32d0 | 690 | } |
sjk127 | 0:00a9f58f32d0 | 691 | } |
sjk127 | 0:00a9f58f32d0 | 692 | |
sjk127 | 0:00a9f58f32d0 | 693 | |
sjk127 | 0:00a9f58f32d0 | 694 | |
sjk127 | 0:00a9f58f32d0 | 695 | |
sjk127 | 0:00a9f58f32d0 | 696 | |
sjk127 | 0:00a9f58f32d0 | 697 | |
sjk127 | 0:00a9f58f32d0 | 698 | |
sjk127 | 0:00a9f58f32d0 | 699 | |
sjk127 | 0:00a9f58f32d0 | 700 | |
sjk127 | 0:00a9f58f32d0 | 701 | |
sjk127 | 0:00a9f58f32d0 | 702 | |
sjk127 | 0:00a9f58f32d0 | 703 |