Shop OBEX P1 Docs P2 Docs Learn Events
Kalman filtered sensors to move servo — Parallax Forums

Kalman filtered sensors to move servo

TygerbotTygerbot Posts: 8
edited 2013-04-20 05:22 in Accessories
Hello everyone,
I’m interested in learning how the Kalman Filter fuses two sensor together to get one result used in Hanno’s tilt object from the dance bot. Duane Degn redid Hanno’s tilt object for crazy robot girl by replacing the memsic accelerometer. I have had luck getting the memsic to work with the gyro. And now I want to be able to use Hanno’s tilt object to take the readings from the gyro, and the memsic fused by the Kalman Filter, and apply this to a servo. So in others words. As I tilt my sensors to the right (they are on the same breadboard). I want the servos horn to move to the right, and so on. How do you write a code from the tilt object to move the servo? Is this ramping? Can someone help, and get me started?
Thanks

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-04-16 08:18
    Hey Tygerbot,

    Welcome to the forums.

    Could you tell us more about what your want the servo to do?

    You might be making this harder than it needs to be. Your accelerometer and gyro are on a breadboard right? You want to move the breadboard and have your servo move some amount and direction based on this movement?

    The sensors are not connected to the servo horn? If not, this is a very different problem (probably easier) than the DanceBot balancing problem.

    What kind of Propeller board to you have?

    Have you learned to control the servo on its own yet?

    While the servo object in the Propeller Tool library has a demo program with it, I also wrote a small demo program it's in post #15 of my QuickStart servo tester thread.

    Edit: Based on what I understand so far, I don't think you'll need the gyro or a Kalman filter. Just use a linear equation to relate the accelerometer reading to a pulse length to send to the servo.
  • TygerbotTygerbot Posts: 8
    edited 2013-04-16 09:04
    Thanks for the rsponse,

    1.Yes i want to be able to move the breadboard with the sensors on it, and have the servo match the movement.

    2.The bread board is not connected to , or mounted to the servos horn.

    3.Im using the prop usb stick

    4. I have been practicing much with the servos. Moving them as well as ramping them.

    5.I probally wont need the gyro or the Kalman filter to achieve this. But I would like to use Hanno's tilt object to accomplish this. i am very interested in fusing these two sensors with the Kalman filter to do this (learning purposes) so I can better understand the balance bot.
    All I need is a little program to get started. Of course I dont want you to take all the fun out of it by doing the hole thing. I just want more or less a starter program. I have a propscope for the signals to help me, and viewport.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-04-16 09:16
    Tygerbot wrote: »
    5.I probally wont need the gyro or the Kalman filter to achieve this. But I would like to use Hanno's tilt object to accomplish this. i am very interested in fusing these two sensors with the Kalman filter to do this (learning purposes) so I can better understand the balance bot.

    Hanno's tilt object doesn't apply to what you want to do. When you have a balancing bot, you'll want to use Hanno's object. It's not applicable to your current application.

    You'll want a loop, probably repeating every 20ms, that reads the accelerometer and uses the reading to compute the desired servo position.

    Start with two points. Have the accelerometer reading be "x" and the servo pulse length "y". What do you want "y" to be at one extreme of "x". What do you want "y" to be at the other extreme? You now should have two points to your linear equation. With two points you can compute the equation of the line. You might need to change the order things are multiplied to take into account the Propeller uses integer math.
  • TygerbotTygerbot Posts: 8
    edited 2013-04-16 09:26
    with all that being said how would you write a program for that? I understand that the memsic gets its readings from a pulse, and you would pulse out to a servo. could you write me something small. Where when the x is not centered and tilts to the right the servo would move slightly that way? Again just something to get me started. i would want to modify it myself. After I get the servo moving on the x. I would write my own for another servo to move onthe y. Does this make sense?
  • TygerbotTygerbot Posts: 8
    edited 2013-04-16 09:56
    I belive a project like this (once modfied and a servo reading on the x of the memsic and another reaing on the y) Has many applications. Im very interested in making a self leveled table.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-04-16 10:17
    Tygerbot wrote: »
    with all that being said how would you write a program for that?

    I'm not sure off the top of my head. You want to come up with a relationship to accelerometer readings and pulse length. It's a nice algebra word problem.

    A self leveling table is a different problem than the one your initially described (not much though). Since on a self leveling table the servo would influence the sensors. In your initial problem the servo didn't affect the sensors.

    As long as the table is stationary (not flying through the air or driving over the ground) an accelerometer would be all that's need to keep it level.

    This isn't a simple problem but it's not real hard either.

    Again, you'll want a loop to read the sensor and the update the servo position. Since most servos are updated 50 times a second (20ms period) your loop doesn't need to be any faster than this (easy for the Prop).
  • TygerbotTygerbot Posts: 8
    edited 2013-04-16 11:21
    Are there any example codes in the obx that you know of?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-04-16 11:30
    Tygerbot wrote: »
    Are there any example codes in the obx that you know of?

    There's example code to use an accelerometer and there's example code to control servos. It's unlikely there's code to do just what you want.

    Chis the Carpenter of Rocket Brand Studios made a nice set of videos about making robots. I highly recommend them.

    From the Rocket Brand Studio's home page click "I want to build a robot and I have no idea where to start." on the left side of the page. He explains the process pretty well.
  • TygerbotTygerbot Posts: 8
    edited 2013-04-16 11:37
    Maybe thats where I need to start.
  • evanhevanh Posts: 15,393
    edited 2013-04-17 04:11
    Not sure if I'm stating the obvious here but I'll give the advice anyway ... Filters in this sort of application are usually tacked inside of a control loop known as a servo. So, in essence what you are trying to build is a servo. The fact that there may be a subservient servo that you are commanding does not detract that you are building a servo yourself.

    The cornerstone of any servo is what's known as a PID structure. They come in many shapes and sizes but have a general similarity about them. They provide, primarily, target tracking but also integrating and/or derivating translations that are typically needed to match sensors to the actuator.

    Filters, or just the raw measurements, will be directly feeding the inputs of the PIDs, with PID outputs commanding the actuators.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2013-04-19 13:38
    You might also want to look up something called a "Complimentary Filter". It does pretty much the same job as a Kalman filter, but it's much simpler and easier to understand. For a self-balancing robot you could easily use complimentary instead of Kalman filtering.

    Duane's point about it being an algebra problem is spot on. Depending on the sensors you choose, you'll have to implement most of the following steps:

    - Get a reading from your sensors

    - Optionally run your filter
    - Complementary or Kalman filter would go here

    - Convert the results into an angular value
    - If the result is from one of the filters, this may already be done
    - If the result is just from accelerometer, you'll probably be using ATAN or ATAN2

    - Map the range of the sensor reading to the range of your servo input
    - Servos generally take an input value of 1000 for "far left", 1500 for "centered", or 2000 for "far right"
    - Your sensor angle may be in degrees, radians, or some other form, like "-512 to +512". You'll need to convert from one to the other.

    - Use the result of your math to set the servo value.
  • TygerbotTygerbot Posts: 8
    edited 2013-04-19 21:13
    Balance table1.spinThanks for the info. But Thats above my level. Instead I think Ill work my way there. I have been been messing around. And Im trying to get this right. What am i doing wrong here?
  • JasonDorieJasonDorie Posts: 1,930
    edited 2013-04-20 00:10
    I can't tell for sure (reading from an IPad) but it looks like your indenting in the repeat loop might be off. Spin uses indenting to determine things like loop scope, so if the body of your loop isn't all indented the same it may mess it up. It looks to me like it might just repeat your first line (reading x from the accel) and skip all the rest of it.

    Jason
  • JasonDorieJasonDorie Posts: 1,930
    edited 2013-04-20 00:10
    I can't tell for sure (reading from an IPad) but it looks like your indenting in the repeat loop might be off. Spin uses indenting to determine things like loop scope, so if the body of your loop isn't all indented the same it may mess it up. It looks to me like it might just repeat your first line (reading x from the accel) and skip all the rest of it.

    Jason
  • TygerbotTygerbot Posts: 8
    edited 2013-04-20 05:22
    Thanks Jason Ill work on that
Sign In or Register to comment.