/*
An Arduino code written by Enterprise Logistics
4/7/2014
Code works with all sensors working plus GPS and writes data to a micro SD and sends data to Comm. System.
*/
#include <Wire.h> //I2C Arduino Library
int tmp102Address = 0x48;
byte res;
byte msb;
byte lsb;
int val;
int val2;
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
//Def. for GPS
#include <SoftwareSerial.h>
#include <stdlib.h>
#define RXPIN 3
#define TXPIN 2
SoftwareSerial nss(RXPIN, TXPIN);
float lathour, latmin, latsec, latsec2, longhour, longmin, longsec, longsec2;
#define BUFFSIZ 90 // plenty big
char buffer[BUFFSIZ];
char buffer2[BUFFSIZ];
char *parseptr;
char buffidx;
uint8_t hour, minute, second, year, month, date;
uint32_t latitude, longitude, Satellites, altitude;
uint8_t groundspeed, trackangle;
char latdir, longdir;
char status;
float lat, lon;
unsigned long fix_age, time, speedvar, coursevar;
unsigned long chars;
unsigned short sentences, failed_checksum;
float flat, flon;
//End Def. GPS
void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
nss.begin(4800);
pinMode(RXPIN,INPUT);
pinMode(TXPIN,OUTPUT);
Serial.println("#START");
}
void loop(){
//Pressure
// read the input on analog pin 6
int sensorValue = analogRead(A6);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float pressvoltage = sensorValue * (5.0 / 1024.0);
//float pressmeasured = (presserrora * pressvoltage + presserrorb) / 0.045 - 4.44;
float pressmeasured = (1/.009*(pressvoltage/5+.095));
//Acceloration
// read the input on analog pin 0
int accelxsensevolt = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float accelx = (accelxsensevolt-345.0)/(415-345);
// read the input on analog pin 1
int accelysensevolt = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float accely = (accelysensevolt-347.0)/(416-347);
// read the input on analog pin 2
int accelzsensevolt = analogRead(A2);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float accelz = (accelzsensevolt-359.0)/(428-359);
//Temperature (2 Sensors, can go to 4)
res = Wire.requestFrom(72,2);
if (res == 2) {
msb = Wire.read();
lsb = Wire.read();
val = ((msb) << 4);
val |= (lsb >> 4);
}
res = Wire.requestFrom(73,2);
if (res == 2) {
msb = Wire.read();
lsb = Wire.read();
val2 = ((msb) << 4);
val2 |= (lsb >> 4);
}
//TMP36
int sensorValueTemp = analogRead(A3);
float TempReadingCelcious = map(sensorValueTemp, 20, 358, -40, 125);
float TempFar = ((TempReadingCelcious)*1.80)+32.0+8.3;
//GPS
uint32_t tmp;
int found = 0;
int tries = 20;
int itry = 0;
while ((found != 1) || itry > tries){
readline();
if (buffer[0] != '$'){
buffer2[0]='$';
char nextchar;
int i = 0;
while(buffer[i+1] != '\0')
{
nextchar = buffer[i];
buffer2[i+1] = nextchar;
i++;
}
}
if ((strncmp(buffer, "$GPGGA",6) == 0)){
found = 1;
}
itry = itry + 1;
}
parseptr = buffer+7;
tmp = parsedecimal(parseptr);
hour = tmp / 10000;
minute = (tmp / 100) % 100;
second = tmp % 100;
parseptr = strchr(parseptr, ',') + 1;
// grab latitude & long data
// latitude
latitude = parsedecimal(parseptr);
if (latitude != 0) {
latitude *= 10000;
parseptr = strchr(parseptr, '.')+1;
latitude += parsedecimal(parseptr);
}
parseptr = strchr(parseptr, ',') + 1;
// read latitude N/S data
if (parseptr[0] != ',') {
latdir = parseptr[0];
}
//Serial.println(latdir);
// longitude
parseptr = strchr(parseptr, ',')+1;
longitude = parsedecimal(parseptr);
if (longitude != 0) {
longitude *= 10000;
parseptr = strchr(parseptr, '.')+1;
longitude += parsedecimal(parseptr);
}
parseptr = strchr(parseptr, ',')+1;
// read longitude E/W data
if (parseptr[0] != ',') {
longdir = parseptr[0];
}
//Satelites
parseptr = strchr(parseptr, ',')+3;
if (parseptr[0] != ',') {
Satellites = parsedecimal(parseptr);
}
parseptr = strchr(parseptr, ',')+5;
altitude = parsedecimal(parseptr);
if (parseptr[0] != ',') {
altitude *= 100;
parseptr = strchr(parseptr, '.')+1;
altitude += parsedecimal(parseptr);
}
//Magnetometer
int x,y,z; //triple axis data
float mx,my,mz;
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
mx = (210.0/240.0)*x+6.25;
my = (210.0/260.0)*y+60.8;
mz = (320.0/600.0)*(-z)+270;
//Here we print each component on one line, seperated by commas.
//Time
Serial.print(hour, DEC); Serial.print(':');
Serial.print(minute, DEC); Serial.print(':');
Serial.print(second, DEC);
Serial.print(",");
//Print Accel
Serial.print(accelxsensevolt);
Serial.print(",");
Serial.print(accelysensevolt);
Serial.print(",");
Serial.print(accelzsensevolt);
Serial.print(",");
//Print Pressure
Serial.print(pressmeasured);
Serial.print(",");
//Print Temp
Serial.print(TempFar);
Serial.print(",");
Serial.print(((val*0.0625)*1.80)+32.0);
Serial.print(",");
Serial.print(((val2*0.0625)*1.80)+32.0);
//Serial.println();
Serial.print(",");
Serial.print(TempFar);
//Print Mag
Serial.print(mx);
Serial.print(",");
Serial.print(my);
Serial.print(",");
Serial.print(mz);
Serial.print(",");
Serial.print(sqrt((mx*mx)+(my*my)+(mz*mz)));
Serial.print(",");
//Print GPS
//Lat
/*
Serial.print(latitude/1000000, DEC); Serial.print('.');
Serial.print((latitude/10000)%100, DEC);
Serial.print((latitude%10000)*6/1000, DEC);
Serial.print(((latitude%10000)*6/10)%100, DEC); Serial.print(",");
*/
lathour=(latitude/1000000);
latmin=((latitude/10000)%100);
latsec=(((latitude%10000)*6/1000));
latsec2=((((latitude%10000)*6/10)%100));
Serial.println(lathour+(latmin/60)+(latsec/36000)+(latsec2/3600), DEC);
//Long
/*
Serial.print(longitude/1000000, DEC); Serial.print('.');
Serial.print(((longitude/10000)%100), DEC);
Serial.print((longitude%10000)*6/1000, DEC);
Serial.print(((longitude%10000)*6/10)%100, DEC);
Serial.print(",");
*/
longhour=(longitude/1000000);
longmin=((longitude/10000)%100);
longsec=(((longitude%10000)*6/1000));
longsec2=((((longitude%10000)*6/10)%100));
Serial.println(longhour+(longmin/60)+(longsec/36000)+(longsec2/3600), DEC);
//Alt
Serial.print(((float) altitude / 100.),DEC);
Serial.print(",");
//Sat
Serial.println(Satellites);
delay(1000);
}
uint32_t parsedecimal(char *str) {
uint32_t d = 0;
while (str[0] != 0) {
if ((str[0] > '9') || (str[0] < '0'))
return d;
d *= 10;
d += str[0] - '0';
str++;
}
return d;
}
void readline(void) {
char c;
buffidx = 0; //start at begninning
int started = 0;
while (1) {
//int started = 0;
c=nss.read();
if (c == 'G'){
started = 1;
}
if (started == 1 && c == '$'){
return;
}
if (c == -1)
continue;
// Serial.print(c);
if (c == '\n')
continue;
if ((buffidx == BUFFSIZ-1) || (c == '\r')) {
buffer[buffidx] = 0;
return;
}
buffer[buffidx++]= c;
}
}
No comments:
Post a Comment