diff --git a/ddcontroller/motor.py b/ddcontroller/motor.py index cce6a55..eefa449 100644 --- a/ddcontroller/motor.py +++ b/ddcontroller/motor.py @@ -18,6 +18,8 @@ along with this program. If not, see . ''' +from ina219 import INA219 +from ina219 import DeviceRangeError import RPi.GPIO as GPIO GPIO.setwarnings(False) @@ -27,7 +29,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 +66,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 +81,9 @@ def __init__(self, pins, pwm_frequency, initial_duty=0, decay_mode='FAST', inver for pin in self._pins: pin.start(self.duty) + self.ina = INA219(0.1, address=0x44) + self.ina.configure() + def set_pwm_frequency(self, frequency): """_summary_ @@ -96,7 +104,10 @@ def set_duty(self, duty): sorted((-1, float(duty), 1))[1], 2 ) - duty = self.duty * 100 + max_duty = (1/self.ina.voltage())*self.max_voltage + + 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 08fa0b6..195ab91 100644 --- a/setup.py +++ b/setup.py @@ -31,6 +31,7 @@ 'smbus2', 'ruamel.yaml', 'simple-pid', + 'pi-ina219', # 'as5048b' ]