I have attempted to recreate guiding a model rocket
Based on the linked project shown in Hack a day
I am not able to recreate their results using another accelerometer (mma 7361)
https://sites.google.com/site/airwavershr/Home/guided-rocket
link to their code download
https://sites.google.com/site/airwavershr/Home/guided-rocket/Rocket_Guidance.pde?attredirects=0&d=1
The more I examine the changes I made the more confused I get.
What I’m looking for is:
Any tips to problem areas in my code.
In addition I can display the values from the MMA 7361 successfully however I question if I’m getting accurate data.
MY modified code, uploads fine but does not zero the rockets position nor does it compensate,
#include <Servo.h>
int xaccPin = 1;
int yaccPin = 2;
long xacc = 0;
long yacc = 0;
int anglex;
int angley;
int servoposx, servoposy;
int duration, inches;
long xacc1 = 0;
long yacc1 = 0;
long xacc2 = 0;
long yacc2 = 0;
long xacc3 = 0;
long yacc3 = 0;
long xaccs = 0;
long yaccs = 0;
long xaccd = 0;
long yaccd = 0;
int counter = 0;
Servo x1servo;
Servo x2servo;
Servo y1servo;
Servo y2servo;
void setup()
{
Serial.begin(9600);
x1servo.attach(9);
x2servo.attach(10);
y1servo.attach(12);
y2servo.attach(13);
pinMode(xaccPin, INPUT);
pinMode(yaccPin, INPUT);
delay(1000);
if (counter <= 1)
{
// starting values
xacc1 = readAcceleration(analogRead(xaccPin));
yacc1 = readAcceleration(analogRead(yaccPin));
xacc2 = readAcceleration(analogRead(xaccPin));
yacc2 = readAcceleration(analogRead(yaccPin));
xacc3 = readAcceleration(analogRead(xaccPin));
yacc3 = readAcceleration(analogRead(yaccPin));
// stable position?
xaccs = (xacc1 + xacc2 + xacc3)/3;
yaccs = (yacc1 + yacc2 + yacc3)/3;
}
}
void loop()
{
xacc = readAcceleration(analogRead(xaccPin));
yacc = readAcceleration(analogRead(yaccPin));
xaccd = xacc - xaccs;
yaccd = yacc - yaccs;
anglex = map(xaccd, 280, -280, -90, 90);
angley = map(yaccd, 280, -280, -90, 90);
servoposx = map(anglex, -90, 90, 20, 160);
servoposy = map(angley, -90, 90, 20, 160);
x1servo.write(servoposx);
x2servo.write(160 - servoposx);
y1servo.write(servoposy);
y2servo.write(160 - servoposy);
// Uncomment for testing purposes
Serial.print("X1: ");
Serial.print(analogRead(xaccPin));
Serial.print("xacc: ");
Serial.print(xacc);
//Serial.print(" X2: ");
//Serial.print(analogRead(160 - xaccPin));
Serial.print("Y1: ");
Serial.print(analogRead(yaccPin));
//(analogRead(yPin));
//Serial.print(" Y2: ");
//Serial.print(analogRead(160 - yaccPin));
Serial.println();
delay(100);
}
long readAcceleration(int axe)
{
int count = 0;
long accel = 0;
int value = 0;
value = analogRead(axe);
while(value == HIGH)
{
value = analogRead(axe);
}
while(value == LOW)
{
value = analogRead(axe);
}
while(value == HIGH)
{
value = analogRead(axe);
count = count + 1;
}
//accel = abs(8 * (count * 18 / 10 - 500)); //this was the original function
accel = count;
return accel;
}
This is the code I use to view the accelerometer values
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
int backLight = 13;
const int xPin = 1; // x-axis
const int yPin = 2; // y-axis
const int zPin = 3; // z-axis
void setup()
{
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
Serial.begin(9600);
}
void loop()
{
//Uncomment the lines below for Serial output
//Serial.print("X=");
//Serial.print(analogRead(xPin));
//Serial.print("\t");
//Serial.print("Y=");
//Serial.print(analogRead(yPin));
//Serial.print("\t");
//Serial.print("Z=");
//Serial.print(analogRead(zPin));
//delay(100);
//Comment if not using a LCD.
lcd.begin(2,16);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("X= ");
lcd.print(analogRead(xPin));
lcd.setCursor(10,0);
lcd.print("Y= ");
lcd.print(analogRead(yPin));
lcd.setCursor(5,1);
lcd.print("Z= ");
lcd.print(analogRead(zPin));
delay(1000);
}
at rest correctly oriented I get x 303-310 y 315-320 z 336
the accelerometer is connected to 5v