Go Down

Topic: Change the gyroscope in the code (Read 130 times) previous topic - next topic

nisko27

May 21, 2018, 05:37 pm Last Edit: May 21, 2018, 05:40 pm by nisko27
hello !

I would like to replace this gyroscope MPU6050 with a tinkerkit T000062 in this program and if possible an explanation of the program .




Code: [Select]
#include <Wire.h>

#include <MPU6050.h>

#include <Servo.h>   

Servo sg90;         

int servo_pin = 2;

MPU6050 sensor ;

int16_t ax, ay, az ;

int16_t gx, gy, gz ;

void setup ( )

{

sg90.attach ( servo_pin );

Wire.begin ( );

Serial.begin  (9600);

Serial.println  ( "Initializing the sensor" );

sensor.initialize ( );

Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed");

delay (1000);

Serial.println ( "Taking Values from the sensor" );

delay (1000);

}




void loop ( )

{

sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);

ax = map (ax, -17000, 17000, 0, 180) ;

Serial.println (ax);

sg90.write (ax);

delay (200);

}






spycatcher2k

OK - you have permission to do so!
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

MorganS

You don't know what it does? Did it come with any instructions on how to wire up the sensor and the servo? Did you do that? Did it work?

Read the loop() function. Try to understand what each of those lines does.

Code: [Select]
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
Calls a function on the sensor library that does something with those 6 variables. Since we already know it's a 3-axis accelerometer and gyroscope, it's probably filling in those variables with 3 accelerations and 3 rotations.

Code: [Select]
ax = map (ax, -17000, 17000, 0, 180) ;
It takes the "ax" value and overwrites it with a version that only goes 0-180.

Code: [Select]
Serial.println (ax);

sg90.write (ax);

It writes that value to the Serial port and to a servo.

Code: [Select]
delay (200);
It waits for an aeon, since the Arduino is too fast for this simple program.

So to convert that to your different sensor, you would have to find a library function to give you the X acceleration, maybe tweak the values a little to make it fit into 0-180 and then you're ready to test it.

"The problem is in the code you didn't post."

nisko27

You don't know what it does? Did it come with any instructions on how to wire up the sensor and the servo? Did you do that? Did it work?

Read the loop() function. Try to understand what each of those lines does.

Code: [Select]
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
Calls a function on the sensor library that does something with those 6 variables. Since we already know it's a 3-axis accelerometer and gyroscope, it's probably filling in those variables with 3 accelerations and 3 rotations.

Code: [Select]
ax = map (ax, -17000, 17000, 0, 180) ;
It takes the "ax" value and overwrites it with a version that only goes 0-180.

Code: [Select]
Serial.println (ax);

sg90.write (ax);

It writes that value to the Serial port and to a servo.

Code: [Select]
delay (200);
It waits for an aeon, since the Arduino is too fast for this simple program.

So to convert that to your different sensor, you would have to find a library function to give you the X acceleration, maybe tweak the values a little to make it fit into 0-180 and then you're ready to test it.


Thank you for the explanation, I have not tested the code, I would want to replace the gyroscope (mpu6050 )  by an other (tinkerkit T000062 ) one and that the program gives the measures of Y (Pitching) forFor my project on the stabilization of the boat .

Go Up