Open-Encoder-MG995

Discussions relating to development and use of the OpenServo hardware.

Moderators: jharvey, Secondary Admin, Admins

Post Reply
jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Open-Encoder-MG995

Post by jharvey » Tue Jul 07, 2009 2:26 am

This topic has been talked about in another thread, however, I felt it was OT and really should be brought into a different tread. The prior references can be found in this thread, titled "Question about precision and repeatability".

http://www.openservo.com/forums/viewtop ... c&start=45

This thread will discuss Open-Encoder specific to modifying a MG995 servo. Below are some items I'm starting to work on.

-- I expect to get the custom magnets in a couple days. When I get them, I plan to use the OSIF to do basic communications with the device receiving the position. That is only one step of getting this working. For this to really work, we will need this to be integrated with the OSV3 code.

I've ordered several extra magnets. For those that are willing to help in the devel of this encode, I'll offer three to match the 3 samples one can get of the encoder chip.

I plan to glue my first magnets on, later version will use a custom shaft that has a rivet feature on it.

-- I've been looking over the OSV3 code, and I'm not sure what the best general approach is. I'm tempted to push for a separate branch similar to the velocity control code, perhaps an upgraded version of the velocity code. One firmware to rule them all would be nice, that way bugs and such don't have to be addressed multiple times, however that also increases the complications, so perhaps separate firmwares for specific operating features is a good idea.

As for how to implement the software I2C, that's a different beast. I'm still studying the normal firmware, is the timer used for anything? Perhaps that can function as a clock signal. Some key issues I see include figuring out how not to waste time waiting for the I2C commands to come down from the encoder chip.

The encoder is 12 bits, but the position reg can handle 16 bits. That allows for 16 revolutions natively, which should work for my application. An option that would be nice to include, but is unimportant for me right now, would be off loading more bits to the controlling PC software. Such that you can control this like it's a 32 bit device, and have lots of revolutions. That would require more I2C commands and I'm not sure what commands might be handy for this feature. Perhaps that's better done later down the road, but a general game plan now would be nice. You know pigeon hole prevention.

Be warned, software is a weaker skill for me, I generally know enough to be dangerous, I'm fairly bad about generating original code.

-- As for the physical setup, I think we are in good shape. I believe we can reuse the AN POT input PC2 pin, as SDA, then the extra pad that Cliff put in for PD5 as SCL. I choose PD5, because it looks like it might be capable of having a VIA in future versions. The resistors can be taken care of on the Open-Encoder PCB. Power can be tapped from the + and _ of the AN POT circuit.

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Tue Jul 07, 2009 11:10 pm

I got the magnets, here's a pic

Image

They measure spot on. A size B drill should work just fine for putting the hole in the PCB. Something like McMastercarr part 30595A32 would work, however I've got one and will do that tomorrow. Drills typically over drill slightly so this should be a nice tight fit.

Also I've made a slight update to the MG995 servo mod. I added a note about the .1uF cap, that I failed to install. I choose Tant caps, and posted Newark numbers for these parts. I believe these parts should be good, however it's my stable in the dark. That page is found here.

http://www.openservo.com/ConstructionTutorial_MG995
Last edited by jharvey on Wed Jul 08, 2009 8:11 pm, edited 1 time in total.

Cliff
Posts: 150
Joined: Tue Jan 23, 2007 1:25 pm
Location: Saratoga, CA

Post by Cliff » Wed Jul 08, 2009 5:24 pm

Jared,

You do NOT want to install a tantalum capacitor across the motor leads - they are polarized and will be backwards half of the time (depending on motor direction). Backward tantalum capacitors make great smoke (and some times flame) generators, but I don't think that is what you had in mind. Choose instead a ceramic capacitor, which is not polarized.

Cliff

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Wed Jul 08, 2009 6:28 pm

Errr, what can I say, I like to live dangerously... Good catch.

I just updated the wiki to call for this cap from Newarks, 36K6164 at $.072 in qty 1. 50VDC Ceramic Multilayer Capacitor.

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Wed Jul 08, 2009 8:03 pm

I've made some progress on the open-encoder. Here's a picture of where it stands now.

Image

The key problem I have at the moment, is that I made a blunder when I made my PCB's. I used a narrow version of the AS5046. So my chip wouldn't work with the PCB I made.

I did the above modifications with a hacked version of a machine shop. I used a file and a vice to first rough in the stud.

Image

Then again at a 45

Image

Eventually I chucked it up in a drill and with the file, finished it off.

Image

In the end, it's not as accurate as it really should be. It's OC by about .015, but for such a hacked attempt, it came out better than I expected. A dab of super glue in the hole, and it should be done. That glue really pulled it's self in.

BasicFox
Posts: 59
Joined: Sun Mar 15, 2009 2:45 pm
Location: Belgium

Post by BasicFox » Fri Aug 14, 2009 9:05 am

My first open-encoder magnet is also mounted :p it looks really great, the magnets are very small but they have incredible strength! used locktite to connect the magnet to the shaft. Here is the result:
Image Image

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Fri Aug 14, 2009 9:22 am

That's looking good. Interesting to hear how it self centered. That makes sense as the metal body does attract the magnet. I think the next time I do one, I'll cut my nub a bit on the small side and see if that helps keep things aligned.

Last night I stopped by a friends and etched some boards. I have to upload this most recent version. I think this time I have it, and I should know for sure this weekend. I also have to figure out how to get some I2C code in the OSV3. My first goal is to see if I can get data out and into the OSIF.

BasicFox
Posts: 59
Joined: Sun Mar 15, 2009 2:45 pm
Location: Belgium

Post by BasicFox » Fri Aug 14, 2009 10:21 am

The osif itself is a I2C interface, i would first try to read the data using a self made program. So just connect the osif with the open-encoder and listen to what it has to say, if that works good you can write extra code in the OS3. First disable the potentiometer code and use the same register to write the open regulater value.

Can you show me your new pcb file for the open-regulator so i can make one too?

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Sat Aug 15, 2009 10:43 am

I have uploaded the most recent of what I have for both Open_Regulator (OR), and Open_Encoder (OE). They can be found here.

http://jaredharvey.com/openservo

In OR I had selected a huge cap, to prevent others from making the same mistake, I blanked that part out of the BOM.

In OE I have made some panels that are mirrored and work well for etching your own boards. pnl3 is the most recent.

Both of these include KICAD files (zip'ed) and PDF's. I renamed the folder structure for OE, so I may have broken a link or two.

Enjoy.

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Sun Aug 16, 2009 3:52 pm

I modified another POT and now have the first open-encoder. eventually I plan to post an assembly wiki like I did with the MG995 OSV3 construction tutorial. For now I'll post parts of significants in the forums. One note is that you should install the caps and SMT components before the magnet. They kept flying off the board and over to the magnet. What a pain that was.

I also find a dremel works better as a make shift lathe. The OD of the POT shaft is technically .135, not .125, but it's close enough. The big thing to remember is that you release dremel bits after you loosen the lock nut, you tap the end pushing it in, breaking the grip, then pull it out.


Image



The next time I do it, I plan to make an alignment shaft. Such that I align the board with the hole, then glue it in place. Then I install the magnet using the PCB as an alignment tool. I'm thinking I can make a low cost version of this tool by wrapping tape on a nail or some such shaft. Change how much you wind on and you change the OD. It's worth I shot I think.

Now I need to figure out how to make the OSIF software read this thing.

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Sun Aug 23, 2009 5:00 am

I fired up open encoder for the first time today. Passed the smoke test. Yup it smoked due to a short. I'm glad D3 on the OSIF was the weak point. The board consisted of a home etched PCB, with the chip, two filter caps, and a wire harness, connecting it to the OSIF, with a fedora box. By using python, and pyusb, I modified a script to get the below code. This worked great and I was getting consistent readings. Yipe!

osif.py

Code: Select all

#
# Classes to communicate with OpenServo InterFace
#
#
# Copyright (C) 2009  Darius Rad
#
# 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 <http&#58;//www.gnu.org/licenses/>.
#

import usb


_OSIF_USB_VID=0x1964
_OSIF_USB_PID=0x0001

_USBI2C_READ=20
_USBI2C_WRITE=21
_USBI2C_STOP=22
_USBI2C_STAT=23

_DEFAULT_TIMEOUT=1000

_EPIN=usb.ENDPOINT_TYPE_CONTROL | usb.ENDPOINT_IN | usb.TYPE_CLASS
_EPOUT=usb.ENDPOINT_TYPE_CONTROL | usb.ENDPOINT_OUT | usb.TYPE_CLASS

class Osif&#58;
    """OpenServo Interface"""

    # todo&#58; support other methods of connecting to OSIF &#40;/dev/i2c-*, uart&#41;


    def __init__&#40;self, adapter=0&#41;&#58;
        self.__adapter = adapter

        dev = _adapters&#91;self.__adapter&#93;
        self.__handle = dev.open&#40;&#41;

        configuration = dev.configurations&#91;0&#93;
        #self.__handle.setConfiguration&#40;configuration&#41;

        interface = configuration.interfaces&#91;0&#93;&#91;0&#93;
        #self.__handle.detachKernelDriver&#40;interface&#41;
        self.__handle.claimInterface&#40;interface&#41;


    def write&#40;self, slave, data=&#91;&#93;&#41;&#58;
        """Write data"""

        ret = self.__handle.controlMsg&#40;_EPOUT, _USBI2C_WRITE, data, index=slave, timeout=_DEFAULT_TIMEOUT
&#41;

        self.__handle.controlMsg&#40;_EPIN, _USBI2C_STOP, 0, timeout=_DEFAULT_TIMEOUT&#41;

        return ret


    def read&#40;self, slave, length=0&#41;&#58;
        """Read data"""

        ret = self.__handle.controlMsg&#40;_EPIN, _USBI2C_READ, length, index=slave, timeout=_DEFAULT_TIMEOUT
&#41;

        self.__handle.controlMsg&#40;_EPIN, _USBI2C_STOP, 0, timeout=_DEFAULT_TIMEOUT&#41;

        return ret


    def status&#40;self&#41;&#58;
        """Get I2C transaction status"""

        ret = self.__handle.controlMsg&#40;_EPIN, _USBI2C_STAT, 10, timeout=_DEFAULT_TIMEOUT&#41;

        return ret


# build list of OSIF adapters
_adapters = &#91;&#93;
for bus in usb.busses&#40;&#41;&#58;
    for dev in bus.devices&#58;
        if dev.idVendor == _OSIF_USB_VID \
                and dev.idProduct == _OSIF_USB_PID&#58;
            _adapters.append&#40;dev&#41;
test_OE_reading.py

Code: Select all

#!/usr/bin/python
#
# OpenServo Scan
#
# Scans the I2C bus provided by OSIF for OpenServo devices and dumps
# the registers of any slave found.
#
#
# Copyright &#40;C&#41; 2009  Darius Rad
# modified by Jared Harvey
#
# This program is free software&#58; 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
# &#40;at your option&#41; 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 <http&#58;//www.gnu.org/licenses/>.
#

def int2bin&#40;n, count=24&#41;&#58;
    """returns the binary of integer n, using count number of digits"""
    return "".join&#40;&#91;str&#40;&#40;n >> y&#41; & 1&#41; for y in range&#40;count-1, -1, -1&#41;&#93;&#41;

import osif

osif = osif.Osif&#40;&#41;

for i2 in range&#40;10&#41;&#58;
  for i in range&#40;40, 41&#41;&#58;
#      print "probing address 0x%x..." % &#40;i&#41;
      osif.write&#40;i, &#91;0x00&#93;&#41;

      stat = osif.status&#40;&#41;
      if len&#40;stat&#41; > 0 and stat&#91;0&#93; != 0x20&#58;
#          print "found device at address 0x%02x" % &#40;i&#41;

#          print "stat =",
#          for s in range&#40;len&#40;stat&#41;&#41;&#58;
#              print "%02x" % stat&#91;s&#93;,
#          print

#          print "regs =",
          regs = osif.read&#40;i,128&#41;
          for r in range&#40;1&#41;&#58;
              print "pos = %02x&#58;%02x " % &#40;regs&#91;0&#93;, regs&#91;3&#93;&#41; ,
              print "status=",int2bin&#40;regs&#91;1&#93;,6&#41;,
              print "mag&#58; %02x" % &#40;regs&#91;2&#93;&#41;,
              print
#          print
I now know the magnet is strong enough, I was able to center it accurately enough, and I know the basics of how the I2C is handled by this device. Won't be long now until I'm getting into the -dev code looking to add the I2C to code base.

BasicFox
Posts: 59
Joined: Sun Mar 15, 2009 2:45 pm
Location: Belgium

Post by BasicFox » Sun Aug 23, 2009 7:38 pm

good to hear that :D, can't wait to play with it... but still have to do some exams, just keep on going ;-)

jharvey
co-admin
Posts: 362
Joined: Sun Mar 15, 2009 12:06 pm
Location: Maine USA
Contact:

Post by jharvey » Sun Sep 06, 2009 10:29 am

BatchPCB is awesome. Just got my first batch of boards. I ordered 12, they gave me 24. Cost me less than $50 for this pile. They are spot on. I can now make a couple open-encoder for real, yeah!

FireBALL
Posts: 28
Joined: Fri Jul 24, 2009 4:31 am

Post by FireBALL » Thu Sep 17, 2009 4:11 pm

my take on open encoder

Hitec HS-5245MG

Image

BasicFox
Posts: 59
Joined: Sun Mar 15, 2009 2:45 pm
Location: Belgium

Post by BasicFox » Thu Sep 17, 2009 5:24 pm

nice how did you attach the magnet?

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests