Blog > Balancing Robot Accelerometer Drop- In Replacement

Home Forums Parallax Propeller: The Official Guide Balancing Robot Accelerometer Drop- In Replacement

This topic contains 4 replies, has 2 voices, and was last updated by  Hanno .

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #10696



    I’ve been working on your bot chapter in the official guide. Got an awesome base going. I’ve hit some snags and hope you can help me out.
    1. I’m not sure the orientation of the sensors in respect to the robot… I’ve pieced some together from your pics. But how should the accelerometer be oriented with respect to the robot base… i.e. which way should the bot be tilting to read negative/positive accel.
    2. Is motor 1 left or right?
    3. Ok here’s a big one…. I have all the hardware you called for in the book… had the LIS accelerometer as well, it was hard to find though. Unfortunately it shorted out and over heats now so I had to replace it. Since they are expensive I opted for the Sparkfun MMA8452Q break out since the specs were similar. Instead of 120/160Hz ODR it has 200 which I suspect is ok (is this assumption correct?) Also the accelerometer you used is 12 – bit right justified, so under the updateBias method in tilt.spin your a:= will be B11B11B11B11 XXXX XXXX XXXX (the 4 MSBs will reflect the Bit 11 of the acclerometer data)… if I did my research correct. The MMA8452 however is 12- bit right justified so the data is XXXX XXXX XXXX 0000 with the 4 LSBs as 0. I want to drop the MMA in and keep the code as similar as possible, so previous to the IF a > 32000 statement in the updateBias method I plan on doing this

    a:= read(MSB) << 8 + read(LSB) so I should get XXXX XXXX XXXX 0000, now I need to arrange this data to get a value 0 – 65535 to use the rest of your code… so…

    if I add the following before the IF a > 32000 statement in the updateBias method,

    IF (a & 0b1000_0000_0000_0000 > 0)
    a = (a>>4) + 1111_0000_0000_0000 ‘ Convert 12-Bit data to 16-bit sign extended data
    a := a>> 4 ‘ Convert 12-Bit data to 16-bit sign extended data
    (should end up with a value 0b0000_0000_0000_0000 to 1111_1111_1111_1111)

    Then after this I would use the rest of your code as is…. would this work?




    Or would it be better to do

    a = read(MSB) << 8 + read(LSB) ‘ should give XXXX XXXX XXXX 0000

    a = ((~~a) >> 4) & $0000_FFFF ‘ Sign-Extend 15 on a, then shift out the 4 LSB (which are padding zeros), then zero out value except the 16 LSbs

    This should give me a value a:= 0 -65535 which is compatible with the updateBias method, is this correct???

    Thanks Again,



    Hi Bryce,
    Very cool that you’re building a balancing bot. Can you post a photo of your base?
    I’ve been playing around with a 9axis IMU from sparkfun, maybe I’ll build another balancing bot with that.
    I believe the accelerometer should read positive when bot is tilted forwards.
    Motor 1 should be Left.
    Yes, 200 is close enough.
    I would try out the MMA8452 and see what values it’s returning. If it really is padding the LSB, then you can shift 4 to the right, that should take care of sign as well.
    Good luck!



    Hi Hanno,

    Thanks for the tip… thinking of it now it seems elementary to do a basic shift with sign extension, thank God for experienced guys like you.
    Here’s some pictures you asked for of my bot. I’m hoping to tune it now so it stays stable… it makes a valiant effort for a few seconds before tumbling completely. Can you tell me the things I need to adjust to tune it? Thanks for all your help… I’m excited to get it stable so I can move into computer vision!


    [attachment=0:1p919ior]Side View.jpg[/attachment:1p919ior]



    Very nice Bryce. Your bot is very short, this makes balancing much more difficult. At least at first, try to increase the height of the center of mass by mounting the battery ~30cm higher than it currently is.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.