From bea78d73b53a95dbc0a91bed0e443084afe442e1 Mon Sep 17 00:00:00 2001 From: Daniyal Ansari Date: Sun, 18 Dec 2022 23:27:53 -0600 Subject: [PATCH 1/2] added INA219 --- ddcontroller/motor.py | 14 +++++++++++++- setup.py | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ddcontroller/motor.py b/ddcontroller/motor.py index cce6a55..f7abcb6 100644 --- a/ddcontroller/motor.py +++ b/ddcontroller/motor.py @@ -18,6 +18,9 @@ along with this program. If not, see . ''' +import board +import busio +import adafruit_ina219 import RPi.GPIO as GPIO GPIO.setwarnings(False) @@ -27,7 +30,7 @@ class Motor: Motor """ - def __init__(self, pins, pwm_frequency, initial_duty=0, decay_mode='FAST', invert=False, rpm=200): + def __init__(self, pins, pwm_frequency, initial_duty=0, decay_mode='FAST', invert=False, rpm=200, max_voltage=12): """_summary_ @@ -64,6 +67,9 @@ def __init__(self, pins, pwm_frequency, initial_duty=0, decay_mode='FAST', inver # Max motor Duty self.max_duty = 1 + # Motor rated voltage + self.max_voltage = max_voltage + if GPIO.getmode() is None: GPIO.setmode(GPIO.BOARD) else: @@ -76,6 +82,9 @@ def __init__(self, pins, pwm_frequency, initial_duty=0, decay_mode='FAST', inver for pin in self._pins: pin.start(self.duty) + self.i2c = busio.I2C(board.SCL, board.SDA) + self.ina219 = adafruit_ina219.INA219(self.i2c, addr=0x44) + def set_pwm_frequency(self, frequency): """_summary_ @@ -96,6 +105,9 @@ def set_duty(self, duty): sorted((-1, float(duty), 1))[1], 2 ) + max_duty = (1/self.ina219.bus_voltage)*self.max_voltage + duty = duty * max_duty + duty = self.duty * 100 if self.decay_mode == 'SLOW': diff --git a/setup.py b/setup.py index 08fa0b6..dc7528a 100644 --- a/setup.py +++ b/setup.py @@ -31,6 +31,7 @@ 'smbus2', 'ruamel.yaml', 'simple-pid', + 'adafruit-circuitpython-ina219', # 'as5048b' ] From 73044cda6630c17f5676e25b72780f90930222cc Mon Sep 17 00:00:00 2001 From: Daniyal Ansari Date: Thu, 22 Dec 2022 14:57:23 -0600 Subject: [PATCH 2/2] removed adafruit ina219 library --- ddcontroller/motor.py | 15 ++++++----- examples/ina_test.py | 24 ++++++++++++++++++ examples/test_motors.py | 55 +++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 4 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 examples/ina_test.py create mode 100644 examples/test_motors.py diff --git a/ddcontroller/motor.py b/ddcontroller/motor.py index f7abcb6..eefa449 100644 --- a/ddcontroller/motor.py +++ b/ddcontroller/motor.py @@ -18,9 +18,8 @@ along with this program. If not, see . ''' -import board -import busio -import adafruit_ina219 +from ina219 import INA219 +from ina219 import DeviceRangeError import RPi.GPIO as GPIO GPIO.setwarnings(False) @@ -82,8 +81,8 @@ def __init__(self, pins, pwm_frequency, initial_duty=0, decay_mode='FAST', inver for pin in self._pins: pin.start(self.duty) - self.i2c = busio.I2C(board.SCL, board.SDA) - self.ina219 = adafruit_ina219.INA219(self.i2c, addr=0x44) + self.ina = INA219(0.1, address=0x44) + self.ina.configure() def set_pwm_frequency(self, frequency): """_summary_ @@ -105,10 +104,10 @@ def set_duty(self, duty): sorted((-1, float(duty), 1))[1], 2 ) - max_duty = (1/self.ina219.bus_voltage)*self.max_voltage - duty = duty * max_duty + max_duty = (1/self.ina.voltage())*self.max_voltage - duty = self.duty * 100 + duty = self.duty * max_duty + duty = duty * 100 if self.decay_mode == 'SLOW': diff --git a/examples/ina_test.py b/examples/ina_test.py new file mode 100644 index 0000000..849d2a3 --- /dev/null +++ b/examples/ina_test.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +from ina219 import INA219 +from ina219 import DeviceRangeError + +SHUNT_OHMS = 0.1 + +ina = INA219(SHUNT_OHMS, address=0x44) +ina.configure() + +def read(): + + print("Bus Voltage: %.3f V" % ina.voltage()) + try: + print("Bus Current: %.3f mA" % ina.current()) + print("Power: %.3f mW" % ina.power()) + print("Shunt voltage: %.3f mV" % ina.shunt_voltage()) + except DeviceRangeError as e: + # Current out of device range with specified shunt resistor + print(e) + + +if __name__ == "__main__": + while True: + read() \ No newline at end of file diff --git a/examples/test_motors.py b/examples/test_motors.py new file mode 100644 index 0000000..74eea8a --- /dev/null +++ b/examples/test_motors.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +""" +This file is part of the ddcontroller library (https://github.com/ansarid/ddcontroller). +Copyright (C) 2022 Daniyal Ansari + +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 3 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 this program. If not, see . +""" + +import time +from ddcontroller.motor import Motor + +# Create motor object ((pins), pwm_frequency) +motor = Motor((15, 16), 220, decay_mode='SLOW') + +try: + + # Create infinite loop + while True: + + time.sleep(5) + + # For loop iterating through values from 100 to -100 with an increment of -1 + for duty in range(100, -100, -1): + + # Divide duty by 100 because duty cycle input needs to be between -1 and 1 + duty /= 100 + + # Print the current duty cycle + print(f"Duty: {duty}") + + # Set the duty cycle to the motor + motor.set_duty(duty) + + # Run loop at 50Hz + time.sleep(1/25) + +except KeyboardInterrupt: + print('Stopping...') + +finally: + # Clean up. + motor.stop() + print('Stopped.') diff --git a/setup.py b/setup.py index dc7528a..195ab91 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ 'smbus2', 'ruamel.yaml', 'simple-pid', - 'adafruit-circuitpython-ina219', + 'pi-ina219', # 'as5048b' ]