Page 1 of 1

I2C communication error

Posted: Thu Apr 27, 2017 10:46 am
by zz2633105
OpenMV3 and PCA9685 module communication errors

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:55 am
by zz2633105

Code: Select all

main.py:

from pyb import I2C
import time
from servoctrl import ServoCtrl


i2c = I2C(2, I2C.MASTER) # The i2c bus must always be 2.

servos = ServoCtrl(i2c)

servos.position(0,90)
time.sleep(1000)
servos.position(0,0)
time.sleep(1000)
servos.position(0,90)
time.sleep(1000)
servos.position(0,0)

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:56 am
by zz2633105
pca9685.py:

Code: Select all

import ustruct
import time


class PCA9685:
    def __init__(self, i2c, address=0x40):
        self.i2c = i2c
        self.address = address
        self.reset()

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

    def _read(self, address):
        return self.i2c.mem_read(1, self.address, address)[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(1)
        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:
            data = self.i2c.mem_read(self.address, 0x06 + 4 * index, 4)
            return ustruct.unpack('<HH', data)
        data = ustruct.pack('<HH', on, off)
        self.i2c.mem_write(data, self.address, 0x06 + 4 * index)

    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
        if not 0 <= value <= 4095:
            return 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)

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:56 am
by zz2633105
servoctrl.py:

Code: Select all

from pca9685 import PCA9685
import math


class ServoCtrl( PCA9685 ):
    """ Servo Controleur - Allow you to control the servo 0 to 15 on the PCA9685 PWM Driver """
    def __init__(self, i2c, address=0x80, freq=50, min_us=600, max_us=2400,
                 degrees=180):
        PCA9685.__init__( self, i2c=i2c, address=address)

        self.period = 1000000 / freq
        self.min_duty = self._us2duty(min_us)
        self.max_duty = self._us2duty(max_us)
        self.degrees = degrees

        # Init the freq on PCA9685
        self.freq(freq)

    def _us2duty(self, value):
        return int(4095 * value / self.period)

    def position(self, index, degrees=None, radians=None, us=None, duty=None):
        span = self.max_duty - self.min_duty
        if degrees is not None:
            duty = self.min_duty + span * degrees / self.degrees
        elif radians is not None:
            duty = self.min_duty + span * radians / math.radians(self.degrees)
        elif us is not None:
            duty = self._us2duty(us)
        elif duty is not None:
            pass
        else:
            return self.duty(index)
        duty = min(self.max_duty, max(self.min_duty, int(duty)))
        self.duty(index, duty)

    def release(self, index):
        self.duty(index, 0)

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:58 am
by zz2633105
Is there an error in the I2C communication program call?

Re: I2C communication error

Posted: Thu Apr 27, 2017 11:01 am
by kwagyeman
Hi zz2633105,

So, not trying to be mean here. But if you want an answer to your problems on the forums you need to structure your question in such a way that I can answer it. You just gave me a lot of code dump with no context.

Can you please try to narrow down the exact line of code that has the issue and then ask me questions about that? This way I can actually answer your question.

Also, maybe give me some pictures of your setup, details about what you are trying to do, etc. Holding back info just makes it harder to help you.

...

Please use the tag on your code so I can read it.

Re: I2C communication error

Posted: Thu Apr 27, 2017 3:17 pm
by deshipu
That code looks a lot like my library for the PCA9685 chip available here: https://github.com/adafruit/micropython ... it-pca9685 but some things are changed. I'm using that library with my servo shield, and it works fine, so if it doesn't work for you, perhaps it's because of the changes you made?

Re: I2C communication error

Posted: Thu Apr 27, 2017 3:18 pm
by deshipu
Ah, I see the problem. You are using i2c from pyb, while this library expects i2c from the machine module, which has a different API.

Re: I2C communication error

Posted: Thu Apr 27, 2017 9:29 pm
by zz2633105
deshipu wrote:
Thu Apr 27, 2017 3:18 pm
Ah, I see the problem. You are using i2c from pyb, while this library expects i2c from the machine module, which has a different API.
Can you give detailed advice?

Image

Re: I2C communication error

Posted: Thu Apr 27, 2017 9:34 pm
by zz2633105
kwagyeman wrote:
Thu Apr 27, 2017 11:01 am
Hi zz2633105,

So, not trying to be mean here. But if you want an answer to your problems on the forums you need to structure your question in such a way that I can answer it. You just gave me a lot of code dump with no context.

Can you please try to narrow down the exact line of code that has the issue and then ask me questions about that? This way I can actually answer your question.

Also, maybe give me some pictures of your setup, details about what you are trying to do, etc. Holding back info just makes it harder to help you.

...

Please use the tag on your code so I can read it.
Sorry, I am learning how to use the forum.

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:09 pm
by kwagyeman
Hi, can you edit your post and re-post your code using the tags? That way the formatting will not be removed.

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:15 pm
by zz2633105
kwagyeman wrote:
Thu Apr 27, 2017 10:09 pm
Hi, can you edit your post and re-post your code using the tags? That way the formatting will not be removed.
Thank you very much, I think I've learned.

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:34 pm
by zz2633105
kwagyeman wrote:
Thu Apr 27, 2017 10:09 pm
Hi, can you edit your post and re-post your code using the tags? That way the formatting will not be removed.
Hi, I have solved this problem. How do I delete posts?

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:40 pm
by kwagyeman
No need. Just try to keep your questions to one post for a particular issue.

Re: I2C communication error

Posted: Thu Apr 27, 2017 10:41 pm
by iabdalkader
Hi,

I fixed the code format for you, next time please use the code tags.

Thanks.

Re: I2C communication error

Posted: Sat Apr 29, 2017 6:40 am
by deshipu
zz2633105 wrote:
Thu Apr 27, 2017 9:29 pm
deshipu wrote:
Thu Apr 27, 2017 3:18 pm
Ah, I see the problem. You are using i2c from pyb, while this library expects i2c from the machine module, which has a different API.
Can you give detailed advice?
Not sure how I can be more detailed. You need to use this: http://docs.openmv.io/library/machine.I2C.html and not this: http://docs.openmv.io/library/pyb.I2C.html

Re: I2C communication error

Posted: Sat Apr 29, 2017 6:43 am
by deshipu
Looking at your "schematic" I can also see that you don't have pull-up resistors on the I2C lines. You need those for I2C communication to work -- perhaps you should read more about the I2C protocol.

Re: I2C communication error

Posted: Sat Apr 29, 2017 1:49 pm
by iabdalkader
This issue was resolved on github. Seems we have a Chinese community and we don't know about it :D