Help me to fix a problem a python driver for micro:bit and card DFR0548 TCS34725.

userHead Patrick.Gelinaud 2024-08-02 02:12:05 143 Views2 Replies

Hi

 

I want to develop a driver in micropython to drive two motors with a micro:bit and the card DFR0548.

I use the i2c bus to drive the motors via the board TCS34725.

I have programmed the i2c bus for other devices in python without any problems.

 

I don't understand why my code is not working !!!
 

I tested my hard platform it works correctly under makecode.

 

Here is my python code :

 

from microbit import *
import ustruct
import time


class PCA9685:


   def __init__(self, address=0x40):
       i2c.init()
       print(i2c.scan())
       self.address = address
       self.reset()

 

   def _write(self, address, value):
       i2c.write(self.address, bytearray([address, value]))

 

   def _read(self, address):
       i2c.write(self.address, bytearray([address]))
       return i2c.read(self.address, 1)[0]

 

   def reset(self):
       self._write(0x00, 0x00) # Mode1

 

   def freq(self, freq=None):
       if freq is None:
           return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
       prescale = int(25000000.0 / 4096.0 / freq + 0.5)
       old_mode = self._read(0x00) # Mode 1
       self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
       self._write(0xfe, prescale) # Prescale
       self._write(0x00, old_mode) # Mode 1
       time.sleep_us(5)
       self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on

 

   def pwm(self, index, on=None, off=None):
       if on is None or off is None:
           i2c.write(self.address, bytearray([0x06 + 4 * index]))
           data = i2c.read(self.address, 4)
           return ustruct.unpack('<HH', data)

       address = bytearray(1)
       address[0] = 0x06 + 4 * index
       data = ustruct.pack('<HH',on, off)
       print(address + data)

       i2c.write(self.address, address + data)

 

   def duty(self, index, value=None, invert=False):
       if value is None:
           pwm = self.pwm(index)
           if pwm == (0, 4096):
               value = 0
           elif pwm == (4096, 0):
               value = 4095
           value = pwm[1]
           if invert:
               value = 4095 - value
           return value
       if not 0 <= value <= 4095:
           raise ValueError("Out of range")
       if invert:
           value = 4095 - value
       if value == 0:
           self.pwm(index, 0, 4096)
       elif value == 4095:
           self.pwm(index, 4096, 0)
       else:
           self.pwm(index, 0, value)


_DC_MOTORS = ((8, 9, 10), (13, 12, 11), (2, 3, 4), (7, 6, 5))


class DCMotors:


   def __init__(self, address=0x60, freq=1600):
       self.pca9685 = PCA9685(address)
       self.pca9685.freq(freq)

 

   def _pin(self, pin, value=None):
       if value is None:
           return bool(self.pca9685.pwm(pin)[0])
       if value:
           self.pca9685.pwm(pin, 4096, 0)
       else:
           self.pca9685.pwm(pin, 0, 0)

 

   def speed(self, index, value=None):
       pwm, in2, in1 = _DC_MOTORS[index]
       if value is None:
           value = self.pca9685.duty(pwm)
           if self._pin(in2) and not self._pin(in1):
               value = -value
           return value
       if value > 0:
           # Forward
           self._pin(in2, False)
           self._pin(in1, True)
       elif value < 0:
           # Backward
           self._pin(in1, False)
           self._pin(in2, True)
       else:
           # Release
           self._pin(in1, False)
           self._pin(in2, False)
       self.pca9685.duty(pwm, abs(value))

 

   def brake(self, index):
       pwm, in2, in1 = _DC_MOTORS[index]
       self._pin(in1, True)
       self._pin(in2, True)
       self.pca9685.duty(pwm, 0)

 

# I tested on both addresses 41 or 64

motors = DCMotors(64)
motors.speed(1,255)

2024-08-02 08:07:17

Verify the address of the PCA9685.

userHeadPic lia.ifat
Patrick.Gelinaud wrote:

The return values ​​of ic2.scan() are 0x40 (64) and 0x70 (112)

I tested both values. 

motors = DCMotors(64) or motors = DCMotors(0x40)

If I use another value I get an error.

 

I don't understand my anomaly !

I can't find any example of micropython code with the micro:bit or any documentation

 

 

2024-08-02 23:22:33
1 Replies