(Last post #115)
Allright, I'm done with my exams, and ready to make that robot balance

@kas
Yes, we're(me and my group members)is building the robot at school. It's a part of a "real time computer science" class. Even though it became almost more of a cybernetics project rather than a programming one. As the class is now finished, I decides I wanted to continue with the robot at home, as I would love to see it balance:)
The reason we built it so high is that it is easier to make a matheatical model of a pendulum with a high center of gravity. In theory it is also easier to balance a high center of gravity rather than a low one, as it will get a bigger momentum of inertia around the pivot point(and then fall slower). The drawback comes when the robot is out of equalibrium with a small angle. The wheels will have to travel a longer distance faster to compansate for the error, compared to if the center of gravity had been low(Think pythagoras and you will see it). This implies that the angle measurement must be accurate and fast, and this is where we believe our(now mine

) problem lies.
I have by the way abandonened the Fez Domino, as we had lot of problems with Visual Studio freezing when downloading the program to the Fez, and a lack of some liberary classes for byte-level operations. Pluss, my computer wouldn't download programs to the Fez at all..

So I swappped it for an Arduino, and I'm back on track

However, my gyro problem continues on, as I'm trying to integrate the angle to verify that the gyro and real(accelrometer) angle got the same scale. Still, the integrated angle is about 33 - 50% of what it should be. I have connected the supply for the IMU to the 3.3V "pin" on the arduino, and also jumped it to the aref. I use the NOT op-amped output from the gyro. I use the factor 1.6(as suggested by kas in post #8) to get the read value in deg/sec.
I attach the program I use to integrate the gyro at the end of this post, so if anyone is interested it would be great to get some feedback! I use analogpin 5 for the gyro, and the autozero is attached to analogpin 2. Just change this to match your setup. The accelromteres is not used. If you use the op-amped gyrooutput, "gyroSensitivity" should be chenged to 9.1.
I ain't got anywhere to host the files for download, so this post becomes a bit long :/
If you want to try the program, you will have to paste the different modules into different files.
Great work on your robots out there folks!

Jon, Norway
The "loop". This is where the integration is done:
#include "IMU.h"
IMU imu;
float angle = 0;
void setup()
{
Serial.begin(9600);
imu.autoZero();
imu.getGyroOffset();
}
void loop()
{
float gyro = imu.getGyro();
angle += gyro * (112.0F/1000.0F); //The sampletime was measured with millis(), and then hardcoded.
Serial.println(angle);
delay(100);
}
The headerfile for the IMU class:
class IMU
{
public:
IMU();
void autoZero();
void getGyroOffset();
float getGyro();
int getAccX();
int getAccZ();
private:
int gyroPIN;
int accxPIN;
int acczPIN;
int azPIN;
int gyroOffset;
int accXOffset;
int accZOffset;
float gyroSensitivity;
float toDegrees;
};
And the implementation for the IMU class. This is where the gyro is read, and then scaled to deg/sec:
#include "IMU.h"
#include <WProgram.h>
IMU::IMU()
{
gyroPIN = 5;
accxPIN = 2;
acczPIN = 4;
azPIN = 2;
gyroSensitivity = 2.0F;
gyroOffset;
accXOffset = 0;
accZOffset = 0;
toDegrees = 3.22F / gyroSensitivity;
analogReference(EXTERNAL);
}
void IMU::autoZero()
{
digitalWrite(azPIN,LOW);
delay(1);
digitalWrite(azPIN,HIGH);
delay(1);
digitalWrite(azPIN,LOW);
delay(1);
}
void IMU::getGyroOffset()
{
long biases = 0;
for (int i = 0; i < 1000; i++)
{
biases += analogRead(gyroPIN);
delay(5);
}
gyroOffset = (int)(biases / 1000);
}
float IMU::getGyro()
{
float gyro = (float)(analogRead(gyroPIN) - gyroOffset);
//if ((gyro <= 2) && (gyro >= -2)) gyro = 0;
return gyro * toDegrees;
}
int IMU::getAccX()
{
return (analogRead(accxPIN) - accXOffset);
}
int IMU::getAccZ()
{
return (analogRead(acczPIN) - accZOffset);
}