Hi all,
I' having trouble getting my gyro stabilized project to work. The project consists of two motors spinning opposite directions which creates a torque on a piece of board which then turns to the desired angle.
The problem I am facing is that when I use an external power source for the arduino nano, the motors just continue to spin at the same rate no matter what i do. It works perfectly however when the usb is the power supply, the motor slow down and up as I adjust the angle.
I am powering the arduino nano with a 3.7V lipo that is connected to a 5V step up converter (link below). I have checked and the voltage it supplies is 4.9V - 5V. This supply is plugged into the 5V pin in the Nano. The MPU's voltage also runs in the working range of 3.3V.
The motor shield is powered by its own power supply.
I've checked the whole thing several times, reading voltages, changing batteries, using 8V to the VIN pin. I've also used some code to see if the PID value of the controller changes using an LED blink. From that I gathered that the PID value stays the same no matter what, which means that the gyro isn't sending any data.
Does any one have a clue of what the issue could be, have been trying for many hours now. I even checked my soldering and battery capacity. I've also checked the power requirements of the system, and the system only requires around 25mA at most.
I've added the code (it's not the prettiest, first time writing anything like this). I'm using the MPU6050 library from Jeff Rowberg along with the I2Cdev library. The device can be seen in the photo below, the MPU is situated on the back.
Thanks
[code]
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
//gyro values
int16_t ax, ay, az;
int16_t gx, gy,gz;
//PID values
float DT = 0;
float position = 0;
float gxc = 0;
//PID constants
float KP = 1000;
float KI = 10;
float KD = 0;
//PID variables
float error = 0;
float integral = 0;
float derivative = 0;
float PID = 0;
float error_previous = 0;
unsigned long start_time = 0;
unsigned long end_time =0;
//Desired Angle
float set_point = 90;
//Motor pins
int D = 12;
int P = 3;
int B = 9;
int D2 = 13;
int P2 = 11;
int B2 = 8;
int led = 13;
#define GYRO_AVERAGE_OFFSET_X ((float) 100)
#define GYRO_GAIN(x) (x/131)
void setup() {
Serial.begin(9600);
//Motor connection Setup 1
pinMode (D, OUTPUT);
pinMode (P, OUTPUT);
pinMode (B, OUTPUT);
//Turn of brake
digitalWrite (B, LOW);
//Motor connection Setup 2
pinMode (D2, OUTPUT);
pinMode (P2, OUTPUT);
pinMode (B2, OUTPUT);
//Turn of brake
digitalWrite (B2, LOW);
}
void loop() {
start_time = millis();
//Read Gyro value and apply offest
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
accelgyro.setXGyroOffset (GYRO_AVERAGE_OFFSET_X);
//Calculate RAW to degrees/s
gxc= GYRO_GAIN(gx);
//Loop time
DT = start_time - end_time;
//Poistion of gyro
position = position + gxc*(DT/1000);
// Serial.print(position); Serial.print("\t");
//PID Controller.
error = position - set_point;
integral = integral + (error*DT);
derivative = (error - error_previous)/DT;
//Saturation filter
if (integral > 40000)
integral = 40000;
if (integral < -40000)
integral = -40000;
//PID controller Output
PID = ((KP*error)+(KI*integral)+(KD*derivative))/10000;
error_previous = error;
//Gyro check
Serial.print(set_point); Serial.print("\t");
Serial.print(error); Serial.print("\t");
Serial.print(integral); Serial.print("\t");
Serial.print(derivative); Serial.print("\t");
//Saturation filter
if(PID>=75)
PID = 75;
if(PID<=-75)
PID = -75;
Serial.print(PID); Serial.print("\n");
delay (100);
//Motor speed 1
digitalWrite(D, LOW);
analogWrite(P,75+PID);
//Motor speed 2
digitalWrite(D2, LOW);
analogWrite(P2,75-PID);
end_time = start_time;
}
[/code]