Controlling a servomotor with the MPU6050 accelerometer

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
tydexe
Posts: 4
Joined: Thu Mar 05, 2020 11:16 am
Has thanked: 1 time
Been thanked: 1 time
Contact:

Controlling a servomotor with the MPU6050 accelerometer

Post by tydexe »

Hello, I'm a beginner with flowcode and I don't know how to program to control my HiTec HS-311 servomotor with the MPU6050...

You'll see what I tried in the attachment...

Can you send me a program that solves my problem? :)

Thank you!

Best regards,
Hugo D
Attachments
1.JPG
1.JPG (85.29 KiB) Viewed 4125 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controlling a servomotor with the MPU6050 accelerometer

Post by Benj »

Hello,

It looks like you are not initialising the MPU6050 component. Also the servo motor should be enabled after initialising the servo component.

Can you attach your project file so we can look at and edit that without having to start again.

tydexe
Posts: 4
Joined: Thu Mar 05, 2020 11:16 am
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Controlling a servomotor with the MPU6050 accelerometer

Post by tydexe »

Hello !

Thank you for your answer. I see what you mean about the initialization but to be honest I don't understand because I have to complete the Return Value and I don't know how to do it.

Attached is my complete program! Thank you in advance, you save me!
Attachments
Flowcode_test.fcfx
(10.12 KiB) Downloaded 199 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controlling a servomotor with the MPU6050 accelerometer

Post by Benj »

Hello,

Try this one instead, I have rearranged things slightly and added in a few more macro calls.
Flowcode_test.fcfx
(10.58 KiB) Downloaded 233 times

tydexe
Posts: 4
Joined: Thu Mar 05, 2020 11:16 am
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Controlling a servomotor with the MPU6050 accelerometer

Post by tydexe »

Wow, thank you!

That's work well! I'll get back to you if I have any more questions! :)

tydexe
Posts: 4
Joined: Thu Mar 05, 2020 11:16 am
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Controlling a servomotor with the MPU6050 accelerometer

Post by tydexe »

SO I'll be right back because I have a new question:

The program you sent me works fine but the sensor is very sensitive and at the slightest movement the engine will rotate 180°. Would you know how to modify the program and make sure that when I turn my accelerometer 90° on the X axis, the servo turns 90°, when I turn my accelerometer 120°, the servo turns 120°, etc...

Thank you in advance!
Attachments
Flowcode_test.fcfx
(10.12 KiB) Downloaded 200 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controlling a servomotor with the MPU6050 accelerometer

Post by Benj »

Hello,

I think you probably first need to filter the values to reduce some of the sensitivity.

A simple way of doing this is to take say 10 readings, then add up all 10 readings into a long variable, divide the result by 10 and this should give you a nice averaged value to assign to the servo. Add more readings for better filtering.

To get the angle correct you will need to appropriatley scale the result before passing to the servo, different servo's turn different amounts so this may be a case of trial and error. You could also tweak the timings on the servo properties to reduce or increase the movement.

These blog posts may be of interest.
https://www.matrixtsl.com/blog/quadroco ... -and-gyro/

Post Reply