Pages: [1]   Go Down
Author Topic: Almost there! A bit more help please!!!  (Read 953 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi again,
Well with regards to my earlier post  http://arduino.cc/forum/index.php/topic,126524.0.html I  have now replaced the 3 servos with a AD5206 digital pot... And so far it works and is controlling the replaced analog pots that were on my xbox controller, so far so good! 

BUT, 

I need some help with a little bit of code that would hold the last position untill the gyro moves again either back to level or the next position!
Example:  Tilt forward with the gyro (x axis) the pot dose the same but if held in this possistion the gyro data returns imedeatly back to it's starting point even when it hasn't moved. But Ijust want it to hold or sustain that last position untill i move it... Just as if you pushed a joystick forward and held it there the analoge pots would stay in that position untill you leveled off...
However with the gyro it wont, it simply returns to it's starting value..
Any Ideas appritiated..

 Here's the code I have sofar.
Code:
#include <Wire.h>
#include <SPI.h>

//control registers for accel
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23

int Addr = 105;                 // I2C address of gyro

// set pin 10 as the slave select for the digital pot:
 const int slaveSelectPin = 10;
 
 #define channelx 1
 #define channely 2
 #define channelz 3


int x, y, z;
int a, b, c;

void setup(){
  Wire.begin();
  Serial.begin(9600);
  writeI2C(CTRL_REG1, 0x1F);    // Turn on all axes, disable power down
  writeI2C(CTRL_REG3, 0x08);    // Enable control ready signal
  writeI2C(CTRL_REG4, 0x80);    // Set scale (500 deg/sec)
  delay(100);                   // Wait to synchronize
 
    // set the slaveSelectPin as an output:
   pinMode (slaveSelectPin, OUTPUT);
   // initialize SPI:
   SPI.begin();

 
 
 
}
// ae mod to outout to digital pot//


void loop(){
  getGyroValues();              // Get new values
  // In following Dividing by 114 reduces noise
  a=(x / 285)+128;
  Serial.print("Raw X:");  Serial.print(a);
  digitalPotWrite(channelx,a);
  b=(y / 285)+128; 
  Serial.print(" Raw Y:"); Serial.print(b);
 digitalPotWrite(channely,b);
  c= (z / 285)+128;
  Serial.print(" Raw Z:"); Serial.println(c);
  digitalPotWrite(channelz,c);
  delay(200);                   // Short delay between reads
}

void getGyroValues () {
  byte MSB, LSB;

  MSB = readI2C(0x29);
  LSB = readI2C(0x28);
  x = ((MSB << 8) | LSB);

  MSB = readI2C(0x2B);
  LSB = readI2C(0x2A);
  y = ((MSB << 8) | LSB);

  MSB = readI2C(0x2D);
  LSB = readI2C(0x2C);
  z = ((MSB << 8) | LSB);
}

int readI2C (byte regAddr) {
    Wire.beginTransmission(Addr);
    Wire.write(regAddr);                // Register address to read
    Wire.endTransmission();             // Terminate request
    Wire.requestFrom(Addr, 1);          // Read a byte
    while(!Wire.available()) { };       // Wait for receipt
    return(Wire.read());                // Get result
}

void writeI2C (byte regAddr, byte val) {
    Wire.beginTransmission(Addr);
    Wire.write(regAddr);
    Wire.write(val);
    Wire.endTransmission();
}

int digitalPotWrite(int address, int value) {
   // take the SS pin low to select the chip:
   digitalWrite(slaveSelectPin,LOW);
   //  send in the address and value via SPI:
   SPI.transfer(address);
   SPI.transfer(value);
   // take the SS pin high to de-select the chip:
   digitalWrite(slaveSelectPin,HIGH);
}

Thanks,
aliboo
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8706
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The gyro tells you how fast you are turning it.  It has no absolute reference.  That's why most controllers us an accelerometer: to detect deviations from level to use as controls.

To measure "how far did I turn" in order to determine when you move back to that position you have to take the speed of rotation times the amount of time it has been rotating at that speed and add that to the last known angle.  The gyroscope will tend to drift.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: