From fbf8ace1d4fe92e46c1e39aff3f8337fd052a24f Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Sat, 1 Jul 2017 23:20:21 -0700 Subject: [PATCH] preliminary support for pwr AVR current measurement --- src/pwr/main.c | 62 +++++++++++++++++++++++++------------------- src/py/bbctrl/AVR.py | 7 +++-- src/py/bbctrl/Pwr.py | 20 +++++++------- 3 files changed, 50 insertions(+), 39 deletions(-) diff --git a/src/pwr/main.c b/src/pwr/main.c index 836896e..1aa7cc2 100644 --- a/src/pwr/main.c +++ b/src/pwr/main.c @@ -36,19 +36,19 @@ #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 diff --git a/src/py/bbctrl/AVR.py b/src/py/bbctrl/AVR.py index 85b3e39..c00b726 100644 --- a/src/py/bbctrl/AVR.py +++ b/src/py/bbctrl/AVR.py @@ -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) diff --git a/src/py/bbctrl/Pwr.py b/src/py/bbctrl/Pwr.py index c97da76..0e9be6e 100644 --- a/src/py/bbctrl/Pwr.py +++ b/src/py/bbctrl/Pwr.py @@ -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) -- 2.27.0