Dear Arduino Enthusiasts,
I'm have a lot of trouble with getting my computer to pick up serial data from my XBee-PRO XSC. It is running the code which I have copied below on a Ardumega 1280. I apologize for not knowing quite how to ask my question, but I'm picking this up as I go, having been left with a nearly complete navigation system for a remote control sailboat after a team-mate had to leave town quite unexpectedly in the heat of development. Other information which might be relevant is that I have the xbee on the computer end connected through an XBee Explorer USB breakout board, and the computer recognizes it on serial port 4. I did not write this code, and the gentleman who did is out of communication, and i'm running out of time and have very little knowledge in this field. Please help!
Many many thanks in advance,
Gaelen Krause
#include <FastSerial.h>
#include <AP_DCM.h>
#include <AP_IMU.h>
#include <AP_Math.h>
#include <AP_Common.h>
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include <DataFlash.h>
#include <AP_Compass.h>
#include <APM_RC.h>
#include <AP_ADC.h> // ArduPilot Mega Analog to Digital Converter Library
#include <Wire.h>
#include <avr/interrupt.h>
#include <math.h>
#include <AP_GPS.h> // ArduPilot GPS libray
#define A_LED_PIN 37 //36 = B, 37 = A, 35 = C
#define B_LED_PIN 36
#define C_LED_PIN 35
#define ToDeg(x) (x*57.2957795131) // *180/pi
AP_ADC_ADS7844 adc;
AP_Compass_HMC5843 compass(146);
FastSerialPort0(Serial); // FTDI/console
FastSerialPort1(Serial1); // GPS port (except for GPS_PROTOCOL_IMU)
FastSerialPort3(Serial3); // Telemetry port (optional, Standard and ArduPilot protocols only)
// All GPS access should be through this pointer.
GPS *g_gps;
AP_GPS_NMEA g_gps_driver(&Serial1);
boolean GPS_light;
AP_IMU_Oilpan imu(&adc, 140);
AP_DCM dcm(&imu, g_gps);
struct Waypoint{
int latitude;
int longitude;
};
int radio_in[3]; // current values from the transmitter - microseconds
int radio_out[2]; // Send to the PWM library
double battery_voltage = 7.4;
int yacc;
int zacc;
double angleOffset;
double angle;
double W; // Wind angle
int weather; //0 (0-3knts) / 1 (4-5) / 2 (6-9) / 3 (10-14)
int navigation_course=1; // 1- navigation course 0-sation keeping
int starboard=1; // Starboard = 1 or Port = 0
long timer=0;
long timer2=0;
long timer3=0;
float G_Dt= 0.02;
void setup()
{
Serial.begin(57600, 128, 128);
Serial1.begin(4800, 128, 16);
Serial3.begin(57600, 128, 128);
Wire.begin();
// Do GPS init
g_gps = &g_gps_driver;
g_gps->init(); // GPS Initialization
pinMode(C_LED_PIN, OUTPUT); // GPS status LED
pinMode(A_LED_PIN, OUTPUT); // GPS status LED
pinMode(B_LED_PIN, OUTPUT); // GPS status LED
APM_RC.Init();
adc.Init();
bool junkbool = compass.init();
compass.set_orientation(AP_COMPASS_COMPONENTS_DOWN_PINS_BACK);
imu.init(IMU::COLD_START);
dcm.set_centripetal(1);
}
void loop()
{
if((millis()- timer2) >= 50)
{
timer = timer2;
timer2=millis();
G_Dt = (float)(timer2-timer)/1000.f;
dcm.update_DCM(G_Dt);
read_radio();
read_heading();
}
if(millis() - timer3 >=500)
{
timer3=millis();
read_tilt();
read_battery();
update_GPS();
}
}
void update_GPS(void)
{
g_gps->update();
update_GPS_light();
Serial3.print("$02");
Serial3.print(", ");
Serial3.print(g_gps->latitude);
Serial3.print(", ");
Serial3.print(g_gps->longitude);
Serial3.print(", ");
Serial3.print(g_gps->ground_course);
Serial3.print(", ");
Serial3.print(g_gps->ground_speed);
Serial3.print(", ");
Serial3.print(g_gps->hdop);
Serial3.print(", ");
Serial3.println(g_gps->status());
}
void update_GPS_light(void)
{
// GPS LED on if we have a fix or Blink GPS LED if we are receiving data
// ---------------------------------------------------------------------
switch (g_gps->status()) {
case(2):
digitalWrite(C_LED_PIN, HIGH); //Turn LED C on when gps has valid fix.
break;
case(1):
if (g_gps->valid_read == true){
GPS_light = !GPS_light; // Toggle light on and off to indicate gps messages being received, but no GPS fix lock
if (GPS_light){
digitalWrite(C_LED_PIN, LOW);
} else {
digitalWrite(C_LED_PIN, HIGH);
}
g_gps->valid_read = false;
}
break;
default:
digitalWrite(C_LED_PIN, LOW);
break;
}
}
void read_radio(void)
{
Serial3.print("$03");
for (int y = 0; y < 3; y++){
radio_in[y] = APM_RC.InputCh(y);
Serial3.print(", ");
Serial3.print(radio_in[y]);
}
Serial3.println("");
}
void read_battery(void)
{
battery_voltage = (((analogRead(1))*(5.0/1024.0))*3.56) * .1 + battery_voltage * .9;
Serial3.print("$04, ");
Serial3.println(battery_voltage);
}
void read_tilt(void)
{
yacc = adc.Ch(5)-2048;
zacc = adc.Ch(6)-2048;
angle = (atan2(zacc, yacc)*180/M_PI);
Serial3.print("$01, ");
Serial3.println(angle);
}
void read_heading(void)
{
compass.read(); // Read magnetometer
compass.calculate(dcm.roll,dcm.pitch); // Calculate heading
compass.null_offsets(dcm.get_dcm_matrix());
Serial3.print("$05, ");
Serial3.println(ToDeg(compass.heading));
}
void read_serial(void)
{
if(Serial3.available()>0){
if (Serial3.read()==255){
switch (Serial3.read()){
case 1: //wind direction
W = Serial3.read();
W = W + Serial3.read();
W = W + Serial3.read()/100;
break;
case 2: //weather input
weather = Serial3.read();
break;
case 3: //navigation challenge input
navigation_course = Serial3.read();
break;
case 4: //Rounding direction input
starboard = Serial3.read();
break;
case 5: //wptMn
break;
case 6: //wptF
break;
case 7: //wptMs
break;
case 8: //wptU
break;
}
}
}
}