Pages: [1]   Go Down
Author Topic: Using an accelerometer to orient a model rocket in light via control surfaces  (Read 1010 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Haggis de Mexico
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have attempted to recreate guiding a model rocket
http://hackaday.com/2010/08/03/diy-guided-missile-err-model-rocket/comment-page-2/#comments
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,
Code:
#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

Code:
#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

Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5597
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have attempted to recreate guiding a model rocket

You may want to be very, very careful with what you are doing and saying in a public forum. I don't know where you live, but in many cases the government does not look kindly on people building their own guided missiles.

Model rocketry (even high power model rocketry) has many limitations on it before you run into things needing licenses, permits, certifications, etc - and one of those tends to be "adding active control surfaces".

Depending on your luck and your location - nothing may happen, or you may face big fines - if you're lucky.

I'm just putting this out there to warn you and others; some governments consider adding active control surfaces (especially "intelligent" ones) to a rocket to be a step towards weapon systems creation. You may not think so; you may think such rules as restrictive - but your government may not care one little bit. Should they decide to pursue it, they will grind you into dust. Fair warning, ok...?

 smiley-wink
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Pages: [1]   Go Up
Jump to: