I2C communication error

Discussion related to "under the hood" OpenMV topics.
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 10:46 am

OpenMV3 and PCA9685 module communication errors
Last edited by zz2633105 on Thu Apr 27, 2017 10:20 pm, edited 1 time in total.
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 10:55 am

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)
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 10:56 am

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)
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 10:56 am

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)
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 10:58 am

Is there an error in the I2C communication program call?
User avatar
kwagyeman
Posts: 4665
Joined: Sun May 24, 2015 2:10 pm

Re: I2C communication error

Postby kwagyeman » 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.
Nyamekye,
User avatar
deshipu
Posts: 74
Joined: Sat Jan 09, 2016 5:36 am
Location: Zürich
Contact:

Re: I2C communication error

Postby deshipu » Thu Apr 27, 2017 3:17 pm

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?
User avatar
deshipu
Posts: 74
Joined: Sat Jan 09, 2016 5:36 am
Location: Zürich
Contact:

Re: I2C communication error

Postby deshipu » 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.
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » 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?

Image
Attachments
pca9685.png
pca9685.png (196.68 KiB) Viewed 10804 times
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 9:34 pm

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.
User avatar
kwagyeman
Posts: 4665
Joined: Sun May 24, 2015 2:10 pm

Re: I2C communication error

Postby kwagyeman » 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.
Nyamekye,
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 10:15 pm

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.
zz2633105
Posts: 12
Joined: Thu Apr 27, 2017 10:41 am

Re: I2C communication error

Postby zz2633105 » Thu Apr 27, 2017 10:34 pm

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?
User avatar
kwagyeman
Posts: 4665
Joined: Sun May 24, 2015 2:10 pm

Re: I2C communication error

Postby kwagyeman » Thu Apr 27, 2017 10:40 pm

No need. Just try to keep your questions to one post for a particular issue.
Nyamekye,
User avatar
iabdalkader
Posts: 1245
Joined: Sun May 24, 2015 3:53 pm

Re: I2C communication error

Postby iabdalkader » Thu Apr 27, 2017 10:41 pm

Hi,

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

Thanks.
User avatar
deshipu
Posts: 74
Joined: Sat Jan 09, 2016 5:36 am
Location: Zürich
Contact:

Re: I2C communication error

Postby deshipu » Sat Apr 29, 2017 6:40 am

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
User avatar
deshipu
Posts: 74
Joined: Sat Jan 09, 2016 5:36 am
Location: Zürich
Contact:

Re: I2C communication error

Postby deshipu » Sat Apr 29, 2017 6:43 am

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.
User avatar
iabdalkader
Posts: 1245
Joined: Sun May 24, 2015 3:53 pm

Re: I2C communication error

Postby iabdalkader » Sat Apr 29, 2017 1:49 pm

This issue was resolved on github. Seems we have a Chinese community and we don't know about it :D

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests