1. iorodeo
  2. panels_controller_nano_firmware

Commits

iorodeo  committed a46f75e

Added serial interface to enable users to set the frame dt. Added matlab class
PanelsNano enabling using to communicate with the controller from Matlab.

  • Participants
  • Parent commits 50c4e10
  • Branches default

Comments (0)

Files changed (3)

File .hgignore

View file
  • Ignore whitespace
 *.pkl
 *.dxf
 *.komodo*
+*.asv
 *.egg-info
 .DS_Store
 build

File matlab_serial_interface/PanelsNano.m

View file
  • Ignore whitespace
+% PanelsNano.m 
+%
+% Provides a simple interface to the Arduino Nano based panels controller
+% enabling the user to set the frame dt in ms. 
+%
+% Usage:
+%  
+%  dev = PanelsNano('com5')
+%  dev.open()
+%  dev.setFrameDt(100)  % Set frame dt to 100ms
+%  dev.close()
+%
+% Author: Will Dickson, IO Rodeo Inc.
+% -------------------------------------------------------------------------
+
+classdef  PanelsNano < handle
+    
+    properties
+        dev = [];    
+    end
+    
+    properties (Constant)
+        % Serial communications parameters
+        baudrate = 9600;
+        databits = 8;
+        stopbits = 1;
+        timeout = 0.5;
+        openDelay = 2.0;
+    end
+    
+    methods
+        
+        function self = PanelsNano(port)
+            % Constructor
+            if nargin ~= 1
+                error('Usage: self = PanelsNano(port)');
+            end
+            self.dev = serial( ...
+                port, ...
+                'baudrate', self.baudrate, ...
+                'databits', self.databits, ...
+                'stopbits', self.stopbits, ...
+                'timeout',  self.timeout ...
+                );
+        end
+        
+        function open(self)
+            % Opens a serial connection to panels controller
+            fopen(self.dev);
+            pause(self.openDelay);
+        end
+        
+        function close(self)
+            % Closes the seraial connection to the panaels controller
+            fclose(self.dev);
+        end
+         
+        function setFrameDt(self,dt)
+            % Set the frame dt in ms 
+            if (dt < 1) || (dt > 255)
+                error('frame dt must be between 1 and 255');
+            end
+            fwrite(self.dev, char(dt));    
+        end
+      
+    end
+    
+end

File panels_controller_nano_firmware.pde

View file
  • Ignore whitespace
  * Author: Peter Polidoro, IO Rodeo Inc. 02/16/2011
  *******************************************************************************/
 #include <Wire.h>
+#include <util/atomic.h>
 
 #define GENERAL_CALL_ADDRESS       0
 #define INITIAL_PATTERN_BYTE_COUNT 8
 #define PATTERN_FRAME_COUNT        4
-#define PATTERN_FRAME_DELAY        200
+// Set up timer 2 w/ 1kHz overflow
+#define TIMER_TCCR2A 0x03
+#define TIMER_TCCR2B 0x0C
+#define TIMER_OCR2A 249
+#define DFLT_MAX_DELAY_CNT 200
 
+boolean sendFrame = false;
+uint8_t delayCnt = 0;
+uint8_t maxDelayCnt = DFLT_MAX_DELAY_CNT;
+uint8_t frame_n = 0;
 unsigned char INITIAL_PATTERN[INITIAL_PATTERN_BYTE_COUNT] =              {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
 unsigned char ALL_OFF_PATTERN[INITIAL_PATTERN_BYTE_COUNT] =              {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 unsigned char ALL_ON_PATTERN[INITIAL_PATTERN_BYTE_COUNT] =               {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 unsigned char PATTERN[PATTERN_FRAME_COUNT][INITIAL_PATTERN_BYTE_COUNT] = {{0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33},
-                                                                          {0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99},
-                                                                          {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC},
-                                                                          {0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}};
+                                                                         {0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99},
+                                                                         {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC},
+                                                                         {0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}};
 
 /*******************************************************************************
  * setup
  *
  *******************************************************************************/
 void setup() {
+  // Setup serial communications
+  Serial.begin(9600);
+
   Wire.begin(); // join i2c bus (address optional for master)
   Wire.beginTransmission(GENERAL_CALL_ADDRESS); // transmit to all devices
   Wire.send(INITIAL_PATTERN,INITIAL_PATTERN_BYTE_COUNT);
   Wire.endTransmission();    // stop transmitting
-  delay(3000);
+
+  // Initialize timer 2 
+  TCCR2A = TIMER_TCCR2A;
+  TCCR2B = TIMER_TCCR2B;
+  OCR2A = TIMER_OCR2A;
+  // Timer 2 overflow interrupt enable 
+  TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);
+  TCNT2 = 0;
 }
 
 /*******************************************************************************
  * loop
  *
  *******************************************************************************/
-int frame_n = 0;
 void loop() {
-  Wire.beginTransmission(GENERAL_CALL_ADDRESS); // transmit to all devices
-  Wire.send(PATTERN[frame_n],INITIAL_PATTERN_BYTE_COUNT);
-  Wire.endTransmission();    // stop transmitting
-  delay(PATTERN_FRAME_DELAY);
-  frame_n = (frame_n + 1) % PATTERN_FRAME_COUNT;
+  while (Serial.available() > 0) {
+    uint8_t byte = (uint8_t) Serial.read();
+    if (byte > 0) {
+      ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+        maxDelayCnt = byte;
+        delayCnt = 0;
+      }
+    }
+  }
+  if (sendFrame == true) {
+    Serial.print(frame_n,DEC);
+    Serial.print(", ");
+    Serial.println(maxDelayCnt,DEC);
+    Wire.beginTransmission(GENERAL_CALL_ADDRESS); // transmit to all devices
+    Wire.send(PATTERN[frame_n],INITIAL_PATTERN_BYTE_COUNT);
+    Wire.endTransmission();    // stop transmitting
+    frame_n = (frame_n + 1) % PATTERN_FRAME_COUNT;
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { 
+      sendFrame = false;
+    }
+  }
 }
+
+
+/******************************************************************************
+ * Timer 2 overflow interrupt service routine
+ *
+ ******************************************************************************/
+ISR(TIMER2_OVF_vect) {
+  delayCnt++;
+  if (delayCnt >= maxDelayCnt) {
+    delayCnt = 0;
+    sendFrame = true;
+  }
+}