preliminary support for pwr AVR current measurement
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 2 Jul 2017 06:20:21 +0000 (23:20 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 2 Jul 2017 06:20:21 +0000 (23:20 -0700)
src/pwr/main.c
src/py/bbctrl/AVR.py
src/py/bbctrl/Pwr.py

index 836896ee4ac06ead223e3323d62dc0f735c5f325..1aa7cc217b9e0363443a9be9f5c2bdf6c3b02626 100644 (file)
 
 #define TEMP_ADC 14
 
-#define CS_PORT PORTA
-#define CS1_PIN 3
-#define CS2_PIN 5
-#define CS3_PIN 6
-#define CS1_ADC 0
-#define CS2_ADC 2
-#define CS3_ADC 3
-
-#define VIN_PORT PORTB
+// Port A
+#define MOTOR_PIN 3
+#define LOAD1_PIN 5
+#define LOAD2_PIN 6
+#define MOTOR_ADC 0
+#define LOAD1_ADC 2
+#define LOAD2_ADC 3
+
+// Port B
 #define VIN_PIN 0
 #define VIN_ADC 5
 
-#define VOUT_PORT PORTC
+// Port C
 #define VOUT_PIN 2
 #define VOUT_ADC 11
 
@@ -72,9 +72,9 @@ typedef enum {
   TEMP_REG,
   VIN_REG,
   VOUT_REG,
-  CS1_REG,
-  CS2_REG,
-  CS3_REG,
+  MOTOR_REG,
+  LOAD1_REG,
+  LOAD2_REG,
   NUM_REGS
 } regs_t;
 
@@ -130,10 +130,19 @@ ISR(TWI_SLAVE_vect) {
 
 inline static uint16_t convert_voltage(uint16_t sample) {
 #define VREF 1.1
-#define R1 34800
-#define R2 1000
+#define VR1 34800
+#define VR2 1000
 
-  return sample * (VREF / 1024.0 * (R1 + R2) / R2 * 100);
+  return sample * (VREF / 1024.0 * (VR1 + VR2) / VR2 * 100);
+}
+
+
+inline static uint16_t convert_current(uint16_t sample) {
+#define CR1 1000
+#define CR2 137
+
+  // TODO This isn't correct
+  return sample * (VREF / 1024.0 * (CR1 + CR2) / CR2 * 100);
 }
 
 
@@ -143,7 +152,7 @@ void adc_conversion() {
 
   switch (ch) {
   case TEMP_ADC:
-    regs[TEMP_REG] = data;
+    regs[TEMP_REG] = data; // Temp in Kelvin
     ch = VIN_ADC;
     break;
 
@@ -154,21 +163,21 @@ void adc_conversion() {
 
   case VOUT_ADC:
     regs[VOUT_REG] = convert_voltage(data);
-    ch = CS1_ADC;
+    ch = MOTOR_ADC;
     break;
 
-  case CS1_ADC:
-    regs[CS1_REG] = data;
-    ch = CS2_ADC;
+  case MOTOR_ADC:
+    regs[MOTOR_REG] = convert_current(data);
+    ch = LOAD1_ADC;
     break;
 
-  case CS2_ADC:
-    regs[CS2_REG] = data;
-    ch = CS3_ADC;
+  case LOAD1_ADC:
+    regs[LOAD1_REG] = convert_current(data);
+    ch = LOAD2_ADC;
     break;
 
-  case CS3_ADC:
-    regs[CS3_REG] = data;
+  case LOAD2_ADC:
+    regs[LOAD2_REG] = convert_current(data);
     ch = TEMP_ADC;
     break;
 
@@ -248,6 +257,7 @@ int main() {
   // Start ADC
   adc_conversion();
 
+  // Delayed start
   _delay_ms(100);
 
   // Enable timer with clk/64
index 85b3e39c7a49a34c18a8a3e8845754ddccf8d563..c00b7263f040e7262a756db1d3f3583c7b39305e 100644 (file)
@@ -20,9 +20,8 @@ I2C_RUN            = 5
 I2C_STEP           = 6
 I2C_FLUSH          = 7
 I2C_REPORT         = 8
-I2C_HOME           = 9
-I2C_REBOOT         = 10
-I2C_ZERO           = 11
+I2C_REBOOT         = 9
+I2C_ZERO           = 10
 
 
 machine_state_vars = '''
@@ -268,7 +267,7 @@ class AVR():
         self.queue_command('${}{}={}'.format(index, code, value))
 
 
-    def home(self): self._i2c_command(I2C_HOME)
+    def home(self): log.debug('NYI')
     def estop(self): self._i2c_command(I2C_ESTOP)
     def clear(self): self._i2c_command(I2C_CLEAR)
 
index c97da76e98e9a0b820fcb6c3ade24a48e3e6dbf1..0e9be6ee3ed56f456746551623d664c6c5ab2a43 100644 (file)
@@ -9,9 +9,9 @@ log = logging.getLogger('PWR')
 TEMP_REG        = 0
 VIN_REG         = 1
 VOUT_REG        = 2
-CS1_REG         = 3
-CS2_REG         = 4
-CS3_REG         = 5
+MOTOR_REG       = 3
+LOAD1_REG       = 4
+LOAD2_REG       = 5
 
 
 class Pwr():
@@ -34,17 +34,19 @@ class Pwr():
                 value = self.ctrl.i2c.read_word(self.i2c_addr + i)
 
                 if i == TEMP_REG: self.regs[TEMP_REG] = value - 273
-                elif i == VIN_REG: self.regs[VIN_REG] = value / 100.0
-                elif i == VOUT_REG: self.regs[VOUT_REG] = value / 100.0
-                else: self.regs[i] = value
+                else: self.regs[i] = value / 100.0
 
         except Exception as e:
             log.warning('Pwr communication failed: %s' % e)
             self.ctrl.ioloop.call_later(1, self._update)
             return
 
-        self.lcd_page.text('%3dC' % self.regs[TEMP_REG],       0, 0)
-        self.lcd_page.text('%5.1fV in' % self.regs[VIN_REG],   0, 1)
-        self.lcd_page.text('%5.1fV out' % self.regs[VOUT_REG], 0, 2)
+        self.lcd_page.text('%3dC'       % self.regs[TEMP_REG], 0, 0)
+        self.lcd_page.text('%5.1fV In'  % self.regs[VIN_REG],  0, 1)
+        self.lcd_page.text('%5.1fV Out' % self.regs[VOUT_REG], 0, 2)
+
+        self.lcd_page.text('%5.1fA Mot' % self.regs[MOTOR_REG], 10, 0)
+        self.lcd_page.text('%5.1fA Ld1' % self.regs[LOAD1_REG], 10, 1)
+        self.lcd_page.text('%5.1fA Ld2' % self.regs[LOAD2_REG], 10, 2)
 
         self.ctrl.ioloop.call_later(0.25, self._update)