Friday, October 5, 2012
The basic sensors
So far I've determined that my robotic tortoise will combine readings from a GPS unit and gyroscope in order to gain a sense of position and balance. I've purchased an LA23060 GPS and an MPU6050 accelerometer/gyroscope and have successfully managed to established a combined data stream through a single Arduino UNO. This has taken quite a bit of work, but I think I have pretty good results with the following code:
/* MPU - VDD to 3.3V
GND to GND
INT to Digital PIN 3
SCL to A5
SDA to A4
VIO to less than 3.3V source
LS23060 - counting from the bottom left to right
PIN 1 to 3.3V
PIN 2 to Digital PIN 9
PIN 3 to Digital PIN 8
PIN 4 to GND */
#include "MPU6050.h"
#include "Wire.h"
#include "I2Cdev.h"
#include "SoftwareSerial.h"
MPU6050 MPU;
int pos;
char buf[150];
int count;
char *p, *i, *t, *lat, *lon, *q, *z, *n;
float tt, llat, llon, zz, t1, t2, t3, t4, t5, lat1, lat2, lat3, lat4, lat5, lon1, lon2, lon3, lon4, lon5, z1, z2, z3, z4, z5;
int qq;
SoftwareSerial softserial(8,9); //RX, TX
int16_t ax, ay, az;
int16_t gx, gy, gz;
uint8_t range = 1;
void setup(){
int error;
uint8_t c;
Wire.begin();
Serial.begin(38400);
softserial.begin(57600);
pos = 0;
z5 = 9;
z4 = 8;
z3 = 7;
z2 = 6;
z1 = 5;
for (int i = 0; i < sizeof(buf); i++)
buf[i] = 0;
softserial.write(50);
int bytesSent = softserial.write("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0*2D\r\n");
MPU.initialize();
Serial.println(MPU.testConnection() ? "True":"False");
MPU.setFullScaleGyroRange(range);
MPU.setFullScaleAccelRange(range);
// MPU.setSleepEnabled(false);
// MPU.setWakeCycleEnabled(false);
}
void loop(){
MPU.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
int xa = ax, ya = ay, za = az, xg = gx, yg = gy, zg = gz;
float axa, aya, aza, axg, ayg, azg;
int error;
char c;
char *p = buf;
char str;
int ind;
/* Serial.print("a/g:\t");
Serial.print(xa); Serial.print("\t");
Serial.print(ya); Serial.print("\t");
Serial.print(za); Serial.print("\t");
Serial.print(xg); Serial.print("\t");
Serial.print(yg); Serial.print("\t");
Serial.println(zg); */
axg = xg * 0.015037594;
ayg = yg * 0.015037594;
azg = zg * 0.015037594;
Serial.println(axg, 3);
Serial.println(ayg, 3);
Serial.println(azg, 3);
axa = xa * 0.00012207;
aya = ya * 0.00012207;
aza = za * 0.00012207;
Serial.println(axa, 3);
Serial.println(aya, 3);
Serial.println(aza, 3);
if (softserial.available() > 0) {
c = softserial.read();
if(c == '$') {
buf[pos] = 0;
Serial.println(buf);
n = strtok_r(p,",",&p);
if(n = "GPGGA") {
t = strtok_r(p,",",&p);
lat = strtok_r(p,",",&p);
n = strtok_r(p,",",&p);
lon = strtok_r(p,",",&p);
n = strtok_r(p,",",&p);
q = strtok_r(p,",",&p);
n = strtok_r(p,",",&p);
n = strtok_r(p,",",&p);
z = strtok_r(p,",",&p);
n = strtok_r(p,",",&p);
n = strtok_r(p,",",&p);
qq = atof(q);
tt = atof(t);
llat = atof(lat);
llon = atof(lon);
zz = atof(z);
// t5, lat5, lon5, z5 = history(qq, tt, llat, llon, zz);
// Serial.println("time");
Serial.println(tt, 4);
// Serial.println("lat");
Serial.println(llat, 4);
// Serial.println("lon");
Serial.println(llon, 4);
// Serial.println("ele");
Serial.println(zz, 4);
// Serial.println("");
delay(50);
}
else {
Serial.println("Not GPGGA");
}
pos = 0;
}
else {
buf[pos] = c;
pos++;
if (pos >= sizeof(buf))
pos = 0;
}
}
}
float history(int qq, float tt, float llat, float llon, float zz){
if (qq =! "0") {
Serial.println("Oh Shit");
}
else {
t5 = t4;
t4 = t3;
t3 = t2;
t1 = tt;
lat5 = lat4;
lat4 = lat3;
lat3 = lat2;
lat2 = lat1;
lat1 = llat;
lon5 = lon4;
lon4 = lon3;
lon3 = lon2;
lon2 = lon1;
lon1 = llon;
z5 = z4;
z4 = z3;
z3 = z2;
z2 = z1;
z1 = zz;
return t5, lat5, lon5, z5;
}
I'm not a programmer in any way, so I'm sure there are plenty of mistakes with this "sketch," but it does produce consistant results that respond in a predictable fashion when there are changes in the sensor positions and orientations.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.