sel balancing single wheel code

I am currently experimenting with an arduino uno with a gyro breakout board l3g4200d. I have been looking everywhere for code that would work as a starting point to play around with. Does anyone have any suggestions specifically a +/- voltage to send to the dc motor to correct balancing angles. Any help would be greatly appreciated

Hello fozz,

I think that the first task for you is understanding how to interface with the gyro, seems that fortunately guys were doing it before http://arduino.cc/forum/index.php?topic=95022.0 Sparkfun sell it, take a look there for code examples etc.

The reaction to the gyro readings is really dependent on your setup, the mechanical part, weights and everything that interacts with the equilibrium.So there's no way to tell how much voltage you need. Before discovering how much voltage you must decide what you should do to "correct" forces and keep equilibrium. If you can detail how this all is working maybe we can try clues/suggestions, maybe some pictures.

Good luck

well I am just starting with a small 5v motor to see if the balancing code would work, I have the gyro working using the code below
I plan on using the balancing code through a motor controller to step up the voltage to 24 volts at up to 40amps but don"t want to start up that part of the program until I have some working code to work with. I was looking for a clean single motor setup with out dead man switches or any other extras yet. The gyro is L3G4200D mounted in a breadboard using the wiring diagram listed here (bildr L3G4200D Tripple Axis Gyroscope + Arduino - bildr)
//Arduino 1.0+ only

#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

int L3G4200D_Address = 105; //I2C address of the L3G4200D

int x;
int y;
int z;

void setup(){

Wire.begin();
Serial.begin(9600);

Serial.println(“starting up L3G4200D”);
setupL3G4200D(2000); // Configure L3G4200 - 250, 500 or 2000 deg/sec

delay(1500); //wait for the sensor to be ready
}

void loop(){
getGyroValues(); // This will update x, y, and z with new values

Serial.print(“X:”);
Serial.print(x);

Serial.print(" Y:");
Serial.print(y);

Serial.print(" Z:");
Serial.println(z);

delay(100); //Just here to slow down the serial to make it more readable
}

void getGyroValues(){

byte xMSB = readRegister(L3G4200D_Address, 0x29);
byte xLSB = readRegister(L3G4200D_Address, 0x28);
x = ((xMSB << 8) | xLSB);

byte yMSB = readRegister(L3G4200D_Address, 0x2B);
byte yLSB = readRegister(L3G4200D_Address, 0x2A);
y = ((yMSB << 8) | yLSB);

byte zMSB = readRegister(L3G4200D_Address, 0x2D);
byte zLSB = readRegister(L3G4200D_Address, 0x2C);
z = ((zMSB << 8) | zLSB);
}

int setupL3G4200D(int scale){
//From Jim Lindblom of Sparkfun’s code

// Enable x, y, z and turn off power down:
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);

// If you’d like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);

// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you’d like to configure INT1
// or INT2 otherwise, consult the datasheet:
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);

// CTRL_REG4 controls the full-scale range, among other things:

if(scale == 250){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
}else if(scale == 500){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
}else{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
}

// CTRL_REG5 controls high-pass filtering of outputs, use it
// if you’d like:
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}

void writeRegister(int deviceAddress, byte address, byte val) {
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}

int readRegister(int deviceAddress, byte address){

int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // register to read
Wire.endTransmission();

Wire.requestFrom(deviceAddress, 1); // read a byte

while(!Wire.available()) {
// waiting
}

v = Wire.read();
return v;
}

The algorithm to make an unstable bot balance itself is complicated. Firstly you have to read the gyro and accelerometer inputs and analyse them to work out what the 'bot is doing, then you have to apply a feedback algorithm to calculate the action needed to move it towards the balance point. Both parts involve some fairly complex maths. Plenty of people have done similar things and I'm sure that Google will turn up plenty of examples so that you can get a feel for the amount of work involved - you may even be able to locate some examples of working code.

I'm intrigued by your reference to a single wheel 'bot and this makes me wonder whether it's going to balance in two dimensions (like a unicycle) rather than just one (like a segway).

the overall goal is to build a self balancing unicycle but I'm new to this. I currently received a bunch of parts listed below for when I sort out the code and proper sensors. I was using http://glennselectric.blogspot.com/ for reference but cannot find the IMU board he was using (discontinued) Sparkfun 5dof IMU. I looked on Sparkfun but this break out IMU (https://www.sparkfun.com/products/10121) has different pin out. Would this work the same way?

Arduino Uno Gyro Sensor Dual H-Bridge DC Motor Driver Peak 60A Controller Two new sealed 12volt scooter batteries 24volt to 12volt converter to drop power for the Arduino Pocket bike rear wheel with gear New Electric scooter Motor with gear so it works out to a 6:1 ratio Electric scooter Charger and Jack Voltage indicator Chain aluminum pegs Fuses Tether Kill Switch in case you fall off We have an Aluminum Welder to make the frame