Go Down

Topic: Servo motor and gyroscope  (Read 712 times) previous topic - next topic

jbraightmeyer

Feb 23, 2018, 09:52 pm Last Edit: Feb 23, 2018, 10:02 pm by jbraightmeyer
Is there a way to get one servo to rotate counterclockwise. And the other to rotate clockwise while still using the same axis of the gyroscope. I am operating 2 servos with a mpu6050.

vinceherman

Is there a way to get one servo to rotate counterclockwise. And the other to rotate clockwise
Yes.  Write a value to one servo that turns it CCW.  Write a different value to the other servo that turns it CW.


Quote
while still using the same axis of the gyroscope.
I am *guessing* that you are using the output of one axis of a gyro to control the servo behavior.  You will probably have to do a bit more describing of how the gyro values control the servos.

jbraightmeyer

#2
Feb 23, 2018, 10:10 pm Last Edit: Feb 23, 2018, 10:15 pm by jbraightmeyer
I am very new to programming so I can't really explain the values. I will try to upload my code so you can understand what i am trying to do.

vinceherman

Good idea.
And since you are new, go read through the How to use this forum - please read sticky post that has much great info for new members.

jbraightmeyer

So I ended up using  this guys code and editing it to work 4 servos. https://maker.pro/education/diy-constant-current-source-power-supply-circuit

For some reason it isnt letting me attach a picture of my code

vinceherman

For some reason it isnt letting me attach a picture of my code
Don't attach a picture of code.  Post the code using code tags, as is fully documented in the sticky post I linked to in reply #3 above.

jbraightmeyer

Here is the code ive been using

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

#include <MPU6050.h>

#include <Servo.h>   

Servo sg90; 
Servo sg91;
Servo sg92;
Servo sg93;       

int servo_pin0 = 8;
int servo_pin1 = 9;
int servo_pin2 = 10;
int servo_pin3 = 11;

MPU6050 sensor ;

int16_t ax, ay, az ;

int16_t gx, gy, gz ;

void setup ( )

{

sg90.attach ( servo_pin0 );
sg91.attach ( servo_pin1 );
sg92.attach ( servo_pin2 );
sg93.attach ( servo_pin3 );

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) ;
ay = map (ay, -17000, 17000, 0, 180) ;


Serial.println (ax);
Serial.println (ay);

void reverse() {
sg90.write (ax);
}

sg91.write (ax);

sg92.write (ay);

sg93.write (ay);

delay (000);

}


Thank you for all the help

Go Up