| 1 |
|
|---|
| 2 | /*
|
|---|
| 3 | Arduino Ethernet for FACT Interlock
|
|---|
| 4 |
|
|---|
| 5 | Written by Dominik Neise
|
|---|
| 6 | June 2012
|
|---|
| 7 | TU Dortmund
|
|---|
| 8 |
|
|---|
| 9 | The FACT Camera as well as the bias voltage supply for its G-APDs
|
|---|
| 10 | may only be switched on, if the dedicated cooling systems are running.
|
|---|
| 11 | This arduino is connected via optocouplers to some status LEDs,
|
|---|
| 12 | and other coolings system output signals, which show
|
|---|
| 13 | the status of both cooling systems:
|
|---|
| 14 | * water cooling pump
|
|---|
| 15 | - flow meter
|
|---|
| 16 | - level meter
|
|---|
| 17 | * bias crate fans
|
|---|
| 18 | - rotation frequency
|
|---|
| 19 | A simple WebServer is implemented for showing the
|
|---|
| 20 | status and access the actual switches.
|
|---|
| 21 |
|
|---|
| 22 | In order to switch on the camera one needs to switch on
|
|---|
| 23 | the water pump first. In case this pump is running, the
|
|---|
| 24 | Camera power suplly (51VDC Agilent) gets an enable signal
|
|---|
| 25 | and will power the camera.
|
|---|
| 26 | In order to enable the bias voltage supply, one needs to swtich on
|
|---|
| 27 | the Bias Crate Fans.
|
|---|
| 28 |
|
|---|
| 29 | Circuit:
|
|---|
| 30 | IL_ON : Interlock on : digital input D1
|
|---|
| 31 | FL_OK : level ok : digital input D2
|
|---|
| 32 | FC_OK : current ok : digital input D3
|
|---|
| 33 | FP_EN : pump enabled : digital input D4
|
|---|
| 34 |
|
|---|
| 35 | FC_ON : enable pump : digital output D5
|
|---|
| 36 | FC_OFF: disable pump : digital output D6
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <SPI.h>
|
|---|
| 40 | #include <Ethernet.h>
|
|---|
| 41 |
|
|---|
| 42 | // inputs
|
|---|
| 43 | int FC_OK = 2;
|
|---|
| 44 | int BP_ON = 3;
|
|---|
| 45 |
|
|---|
| 46 | int IL_ON = 14;
|
|---|
| 47 | int FL_OK = 15;
|
|---|
| 48 | int FP_EN = 16;
|
|---|
| 49 | int X_ON = 17;
|
|---|
| 50 | int Y_ON = 18;
|
|---|
| 51 | int Z_OK = 19;
|
|---|
| 52 |
|
|---|
| 53 | int my_inputs[] = {FC_OK, BP_ON, IL_ON, FL_OK, FP_EN, X_ON, Y_ON, Z_OK};
|
|---|
| 54 | char * my_input_names[] = {"FC_OK", "BP_ON", "IL_ON", "FL_OK", "FP_EN", "X_ON", "Y_ON", "Z_OK"};
|
|---|
| 55 |
|
|---|
| 56 | // outputs
|
|---|
| 57 | int FC_ON = 4;
|
|---|
| 58 | int FC_OFF = 5;
|
|---|
| 59 | int X = 6;
|
|---|
| 60 | int Y = 7;
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | unsigned long last_time = millis();
|
|---|
| 64 | unsigned long current_time = millis();
|
|---|
| 65 | unsigned int fc_on_time = 0; // time in ms, for FC_ON to be HIGH
|
|---|
| 66 | unsigned int fc_off_time = 0;// time in ms, for FC_OFF to be HIGH
|
|---|
| 67 |
|
|---|
| 68 | //byte mac[] = { 0xFA, 0xC7, 0x0F, 0xAD, 0x22, 0x01 }; // ETHZ
|
|---|
| 69 | byte mac[] = { 0xFA, 0xC7, 0xFC, 0xB1, 0x00, 0x01 }; // TUDO
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | // Initialize the Ethernet server library
|
|---|
| 73 | // with the IP address and port you want to use
|
|---|
| 74 | // (port 80 is default for HTTP):
|
|---|
| 75 | EthernetServer server(80);
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | boolean x_signal = false;
|
|---|
| 79 | boolean y_signal = false;
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | void setup() {
|
|---|
| 84 | // Open serial communications and wait for port to open:
|
|---|
| 85 | Serial.begin(9600);
|
|---|
| 86 | while (!Serial) {
|
|---|
| 87 | ; // wait for serial port to connect. Needed for Leonardo only
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | // start the Ethernet connection and the server:
|
|---|
| 92 | Ethernet.begin(mac);
|
|---|
| 93 | server.begin();
|
|---|
| 94 | Serial.print("server is at ");
|
|---|
| 95 | Serial.println(Ethernet.localIP());
|
|---|
| 96 |
|
|---|
| 97 | // Enable ATmega Pullups
|
|---|
| 98 | for (int i = 0 ; i<8; i++)
|
|---|
| 99 | {
|
|---|
| 100 | digitalWrite(my_inputs[i], HIGH);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | pinMode( FC_ON, OUTPUT );
|
|---|
| 106 | pinMode( FC_OFF,OUTPUT );
|
|---|
| 107 | pinMode( X, OUTPUT );
|
|---|
| 108 | pinMode( Y,OUTPUT );
|
|---|
| 109 |
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | void loop() {
|
|---|
| 114 | Check_n_Set_FC_ON_OFF();
|
|---|
| 115 | unsigned long before = millis();
|
|---|
| 116 | if (Check_for_clients() )
|
|---|
| 117 | {
|
|---|
| 118 | unsigned long duration = millis() - before;
|
|---|
| 119 |
|
|---|
| 120 | Serial.println( duration, DEC);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | delay(300);
|
|---|
| 124 | }
|
|---|
| 125 | /////////////////////////////////////////////////////////////////////////////////////////////
|
|---|
| 126 | int Check_for_clients()
|
|---|
| 127 | {
|
|---|
| 128 |
|
|---|
| 129 | int got_client = 0;
|
|---|
| 130 | // listen for incoming clients
|
|---|
| 131 | EthernetClient client = server.available();
|
|---|
| 132 | if (client) {
|
|---|
| 133 | got_client = 1;
|
|---|
| 134 | Serial.println("new client");
|
|---|
| 135 | // an http request ends with a blank line
|
|---|
| 136 | boolean currentLineIsBlank = true;
|
|---|
| 137 | boolean get_http_found = false;
|
|---|
| 138 | String eth = "";
|
|---|
| 139 |
|
|---|
| 140 | while (client.connected()) {
|
|---|
| 141 | if (client.available()) {
|
|---|
| 142 | char c = client.read();
|
|---|
| 143 | if (!get_http_found)
|
|---|
| 144 | {
|
|---|
| 145 | eth += c;
|
|---|
| 146 | // Serial.write(c);
|
|---|
| 147 | //Serial.println(eth.length(), DEC);
|
|---|
| 148 | if ( eth.indexOf('\n') != -1 )
|
|---|
| 149 | {
|
|---|
| 150 | if (eth.indexOf(String("GET /")) !=-1 && eth.indexOf(String("HTTP")) !=-1 )
|
|---|
| 151 | {
|
|---|
| 152 | get_http_found = true;
|
|---|
| 153 | if (eth.indexOf(String("D2on=")) !=-1){
|
|---|
| 154 | Serial.println("User request: Enable Pump");
|
|---|
| 155 | digitalWrite( FC_ON, HIGH);
|
|---|
| 156 | fc_on_time = 3000;
|
|---|
| 157 | }
|
|---|
| 158 | else if (eth.indexOf(String("D2off=")) !=-1)
|
|---|
| 159 | {
|
|---|
| 160 | Serial.println("User request: Disable Pump");
|
|---|
| 161 | digitalWrite( FC_OFF, HIGH);
|
|---|
| 162 | fc_off_time = 500;
|
|---|
| 163 | }
|
|---|
| 164 | else if (eth.indexOf(String("toggle_x=")) !=-1)
|
|---|
| 165 | {
|
|---|
| 166 | Serial.println("User request: Toggle X");
|
|---|
| 167 | x_signal = !x_signal;
|
|---|
| 168 | digitalWrite( X, x_signal );
|
|---|
| 169 | }
|
|---|
| 170 | else if (eth.indexOf(String("toggle_y=")) !=-1)
|
|---|
| 171 | {
|
|---|
| 172 | Serial.println("User request: Toggle Y");
|
|---|
| 173 | y_signal = !y_signal;
|
|---|
| 174 | digitalWrite( Y, y_signal );
|
|---|
| 175 | }
|
|---|
| 176 | else
|
|---|
| 177 | {
|
|---|
| 178 | Serial.println("No User request - just sending webpage");
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | //Serial.println("found it");
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | // if you've gotten to the end of the line (received a newline
|
|---|
| 187 | // character) and the line is blank, the http request has ended,
|
|---|
| 188 | // so you can send a reply
|
|---|
| 189 | if (c == '\n' && currentLineIsBlank) {
|
|---|
| 190 | // send a standard http response header
|
|---|
| 191 | html_header( &client );
|
|---|
| 192 | html_page( &client );
|
|---|
| 193 | html_footer( &client );
|
|---|
| 194 | break; // this breaks out of the while loop!!!
|
|---|
| 195 | }
|
|---|
| 196 | if (c == '\n') {
|
|---|
| 197 | // you're starting a new line
|
|---|
| 198 | currentLineIsBlank = true;
|
|---|
| 199 | }
|
|---|
| 200 | else if (c != '\r') {
|
|---|
| 201 | // you've gotten a character on the current line
|
|---|
| 202 | currentLineIsBlank = false;
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | // give the web browser time to receive the data
|
|---|
| 207 | delay(1);
|
|---|
| 208 | // close the connection:
|
|---|
| 209 | client.stop();
|
|---|
| 210 | Serial.println("client disonnected");
|
|---|
| 211 | }
|
|---|
| 212 | return got_client;
|
|---|
| 213 | }
|
|---|
| 214 | /////////////////////////////////////////////////////////////////////////////////////////////
|
|---|
| 215 |
|
|---|
| 216 | //////////// HTML Generators ////////////////////////////////////////////////////////////////
|
|---|
| 217 | void html_header(EthernetClient *client)
|
|---|
| 218 | {
|
|---|
| 219 | // send a standard http response header
|
|---|
| 220 | client->println("HTTP/1.1 200 OK");
|
|---|
| 221 | client->println("Content-Type: text/html");
|
|---|
| 222 | client->println("Connnection: close");
|
|---|
| 223 | client->println();
|
|---|
| 224 | client->println("<!DOCTYPE HTML>");
|
|---|
| 225 | client->println("<html><head>");
|
|---|
| 226 |
|
|---|
| 227 | // add a meta refresh tag, so the browser pulls again every 5 seconds:
|
|---|
| 228 | client->print("<meta http-equiv=\"refresh\" content=\"1 URL=index.html\">");
|
|---|
| 229 |
|
|---|
| 230 | client->println("<title>FACT camera power interlock control</title></head><body>");
|
|---|
| 231 | client->println("<FORM NAME=\"Form\">");
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | void send_ana_outs_example( EthernetClient *client) {
|
|---|
| 235 | // output the value of each analog input pin
|
|---|
| 236 | for (int analogChannel = 0; analogChannel < 6; analogChannel++)
|
|---|
| 237 | {
|
|---|
| 238 | client->print("analog input ");
|
|---|
| 239 | client->print(analogChannel);
|
|---|
| 240 | client->print(" is ");
|
|---|
| 241 | client->print(analogRead(analogChannel));
|
|---|
| 242 | client->println("<br />");
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | void send_dig_ins( EthernetClient *client)
|
|---|
| 247 | {
|
|---|
| 248 | for (int ch = 4; ch < 9; ch++)
|
|---|
| 249 | {
|
|---|
| 250 | client->print("digital input ");
|
|---|
| 251 | client->print(ch);
|
|---|
| 252 | client->print(" is ");
|
|---|
| 253 | if (digitalRead(ch))
|
|---|
| 254 | {
|
|---|
| 255 | client->print("<font color=\"green\" size=20>HIGH</font>");
|
|---|
| 256 | }
|
|---|
| 257 | else
|
|---|
| 258 | {
|
|---|
| 259 | client->print("<font color=\"red\" size=20>LOW</font>");
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 | client->println("<br />");
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 | void send_my_inputs( EthernetClient *client)
|
|---|
| 272 | {
|
|---|
| 273 | for (int ch = 0; ch < 8; ch++)
|
|---|
| 274 | {
|
|---|
| 275 | client->print(my_input_names[ch]);
|
|---|
| 276 | client->print(" :");
|
|---|
| 277 | client->print(my_inputs[ch]);
|
|---|
| 278 | client->print(" is ");
|
|---|
| 279 | if ( digitalRead(my_inputs[ch]) )
|
|---|
| 280 | {
|
|---|
| 281 | client->print("<font color=\"green\" size=20>HIGH</font>");
|
|---|
| 282 | }
|
|---|
| 283 | else
|
|---|
| 284 | {
|
|---|
| 285 | client->print("<font color=\"red\" size=20>LOW</font>");
|
|---|
| 286 | }
|
|---|
| 287 | client->println("<br />");
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 | void html_page( EthernetClient *client)
|
|---|
| 294 | {
|
|---|
| 295 | client->println("<P><INPUT TYPE=SUBMIT NAME=\"D2on\" VALUE=\"Enable Pump\"> --> FC_ON HIGH for 3 seconds <br />");
|
|---|
| 296 | client->println("<P><INPUT TYPE=SUBMIT NAME=\"D2off\" VALUE=\"Disable Pump\">--> FC_OFF HIGH for 0.5 seconds <br />");
|
|---|
| 297 | client->println("<P><INPUT TYPE=SUBMIT NAME=\"toggle_x\" VALUE=\"Toggle X\">--> ... well it toggles X <br />");
|
|---|
| 298 | client->println("<P><INPUT TYPE=SUBMIT NAME=\"toggle_y\" VALUE=\"Toggle Y\">--> ... well it toggles Y <br />");
|
|---|
| 299 |
|
|---|
| 300 | // send_dig_ins( client );
|
|---|
| 301 | send_my_inputs( client );
|
|---|
| 302 |
|
|---|
| 303 | //send_ana_outs_example( client );
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | void html_footer(EthernetClient *client)
|
|---|
| 307 | {
|
|---|
| 308 | client->print("</FORM></body></html>\n");
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 | //////////// HTML Generators ---- END /////////////////////////////////////////////////////////
|
|---|
| 313 |
|
|---|
| 314 | void
|
|---|
| 315 | Check_n_Set_FC_ON_OFF()
|
|---|
| 316 | {
|
|---|
| 317 | // Serial.print("FC ON TIME");
|
|---|
| 318 | // Serial.println(fc_on_time);
|
|---|
| 319 | // Serial.print("FC OFF TIME");
|
|---|
| 320 | // Serial.println(fc_off_time);
|
|---|
| 321 |
|
|---|
| 322 | unsigned long since_last = millis() - last_time;
|
|---|
| 323 |
|
|---|
| 324 | if (since_last == 0)
|
|---|
| 325 | {
|
|---|
| 326 | // not even a milisecond passed by
|
|---|
| 327 | // strange, should not happen
|
|---|
| 328 | }
|
|---|
| 329 | else if (since_last >= fc_on_time)
|
|---|
| 330 | {
|
|---|
| 331 | // FC_ON was long enough HIGH
|
|---|
| 332 | fc_on_time = 0;
|
|---|
| 333 | }
|
|---|
| 334 | else if (since_last < fc_on_time)
|
|---|
| 335 | {
|
|---|
| 336 | // fc_on_time is decreased
|
|---|
| 337 | fc_on_time -= since_last;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | if (fc_on_time == 0)
|
|---|
| 341 | {
|
|---|
| 342 | digitalWrite( FC_ON, LOW);
|
|---|
| 343 | }
|
|---|
| 344 | else
|
|---|
| 345 | {
|
|---|
| 346 | //digitalWrite( FC_ON, HIGH);
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | if (since_last == 0)
|
|---|
| 350 | {
|
|---|
| 351 | // not even a milisecond passed by
|
|---|
| 352 | // strange, should not happen
|
|---|
| 353 | }
|
|---|
| 354 | else if (since_last >= fc_off_time)
|
|---|
| 355 | {
|
|---|
| 356 | // FC_OFF was long enough HIGH
|
|---|
| 357 | fc_off_time = 0;
|
|---|
| 358 | }
|
|---|
| 359 | else if (since_last < fc_off_time)
|
|---|
| 360 | {
|
|---|
| 361 | // fc_on_time is decreased
|
|---|
| 362 | fc_off_time -= since_last;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | if (fc_off_time == 0)
|
|---|
| 366 | {
|
|---|
| 367 | digitalWrite( FC_OFF, LOW);
|
|---|
| 368 | }
|
|---|
| 369 | else
|
|---|
| 370 | {
|
|---|
| 371 | //digitalWrite( FC_OFF, HIGH);
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | last_time = millis();
|
|---|
| 375 | }
|
|---|
| 376 | /////////////////////////////////////////////////////////////////////////////////////////////
|
|---|
| 377 |
|
|---|