pwr_arduino_uno.ino 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
//
// Proview
// Copyright (C) 2010 SSAB Oxelösund AB.
//
// This program is free software; you can redistribute it and/or 
// modify it under the terms of the GNU General Public License as 
// published by the Free Software Foundation, either version 2 of 
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License 
// along with the program, if not, write to the Free Software 
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// Description:
// Sketch for Arduino USB board to communicate with Proview I/O
// object OtherIO:Arduino_Uno.
//
claes's avatar
claes committed
23
byte firmware[20] = "Proview V2.0.0-1";
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
byte version_major = 2;
byte version_minor = 0;
byte msg = 0;
byte sts;
int val = 0;
byte amsg[50];
byte smsg[10];
byte diSize = 0;
byte doSize = 0;
byte aiSize = 0;
byte aoSize = 0;
byte diMask[10];
byte doMask[10];
byte aiMask[4];
byte aoMask[4];
int watchdogTime = 5000;
int status;
int i;
int j;
int aiList[32];
int aiCnt;
int aoList[32];
int aoCnt;
byte msgType;
byte msgId;
byte msgSize;
byte msgData[100];
byte rmsg[40];
int sizeErrorCnt = 0;
int noMessageCnt = 0;
const int delayTime = 1;
const int debug = 0;

const int MSG_TYPE_DOWRITE = 1;
const int MSG_TYPE_DIREAD = 2;
const int MSG_TYPE_AIREAD = 3;
const int MSG_TYPE_AOWRITE = 4;
const int MSG_TYPE_CONFIGURE = 5;
const int MSG_TYPE_STATUS = 6;
const int MSG_TYPE_DEBUG = 7;
const int MSG_TYPE_WRITEALL = 8;
const int MSG_TYPE_READALL = 9;
const int MSG_TYPE_CONNECTREQ = 10;
const int MSG_TYPE_CONNECTRES = 11;

const int ARD__SUCCESS = 1;
const int ARD__DICONFIG = 2;
const int ARD__DOCONFIG = 4;
const int ARD__AICONFIG = 6;
const int ARD__AOCONFIG = 8;
const int ARD__COMMERROR = 10;
const int ARD__MSGSIZE = 12;
const int ARD__NOMSG = 14;
const int ARD__CHECKSUM = 16;

void sendDebug( byte sts)
{
  rmsg[0] = 4;
  rmsg[1] = 0;
  rmsg[2] = MSG_TYPE_DEBUG;
  rmsg[3] = sts;
  add_checksum( rmsg);
  Serial.write( rmsg, rmsg[0]);
}

void add_checksum( byte *buf)
{
  int i;
  byte sum = 0;

  buf[0]++;
  for ( i = 0; i < buf[0] - 1; i++)
    sum ^= buf[i];

  buf[buf[0]-1] = sum;
}

int check_checksum( byte size, byte id, byte type, byte *buf)
{
  int i;
claes's avatar
claes committed
104
  byte sum = 0;
105 106 107 108 109 110 111

  sum ^= size;
  sum ^= id;
  sum ^= type;
  for ( i = 0; i < size - 4; i++)
    sum ^= buf[i];

claes's avatar
claes committed
112
  if ( buf[size-4] == sum)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    return 1;
  return 0;
}

//
//  Reset all outputs when communication communication is down
//
void resetOutput()
{  
  for ( i = 0; i < doSize; i++) {
    for ( j = 0; j < 8; j++) {
      if ( ((1 << j) & doMask[i]) != 0)
        digitalWrite( i * 8 + j, LOW);
    }
  }
  for ( i = 0; i < aoCnt; i++)
    analogWrite( aoList[i], 0);
}

//
//  Read a message from the serial port
//
int serialRead()
{
  int num;
claes's avatar
claes committed
138
  int csum;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

  num = Serial.available();
  if ( num == 0)
    return ARD__NOMSG;
    
  msgSize = Serial.peek();
  if ( num < msgSize)
    return ARD__MSGSIZE;
    
  msgSize = Serial.read();

  msgId = Serial.read();
  msgType = Serial.read();
  msgSize -= 3;

  for ( int i = 0; i < msgSize; i++)
    msgData[i] = Serial.read();
    
claes's avatar
claes committed
157
  csum = check_checksum( msgSize + 3, msgId, msgType, msgData);
158 159

  if ( debug) {
claes's avatar
claes committed
160
    rmsg[0] = msgSize + 3 - 1;
161 162 163 164 165 166 167
    rmsg[1] = msgId;
    rmsg[2] = MSG_TYPE_DEBUG;
    for ( int j = 0; j < msgSize-1; j++)
      rmsg[j+3] = msgData[j];
    add_checksum( rmsg);
    Serial.write( rmsg, rmsg[0]);
  }
claes's avatar
claes committed
168 169 170 171
  msgSize--;
 
  if ( !csum)
    return ARD__CHECKSUM;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  
  return ARD__SUCCESS;
}

void setup()
{
  // Start serial port at the configured baud rate
  Serial.begin(9600);
  Serial.flush();
}

void loop()
{
  
  status = serialRead();
  if ( status == ARD__NOMSG) {
    if ( watchdogTime != 0) {
      // Increment watchdog counter
      noMessageCnt++;
       if ( noMessageCnt * delayTime > watchdogTime)
         resetOutput();
    }
  }
  else if ( status == ARD__MSGSIZE) {
    sizeErrorCnt++;
    if ( sizeErrorCnt > 50) {
      Serial.flush();
      sizeErrorCnt = 0;
    }
  }
  else if ( (status & 1) != 0) {
    // A message is received
    sizeErrorCnt = 0;
    noMessageCnt = 0;

    if ( msgType == MSG_TYPE_DOWRITE) {
      // Write digital outputs

      if ( msgSize == doSize) {
        for ( i = 0; i < doSize; i++) {
          for ( j = 0; j < 8; j++) {
            if ( ((1 << j) & doMask[i]) != 0) {
              if ( ((1 << j) & msgData[i]) != 0)
                digitalWrite( i * 8 + j, HIGH);          
              else
                digitalWrite( i * 8 + j, LOW);          
            }
          }
        }
        sts = ARD__SUCCESS;
      }
      else {
        sts = ARD__COMMERROR;
      }
    }
    else if ( msgType == MSG_TYPE_AOWRITE) {
      // Write analog outputs

      if ( msgSize == aoCnt) {
        for ( i = 0; i < aoCnt; i++)
   	  analogWrite( aoList[i], msgData[i]);

        sts = ARD__SUCCESS;
      }
      else {
        sts = ARD__COMMERROR;
      }
    }
    else if ( msgType == MSG_TYPE_WRITEALL) {
      // Write digital outputs

      if ( msgSize == doSize + aoCnt) {
        for ( i = 0; i < doSize; i++) {
          for ( j = 0; j < 8; j++) {
            if ( ((1 << j) & doMask[i]) != 0) {
              if ( ((1 << j) & msgData[i]) != 0)
                digitalWrite( i * 8 + j, HIGH);          
              else
                digitalWrite( i * 8 + j, LOW);          
            }
          }
        }

        for ( i = 0; i < aoCnt; i++)
   	  analogWrite( aoList[i], msgData[doSize + i]);

        sts = ARD__SUCCESS;
      }
      else {
        sts = ARD__COMMERROR;
      }
    }
    else if ( msgType == MSG_TYPE_DIREAD) {
      // Read Digital inputs
      smsg[0] = diSize + 3;
      smsg[1] = msgId;
      smsg[2] = MSG_TYPE_DIREAD;
      for ( i = 0; i < diSize; i++) {
        smsg[i + 3] = 0;
        for ( j = 0; j < 8; j++) {
          if ( ((1 << j) & diMask[i]) != 0) {
            val = digitalRead( i * 8 + j);
            if ( val == HIGH) 
              smsg[i + 3] |= 1 << j;
          }
        }
      }
      add_checksum( smsg);
      Serial.write( smsg, smsg[0]);
    }
    else if ( msgType == MSG_TYPE_AIREAD) {
      // Read analog inputs
      amsg[0] = aiCnt * 2 + 3;
      amsg[1] = msgId;
      amsg[2] = MSG_TYPE_AIREAD;
      for ( i = 0; i < aiCnt; i++) {
        val = analogRead( aiList[i]);
        amsg[i*2 + 3] = val / 256;
        amsg[i*2 + 1 + 3] = val % 256;
      }
      add_checksum( amsg);
      Serial.write( amsg, amsg[0]);
    }
    else if ( msgType == MSG_TYPE_READALL) {
      // Read Digital inputs
      amsg[0] = diSize + aiCnt * 2 + 3;
      amsg[1] = msgId;
      amsg[2] = MSG_TYPE_READALL;
      for ( i = 0; i < diSize; i++) {
        amsg[i + 3] = 0;
        for ( j = 0; j < 8; j++) {
          if ( ((1 << j) & diMask[i]) != 0) {
            val = digitalRead( i * 8 + j);
            if ( val == HIGH) 
              amsg[i + 3] |= 1 << j;
          }
        }
      }

      for ( i = 0; i < aiCnt; i++) {
        val = analogRead( aiList[i]);
        amsg[diSize + i*2 + 3] = val / 256;
        amsg[diSize + i*2 + 1 + 3] = val % 256;
      }
      add_checksum( amsg);
      Serial.write( amsg, amsg[0]);
    }
    else if ( msgType == MSG_TYPE_CONFIGURE) {
      // Configure message
      int offs = 0;
      sts = ARD__SUCCESS;

      if ( debug) {
        smsg[0] = msgSize + 3;
        smsg[1] = msgId;
        smsg[2] = MSG_TYPE_DEBUG;
        for ( int j = 0; j < msgSize; j++)
          smsg[j+3] = msgData[j];
	add_checksum( smsg);
        Serial.write( smsg, smsg[0]);
      }
      
      watchdogTime = msgData[offs++] * 100;
      
      diSize = msgData[offs++];
      if ( diSize > 10) {
        diSize = 10;
        sts = ARD__DICONFIG;
      }
      if ( sts & 1 != 0) {  
        for ( i = 0; i < diSize; i++)
          diMask[i] = msgData[offs++];
      }        

      if ( sts & 1 != 0) {  
        doSize = msgData[offs++];
        if ( doSize > 10) {
          doSize = 10;
          sts = ARD__DOCONFIG;
        }
      }
      if ( sts & 1 != 0) {        
        for ( i = 0; i < doSize; i++)
          doMask[i] = msgData[offs++];
      }

      if ( sts & 1 != 0) {        
        aiSize = msgData[offs++];
        if ( aiSize > 4) {
          aiSize = 4;
          sts = ARD__AICONFIG;
        } 
      }

      if ( sts & 1 != 0) {        
        for ( i = 0; i < aiSize; i++)
          aiMask[i] = msgData[offs++];
      }

      if ( sts & 1 != 0) {        
        aoSize = msgData[offs++];
        if ( aoSize > 4) {
          aoSize = 4;
          sts = ARD__AOCONFIG;
        }        
      }
      
      if ( sts & 1 != 0) {  
        for ( i = 0; i < aoSize; i++)
          aoMask[i] = msgData[offs++];
      }

      if ( sts & 1 != 0) {  
        // Set Di pinmode
        for ( i = 0; i < diSize; i++) {
          for ( j = 0; j < 8; j++) {
            if ( ((1 << j) & diMask[i]) != 0)
              pinMode( i * 8 + j, INPUT);          
          }
        }
        
        // Set Do pinmode
        for ( i = 0; i < doSize; i++) {
          for ( j = 0; j < 8; j++) {
            if ( ((1 << j) & doMask[i]) != 0)
              pinMode( i * 8 + j, OUTPUT);          
          }
        }  

        // Create list of configured Ai
        aiCnt = 0;
        for ( i = 0; i < aiSize; i++) {
          for ( j = 0; j < 8; j++) {
            if ( ((1 << j) & aiMask[i]) != 0) {
              aiList[aiCnt] = i * 8 + j;
              aiCnt++;
            }
          }
        }

        // Create list of configured Ao
        aoCnt = 0;
        for ( i = 0; i < aoSize; i++) {
          for ( j = 0; j < 8; j++) {
            if ( ((1 << j) & aoMask[i]) != 0) {
              aoList[aoCnt] = i * 8 + j;
              aoCnt++;
            }
          }
        }
      }
      
      // Send configuration status
      smsg[0] = 4;
      smsg[1] = msgId;
      smsg[2] = MSG_TYPE_STATUS;
      smsg[3] = sts;
      add_checksum( smsg);
      Serial.write(smsg, smsg[0]);
       
    }
    else if ( msgType == MSG_TYPE_CONNECTREQ) {
      // Connect reqeust
      amsg[0] = 23 + 3;
      amsg[1] = msgId;
      amsg[2] = MSG_TYPE_CONNECTRES;
      amsg[3] = ARD__SUCCESS;
      amsg[4] = version_major;
      amsg[5] = version_minor;
      for ( i = 0; i < 20; i++)
        amsg[i+6] = firmware[i];
      add_checksum( amsg);
      Serial.write( amsg, amsg[0]);
    }
  }
  else {
    // Return error status
    smsg[0] = 4;
    smsg[1] = msgId;
    smsg[2] = MSG_TYPE_STATUS;
    smsg[3] = status;
    add_checksum( smsg);
    Serial.write(smsg, smsg[0]);
    
  }

  delay(delayTime); 
}