Show Posts
Pages: [1]
1  Topics / Device Hacking / Wii camera demo + digitalpot's problem continues. on: January 08, 2013, 07:36:28 am
It works ! kinda !!
OK, after some real head banging and coding issues I finally am getting some where, or so I thought...
Using wiimote ir camera hack and the AD5206 Digital pot to control my xbox wired controller I have hit another problem ! After being helped by a great guy with the code I find that I only get the X axis to work (ie Left /right)
the Y axis still refuses to work (up and down), I see in the serial monitor that it is functioning fine, but get nothing,
no voltage output from the digi-pot (channel 2), I have check the wiring etc , and even reolaced the digi-pot,
I have even re-wired it so that the X axis wiper wire controls  the Y axis on the xbox controller ,  it works! I have tried a different channel too, still nothing!  So I'm thinking maybe it's still a coding issue, so again here is the code I have, Am I just missing something ??? Can some one have a look  PLEASE, before I go mad !!

Code:
                                                                     
                                                                     
                                                                     
                                             

#include <Wire.h>
#include <PVision.h>
#include <SPI.h>
//Select one of the following to be 0, and the other 1
//'0': ON, '1': OFF
#define serial_display 1
#define processing_display 0

#define channelx 1
#define channely 2
PVision ircam;
byte result;


// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
int digpot_x = 500;
int digpot_y = 500;

//MUX testing
int S0 = 5;
int S1 = 6;
void setup()
{
  Serial.begin(115200);
  ircam.init();
   // while (Serial.available() <= 0) {
     //Serial.print("AE v1.0");   // send a capital A
    // delay(300);
    //}
//SPI stuff here
   pinMode (slaveSelectPin, OUTPUT);
   SPI.begin();
}

void loop()
{
   
  result = ircam.read();
 
  if(serial_display == 1){
    if (result & BLOB1)
    {
      digpot_x = ircam.Blob1.X >> 2;
      digpot_y = ircam.Blob1.Y >> 2;
      Serial.print("BLOB1 detected. X:");
      Serial.print(digpot_x);
      Serial.print(" Y:");
      Serial.print(digpot_y);
      Serial.print(" Size:");
      Serial.println(ircam.Blob1.Size);
    }
   
    if (result & BLOB2)
    {
      Serial.print("BLOB2 detected. X:");
      Serial.print(ircam.Blob2.X >> 2);
      Serial.print(" Y:");
      Serial.print(ircam.Blob2.Y >> 2);
      Serial.print(" Size:");
      Serial.println(ircam.Blob2.Size);
    }
    if (result & BLOB3)
    {
      Serial.print("BLOB3 detected. X:");
      Serial.print(ircam.Blob3.X >> 2);
      Serial.print(" Y:");
      Serial.print(ircam.Blob3.Y >> 2);
      Serial.print(" Size:");
      Serial.println(ircam.Blob3.Size);
    }
    if (result & BLOB4)
    {
      Serial.print("BLOB4 detected. X:");
      Serial.print(ircam.Blob4.X >> 2);
      Serial.print(" Y:");
      Serial.print(ircam.Blob4.Y >> 2);
      Serial.print(" Size:");
      Serial.println(ircam.Blob4.Size);
    }
   
     digitalPotWrite (channelx,(int)digpot_x);
     digitalPotWrite (channely,(int)digpot_y);
  }//endif serial_display
 
  else if(processing_display == 1){
    if( Serial.available() > 0){
   //   print header
     uint8_t XL1 = ircam.Blob1.X & 0xFF;
     uint8_t XH1 = (ircam.Blob1.X >> 8) & 0x03;
     uint8_t YL1 = ircam.Blob1.Y & 0xFF;
     uint8_t YH1 = (ircam.Blob1.Y >> 8) & 0x03;
     uint8_t XL2 = ircam.Blob2.X & 0xFF;
     uint8_t XH2 = (ircam.Blob2.X >> 8) & 0x03;
     uint8_t YL2 = ircam.Blob2.Y & 0xFF;
     uint8_t YH2 = (ircam.Blob2.Y >> 8) & 0x03;

     
     
      Serial.write(XL1);
      Serial.write(XH1);
      Serial.write(YL1);
      Serial.write(YH1);
      Serial.write(XL2);
      Serial.write(XH2);
      Serial.write(YL2);
      Serial.write(YH2);
    }//end ifSerial.available
  }//endif processing_display
 
}//End loop()

void 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);
}

 smiley-roll Thanks !!
2  Using Arduino / Programming Questions / Re: Wii camera demo HELP on: January 05, 2013, 06:35:43 am
Thanks for the quick reply...
I'm going to show my complete ignorance now, I'm not really a "coder" and have a limited understanding on such things, so if you could be a bit more specific that would be a pretty cool thing,
Thanks..
3  Using Arduino / Programming Questions / Wii camera demo HELP on: January 05, 2013, 06:22:32 am
Hi Guy's,
I'm almost there! This code works OK ( sort of) untill I try to write to the digitl pots and compile, I just get an expected declaration before ' } ' token ... What dose this mean? Any help please.. Plus the code for the "prossessing is comented out as it was just to full of bugs !! Heres the code I have.

Code:
/*Wii Camera demo using Stephen Hobley's PVision library
Use serial_display 1, processing_display 0
for standard ASCII output to Arduino serial monitor

Use serial_display 0, processing_display 1
for 2-blob communication and plotting to processing application

Author: Johnnyonthespot
*/

#include <Wire.h>
#include <PVision.h>
#include <SPI.h>
//Select one of the following to be 0, and the other 1
//'0': ON, '1': OFF
#define serial_display 1
#define processing_display 0

#define channelx 1
#define channely 2
PVision ircam;
byte result;


// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
int digpot_x = 500;
int digpot_y = 500;
}
 // set the slaveSelectPin as an output:
   pinMode (slaveSelectPin, OUTPUT);
   // initialize SPI:
   SPI.begin();

//MUX testing
int S0 = 5;
int S1 = 6;
void setup()
{
  Serial.begin(115200);
  ircam.init();
   // while (Serial.available() <= 0) {
     //Serial.print("AE v1.0");   // send a capital A
    // delay(300);
    //}
}

void loop()
{
   
  result = ircam.read();
 
  if(serial_display == 1){
    if (result & BLOB1)
    {
      digpot_y = ircam.Blob1.X >> 2;
      digpot_x = ircam.Blob1.Y >> 2;
      Serial.print("BLOB1 detected. X:");
      Serial.print(digpot_x);
      Serial.print(" Y:");
      Serial.print(digpot_y);
      Serial.print(" Size:");
      Serial.println(ircam.Blob1.Size);
    }
   
    if (result & BLOB2)
    {
      Serial.print("BLOB2 detected. X:");
      Serial.print(ircam.Blob2.X >> 2);
      Serial.print(" Y:");
      Serial.print(ircam.Blob2.Y >> 2);
      Serial.print(" Size:");
      Serial.println(ircam.Blob2.Size);
    }
    if (result & BLOB3)
    {
      Serial.print("BLOB3 detected. X:");
      Serial.print(ircam.Blob3.X >> 2);
      Serial.print(" Y:");
      Serial.print(ircam.Blob3.Y >> 2);
      Serial.print(" Size:");
      Serial.println(ircam.Blob3.Size);
    }
    if (result & BLOB4)
    {
      Serial.print("BLOB4 detected. X:");
      Serial.print(ircam.Blob4.X >> 2);
      Serial.print(" Y:");
      Serial.print(ircam.Blob4.Y >> 2);
      Serial.print(" Size:");
      Serial.println(ircam.Blob4.Size);
    }
   
  digitalPotWrite (channelx,(int)attX);
  digitalPotWrite (channely,(int)attY);
  }
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);
}
  }//endif serial_display
 
  else if(processing_display == 1){
    if( Serial.available() > 0){
   //   print header
     uint8_t XL1 = ircam.Blob1.X & 0xFF;
     uint8_t XH1 = (ircam.Blob1.X >> 8) & 0x03;
     uint8_t YL1 = ircam.Blob1.Y & 0xFF;
     uint8_t YH1 = (ircam.Blob1.Y >> 8) & 0x03;
     uint8_t XL2 = ircam.Blob2.X & 0xFF;
     uint8_t XH2 = (ircam.Blob2.X >> 8) & 0x03;
     uint8_t YL2 = ircam.Blob2.Y & 0xFF;
     uint8_t YH2 = (ircam.Blob2.Y >> 8) & 0x03;

     
     
      Serial.write(XL1);
      Serial.write(XH1);
      Serial.write(YL1);
      Serial.write(YH1);
      Serial.write(XL2);
      Serial.write(XH2);
      Serial.write(YL2);
      Serial.write(YH2);
    }//end ifSerial.available
  }//endif processing_display
 

}

/*
Processing code for 2-blob output
import processing.serial.*;

int bgcolor = 250;
int fill_color = 255;      // Background color
int fgcolor;      // Fill color
Serial myPort;                       // The serial port
int[] serialInArray = new int[8];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int xpos_1, xpos_2, ypos_1, ypos_2;              // Starting position of the ball
boolean firstContact = false;        // Whether we've heard from the microcontroller
float angle, hyp;
int last_update = 0;
int this_update = 0;
//Blob data
int ycoord_max = 747;
int xcoord_max = 1023;
int[] xcoord = new int[4];
int[] ycoord = new int[4];

void setup() {
  size(1024, 750);  // Stage size
  noStroke();      // No border on the next thing drawn
  frameRate(400);
  // Set the starting position of the ball (middle of the stage)
  xpos_1 = width/2 - width/4;
  ypos_1 = height/2 - width/4;
  xpos_2 = width/2 + width/4;
  ypos_2 = height/2 + width/4;
  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, "COM4", 115200);
}

void draw() {
  background(bgcolor);
  fill(0);
  // Draw the shape
  if(xcoord[0] < xcoord_max && ycoord[0] < ycoord_max && xcoord[1] < xcoord_max && ycoord[1] < ycoord_max){
    //hyp = sqrt( sq(xcoord[1] - xcoord[0]) + sq(ycoord[1] - ycoord[0]));
   // angle = degrees( acos((xcoord[1]-xcoord[0])/hyp));
    strokeWeight(2);
    stroke(255,10,10);
    line(xpos_1, ypos_1, xpos_2, ypos_2);
    stroke(0);
    line(xpos_1, ypos_1, xpos_2, ypos_1);
    stroke(0);
    line(xpos_2, ypos_1, xpos_2, ypos_2);
  }
  else{
   angle = 0;
  }
  ellipse(xpos_1, ypos_1, 30, 30);
  ellipse(xpos_2, ypos_2, 30, 30);


}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller.
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write('A');       // ask for more
    }
  }
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;
   
    // If we have 3 bytes:
    if (serialCount > 7 ) {
     
      //Parse incoming data
      xcoord[0] = serialInArray[0] + (serialInArray[1] << 8);
      ycoord[0] = serialInArray[2] + (serialInArray[3] << 8);
      xcoord[1] = serialInArray[4] + (serialInArray[5] << 8);
      ycoord[1] = serialInArray[6] + (serialInArray[7] << 8);     
      //Update coordinate data
     xpos_1 = xcoord[0]*width/xcoord_max;
     ypos_1 = ycoord[0]*height/ycoord_max;
     xpos_2 = xcoord[1]*width/xcoord_max;
     ypos_2 = ycoord[1]*height/ycoord_max;
     
      // print the values (for debugging purposes only):
     // println(xcoord[0] + "\t" + ycoord[0] + "\t" + xcoord[1]+"\t" + ycoord[1] + "\tangle: " + angle + "\tRate: " + (this_update - last_update));

      // Send a capital A to request new sensor readi
     // myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}
*/
4  Topics / Robotics / Almost there! A bit more help please!!! on: October 25, 2012, 02:19:52 am
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
5  Topics / Robotics / Re: Gyro control of a xbox controller (help!) on: October 10, 2012, 12:28:12 pm
All I know is that the pot's on the controller travel 35 degres eitherside of zero, so I guess the servos will need to travel only the same amount!
6  Topics / Robotics / Re: Gyro control of a xbox controller (help!) on: October 10, 2012, 12:14:28 pm
Hi,
Dont know just yet what the servo ranges are!

Q: "  Serial.print("Raw X:");  Serial.print(x / 114);
  Serial.print(" Raw Y:"); Serial.print(y / 114);
  Serial.print(" Raw Z:"); Serial.println(z / 114);
Is that a typo on the third line for the  serial.println(z/

And do I just add the code to the bottom of the other?
Thanks..
7  Topics / Robotics / Gyro control of a xbox controller (help!) on: October 10, 2012, 06:48:39 am
Hi all,

Yep I'm a total newbie, know jack-sh$t about programming (sorta)..

OK, first I have MS, so I have a few problems, so,  I am currently building a project to control my xbox controller! ( it's the hands you know!)

So far, I have a an L3G4200D gyro hooked up to my arduino,

and I found some code that gives me a "stable" output and a good solid o position etc,

this hopefully will drive (for the moment) 3 servos, one one for each, X, Y, Z, axis,

as if controlling a Helicopter etc, These will in-turn turn the potentiometers on the xbox controllers right and left thumb sticks, To control view direction ie, pitch, roll, yaw etc

in game...

The Gyro will be on a pair of video glasses (Vuzix Wrap 1200), the idea being that when I move my head up & down and roll left to right the the gyro output will replace the xbox controllers sticks! same thing as FPV (first person view) for flying model air planes and heli's etc, accept this is all in game!

OK,

So now I need help with the code for converting the output of the gyro to the servos!

The basic code for playing with a servo obviously is not the way...

The servos are connected to pins 9, 10, 11 ( white signal wire) red and black to power and grnd ( 5v battery pack to supply servos with common ground with arduino) noted from another thread..

(NOTE: the servos will be replaced soon-ish with an AD5206 digital pot) with luck..

Here's the code I have already:

-----------------------------------------------------------------------------------------------------------

#include <Wire.h>

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

int Addr = 105;                 // I2C address of gyro
int x, y, z;

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
}

void loop(){
  getGyroValues();              // Get new values
  // In following Dividing by 114 reduces noise
  Serial.print("Raw X:");  Serial.print(x / 114);
  Serial.print(" Raw Y:"); Serial.print(y / 114);
  Serial.print(" Raw Z:"); Serial.println(z / 114);
  delay(500);                   // Short delay between reads
}

void getGyroValues () {
  byte MSB, LSB;

  MSB = readI2C(0x29);
  LSB = readI2C(0x28);
  x = ((MSB << smiley-cool | LSB);

  MSB = readI2C(0x2B);
  LSB = readI2C(0x2A);
  y = ((MSB << smiley-cool | LSB);

  MSB = readI2C(0x2D);
  LSB = readI2C(0x2C);
  z = ((MSB << smiley-cool | 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();
}

--------------------------------------------------------------------------------------------------------

Most of it means nothing to me (yet!)

How do I add to it and incorporate the code for the servos????

A very steep learning curve I know, But really any help is so appreciated..

Regards to all,

Alex   smiley-eek-blue

Pages: [1]