Compass arduino codes HELP PLS!!!

Hi, i'm working on my final year project and I wish to get some guidance over here.
I'm given 9DOF sensor stick to used as compass and currently i'm working on a following object robot.
The 9DOF sensor is mounted on the transmitter and the receiver(car) will follow the compass direction of transmitter to move.
Can someone guide me on how to start for the arduino codes?
I need a little bit of help on how to send compass direction of transmitter to receiver and how the receiver will receive it..

The 9DOF module will probably have an associated Arduino library to help you use it. You do not say what model the sensor is, so we can't help you find the library. When you install the library, some example sketches will appear in the File->examples menu in the IDE. One of those sketches would be a good place to start.

the model is SEN-10724, i have already search online and i managed to get the compass heading readings.

my current problem is how do i send the compass heading readings(transmitter) to my receiver arduino so that the receiver will follow the bearings of transmitter?

i need some guidance to start...

annyee:
my current problem is how do i send the compass heading readings(transmitter) to my receiver arduino so that the receiver will follow the bearings of transmitter?

You are not telling us nearly enough about your project.

  • What Arduino is connected to the compass module.
  • What Arduino is connected to the car.
  • How do you propose to send data from one to the other - by wire? by wireless?
  • and anything else that you know and we dont

This Simple nRF24L01+ Tutorial may be of interest.

...R

Hi,

Can you tell us how the direction the Tx is POINTING being sent to the Rx, will tell the Rx the DIRECTION to the Tx?

Tom... :slight_smile:

uint8_t payload[] = { 'H', 'i' };

Robin2:
You are not telling us nearly enough about your project.

  • What Arduino is connected to the compass module.
  • What Arduino is connected to the car.
  • How do you propose to send data from one to the other - by wire? by wireless?
  • and anything else that you know and we dont

This Simple nRF24L01+ Tutorial may be of interest.

...R

I'm using arduino uno fro this project. Arduino uno connected to the compass module, arduino uno to the car.
I wanted to send data from one to other wireless.

I'm using XBee series 1 in this project to determine RSSI to determine distance between transmitter to receiver,
in my programming, i have already included <xbee.h> library and can i send my compass data through the xbee.send() command?
E.g xbee.send(compass)

if this method can't, how can i send the compass data from transmitter to receiver by wireless?
or can i use the wire.send() command to send?
I'm not sure about it

appreciate all replies :slight_smile:
thanks everyone!

I am not familiar with XBees but I believe their interface with the Arduino is similar to Serial and the examples in Serial Input Basics may be useful.

...R

this is the code for my transmitter

#include <SoftwareSerial.h>
#include <XBee.h>
#include <Wire.h>
#define COMPASSADDR 0x1e

// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial serial1(2, 3); // RX, TX

// create the XBee object
XBee xbee = XBee();

uint8_t payload[] = { 'H', 'i' };

// SH + SL Address of receiving XBee (only use when not broadcast)
//XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x4008afdc);
Tx16Request Tx16 = Tx16Request(0xFFFF, payload, sizeof(payload)); //0xFFFF bcuz broadcast else replace it to addr64
TxStatusResponse txStatus = TxStatusResponse();

union XYZBuffer 
{
    struct 
    {
        short x,y,z;
    } 
    value;
    byte buff[6];
};

void setup()
{
    Serial.begin(9600);   // start serial for output
    serial1.begin(9600);    //start software serial
    xbee.setSerial(serial1);    //start xbee
    Wire.begin();        // join i2c bus (address optional for master)
    setupCompass(COMPASSADDR);
}

void loop()
{
    xbee_send();
    union XYZBuffer compass;
    readCompass(COMPASSADDR,&compass);
    Serial.print("Compass x, y, z - ");
    output(compass); //call function to show output of compass
    Serial.println(); 
}

//------------------compass function--------------

void output(union XYZBuffer xyz) 
{
    pad(6,xyz.value.x);
    Serial.print(xyz.value.x);
    Serial.print(',');    
    pad(6,xyz.value.y);
    Serial.print(xyz.value.y);
    Serial.print(',');   
    pad(6,xyz.value.z);
    Serial.println(xyz.value.z);
    
    float heading = atan2(xyz.value.y, xyz.value.x);

    //Correcting the heading with the declination angle depending on your location
    //In singapore it's 0.14 degrees => 0.0024 rad
    float declination = 0.0024;
    heading += declination;
    
    // Correct for when signs are reversed.
    if(heading < 0)
    heading += 2*PI;

    // Check for wrap due to addition of declination.
    if(heading > 2*PI)
    heading -= 2*PI;

    // Convert radians to degrees for readability.
    float headingDegrees = heading * 180/M_PI; 
    Serial.print("Heading : ");
    Serial.println(heading);
    Serial.print("Heading Degrees : ");
    Serial.println(headingDegrees);

}

void pad(int width,int number) 
{
    int n=abs(number);
    int w=width;

    if (number<0) 
        w--;

    while (n>0) 
    {
        w--;
        n = n/10;
    }

    if (number==0) 
        w--;

    for (int i=0;i<w;i++) 
    {
        Serial.print(' ');
    }

}

void changeEndian(union XYZBuffer *xyz) 
{
    for (int i=0;i<6;i+=2) 
    {
        byte t=xyz->buff[i];
        xyz->buff[i] = xyz->buff[i+1];
        xyz->buff[i+1]=t;
    }
}

void readXYZ(int device,union XYZBuffer *xyz) 
{     
    Wire.requestFrom(device, 6);      
    long start=millis();
    while (!Wire.available() && (millis()-start)<100);
    if (millis()-start<100) 
    {
        for (int i=0;i<6;i++)
        {
            xyz->buff[i]=Wire.read();
        }
    }
}

void setupCompass(int device) 
{
    // Check ID to see if we are communicating
    Serial.print("Compass id is ");

    Wire.beginTransmission(device);
    Wire.write(10); // One Reading
    Wire.endTransmission(); 

    Wire.requestFrom(device,2); 
    while (!Wire.available());
    char ch=Wire.read();
    Serial.print(ch);   
    ch=Wire.read();
    Serial.println(ch); // Should output H4  

    // Configure Register A
    // Set 8 samples average per measurement output
    // Set Data Output Rate = 15Hz
    Wire.beginTransmission(device);
    Wire.write(0x00); 
    Wire.write(0x70);
    Wire.endTransmission();

    // Configure Register B
    // Set Sensor Field Gain +/- 4.7Ga
    Wire.beginTransmission(device);
    Wire.write(0x01); 
    Wire.write(0xA0);
    Wire.endTransmission();

    // Configure Mode Register
    // Continuous-Measurement Mode.  
    // In continuous-measurement mode, the device continuously performs measurements and places the result in the data      register.  
    // RDY goes high when new data is placed in all three registers. 
    Wire.beginTransmission(device);
    Wire.write(0x02); 
    Wire.write(0x00); //  Reading
    Wire.endTransmission(); 
    delay(6);
}

void readCompass(int device,union XYZBuffer *xyz) 
{
    readXYZ(device,xyz);
    changeEndian(xyz);
    Wire.beginTransmission(device);
    Wire.write(0x03);
    Wire.endTransmission(); 
}

//----------------XBEE function-------------------

void xbee_send()
{
  xbee.send(Tx16);
  
  // after sending a tx request, we expect a status response
  // wait up to half second for the status response
  if (xbee.readPacket(500)) 
  {
    // got a response!

    // should be a znet tx status              
    if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) 
    {
      xbee.getResponse().getTxStatusResponse(txStatus);

     Serial.println("XBEE get response, send successfully");
    }
  } 
  else 
  {
  if (xbee.getResponse().isError()) 
  {
    Serial.println("ERROR!!");
  } 
  else 
  {
    // local XBee did not provide a timely TX Status Response -- should not happen
    Serial.println("XBee didn't provide tx status response");
  }
  }
}

my current code is succeed to achieve the compass direction and send xbee data out but how do i send my compass direction data out wirelessly with xbee with serial software coding?


this is the code for my receiver

#include <XBee.h>
#include <SoftwareSerial.h>

#define FREQ 25   //sample freq in Hz
#define TIME_INTERVAL_100Hz 10000   //100Hz - 10ms
#define fast_loop_timer 2   //50Hz - 20ms
#define mid_loop_timer 4    //25Hz - 40ms
#define slow_loop_timer 10  //10Hz - 100ms
#define very_slow_loop_timer 20     //5Hz - 200ms

//capture time
unsigned long previous_time, current_time;
unsigned int counter = 0;

// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial serial1(2, 3); // RX, TX

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
//create reusable response objects for responses we expect to handle
Rx16Response rx16 = Rx16Response();

uint8_t option = 0;
uint8_t data = 0;
uint8_t rssi = 0;

int IN1=6; // left backward
int IN2=10; //right forward
int IN3=5; //left forward
int IN4=9; //right backward


void setup()
{
  serial1.begin(9600);
  xbee.setSerial(serial1);
  Serial.begin(9600);
  car_setup();    //calling function that initialise the car
  previous_time = micros();
}

void loop()
{
  current_time = micros();

  //100Hz interval
  if(current_time - previous_time >= TIME_INTERVAL_100Hz)
  {
    if((counter%fast_loop_timer) == 0)
    {
      //fast routines running at 50Hz interval
      fast_loop();
    }

    if((counter%mid_loop_timer) == 0)
    {
      //mid routines running at 25Hz interval
      mid_loop();
    }

    if((counter%slow_loop_timer) == 0)
    {
      //slow routines running at 10Hz interval
      slow_loop();
    }

    if((counter%very_slow_loop_timer) == 0)
    {
      //slow routines running at 5Hz interval
      very_slow_loop();
    }

    if(++counter == 20)
    counter = 0;
    previous_time = current_time;
    //balance time to 10ms interval
    delayMicroseconds(TIME_INTERVAL_100Hz - (micros()-previous_time));
  }
}

the receiver is also succeed to work but how i add code so that i can receive the compass direction that send out from the transmitter? (the receiver code that i have included over here is not full coding.. i just paste some main coding over here else it's too long)

really need some help...

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Hi,
Have you code that just transmits and receives a single item of data?
That is did you write this code all at once, or develop it in stages?
Have you used XBee examples.

Tom.. :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

done :slight_smile:

TomGeorge:
Hi,
Have you code that just transmits and receives a single item of data?
That is did you write this code all at once, or develop it in stages?
Have you used XBee examples.

Tom.. :slight_smile:

i did it in stages and now my job is to transmit compass data from transmitter to receiver so that the car will move to the direction of transmitter

i have already succeed in getting the RSSI value of xbee, car movement, compass reading, xbee sending data wirelessly...

i left with sending compass direction wirelessly to receiver (through software serial or any other method that you guys can help me with)

Hi,
Please read my post #4,

Explain how the compass reading of the direction the TX is pointing will tell the Rx what direction the TX is in from the Rx.

Thanks.. Tom.. :slight_smile: