Shop OBEX P1 Docs P2 Docs Learn Events
Switching accelerameters question for dancebot — Parallax Forums

Switching accelerameters question for dancebot

crazyrobotgirlcrazyrobotgirl Posts: 32
edited 2012-11-30 19:50 in Propeller 1
Hello everyone,

Im new to the world of microcontrollers. I have been studying the dancebot by Hanno Sander, and I decided to make his dance bot my learning platform. I bought all the parts except for the LIS3LV02DQ accelerameter he uses. I cant seem to find this chip. But I do have the Memsic 2125 Dual-axis Accelerometer from parallax.
My question is how would I go about replacing the LIS3LV02DQ with the Memsic 2125 I have a looked over the code but do not know where to start?

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-28 10:56
    Are you sure you don't want to blink a LED? (A common beginning project.)

    I looked at Hanno's code you attached, and my guess it the accelerometer is read by the "updateBias" method.

    If I were trying to do this, I think I'd find out what kind of value the line
    a:=i2c.readLocation(ACC3D_Addr,$28 ,8,8)<<8+i2c.readLocation(ACC3D_Addr,$29 ,8,8)
    

    produces and then try to get the same range of values from the Memsic. IIRC, the Memsic outputs a pulse. So I'd try to come up with a formula to convert this pulse length to a value similar to the "a" value in the code above.

    The alternative is to understand the code Hanno uses and modify it use a pulse length as a parameter.

    You'll probably need to read the datasheet on the accelerometer Hanno is using to know what values it returns.

    Edit: It looks like the code reads two bytes and combines them to make a 16-bit value.
  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2012-11-28 11:34
    Since the Memsic uses a different communication method I would save that for a different project and try using a sensor that is closer to the one used in the original project. It should be a lot less work to tweak the code to adjust the values for the new sensor instead of doing that and also change the method used to read the sensor.

    It looks like this sensor is a close replacement:

    https://www.sparkfun.com/products/9836?

    It was referenced from a breakout board for the sensor originally used on the project:

    https://www.sparkfun.com/products/758

    If you haven't done much with the Propeller yet I would suggest some simple examples like Duane suggested just to ensure you can program the Propeller and get familiar with the Programming environment.

    Robert
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-28 12:59
    Lol.
    I have been blinking leds, messing with ping, and other sensers.I have been toying with the propeller for about 6 months Mr. funny guy. But thanks alot for the info Duane. I have downloaded both datasheets and plan to study them.
    RobotWorkshop, thank you also I will look at that part from Sparkfun.

    Thanks guys!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-28 15:08
    Lol.
    I have been blinking leds, messing with ping, and other sensers.I have been toying with the propeller for about 6 months Mr. funny guy. But thanks alot for the info Duane. I have downloaded both datasheets and plan to study them.

    Good to know this isn't your first project.

    I'm a big fan of Hanno's stuff. I've tried his method of machine vision but I haven't tried to make a balancing bot yet.

    I do plan to work on this sometime soon myself since I've recently gotten interested in quadcopters and I think making a ground robot balance on two wheels would be useful in my attempt to understand making a quadcopter stable.

    I looked at the LIS3LV02DQ datasheet and at Hanno's code a little more.

    I think Hanno is checking to see if the sensor returned a negative acceleration and sign extending it (the hard way) to 32 bits with this code:

    [CODE if a>32000
    accel:=800*(a-65536)
    [/code]

    I think he could have changed this section:
    if a>32000
          accel:=800*(a-65536)
        else
          accel:=800*a
    

    to:
        accel:=800* (~~a)
    

    I just learned the "~~" (sign extend 16-bit to 32-bit) trick from someone on the forum.

    Based on the datasheet, it looks like the expected values returned from the accelerometer are between -2047 and 2047.

    So if you could change the pulse received from Memsic device to a similar range of numbers, the code could be made to work.

    I think the Memsic will output a pulse of 3333us for -1g and 6667us for 1g (with a 5000us pulse at 0g).

    I'd think a linear equation should be able to convert one set of numbers to the other.
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-28 22:51
    Duane thank you , I will spend some time getting this together, when its done I will post videos, and share code.
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-29 09:32
    Duane it does seem like there is going to be some nail biting math with converting the Memsic values for such a newby like myself. I could use some help with that if you are up to the challenge.
    I was looking over the main code for the dance bot. It seems the (to me) the only way to get the bot to move forward or backwards would be to trick the motor encoder into believing its some where its not.
    I could be wrong. But I had a mentos (candy) moment. But before I go further with controlling it. Maybe I need to get it to balance.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-29 10:11
    Here's what I have for the conversion.

    I hope my assumptions are correct. I think I have both of the accelerometers in question, so if needed I could make the comparison directly.

    The constant "ZERO_G_MEMSIC" may need to be adjusted based on your own device.
    CON
      ZERO_G_MEMSIC = 5000 ' µs This may need to be adjusted to match your accelerometer.
      CONVERSION_FACTOR = (2047 * 800) / 1667 ' assumes one G on original accelerometer
                                               ' returns "2047" and the pulse on the Memsic
                                               ' varies by 1667µs at one G.
                                               ' The "800" is included from Hanno's code.
                                                           
    PUB updateBias | pulseFromMemsic, ncnt
      
      StartKalman      
      ncnt:=cnt
      repeat
        'pulseFromMemsic in µs
        pulseFromMemsic := Memsic.GetPulse ' Assumes "Memsic" is an object to read
                                           ' the accelerometer and that the method
                                           ' "GetPulse" returns a value in microseconds.
        accel:= (pulseFromMemsic - ZERO_G_MEMSIC) * CONVERSION_FACTOR
        doKalman
        waitcnt(ncnt+=biasUpdate) '20ms  
    

    The method "Memsic.GetPulse" does not exist (that I know of). This has been left as "an exersize for the reader" as they say.

    Let me know if you have trouble getting a pulse from the Memsic in microseconds. Besides the Memsic objects, the code for the Ping could also be used to get the pulse length.

    I think the 20ms of the loop should be enough time to read from the Memsic. If not, the Memsic could be read from a different cog.

    EDIT: See below for code using the Memsic2125_v1.2 object.
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-29 11:40
    Just to make sure Im on the right page : ZERO_G_MEMSIC = 5000 ' µs This may need to be adjusted to match your accelerometer.
    CONVERSION_FACTOR = (2047 * 800) / 1667 ' assumes one G on original accelerometer
    ' returns "2047" and the pulse on the Memsic
    ' varies by 1667µs at one G.
    ' The "800" is included from Hanno's code.

    is replacing "msec=80_000" in the tilt object ?

    Will I need to change the ACC3D_Addr = %0011_1010 for the Mesmic 2125 address? Or is it a drop in conversion?
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-29 11:57
    my phone 251.jpg
    This is my balance bot with the motors and the encoder attached to the frame. I had to modify the encoder to get it to fit on the back of the 540 motors I am using. My brother had a bunch
    of rc truck parts I used including gears. I bought a breadboard from radio shack that is housing the electronics. Hopefully this weekend I will have videos to share.
    1024 x 1365 - 95K
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-29 12:08
    my phone 248.jpg
    my phone 249.jpg
    my phone 250.jpg


    Here are some pics of my bot with the motors, and the modified encoder to fit on the back of one of the two 540 motors. I got the parts from my brothers monster truck r/c grave yard collection.
    Bottom may look alittle sloppy but the wheels are parallel, and they move with out friction. I bought a bread board from radio shack to house the electronics. hopefully I wil have a video to post this weekend.
    1024 x 1365 - 112K
    1024 x 1365 - 139K
    1024 x 1365 - 131K
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-29 16:25
    For some reason the "reply with quote" isn't working right now.

    The constants I used are just to be added to the "CON" section. Nothing needs to be removed but the following don't look like they get used so could be removed.
    ACC3D_Addr    = %0011_1010
      accelSCLP=6
      accelSDAP=7
      accelS   =1
      accelO   =0
    

    Extra constants don't actually make the code size any larger (just the spin file).

    The Memsic isn't an I2C device. It isn't read the same way as the other accelerometer so there isn't an address for it. It's read a lot like a Ping sensor. You need to have a pin to read from the sensor and a method to read from it.

    So while my "updateBias" method should replace the original "updateBias " method, it's not exactly a drop in replacement. My method calls a non-existing method "Memsic.GetPulse". I haven't written such a method (I wanted to leave some of the fun to you). If you need help with it, let me know.

    I really like your robot. I love the monster wheels.

    Do you have an h-bridge curcuit yet? Which Propeller board are you using to control it?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-29 20:13
    crazyrobotgirl,

    I took a look at the Memsic2125_v1.2 object. It looks like it returns values in clock cycles. The Memsic2125_v1.2 runs in its own cog so it should return results very quickly. If you're out of cogs, it should be possible to read from the device a different way but I used the Memsic object since it was easy to use.

    Here's some updated code.
    CON ' replaces original "con" section
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
      msec = 80_000
      biasUpdate= 80_000_000/50 '20ms
      gAvgNum = 1 'shifted in code, so change that too
      MEMSIC_PIN_X = 6
      MEMSIC_PIN_Y = 7
      gyroOutP = 8
      gyroInP  = 5
      gyroS    = 40
      gyroO    = 82535
      ZERO_G_MEMSIC = 400_000 ' clock cycles
                              ' This may need to be adjusted to match your accelerometer.
                              
      CONVERSION_FACTOR = (2047 * 800) / (ZERO_G_MEMSIC / 3)
                    ' assumes one G on original accelerometer
                    ' returns "2047" and the pulse on the Memsic
                    ' varies by 400_000 at over three Gs (divide by three
                    ' to get one G).
                    ' The "800" is included from Hanno's code.
                                                           
    OBJ ' replaces original "obj section"
      Memsic : "Memsic2125_v1.2"
      m : "FloatMath"
      vp : "Conduit"
      qs : "QuickSample"
      
    PUB updateBias | pulseFromMemsic, ncnt ' replaces original updateBias method
      
      StartKalman      
      Memsic.start(MEMSIC_PIN_X, MEMSIC_PIN_Y)      '' Initialize Memsic2125 starts a cog
      
      ncnt:=cnt
      repeat
        pulseFromMemsic := Memsic.Mx 
        accel:= (pulseFromMemsic - ZERO_G_MEMSIC) * CONVERSION_FACTOR
        doKalman
        waitcnt(ncnt+=biasUpdate) '20ms  
    

    The above code replaces the original "CON" section, the original "OBJ" section and the original "updateBias" method.

    You can adjust the "ZERO_G_MEMSIC" by running "Memsic2125v2_Serial_Demo.spin" and seeing what your sensor outputs for the raw x value when level. This value should be somewhere around 400,000. Use this value for your "ZERO_G_MEMSIC" constant.
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-29 23:32
    Awesome Duane,Looks like Im going to have some fun this weekend putting together my balanceobot. I plan to learn from what you did by comparing the old from the new.I am using the Parallax semiconductor for all my projects.Thats all I have. And the reason for this is because about 8 months ago. I went to a yard sale, and a woman was selling her late husbands hobbyequipent( a whole work shop). I bought the lot for fifty dollars. I had to make three trips to get it all home. I have almost every senser that parallax makes plus alot of digikey circuits. And so I decided tostart programing. And thats my story. I will later work on replacing the QME-01encoder with the #27906 Position controller kit from parallax. The lot I bought contained a couple of dozen of them. Plus it would be easier for others to be able to use on their bot. I believe the disk that comes with this encoder can be engineered to the shaft of the gear thats coming from one of the wheels.If I made that sound confusing just look at the photos of my bot, and you will notice the extending shafts comeing from each wheel. Wel those shafts are connecting the gear to the wheel via ball bearings. I could extend them inward allowing the connection of the Position controller kit.Unlike tthe QME-01 encoder that has to mount to the back of a certain size motor. BTW I will be cutting those extended shafts soon. Im just not strong enough to snip them. I will also make plans to replace the the two LMD18200. with the Hbridges that I have. The LMD18200 uses two lines for communication PWM, and Direction. The ones I bought at the yard sale uses -100,0,100 three lines for communication. These hbridges are of mosffetts. More power, bigger motors, bigger balance bot, or segway.Thank you again Duane
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-30 07:21
    I will later work on replacing the QME-01encoder with the #27906 Position controller kit from parallax.

    I don't know what kind of encoders Hanno used. My guess is using the #27906 on the final drive shaft will likely not give you enough resolution to balance the bot.

    You'll probably be better off with an encoder on the motor shaft. From the pictures, it looks like you may be able to attach an encoder disk to the shaft coming from the motor. You'll get much better resolution if you can attach the encoder to the motor.

    Do your motors already have the QME-01 encoders? If so, do the work? If they work, leave them! They'll be much more precise than the other encoders which will help in balancing the robot.

    Do you know what the voltage and current requirements are for the motors?

    If you post a picture of your H-bridges someone around here will likely know what the are.

    You mention:
    I am using the Parallax semiconductor for all my projects.

    I assume you're using the Propeller chip but there are lots of different boards with a Propeller chip on it. There are the Propeller Protoboards (two kinds), QuickStart and a bunch of others. Just about any Propeller board should work fine for controlling your robot but some are more robot friendly than others. Is there a part number or name on the board(s) you're using?

    Do you have a gyro for your bot? The dance bot needs both an acclerometer and a gyro to balance.
  • PublisonPublison Posts: 12,366
    edited 2012-11-30 08:16
    Hanno used the Lynxmotion GHM-16 and added the QME-01 encoders as it was a nice fit for these motors.

    Single sourced from one supplier.
  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2012-11-30 08:24
    Duane Degn wrote: »
    I don't know what kind of encoders Hanno used. My guess is using the #27906 on the final drive shaft will likely not give you enough resolution to balance the bot.

    I agree with Duane. The 27906 position encoders work well for the original application they were intended for but I don't think they would work well for a balancing robot. The QME-01 encoders or even a hacked mouse encoder would probably be a better option.

    If the motors have an exposed shaft for the armature then you should probably leave them. Motors like that are getting harder to find and I prefer motors like that whenever I can get them since it makes fitting an encoder much easier if you want to use one. Having the encoder on the armature can not only help with accuracy but is extremely useful to regulate the speed of the motor.

    Sounds like you got a fantastic deal on all the equipment and it is good to hear that it all went to someone who will appreciate it and learn with it. The sad part is that if it was someone with a large cache of Parallax stuff we may have lost a forum member....

    Robert
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-30 12:52
    I have all the parts Hanno is using for his dancebot except the accelerometer. And thanks to Duane Im good to go. I am using the QuickStart board. I have a endless supply of them, and the person I got them from must have been a very experienced progragrammer.By looking at the chips and tools I purchased. My encoder is connected directly to the shaft of the motor(QME-01). By comparing the disk of the #27906 disk to the QME-01 disk. RobotWorkshop I think you are right about the low resolution on the #27906. And it not working well. The hbridge I plan to use on my larger balance bot are the same hbridges on the EDDIE board. I have many of those chips from sparkfun.
    I wished I would have opened up to you guys months ago for questions. This is fun!
    Thanks all
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-30 13:05
    RobotWorkshop how would a hacked mouse encoder work?
  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2012-11-30 13:58
    RobotWorkshop how would a hacked mouse encoder work?

    You can make them from the older (Non-optical) mice. Those mice are usually cheap now (sometimes free) and most have decent encoders in them. Here are just a couple examples:

    http://members.shaw.ca/swstuff/mouse.html

    http://retrointerfacing.com/?tag=mouse-encoder

    Also, if you want to make your own I have a thread I started that has some great tools for printing your own:

    http://forums.parallax.com/showthread.php?138597-Encoder-generators-Quadrature-etc.&highlight=encoder
  • crazyrobotgirlcrazyrobotgirl Posts: 32
    edited 2012-11-30 19:50
    How cool is that. Thanks for the info
Sign In or Register to comment.